From 93826f1ce8534c965bca71dc1a18e6e70e759df5 Mon Sep 17 00:00:00 2001 From: Goffi Date: Sat, 22 Mar 2014 15:34:05 +0100 Subject: [PATCH] core: sendMessageTrigger now manage pre and post treatments, which happen before or after XML generation --- src/core/sat_main.py | 36 ++++++++++++++---------- src/plugins/plugin_misc_text_commands.py | 4 +-- src/plugins/plugin_xep_0033.py | 4 +-- src/plugins/plugin_xep_0071.py | 4 +-- src/plugins/plugin_xep_0085.py | 4 +-- 5 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/core/sat_main.py b/src/core/sat_main.py index 5843d45a..f595c7f3 100644 --- a/src/core/sat_main.py +++ b/src/core/sat_main.py @@ -489,7 +489,8 @@ class SAT(service.Service): "type": mess_type, "extra": extra, } - treatments = defer.Deferred() # XXX: plugin can add their treatments to this deferred + pre_xml_treatments = defer.Deferred() # XXX: plugin can add their pre XML treatments to this deferred + post_xml_treatments = defer.Deferred() # XXX: plugin can add their post XML treatments to this deferred if mess_data["type"] == "auto": # we try to guess the type @@ -512,19 +513,22 @@ class SAT(service.Service): mess_data["type"] == "chat" if mess_data["subject"] else "normal" if not no_trigger: - if not self.trigger.point("sendMessage", mess_data, treatments, profile): + if not self.trigger.point("sendMessage", mess_data, pre_xml_treatments, post_xml_treatments, profile): return defer.succeed(None) debug(_("Sending jabber message of type [%(type)s] to %(to)s...") % {"type": mess_data["type"], "to": to_jid.full()}) - mess_data['xml'] = domish.Element((None, 'message')) - mess_data['xml']["to"] = mess_data["to"].full() - mess_data['xml']["from"] = current_jid.full() - mess_data['xml']["type"] = mess_data["type"] - if mess_data["subject"]: - mess_data['xml'].addElement("subject", None, subject) - # message without body are used to send chat states - if mess_data["message"]: - mess_data['xml'].addElement("body", None, mess_data["message"]) + + def generateXML(mess_data): + mess_data['xml'] = domish.Element((None, 'message')) + mess_data['xml']["to"] = mess_data["to"].full() + mess_data['xml']["from"] = current_jid.full() + mess_data['xml']["type"] = mess_data["type"] + if mess_data["subject"]: + mess_data['xml'].addElement("subject", None, subject) + # message without body are used to send chat states + if mess_data["message"]: + mess_data['xml'].addElement("body", None, mess_data["message"]) + return mess_data def sendErrback(e): text = '%s: %s' % (e.value.__class__.__name__, e.getErrorMessage()) @@ -536,10 +540,12 @@ class SAT(service.Service): else: error("Unmanaged exception: %s" % text) return e - - treatments.addCallbacks(self.sendAndStoreMessage, sendErrback, [False, profile]) - treatments.callback(mess_data) - return treatments + pre_xml_treatments.addCallback(generateXML) + pre_xml_treatments.chainDeferred(post_xml_treatments) + post_xml_treatments.addCallback(self.sendAndStoreMessage, False, profile) + post_xml_treatments.addErrback(sendErrback) + pre_xml_treatments.callback(mess_data) + return pre_xml_treatments def sendAndStoreMessage(self, mess_data, skip_send=False, profile=None): """Actually send and store the message to history, after all the treatments have been done. diff --git a/src/plugins/plugin_misc_text_commands.py b/src/plugins/plugin_misc_text_commands.py index e3c578ca..a2626b20 100755 --- a/src/plugins/plugin_misc_text_commands.py +++ b/src/plugins/plugin_misc_text_commands.py @@ -46,9 +46,9 @@ class TextCommands(object): self.host = host host.trigger.add("sendMessage", self.sendMessageTrigger) - def sendMessageTrigger(self, mess_data, treatments, profile): + def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile): """ Install SendMessage command hook """ - treatments.addCallback(self._sendMessageCmdHook, profile) + pre_xml_treatments.addCallback(self._sendMessageCmdHook, profile) return True def _sendMessageCmdHook(self, mess_data, profile): diff --git a/src/plugins/plugin_xep_0033.py b/src/plugins/plugin_xep_0033.py index c6428306..c8e004a5 100644 --- a/src/plugins/plugin_xep_0033.py +++ b/src/plugins/plugin_xep_0033.py @@ -76,7 +76,7 @@ class XEP_0033(object): host.trigger.add("sendMessage", self.sendMessageTrigger, TriggerManager.MIN_PRIORITY) host.trigger.add("MessageReceived", self.messageReceivedTrigger) - def sendMessageTrigger(self, mess_data, treatments, profile): + def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile): """Process the XEP-0033 related data to be sent""" def treatment(mess_data): @@ -101,7 +101,7 @@ class XEP_0033(object): d.addCallbacks(discoCallback, lambda dummy: discoCallback(None)) return d - treatments.addCallback(treatment) + post_xml_treatments.addCallback(treatment) return True def sendAndStoreMessage(self, mess_data, entries, profile): diff --git a/src/plugins/plugin_xep_0071.py b/src/plugins/plugin_xep_0071.py index 10b6ccf4..5a1a9b63 100644 --- a/src/plugins/plugin_xep_0071.py +++ b/src/plugins/plugin_xep_0071.py @@ -130,11 +130,11 @@ class XEP_0071(object): pass return True - def sendMessageTrigger(self, mess_data, treatments, profile): + def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile): """ Check presence of rich text in extra """ if 'rich' in mess_data['extra'] or 'xhtml' in mess_data['extra']: - treatments.addCallback(self._sendMessageAddRich, profile) + post_xml_treatments.addCallback(self._sendMessageAddRich, profile) return True def _purgeStyle(self, styles_raw): diff --git a/src/plugins/plugin_xep_0085.py b/src/plugins/plugin_xep_0085.py index d8099f2c..8ef1c8b2 100755 --- a/src/plugins/plugin_xep_0085.py +++ b/src/plugins/plugin_xep_0085.py @@ -175,7 +175,7 @@ class XEP_0085(object): break return True - def sendMessageTrigger(self, mess_data, treatments, profile): + def sendMessageTrigger(self, mess_data, pre_xml_treatments, post_xml_treatments, profile): """ Eventually add the chat state to the message and initiate the state machine when sending an "active" state. @@ -198,7 +198,7 @@ class XEP_0085(object): message.addElement(state, NS_CHAT_STATES) return mess_data - treatments.addCallback(treatment) + post_xml_treatments.addCallback(treatment) return True def __checkActivation(self, to_jid, forceEntityData, profile):