Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0033.py @ 1052:e88bff4c8b77
core (XMPP): sendMessage refactoring:
- better separation of message sending actions
- use of more generic exceptions to hook the behaviour (SkipHistory and CancelError)
- use of raise instead of return
- use of failure.trap
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 07 Jun 2014 16:35:29 +0200 |
parents | c37a24922f27 |
children | 3e234902177a |
comparison
equal
deleted
inserted
replaced
1051:854880a31717 | 1052:e88bff4c8b77 |
---|---|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
19 | 19 |
20 from sat.core.i18n import _ | 20 from sat.core.i18n import _ |
21 from sat.core.log import getLogger | 21 from sat.core.log import getLogger |
22 log = getLogger(__name__) | 22 log = getLogger(__name__) |
23 from sat.core import exceptions | |
23 from wokkel import disco, iwokkel | 24 from wokkel import disco, iwokkel |
24 from zope.interface import implements | 25 from zope.interface import implements |
25 from twisted.words.protocols.jabber.jid import JID | 26 from twisted.words.protocols.jabber.jid import JID |
26 from twisted.python.failure import Failure | 27 from twisted.python import failure |
27 import copy | 28 import copy |
28 try: | 29 try: |
29 from twisted.words.protocols.xmlstream import XMPPHandler | 30 from twisted.words.protocols.xmlstream import XMPPHandler |
30 except ImportError: | 31 except ImportError: |
31 from wokkel.subprotocols import XMPPHandler | 32 from wokkel.subprotocols import XMPPHandler |
32 from twisted.words.xish import domish | 33 from twisted.words.xish import domish |
33 from twisted.internet import defer | 34 from twisted.internet import defer |
34 | 35 |
35 from sat.core.sat_main import MessageSentAndStored, AbortSendMessage | |
36 from sat.tools.misc import TriggerManager | 36 from sat.tools.misc import TriggerManager |
37 from time import time | 37 from time import time |
38 | 38 |
39 # TODO: fix Prosody "addressing" plugin to leave the concerned bcc according to the spec: | 39 # TODO: fix Prosody "addressing" plugin to leave the concerned bcc according to the spec: |
40 # | 40 # |
82 if not 'address' in mess_data['extra']: | 82 if not 'address' in mess_data['extra']: |
83 return mess_data | 83 return mess_data |
84 | 84 |
85 def discoCallback(entities): | 85 def discoCallback(entities): |
86 if not entities: | 86 if not entities: |
87 return Failure(AbortSendMessage(_("XEP-0033 is being used but the server doesn't support it!"))) | 87 log.warning(_("XEP-0033 is being used but the server doesn't support it!")) |
88 raise failure.Failure(exceptions.CancelError()) | |
88 if mess_data["to"] not in entities: | 89 if mess_data["to"] not in entities: |
89 expected = _(' or ').join([entity.userhost() for entity in entities]) | 90 expected = _(' or ').join([entity.userhost() for entity in entities]) |
90 log.warning(_("Stanzas using XEP-0033 should be addressed to %(expected)s, not %(current)s!") % {'expected': expected, 'current': mess_data["to"]}) | 91 log.warning(_("Stanzas using XEP-0033 should be addressed to %(expected)s, not %(current)s!") % {'expected': expected, 'current': mess_data["to"]}) |
91 log.warning(_("TODO: addressing has been fixed by the backend... fix it in the frontend!")) | 92 log.warning(_("TODO: addressing has been fixed by the backend... fix it in the frontend!")) |
92 mess_data["to"] = list(entities)[0].userhostJID() | 93 mess_data["to"] = list(entities)[0].userhostJID() |
94 entries = [entry.split(':') for entry in mess_data['extra']['address'].split('\n') if entry != ''] | 95 entries = [entry.split(':') for entry in mess_data['extra']['address'].split('\n') if entry != ''] |
95 for type_, jid_ in entries: | 96 for type_, jid_ in entries: |
96 element.addChild(domish.Element((None, 'address'), None, {'type': type_, 'jid': jid_})) | 97 element.addChild(domish.Element((None, 'address'), None, {'type': type_, 'jid': jid_})) |
97 # when the prosody plugin is completed, we can immediately return mess_data from here | 98 # when the prosody plugin is completed, we can immediately return mess_data from here |
98 self.sendAndStoreMessage(mess_data, entries, profile) | 99 self.sendAndStoreMessage(mess_data, entries, profile) |
99 return Failure(MessageSentAndStored("XEP-0033 took over", mess_data)) | 100 log.debug("XEP-0033 took over") |
101 raise failure.Failure(exceptions.CancelError()) | |
100 d = self.host.findFeaturesSet([NS_ADDRESS], profile_key=profile) | 102 d = self.host.findFeaturesSet([NS_ADDRESS], profile_key=profile) |
101 d.addCallbacks(discoCallback, lambda dummy: discoCallback(None)) | 103 d.addCallbacks(discoCallback, lambda dummy: discoCallback(None)) |
102 return d | 104 return d |
103 | 105 |
104 post_xml_treatments.addCallback(treatment) | 106 post_xml_treatments.addCallback(treatment) |