diff src/plugins/plugin_xep_0033.py @ 999:c37a24922f27

plugin XEP_0033: fixes the server part and the tests
author souliane <souliane@mailoo.org>
date Fri, 11 Apr 2014 11:02:42 +0200
parents 301b342c697a
children e88bff4c8b77
line wrap: on
line diff
--- a/src/plugins/plugin_xep_0033.py	Wed Apr 23 12:01:59 2014 +0200
+++ b/src/plugins/plugin_xep_0033.py	Fri Apr 11 11:02:42 2014 +0200
@@ -82,13 +82,14 @@
             if not 'address' in mess_data['extra']:
                 return mess_data
 
-            def discoCallback(entity):
-                if entity is None:
+            def discoCallback(entities):
+                if not entities:
                     return Failure(AbortSendMessage(_("XEP-0033 is being used but the server doesn't support it!")))
-                if mess_data["to"] != entity:
-                    log.warning(_("Stanzas using XEP-0033 should be addressed to %(expected)s, not %(current)s!") % {'expected': entity, 'current': mess_data["to"]})
-                    log.warning(_("TODO: addressing has be fixed by the backend... fix it in the frontend!"))
-                    mess_data["to"] = entity
+                if mess_data["to"] not in entities:
+                    expected = _(' or ').join([entity.userhost() for entity in entities])
+                    log.warning(_("Stanzas using XEP-0033 should be addressed to %(expected)s, not %(current)s!") % {'expected': expected, 'current': mess_data["to"]})
+                    log.warning(_("TODO: addressing has been fixed by the backend... fix it in the frontend!"))
+                    mess_data["to"] = list(entities)[0].userhostJID()
                 element = mess_data['xml'].addElement('addresses', NS_ADDRESS)
                 entries = [entry.split(':') for entry in mess_data['extra']['address'].split('\n') if entry != '']
                 for type_, jid_ in entries:
@@ -96,7 +97,7 @@
                 # when the prosody plugin is completed, we can immediately return mess_data from here
                 self.sendAndStoreMessage(mess_data, entries, profile)
                 return Failure(MessageSentAndStored("XEP-0033 took over", mess_data))
-            d = self.host.requestServerDisco(NS_ADDRESS, profile_key=profile)
+            d = self.host.findFeaturesSet([NS_ADDRESS], profile_key=profile)
             d.addCallbacks(discoCallback, lambda dummy: discoCallback(None))
             return d
 
@@ -116,19 +117,22 @@
         - redesign the database to save only one entry to the database
         - change the newMessage signal to eventually pass more than one recipient
         """
-        def discoCallback(entity, to_jid):
-            new_data = copy.deepcopy(mess_data)
-            new_data['to'] = JID(to_jid)
-            new_data['xml']['to'] = to_jid
-            if entity:
-                if entity not in self.internal_data[timestamp]:
-                    self.host.sendAndStoreMessage(mess_data, False, profile)
-                    self.internal_data[timestamp].append(entity)
+        def discoCallback(entities, to_jid_s):
+            history_data = copy.deepcopy(mess_data)
+            history_data['to'] = JID(to_jid_s)
+            history_data['xml']['to'] = to_jid_s
+            if entities:
+                if entities not in self.internal_data[timestamp]:
+                    sent_data = copy.deepcopy(mess_data)
+                    sent_data['to'] = JID(JID(to_jid_s).host)
+                    sent_data['xml']['to'] = JID(to_jid_s).host
+                    self.host.sendAndStoreMessage(sent_data, False, profile)
+                    self.internal_data[timestamp].append(entities)
                 # we still need to fill the history and signal the echo...
-                self.host.sendAndStoreMessage(new_data, True, profile)
+                self.host.sendAndStoreMessage(history_data, True, profile)
             else:
                 # target server misses the addressing feature
-                self.host.sendAndStoreMessage(new_data, False, profile)
+                self.host.sendAndStoreMessage(history_data, False, profile)
 
         def errback(failure, to_jid):
             discoCallback(None, to_jid)
@@ -138,9 +142,9 @@
         defer_list = []
         for type_, jid_ in entries:
             d = defer.Deferred()
-            d.addCallback(self.host.requestServerDisco, JID(JID(jid_).host), profile_key=profile)
+            d.addCallback(self.host.findFeaturesSet, jid_=JID(JID(jid_).host), profile_key=profile)
             d.addCallbacks(discoCallback, errback, callbackArgs=[jid_], errbackArgs=[jid_])
-            d.callback(NS_ADDRESS)
+            d.callback([NS_ADDRESS])
             defer_list.append(d)
         d = defer.Deferred().addCallback(lambda dummy: self.internal_data.pop(timestamp))
         defer.DeferredList(defer_list).chainDeferred(d)