core: sendMessage triggers now use a treatments deferred;

- treaments deferred can be used by plugins to change XML elements before sending them, in a similar way as for newMessage
    - sendMessageXml trigger became useless, so it as been removed in favor of the new deferred
This commit is contained in:
Goffi 2013-11-13 13:57:36 +01:00
commit 60f03a623e
5 changed files with 52 additions and 45 deletions

View file

@ -490,6 +490,7 @@ class SAT(service.Service):
"type": mess_type, "type": mess_type,
"options": options, "options": options,
} }
treatments = defer.Deferred() # XXX: plugin can add their treatments to this deferred
if mess_data["type"] == "auto": if mess_data["type"] == "auto":
# we try to guess the type # we try to guess the type
@ -512,38 +513,39 @@ class SAT(service.Service):
mess_data["type"] == "chat" if mess_data["subject"] else "normal" mess_data["type"] == "chat" if mess_data["subject"] else "normal"
if not no_trigger: if not no_trigger:
if not self.trigger.point("sendMessage", mess_data, profile): if not self.trigger.point("sendMessage", mess_data, treatments, profile):
return return
debug(_("Sending jabber message of type [%(type)s] to %(to)s...") % {"type": mess_data["type"], "to": to_jid.full()}) debug(_("Sending jabber message of type [%(type)s] to %(to)s...") % {"type": mess_data["type"], "to": to_jid.full()})
message = domish.Element((None, 'message')) mess_data['xml'] = domish.Element((None, 'message'))
message["to"] = mess_data["to"].full() mess_data['xml']["to"] = mess_data["to"].full()
message["from"] = current_jid.full() mess_data['xml']["from"] = current_jid.full()
message["type"] = mess_data["type"] mess_data['xml']["type"] = mess_data["type"]
if mess_data["subject"]: if mess_data["subject"]:
message.addElement("subject", None, subject) mess_data['xml'].addElement("subject", None, subject)
# message without body are used to send chat states # message without body are used to send chat states
if mess_data["message"]: if mess_data["message"]:
message.addElement("body", None, mess_data["message"]) mess_data['xml'].addElement("body", None, mess_data["message"])
if not no_trigger:
if not self.trigger.point("sendMessageXml", message, def sendAndStore(mess_data):
mess_data, profile): client.xmlstream.send(mess_data['xml'])
return if mess_data["type"] != "groupchat":
client.xmlstream.send(message) # we don't add groupchat message to history, as we get them back
if mess_data["type"] != "groupchat": # and they will be added then
# we don't add groupchat message to history, as we get them back self.memory.addToHistory(current_jid, mess_data['to'],
# and they will be added then unicode(mess_data["message"]),
self.memory.addToHistory(current_jid, mess_data['to'], unicode(mess_data["type"]),
unicode(mess_data["message"]), profile=profile)
unicode(mess_data["type"]), # We send back the message, so all clients are aware of it
profile=profile) if mess_data["message"]:
# We send back the message, so all clients are aware of it self.bridge.newMessage(mess_data['xml']['from'],
if mess_data["message"]: unicode(mess_data["message"]),
self.bridge.newMessage(message['from'], mess_type=mess_data["type"],
unicode(mess_data["message"]), to_jid=mess_data['xml']['to'], extra={},
mess_type=mess_data["type"], profile=profile)
to_jid=message['to'], extra={},
profile=profile) treatments.addCallback(sendAndStore)
treatments.callback(mess_data)
def setPresence(self, to="", show="", priority=0, statuses={}, profile_key='@DEFAULT@'): def setPresence(self, to="", show="", priority=0, statuses={}, profile_key='@DEFAULT@'):
"""Send our presence information""" """Send our presence information"""

View file

@ -112,7 +112,7 @@ class SatMessageProtocol(xmppim.MessageProtocol):
def onMessage(self, message): def onMessage(self, message):
debug(_(u"got message from: %s"), message["from"]) debug(_(u"got message from: %s"), message["from"])
post_treat = defer.Deferred() # XXX: plugin can add there treatments to this deferred post_treat = defer.Deferred() # XXX: plugin can add their treatments to this deferred
if not self.host.trigger.point("MessageReceived", message, post_treat, profile=self.parent.profile): if not self.host.trigger.point("MessageReceived", message, post_treat, profile=self.parent.profile):
return return

View file

@ -48,7 +48,7 @@ class Exp_Parrot(object):
host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100) host.trigger.add("MessageReceived", self.MessageReceivedTrigger, priority=100)
#host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100) #host.trigger.add("sendMessage", self.sendMessageTrigger, priority=100)
#def sendMessageTrigger(self, mess_data, profile): #def sendMessageTrigger(self, mess_data, treatments, profile):
# """ Deactivate other triggers if recipient is in parrot links """ # """ Deactivate other triggers if recipient is in parrot links """
# client = self.host.getClient(profile) # client = self.host.getClient(profile)
# try: # try:

View file

@ -42,7 +42,7 @@ class TextCommands(object):
self.host = host self.host = host
host.trigger.add("sendMessage", self.sendMessageTrigger) host.trigger.add("sendMessage", self.sendMessageTrigger)
def sendMessageTrigger(self, mess_data, profile): def sendMessageTrigger(self, mess_data, treatments, profile):
""" Check text commands in message, and react consequently """ """ Check text commands in message, and react consequently """
msg = mess_data["message"] msg = mess_data["message"]
if msg: if msg:

View file

@ -97,7 +97,7 @@ class XEP_0085(object):
# triggers from core # triggers from core
host.trigger.add("MessageReceived", self.messageReceivedTrigger) host.trigger.add("MessageReceived", self.messageReceivedTrigger)
host.trigger.add("sendMessageXml", self.sendMessageXmlTrigger) host.trigger.add("sendMessage", self.sendMessageTrigger)
host.trigger.add("paramUpdateTrigger", self.paramUpdateTrigger) host.trigger.add("paramUpdateTrigger", self.paramUpdateTrigger)
# TODO: handle profile disconnection (free memory in entity data) # TODO: handle profile disconnection (free memory in entity data)
@ -168,25 +168,30 @@ class XEP_0085(object):
break break
return True return True
def sendMessageXmlTrigger(self, message, mess_data, profile): def sendMessageTrigger(self, mess_data, treatments, profile):
""" """
Eventually add the chat state to the message and initiate Eventually add the chat state to the message and initiate
the state machine when sending an "active" state. the state machine when sending an "active" state.
""" """
to_jid = JID(message.getAttribute("to")) def treatment(mess_data):
if not self.__checkActivation(to_jid, forceEntityData=True, profile=profile): message = mess_data['xml']
return True to_jid = JID(message.getAttribute("to"))
try: if not self.__checkActivation(to_jid, forceEntityData=True, profile=profile):
# message with a body always mean active state return True
domish.generateElementsNamed(message.elements(), name="body").next() try:
message.addElement('active', NS_CHAT_STATES) # message with a body always mean active state
# launch the chat state machine (init the timer) domish.generateElementsNamed(message.elements(), name="body").next()
self.__chatStateActive(to_jid, mess_data["type"], profile) message.addElement('active', NS_CHAT_STATES)
except StopIteration: # launch the chat state machine (init the timer)
if "chat_state" in mess_data["options"]: self.__chatStateActive(to_jid, mess_data["type"], profile)
state = mess_data["options"]["chat_state"] except StopIteration:
assert(state in CHAT_STATES) if "chat_state" in mess_data["options"]:
message.addElement(state, NS_CHAT_STATES) state = mess_data["options"]["chat_state"]
assert(state in CHAT_STATES)
message.addElement(state, NS_CHAT_STATES)
return mess_data
treatments.addCallback(treatment)
return True return True
def __checkActivation(self, to_jid, forceEntityData, profile): def __checkActivation(self, to_jid, forceEntityData, profile):