Commits

Konstantine Rybnikov committed f023879

ok, I really did some good work on xmpppy. God it's code is so so ugly)) Need to rewrite everything

  • Participants
  • Parent commits 72ba549

Comments (0)

Files changed (3)

 
 if __name__ == '__main__':
     run()
-
-# from openju.messagecb import message_cb
-
-# from openju.config import set_config_profile, get_conf_val
-# from openju.util import *
-
-# def StepOn(conn):
-#     try:
-#         conn.Process(1)
-#     except KeyboardInterrupt: return 0
-#     return 1
-
-# def main():
-#     set_config_profile('dev')
-    
-#     jid = xmpp.JID(get_conf_val('jid'))
-#     user, server, password = jid.getNode(), jid.getDomain(), get_conf_val('pwd')
-    
-#     conn = xmpp.Client(server, debug=[])
-#     conres = conn.connect()
-#     if not conres:
-#         print "> Unable to connect to server %s!" % server
-#         sys.exit(1)
-#     if conres != 'tls':
-#         print "> Warning: unable to estabilish secure connection - TLS failed!"
-#     authres = conn.auth(user,password)
-#     if not authres:
-#         print "> Unable to authorize on %s - check login/password." % server
-#         sys.exit(1)
-#     conn.RegisterHandler('message', message_cb)
-#     conn.sendInitPresence()
-#     print "> Bot started."
-#     while StepOn(conn):
-#         pass
-
-# if __name__ == '__main__':
-#     main()
-    

File openju/app.py

         jid = xmpp.JID(self.config['JID'])
         user, server, password = jid.getNode(), jid.getDomain(), self.config['PASSWORD']
         if self.config['DEBUG']:
-            conn = xmpp.Client(server, debug=[])
+            self.xmpp_cli = xmpp.Client(server)
         else:
-            conn = xmpp.Client(server)
-        conres = conn.connect()
-        if not conres:
+            self.xmpp_cli = xmpp.Client(server, debug=[])
+        conn_type = self.xmpp_cli.connect()
+        if conn_type is None:
             raise Exception("Unable to connect to server %s!" % server)
-        if conres != 'tls':
+        if conn_type != 'tls':
             self.logger.warning("Unable to estabilish secure connection - TLS failed!")
-        authres = conn.auth(user, password)
-        if not authres:
+        auth_type = self.xmpp_cli.auth(user, password)
+        if auth_type is None:
             raise Exception("Unable to authorize on %s - check login/password." % server)
         
-        from openju.message_bus import message_bus
-        conn.RegisterHandler('message', message_bus)
-        conn.sendInitPresence()
+        from openju.message_bus import message_bus, presence_bus
+        self.xmpp_cli.RegisterHandler('message', message_bus)
+        self.xmpp_cli.RegisterHandler('presence', presence_bus)
+        self.xmpp_cli.sendInitPresence()
+        
         self.logger.info('Bot started')
         
         class MetaObject(object):
         meta = MetaObject()
         meta.current_app = self
         meta.g = MetaObject()
+        meta.g.xmpp_cli = self.xmpp_cli
         connect(self.config.get('MONGO_DBNAME'))
         # meta.g.connection = Connection(*params)
         # meta.g.db = meta.g.connection.openju
             return True
         
         # step on
-        while step_on(conn):
+        while step_on(self.xmpp_cli):
             pass

File openju/message_bus.py

             conn.send(xmpp.Message(g.xmpp_user, unicode(e)))
     return new_f
 
+def auth_user(user_jid):
+    from openju.model import User
+    try:
+        return User.objects.get(jid=user_jid)
+    except DoesNotExist:
+        return  None
+
 @error_handler
 def message_bus(conn, mess):
     """ kind a routing thing """
     user_jid = u'%(node)s@%(domain)s' % dict(
         node=xmpp_user.getNode(),
         domain=xmpp_user.getDomain())
-    
     if text is not None: # don't know why, but sometimes it's None. TODO: debug it
-        def auth_user():
-            from openju.model import User
-            try:
-                return User.objects.get(jid=user_jid)
-            except DoesNotExist:
-                return  None
-        
         def dispatch(xmpp_user):
             def call_list(*f_list):
                 for f in f_list:
                 cmd_post_message,
                 )
         
-        def notify_logic(xmpp_user):
-            def filter_empty(l):
-                return (x for x in l if x is not None)
-            
-            def process_notifications(notifications):
-                def process_notification(notification):
-                    from openju.controllers import notify
-                    action = getattr(notify, notification.action, default=None)
-                    if action is not None:
-                        res = action(notification.user, notification.meta)
-                        notification.delete()
-                        if res is not None:
-                            return res
-                    else:
-                        app.logger.exception(
-                            Exception(u'Notification action %s not found' % action))
-                
-                return (process_notification(x) for x in notifications)
-            
-            notify_text = (
-                u"\n\n"
-                .join(
-                    filter_empty(
-                        process_notifications(
-                            g.auth_user.get_notifications()))))
-            if notify_text:
-                conn.send(xmpp.Message(xmpp_user, ))
-        
         # 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 = auth_user()
+        g.auth_user = auth_user(user_jid)
         dispatch(xmpp_user)
         notify_logic(xmpp_user)
+        g.auth_user = None
+        g.user_jid = None
+
+def presence_bus(conn, mess):
+    xmpp_user = mess.getFrom()
+    user_jid = u'%(node)s@%(domain)s' % dict(
+        node=xmpp_user.getNode(),
+        domain=xmpp_user.getDomain())
+    g.auth_user = auth_user(user_jid)
+    if mess.getType() == 'subscribe':
+        g.xmpp_cli.Roster.Authorize(user_jid)
+    
+    # ask for subscription
+    if xmpp_user not in g.xmpp_cli.Roster.getItems():
+        g.xmpp_cli.Roster.Subscribe(user_jid)
+
+    if mess.getName() == 'presence' and \
+       g.auth_user is not None:
+        notify_logic(xmpp_user)
+    g.auth_user = None
+
+def notify_logic(xmpp_user):
+    def filter_empty(l):
+        return (x for x in l if x is not None)
+
+    def process_notifications(notifications):
+        def process_notification(notification):
+            from openju.controllers import notify
+            action = getattr(notify, notification.action, None)
+            if action is not None:
+                res = action(notification.user, notification.info)
+                notification.delete()
+                if res is not None:
+                    return res
+            else:
+                app.logger.exception(
+                    Exception(u'Notification action %s not found' % action))
+
+        return (process_notification(x) for x in notifications)
+
+    if g.auth_user is not None:
+        notify_text = (
+            u"\n\n"
+            .join(
+                filter_empty(
+                    process_notifications(
+                        g.auth_user.get_notifications()))))
+        if notify_text:
+            g.xmpp_cli.send(xmpp.Message(xmpp_user, unicode(notify_text)))