changeset 1053:71d63750963e

core (XMPP): message received (onMessage) refactoring: - better separation of actions - use of failure.trap and generic exception (CancelError/SkipHistory)
author Goffi <goffi@goffi.org>
date Sat, 07 Jun 2014 16:38:53 +0200
parents e88bff4c8b77
children a32ef03d4af0
files src/core/xmpp.py
diffstat 1 files changed, 20 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- 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 <http://www.gnu.org/licenses/>.
 
-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)