Source

openju / openju / routes.py

# -*- coding: utf-8 -*-

import re
from openju import g, _
import openju.controllers as controllers

PATTERNS = {
    'MESSAGE_ID': ur'(?P<message_id>[0-9a-zA-Z]+)',
    'NICK': ur'(?P<nick>[a-zA-Z0-9\-_]+)',
}

def cmd_register(text):
    # logged or not?
    if g.auth_user is not None:
        return
    
    pattern = re.compile(ur'\s*REGISTER\s+%(p_nick)s\s*' % {
            'p_nick': PATTERNS['NICK']
            })
    m = pattern.match(text)
    if m is None:
        raise UserAlertError(_(
            u"\nYou are not registered. Pleace, type:\n"
            u"REGISTER %your_nick_name%\nto register"))
    d = m.groupdict()
    return controllers.user.register(d['nick'])

def cmd_last_messages(text):
    pattern = re.compile(ur'\s*#\s*$')
    m = pattern.match(text)
    if m is not None:
        return controllers.message.last_messages()

def cmd_read_message(text):
    pattern = re.compile(ur'^#%(p_message_id)s\s*$' % {
            'p_message_id': PATTERNS['MESSAGE_ID'],
            })
    m = pattern.match(text)
    if m is not None:
        d = m.groupdict()
        return controllers.message.read(d['message_id'])

def cmd_read_message_with_replies(text):
    pattern = re.compile(ur'^#%(p_message_id)s\+\s*$' % {
            'p_message_id': PATTERNS['MESSAGE_ID']
            })
    m = pattern.match(text)
    if m is not None:
        d = m.groupdict()
        return controllers.message.read_more(d['message_id'])

def cmd_post_reply(text):
    pattern = re.compile(ur'#%(p_message_id)s\s(?P<message_body>.+)' % {
            'p_message_id': PATTERNS['MESSAGE_ID']
            })
    m = pattern.match(text)
    if m is not None:
        d = m.groupdict()
        return controllers.message.post_reply(d['message_id'],
                                              d['message_body'])

# def cmd_new_nick(user_jid, text):
#     cmd_text = u'NICK'
#     if text.startswith(cmd_text):
#         nick = text[len(cmd_text) + 1:].strip()
#         return controllers.user.new_nick(nick)

# def cmd_post_reply(user_jid, text):
#     pattern = re.compile(ur'#(?P<message_hrid>[0-9]+)(/(?P<r_no>[0-9]+))?(?P<reply>\s.*)')
#     m = pattern.match(text)
#     if m is not None:
#         d = m.groupdict()
#         d['r_no'] = none_or_int(d['r_no'])
#         if d['r_no'] is not None: d['r_no'] = int(d['r_no'])
#         return controllers.message.post_reply(
#             int(d['message_hrid']),
#             d['reply'],
#             r_no=d['r_no'])

def cmd_post_message(text):
    return controllers.message.post(text)