# HG changeset patch # User Goffi # Date 1402151933 -7200 # Node ID 71d63750963ea375b3814f21135d65e1c68d3ac7 # Parent e88bff4c8b777bb45c25a31c432ecf2c51541ded core (XMPP): message received (onMessage) refactoring: - better separation of actions - use of failure.trap and generic exception (CancelError/SkipHistory) diff -r e88bff4c8b77 -r 71d63750963e src/core/xmpp.py --- a/src/core/xmpp.py Sat Jun 07 16:35:29 2014 +0200 +++ b/src/core/xmpp.py Sat Jun 07 16:38:53 2014 +0200 @@ -17,7 +17,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from sat.core.i18n import _, D_ +from sat.core.i18n import _ from sat.core.constants import Const as C from twisted.internet import task, defer from twisted.words.protocols.jabber import jid, xmlstream @@ -130,7 +130,12 @@ data['type'] = message['type'] if message.hasAttribute('type') else 'normal' - def after_treatments(data): + def bridgeSignal(data): + if data is not None: + self.host.bridge.newMessage(data['from'], data['body'], data['type'], data['to'], data['extra'], profile=self.parent.profile) + return data + + def addToHistory(data): # set message body to empty string by default, and later # also forward message without body (chat state notification...) try: @@ -142,9 +147,20 @@ self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], data['extra'], timestamp, profile=self.parent.profile) except IndexError: self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], data['extra'], profile=self.parent.profile) - self.host.bridge.newMessage(data['from'], data['body'], data['type'], data['to'], data['extra'], profile=self.parent.profile) + return data + + def treatmentsEb(failure): + failure.trap(exceptions.SkipHistory) + return data - post_treat.addCallback(after_treatments) + def cancelErrorTrap(failure): + """A message sending can be cancelled by a plugin treatment""" + failure.trap(exceptions.CancelError) + + post_treat.addCallback(addToHistory) + post_treat.addErrback(treatmentsEb) + post_treat.addCallback(bridgeSignal) + post_treat.addErrback(cancelErrorTrap) post_treat.callback(data)