core: sendMessage refactoring:

- "auto" message type which autodetect  XMPP message type according to context
		- added "sendMessage" trigger so plugins can hook messages
This commit is contained in:
Goffi 2012-08-17 03:11:03 +02:00
commit fa7537b017

View file

@ -26,6 +26,7 @@ CONST = {
from twisted.application import service from twisted.application import service
from twisted.internet import defer from twisted.internet import defer
from twisted.internet.defer import inlineCallbacks, returnValue
from twisted.words.protocols.jabber import jid, xmlstream from twisted.words.protocols.jabber import jid, xmlstream
from twisted.words.xish import domish from twisted.words.xish import domish
@ -43,7 +44,7 @@ import sys
import os.path import os.path
from sat.core import xmpp 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.memory.memory import Memory
from sat.tools.xml_tools import tupleList2dataForm from sat.tools.xml_tools import tupleList2dataForm
from sat.tools.misc import TriggerManager from sat.tools.misc import TriggerManager
@ -445,24 +446,53 @@ class SAT(service.Service):
## jabber methods ## ## 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 #FIXME: check validity of recipient
profile = self.memory.getProfileName(profile_key) profile = self.memory.getProfileName(profile_key)
assert(profile) assert(profile)
current_jid = self.profiles[profile].jid client = self.profiles[profile]
debug(_("Sending jabber message to %s..."), to) 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 = domish.Element(('jabber:client','message'))
message["to"] = jid.JID(to).full() message["to"] = mess_data["to"].full()
message["from"] = current_jid.full() message["from"] = current_jid.full()
message["type"] = type message["type"] = mess_data["type"]
if subject: if mess_data["subject"]:
message.addElement("subject", "jabber:client", subject) message.addElement("subject", "jabber:client", subject)
message.addElement("body", "jabber:client", msg) message.addElement("body", "jabber:client", msg)
self.profiles[profile].xmlstream.send(message) client.xmlstream.send(message)
if type!="groupchat": 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 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 #and they will be 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 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@'): def setPresence(self, to="", show="", priority = 0, statuses={}, profile_key='@DEFAULT@'):