plugin XEP-0033: avoid the controlled error to explode (use return Failure(...) instead of raise)

This commit is contained in:
souliane 2013-12-16 17:49:54 +01:00
commit 09e5cc47bb

View file

@ -22,6 +22,7 @@ from sat.core import exceptions
from wokkel import disco, iwokkel from wokkel import disco, iwokkel
from zope.interface import implements from zope.interface import implements
from twisted.words.protocols.jabber.jid import JID from twisted.words.protocols.jabber.jid import JID
from twisted.python.failure import Failure
import copy import copy
try: try:
from twisted.words.protocols.xmlstream import XMPPHandler from twisted.words.protocols.xmlstream import XMPPHandler
@ -83,7 +84,7 @@ class XEP_0033(object):
def discoCallback(entity): def discoCallback(entity):
if entity is None: if entity is None:
raise AbortSendMessage(_("XEP-0033 is being used but the server doesn't support it!")) return Failure(AbortSendMessage(_("XEP-0033 is being used but the server doesn't support it!")))
to = JID(mess_data["to"].host) to = JID(mess_data["to"].host)
if to != entity: if to != entity:
logging.warning(_("Stanzas using XEP-0033 should be addressed to %s, not %s!") % (entity, to)) logging.warning(_("Stanzas using XEP-0033 should be addressed to %s, not %s!") % (entity, to))
@ -94,8 +95,8 @@ class XEP_0033(object):
for type_, jid_ in entries: for type_, jid_ in entries:
element.addChild(domish.Element((None, 'address'), None, {'type': type_, 'jid': jid_})) 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 # when the prosody plugin is completed, we can immediately return mess_data from here
return self.sendAndStoreMessage(mess_data, entries, profile) self.sendAndStoreMessage(mess_data, entries, profile)
return Failure(MessageSentAndStored("XEP-0033 took over"))
d = self.host.requestServerDisco(NS_ADDRESS, profile_key=profile) d = self.host.requestServerDisco(NS_ADDRESS, profile_key=profile)
d.addCallbacks(discoCallback, lambda dummy: discoCallback(None)) d.addCallbacks(discoCallback, lambda dummy: discoCallback(None))
return d return d
@ -105,7 +106,7 @@ class XEP_0033(object):
def sendAndStoreMessage(self, mess_data, entries, profile): def sendAndStoreMessage(self, mess_data, entries, profile):
"""Check if target servers support XEP-0033, send and store the messages """Check if target servers support XEP-0033, send and store the messages
@raise: a friendly failure to let the core know that we sent the message already @return: a friendly failure to let the core know that we sent the message already
Later we should be able to remove this method because: Later we should be able to remove this method because:
# XXX: sending the messages should be done by the local server # XXX: sending the messages should be done by the local server
@ -145,8 +146,6 @@ class XEP_0033(object):
d = defer.Deferred().addCallback(lambda dummy: self.internal_data.pop(timestamp)) d = defer.Deferred().addCallback(lambda dummy: self.internal_data.pop(timestamp))
defer.DeferredList(defer_list).chainDeferred(d) defer.DeferredList(defer_list).chainDeferred(d)
raise MessageSentAndStored("XEP-0033 took over")
def messageReceivedTrigger(self, message, post_treat, profile): def messageReceivedTrigger(self, message, post_treat, profile):
"""In order to save the addressing information in the history""" """In order to save the addressing information in the history"""
def post_treat_addr(data, addresses): def post_treat_addr(data, addresses):