diff src/core/xmpp.py @ 1963:a2bc5089c2eb

backend, frontends: message refactoring (huge commit): /!\ several features are temporarily disabled, like notifications in frontends next step in refactoring, with the following changes: - jp: updated jp message to follow changes in backend/bridge - jp: added --lang, --subject, --subject_lang, and --type options to jp message + fixed unicode handling for jid - quick_frontend (QuickApp, QuickChat): - follow backend changes - refactored chat, message are now handled in OrderedDict and uid are kept so they can be updated - Message and Occupant classes handle metadata, so frontend just have to display them - Primitivus (Chat): - follow backend/QuickFrontend changes - info & standard messages are handled in the same MessageWidget class - improved/simplified handling of messages, removed update() method - user joined/left messages are merged when next to each other - a separator is shown when message is received while widget is out of focus, so user can quickly see the new messages - affiliation/role are shown (in a basic way for now) in occupants panel - removed "/me" messages handling, as it will be done by a backend plugin - message language is displayed when available (only one language per message for now) - fixed :history and :search commands - core (constants): new constants for messages type, XML namespace, entity type - core: *Message methods renamed to follow new code sytle (e.g. sendMessageToBridge => messageSendToBridge) - core (messages handling): fixed handling of language - core (messages handling): mes_data['from'] and ['to'] are now jid.JID - core (core.xmpp): reorganised message methods, added getNick() method to client.roster - plugin text commands: fixed plugin and adapted to new messages behaviour. client is now used in arguments instead of profile - plugins: added information for cancellation reason in CancelError calls - plugin XEP-0045: various improvments, but this plugin still need work: - trigger is used to avoid message already handled by the plugin to be handled a second time - changed the way to handle history, the last message from DB is checked and we request only messages since this one, in seconds (thanks Poezio folks :)) - subject reception is waited before sending the roomJoined signal, this way we are sure that everything including history is ready - cmd_* method now follow the new convention with client instead of profile - roomUserJoined and roomUserLeft messages are removed, the events are now handled with info message with a "ROOM_USER_JOINED" info subtype - probably other forgotten stuffs :p
author Goffi <goffi@goffi.org>
date Mon, 20 Jun 2016 18:41:53 +0200
parents 633b5c21aefd
children 046449cc2bff
line wrap: on
line diff
--- a/src/core/xmpp.py	Sun Jun 19 22:22:13 2016 +0200
+++ b/src/core/xmpp.py	Mon Jun 20 18:41:53 2016 +0200
@@ -25,7 +25,7 @@
 from twisted.words.protocols.jabber import error
 from twisted.words.protocols.jabber import jid
 from twisted.python import failure
-from wokkel import client, disco, xmppim, generic, iwokkel
+from wokkel import client as wokkel_client, disco, xmppim, generic, iwokkel
 from wokkel import delay
 from sat.core.log import getLogger
 log = getLogger(__name__)
@@ -36,13 +36,13 @@
 import uuid
 
 
-class SatXMPPClient(client.XMPPClient):
+class SatXMPPClient(wokkel_client.XMPPClient):
     implements(iwokkel.IDisco)
 
     def __init__(self, host_app, profile, user_jid, password, host=None, port=C.XMPP_C2S_PORT, max_retries=C.XMPP_MAX_RETRIES):
         # XXX: DNS SRV records are checked when the host is not specified.
         # If no SRV record is found, the host is directly extracted from the JID.
-        client.XMPPClient.__init__(self, user_jid, password, host or None, port or C.XMPP_C2S_PORT)
+        wokkel_client.XMPPClient.__init__(self, user_jid, password, host or None, port or C.XMPP_C2S_PORT)
         self.factory.clientConnectionLost = self.connectionLost
         self.factory.maxRetries = max_retries
         self.__connected = False
@@ -80,7 +80,7 @@
     def _authd(self, xmlstream):
         if not self.host_app.trigger.point("XML Initialized", xmlstream, self.profile):
             return
-        client.XMPPClient._authd(self, xmlstream)
+        wokkel_client.XMPPClient._authd(self, xmlstream)
         self.__connected = True
         log.info(_("********** [%s] CONNECTED **********") % self.profile)
         self.streamInitialized()
@@ -112,7 +112,7 @@
         log.error(_(u"ERROR: XMPP connection failed for profile '%(profile)s': %(reason)s" % {'profile': self.profile, 'reason': reason}))
         self.conn_deferred.errback(reason.value)
         try:
-            client.XMPPClient.initializationFailed(self, reason)
+            wokkel_client.XMPPClient.initializationFailed(self, reason)
         except:
             # we already chained an errback, no need to raise an exception
             pass
@@ -152,8 +152,8 @@
         message = {}
         subject = {}
         extra = {}
-        data = {"from": message_elt['from'],
-                "to": message_elt['to'],
+        data = {"from": jid.JID(message_elt['from']),
+                "to": jid.JID(message_elt['to']),
                 "uid": message_elt.getAttribute('uid', unicode(uuid.uuid4())), # XXX: uid is not a standard attribute but may be added by plugins
                 "message": message,
                 "subject": subject,
@@ -169,11 +169,11 @@
 
         # message
         for e in message_elt.elements(C.NS_CLIENT, 'body'):
-            message[e.getAttribute('xml:lang','')] = unicode(e)
+            message[e.getAttribute((C.NS_XML,'lang'),'')] = unicode(e)
 
         # subject
         for e in message_elt.elements(C.NS_CLIENT, 'subject'):
-            subject[e.getAttribute('xml:lang','')] = unicode(e)
+            subject[e.getAttribute((C.NS_XML, 'lang'),'')] = unicode(e)
 
         # delay and timestamp
         try:
@@ -187,41 +187,38 @@
             if parsed_delay.sender:
                 data['delay_sender'] = parsed_delay.sender.full()
 
-        def skipEmptyMessage(data):
-            if not data['message'] and not data['extra']:
-                raise failure.Failure(exceptions.CancelError())
-            return data
+
+        post_treat.addCallback(self.skipEmptyMessage)
+        post_treat.addCallback(self.addToHistory, client)
+        post_treat.addErrback(self.treatmentsEb)
+        post_treat.addCallback(self.bridgeSignal, client, data)
+        post_treat.addErrback(self.cancelErrorTrap)
+        post_treat.callback(data)
 
-        def bridgeSignal(data):
-            try:
-                data['extra']['received_timestamp'] = data['received_timestamp']
-                data['extra']['delay_sender'] = data['delay_sender']
-            except KeyError:
-                pass
-            if data is not None:
-                self.host.bridge.messageNew(data['uid'], data['timestamp'], data['from'].full(), data['to'].full(), data['message'], data['subject'], data['type'], data['extra'], profile=client.profile)
-            return data
+    def skipEmptyMessage(self, data):
+        if not data['message'] and not data['extra'] and not data['subject']:
+            raise failure.Failure(exceptions.CancelError("Cancelled empty message"))
+        return data
+
+    def addToHistory(self, data, client):
+        return self.host.memory.addToHistory(client, data)
 
-        def addToHistory(data):
-            data['from'] = jid.JID(data['from'])
-            data['to'] = jid.JID(data['to'])
-            self.host.memory.addToHistory(client, data)
-            return data
-
-        def treatmentsEb(failure_):
-            failure_.trap(exceptions.SkipHistory)
-            return data
+    def treatmentsEb(self, failure_):
+        failure_.trap(exceptions.SkipHistory)
 
-        def cancelErrorTrap(failure_):
-            """A message sending can be cancelled by a plugin treatment"""
-            failure_.trap(exceptions.CancelError)
+    def bridgeSignal(self, dummy, client, data):
+        try:
+            data['extra']['received_timestamp'] = data['received_timestamp']
+            data['extra']['delay_sender'] = data['delay_sender']
+        except KeyError:
+            pass
+        if data is not None:
+            self.host.bridge.messageNew(data['uid'], data['timestamp'], data['from'].full(), data['to'].full(), data['message'], data['subject'], data['type'], data['extra'], profile=client.profile)
+        return data
 
-        post_treat.addCallback(skipEmptyMessage)
-        post_treat.addCallback(addToHistory)
-        post_treat.addErrback(treatmentsEb)
-        post_treat.addCallback(bridgeSignal)
-        post_treat.addErrback(cancelErrorTrap)
-        post_treat.callback(data)
+    def cancelErrorTrap(self, failure_):
+        """A message sending can be cancelled by a plugin treatment"""
+        failure_.trap(exceptions.CancelError)
 
 
 class SatRosterProtocol(xmppim.RosterClientProtocol):
@@ -283,8 +280,10 @@
 
     def getAttributes(self, item):
         """Return dictionary of attributes as used in bridge from a RosterItem
+
         @param item: RosterItem
-        @return: dictionary of attributes"""
+        @return: dictionary of attributes
+        """
         item_attr = {'to': unicode(item.subscriptionTo),
                      'from': unicode(item.subscriptionFrom),
                      'ask': unicode(item.ask)
@@ -339,8 +338,9 @@
     def getItem(self, entity_jid):
         """Return RosterItem for a given jid
 
-        @param entity_jid: jid of the contact
-        @return: RosterItem or None if contact is not in cache
+        @param entity_jid(jid.JID): jid of the contact
+        @return(RosterItem, None): RosterItem instance
+            None if contact is not in cache
         """
         return self._jids.get(entity_jid, None)
 
@@ -384,6 +384,18 @@
         else:
             raise ValueError(u'Unexpected type_ {}'.format(type_))
 
+    def getNick(self, entity_jid):
+        """Return a nick name for an entity
+
+        return nick choosed by user if available
+        else return user part of entity_jid
+        """
+        item = self.getItem(entity_jid)
+        if item is None:
+            return entity_jid.user
+        else:
+            return item.name or entity_jid.user
+
 
 class SatPresenceProtocol(xmppim.PresenceClientProtocol):
 
@@ -474,7 +486,9 @@
             return
         self.send(presence_elt)
 
+    @defer.inlineCallbacks
     def subscribed(self, entity):
+        yield self.parent.roster.got_roster
         xmppim.PresenceClientProtocol.subscribed(self, entity)
         self.host.memory.delWaitingSub(entity.userhost(), self.parent.profile)
         item = self.parent.roster.getItem(entity)
@@ -494,8 +508,10 @@
         log.debug(_(u"unsubscription confirmed for [%s]") % entity.userhost())
         self.host.bridge.subscribe('unsubscribed', entity.userhost(), self.parent.profile)
 
+    @defer.inlineCallbacks
     def subscribeReceived(self, entity):
         log.debug(_(u"subscription request from [%s]") % entity.userhost())
+        yield self.parent.roster.got_roster
         item = self.parent.roster.getItem(entity)
         if item and item.subscriptionTo:
             # We automatically accept subscription if we are already subscribed to contact presence
@@ -505,8 +521,10 @@
             self.host.memory.addWaitingSub('subscribe', entity.userhost(), self.parent.profile)
             self.host.bridge.subscribe('subscribe', entity.userhost(), self.parent.profile)
 
+    @defer.inlineCallbacks
     def unsubscribeReceived(self, entity):
         log.debug(_(u"unsubscription asked for [%s]") % entity.userhost())
+        yield self.parent.roster.got_roster
         item = self.parent.roster.getItem(entity)
         if item and item.subscriptionFrom:  # we automatically remove contact
             log.debug(_('automatic contact deletion'))