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
This commit is contained in:
Goffi 2014-06-07 16:35:29 +02:00
commit 87b3e413ee
4 changed files with 74 additions and 71 deletions

View file

@ -20,10 +20,11 @@
from sat.core.i18n import _
from sat.core.log import getLogger
log = getLogger(__name__)
from sat.core import exceptions
from wokkel import disco, iwokkel
from zope.interface import implements
from twisted.words.protocols.jabber.jid import JID
from twisted.python.failure import Failure
from twisted.python import failure
import copy
try:
from twisted.words.protocols.xmlstream import XMPPHandler
@ -32,7 +33,6 @@ except ImportError:
from twisted.words.xish import domish
from twisted.internet import defer
from sat.core.sat_main import MessageSentAndStored, AbortSendMessage
from sat.tools.misc import TriggerManager
from time import time
@ -84,7 +84,8 @@ class XEP_0033(object):
def discoCallback(entities):
if not entities:
return Failure(AbortSendMessage(_("XEP-0033 is being used but the server doesn't support it!")))
log.warning(_("XEP-0033 is being used but the server doesn't support it!"))
raise failure.Failure(exceptions.CancelError())
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"]})
@ -96,7 +97,8 @@ class XEP_0033(object):
element.addChild(domish.Element((None, 'address'), None, {'type': type_, 'jid': jid_}))
# 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))
log.debug("XEP-0033 took over")
raise failure.Failure(exceptions.CancelError())
d = self.host.findFeaturesSet([NS_ADDRESS], profile_key=profile)
d.addCallbacks(discoCallback, lambda dummy: discoCallback(None))
return d