1. Konstantine Rybnikov
  2. openju


openju / openju / message_bus.py

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

import xmpp
import re

# from openju.util import none_or_int
from openju import g, _
from openju.validators import UserAlertError
import openju.controllers as controllers
from openju.model import *
from mongoengine.queryset import DoesNotExist

# from openju.globals import current_app as app

def error_handler(f):
    def new_f(conn, *args, **kw):
            f(conn, *args, **kw)
        except UserAlertError, e:
            conn.send(xmpp.Message(g.xmpp_user, unicode(e)))
    return new_f

def message_bus(conn, mess):
    """ kind a routing thing """
    def cmd_register(text):
        # logged or not?
        if g.auth_user is not None:
        pattern = re.compile(ur'\s*REGISTER\s+(?P<nick>[a-zA-Z0-9\-_]+)\s*')
        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_hrid>[0-9a-zA-Z]+)\s*$')
        m = pattern.match(text)
        if m is not None:
            d = m.groupdict()
            return controllers.message.read(d['message_hrid'])
    # 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)
    text = mess.getBody()
    xmpp_user = mess.getFrom()
    user_jid = u'%(node)s@%(domain)s' % dict(
    if text is not None: # don't know why, but sometimes it's None. TODO: debug it
        def call_list(*f_list):
            for f in f_list:
                res = f(text)
                if isinstance(res, unicode):
                    conn.send(xmpp.Message(xmpp_user, res))
                if res is not None:
        # should be only place to put some g.-stuff
        g.xmpp_user = xmpp_user
        g.text = text
        g.user_jid = user_jid
            g.auth_user = User.objects.get(jid=user_jid)
        except DoesNotExist:
            g.auth_user = None
            # cmd_new_nick,
            # cmd_post_reply,