# HG changeset patch # User Goffi # Date 1345165863 -7200 # Node ID f36c705a53105d4a243dd98d22c684d3742619c0 # Parent 6edb4219fcf7b451659cfba70d5619fdeae61682 core: sendMessage refactoring: - "auto" message type which autodetect XMPP message type according to context - added "sendMessage" trigger so plugins can hook messages diff -r 6edb4219fcf7 -r f36c705a5310 src/core/sat_main.py --- a/src/core/sat_main.py Fri Aug 17 03:08:37 2012 +0200 +++ b/src/core/sat_main.py Fri Aug 17 03:11:03 2012 +0200 @@ -26,6 +26,7 @@ from twisted.application import service from twisted.internet import defer +from twisted.internet.defer import inlineCallbacks, returnValue from twisted.words.protocols.jabber import jid, xmlstream from twisted.words.xish import domish @@ -43,7 +44,7 @@ import os.path from sat.core import xmpp -from sat.core.exceptions import ProfileUnknownError +from sat.core.exceptions import ProfileUnknownError, UnknownEntityError from sat.memory.memory import Memory from sat.tools.xml_tools import tupleList2dataForm from sat.tools.misc import TriggerManager @@ -445,24 +446,53 @@ ## jabber methods ## - def sendMessage(self, to, msg, subject=None, type='chat', profile_key='@DEFAULT@'): + def sendMessage(self, to, msg, subject=None, mess_type='auto', profile_key='@DEFAULT@'): #FIXME: check validity of recipient profile = self.memory.getProfileName(profile_key) assert(profile) - current_jid = self.profiles[profile].jid - debug(_("Sending jabber message to %s..."), to) + 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), + "message": msg, + "subject": subject, + "type": mess_type + } + if not self.trigger.point("sendMessage", mess_data, profile): + return + if mess_data["type"] == "auto": + #XXX: if the type has not been changed by a trigger method, + # we try to guess it + if mess_data["subject"]: + mess_data["type"] = 'normal' + elif not mess_data["to"].resource: #if to JID has a resource, the type is not 'groupchat' + #we may have a groupchat message, we check if the we know this jid + try: + entity_type = self.memory.getEntityData(mess_data["to"], ['type'], profile)["type"] + #FIXME: should entity_type manage ressources ? + except (UnknownEntityError, KeyError): + entity_type = "contact" + + if entity_type == "chatroom": + mess_data["type"] = 'groupchat' + else: + mess_data["type"] = 'chat' + else: + mess_data["type"] == 'chat' + mess_data["type"] == "chat" if mess_data["subject"] else "normal" + debug(_("Sending jabber message of type [%(type)s] to %(to)s...") % {"type": mess_data["type"], "to": to}) message = domish.Element(('jabber:client','message')) - message["to"] = jid.JID(to).full() + message["to"] = mess_data["to"].full() message["from"] = current_jid.full() - message["type"] = type - if subject: + message["type"] = mess_data["type"] + if mess_data["subject"]: message.addElement("subject", "jabber:client", subject) message.addElement("body", "jabber:client", msg) - self.profiles[profile].xmlstream.send(message) - if type!="groupchat": + client.xmlstream.send(message) + if mess_data["type"]!="groupchat": self.memory.addToHistory(current_jid, jid.JID(to), unicode(msg), profile=profile) #we don't add groupchat message to history, as we get them back - #and they will we added then - self.bridge.newMessage(message['from'], unicode(msg), mess_type=type, to_jid=message['to'], profile=profile) #We send back the message, so all clients are aware of it + #and they will be added then + self.bridge.newMessage(message['from'], unicode(msg), mess_type=mess_data["type"], to_jid=message['to'], profile=profile) #We send back the message, so all clients are aware of it def setPresence(self, to="", show="", priority = 0, statuses={}, profile_key='@DEFAULT@'):