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:
parent
df74d5f15e
commit
fa7537b017
1 changed files with 41 additions and 11 deletions
|
|
@ -26,6 +26,7 @@ CONST = {
|
|||
|
||||
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 sys
|
|||
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 @@ class SAT(service.Service):
|
|||
|
||||
## 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@'):
|
||||
|
|
|
|||
Loading…
Reference in a new issue