comparison src/plugins/plugin_xep_0033.py @ 750:c8b9f675ac17

plugin XEP-0033: avoid the controlled error to explode (use return Failure(...) instead of raise)
author souliane <souliane@mailoo.org>
date Mon, 16 Dec 2013 17:49:54 +0100
parents 192b804ee446
children bfabeedbf32e
comparison
equal deleted inserted replaced
749:192b804ee446 750:c8b9f675ac17
20 import logging 20 import logging
21 from sat.core import exceptions 21 from sat.core import exceptions
22 from wokkel import disco, iwokkel 22 from wokkel import disco, iwokkel
23 from zope.interface import implements 23 from zope.interface import implements
24 from twisted.words.protocols.jabber.jid import JID 24 from twisted.words.protocols.jabber.jid import JID
25 from twisted.python.failure import Failure
25 import copy 26 import copy
26 try: 27 try:
27 from twisted.words.protocols.xmlstream import XMPPHandler 28 from twisted.words.protocols.xmlstream import XMPPHandler
28 except ImportError: 29 except ImportError:
29 from wokkel.subprotocols import XMPPHandler 30 from wokkel.subprotocols import XMPPHandler
81 if not 'address' in mess_data['extra']: 82 if not 'address' in mess_data['extra']:
82 return mess_data 83 return mess_data
83 84
84 def discoCallback(entity): 85 def discoCallback(entity):
85 if entity is None: 86 if entity is None:
86 raise AbortSendMessage(_("XEP-0033 is being used but the server doesn't support it!")) 87 return Failure(AbortSendMessage(_("XEP-0033 is being used but the server doesn't support it!")))
87 to = JID(mess_data["to"].host) 88 to = JID(mess_data["to"].host)
88 if to != entity: 89 if to != entity:
89 logging.warning(_("Stanzas using XEP-0033 should be addressed to %s, not %s!") % (entity, to)) 90 logging.warning(_("Stanzas using XEP-0033 should be addressed to %s, not %s!") % (entity, to))
90 logging.warning(_("TODO: addressing has be fixed by the backend... fix it in the frontend!")) 91 logging.warning(_("TODO: addressing has be fixed by the backend... fix it in the frontend!"))
91 mess_data["to"] = entity 92 mess_data["to"] = entity
92 element = mess_data['xml'].addElement('addresses', NS_ADDRESS) 93 element = mess_data['xml'].addElement('addresses', NS_ADDRESS)
93 entries = [entry.split(':') for entry in mess_data['extra']['address'].split('\n') if entry != ''] 94 entries = [entry.split(':') for entry in mess_data['extra']['address'].split('\n') if entry != '']
94 for type_, jid_ in entries: 95 for type_, jid_ in entries:
95 element.addChild(domish.Element((None, 'address'), None, {'type': type_, 'jid': jid_})) 96 element.addChild(domish.Element((None, 'address'), None, {'type': type_, 'jid': jid_}))
96 # when the prosody plugin is completed, we can immediately return mess_data from here 97 # when the prosody plugin is completed, we can immediately return mess_data from here
97 return self.sendAndStoreMessage(mess_data, entries, profile) 98 self.sendAndStoreMessage(mess_data, entries, profile)
98 99 return Failure(MessageSentAndStored("XEP-0033 took over"))
99 d = self.host.requestServerDisco(NS_ADDRESS, profile_key=profile) 100 d = self.host.requestServerDisco(NS_ADDRESS, profile_key=profile)
100 d.addCallbacks(discoCallback, lambda dummy: discoCallback(None)) 101 d.addCallbacks(discoCallback, lambda dummy: discoCallback(None))
101 return d 102 return d
102 103
103 treatments.addCallback(treatment) 104 treatments.addCallback(treatment)
104 return True 105 return True
105 106
106 def sendAndStoreMessage(self, mess_data, entries, profile): 107 def sendAndStoreMessage(self, mess_data, entries, profile):
107 """Check if target servers support XEP-0033, send and store the messages 108 """Check if target servers support XEP-0033, send and store the messages
108 @raise: a friendly failure to let the core know that we sent the message already 109 @return: a friendly failure to let the core know that we sent the message already
109 110
110 Later we should be able to remove this method because: 111 Later we should be able to remove this method because:
111 # XXX: sending the messages should be done by the local server 112 # XXX: sending the messages should be done by the local server
112 # FIXME: for now we duplicate the messages in the history for each recipient, this should change 113 # FIXME: for now we duplicate the messages in the history for each recipient, this should change
113 # FIXME: for now we duplicate the echoes to the sender, this should also change 114 # FIXME: for now we duplicate the echoes to the sender, this should also change
143 d.callback(NS_ADDRESS) 144 d.callback(NS_ADDRESS)
144 defer_list.append(d) 145 defer_list.append(d)
145 d = defer.Deferred().addCallback(lambda dummy: self.internal_data.pop(timestamp)) 146 d = defer.Deferred().addCallback(lambda dummy: self.internal_data.pop(timestamp))
146 defer.DeferredList(defer_list).chainDeferred(d) 147 defer.DeferredList(defer_list).chainDeferred(d)
147 148
148 raise MessageSentAndStored("XEP-0033 took over")
149
150 def messageReceivedTrigger(self, message, post_treat, profile): 149 def messageReceivedTrigger(self, message, post_treat, profile):
151 """In order to save the addressing information in the history""" 150 """In order to save the addressing information in the history"""
152 def post_treat_addr(data, addresses): 151 def post_treat_addr(data, addresses):
153 data['extra']['addresses'] = "" 152 data['extra']['addresses'] = ""
154 for address in addresses: 153 for address in addresses: