From d339e79b1c64fb8b41245217d260eb997b784570 Mon Sep 17 00:00:00 2001 From: Goffi Date: Fri, 22 Feb 2013 00:15:50 +0100 Subject: [PATCH] core: sendMessage refactoring: - sendMessage now use JID instead of unicode, bridge method using unicode is now _sendMessage - added no_trigger argument, so it's now possible to forbid trigger execution (to avoid any plugin when we send a message) --- src/core/sat_main.py | 19 ++++++++++++------- src/plugins/plugin_exp_parrot.py | 2 +- src/plugins/plugin_misc_smtp.py | 2 +- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/core/sat_main.py b/src/core/sat_main.py index 1852e735..4bed334f 100644 --- a/src/core/sat_main.py +++ b/src/core/sat_main.py @@ -125,7 +125,7 @@ class SAT(service.Service): self.bridge.register("getPresenceStatus", self.memory.getPresenceStatus) self.bridge.register("getWaitingSub", self.memory.getWaitingSub) self.bridge.register("getWaitingConf", self.getWaitingConf) - self.bridge.register("sendMessage", self.sendMessage) + self.bridge.register("sendMessage", self._sendMessage) self.bridge.register("getConfig", self.memory.getConfig) self.bridge.register("setParam", self.setParam) self.bridge.register("getParamA", self.memory.getStringParamA) @@ -467,14 +467,18 @@ class SAT(service.Service): ret.append((conf_id, conf_type, data)) return ret - def sendMessage(self, to, msg, subject=None, mess_type='auto', profile_key='@DEFAULT@'): + def _sendMessage(self, to_s, msg, subject=None, mess_type='auto', profile_key='@NONE@'): + to_jid = jid.JID(to_s) + self.sendMessage(to_jid, msg, subject, mess_type, profile_key) + + def sendMessage(self, to_jid, msg, subject=None, mess_type='auto', no_trigger = False, profile_key='@NONE@'): #FIXME: check validity of recipient profile = self.memory.getProfileName(profile_key) assert(profile) client = self.profiles[profile] current_jid = client.jid mess_data = { # we put data in a dict, so trigger methods can change them - "to": jid.JID(to), + "to": to_jid, "message": msg, "subject": subject, "type": mess_type @@ -500,10 +504,11 @@ class SAT(service.Service): mess_data["type"] == 'chat' mess_data["type"] == "chat" if mess_data["subject"] else "normal" - if not self.trigger.point("sendMessage", mess_data, profile): - return + if not no_trigger: + if not self.trigger.point("sendMessage", mess_data, profile): + return - debug(_("Sending jabber message of type [%(type)s] to %(to)s...") % {"type": mess_data["type"], "to": to}) + debug(_("Sending jabber message of type [%(type)s] to %(to)s...") % {"type": mess_data["type"], "to": to_jid.full()}) message = domish.Element((None, 'message')) message["to"] = mess_data["to"].full() message["from"] = current_jid.full() @@ -513,7 +518,7 @@ class SAT(service.Service): message.addElement("body", None, mess_data["message"]) client.xmlstream.send(message) if mess_data["type"] != "groupchat": - self.memory.addToHistory(current_jid, jid.JID(to), unicode(mess_data["message"]), unicode(mess_data["type"]), profile=profile) # we don't add groupchat message to history, as we get them back + self.memory.addToHistory(current_jid, mess_data['to'], unicode(mess_data["message"]), unicode(mess_data["type"]), profile=profile) # we don't add groupchat message to history, as we get them back # and they will be added then self.bridge.newMessage(message['from'], unicode(mess_data["message"]), mess_type=mess_data["type"], to_jid=message['to'], extra={}, profile=profile) # We send back the message, so all clients are aware of it diff --git a/src/plugins/plugin_exp_parrot.py b/src/plugins/plugin_exp_parrot.py index 784c59b1..cac76ad0 100755 --- a/src/plugins/plugin_exp_parrot.py +++ b/src/plugins/plugin_exp_parrot.py @@ -94,7 +94,7 @@ class Exp_Parrot(object): linked = _links[from_jid.userhostJID()] - self.host.sendMessage(unicode(linked), msg, None, "auto", profile_key=profile) + self.host._sendMessage(unicode(linked), msg, None, "auto", profile_key=profile) else: warning("No body element found in message, following normal behaviour") diff --git a/src/plugins/plugin_misc_smtp.py b/src/plugins/plugin_misc_smtp.py index 059a4153..d60d4bc0 100755 --- a/src/plugins/plugin_misc_smtp.py +++ b/src/plugins/plugin_misc_smtp.py @@ -90,7 +90,7 @@ class SatSmtpMessage(object): """handle end of message""" mail = Parser().parsestr("\n".join(self.message)) try: - self.host.sendMessage(parseaddr(mail['to'].decode('utf-8', 'replace'))[1], mail.get_payload().decode('utf-8', 'replace'), # TODO: manage other charsets + self.host._sendMessage(parseaddr(mail['to'].decode('utf-8', 'replace'))[1], mail.get_payload().decode('utf-8', 'replace'), # TODO: manage other charsets subject=mail['subject'].decode('utf-8', 'replace'), mess_type='normal', profile_key=self.profile) except: exc_type, exc_value, exc_traceback = sys.exc_info()