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

@ -48,7 +48,7 @@ class Exp_Parrot(object):
host.trigger.add("MessageReceived", self.MessageReceivedTrigger, 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 """
# client = self.host.getClient(profile)
# try:

View file

@ -42,7 +42,7 @@ class TextCommands(object):
self.host = host
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 """
msg = mess_data["message"]
if msg:

View file

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