core: Deferred in onMessage.

onMessage now use a deferred which is passed to MessageReceived trigger through the post_treat parameter. This can be used by plugins to add deferred in the callback chain.
This commit is contained in:
Goffi 2013-10-31 17:18:04 +01:00
commit 59b4505dfd
5 changed files with 32 additions and 21 deletions

View file

@ -106,28 +106,39 @@ 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"])
if not self.host.trigger.point("MessageReceived", message, profile=self.parent.profile): post_treat = defer.Deferred() # XXX: plugin can add there treatments to this deferred
if not self.host.trigger.point("MessageReceived", message, post_treat, profile=self.parent.profile):
return return
# set message body to empty string by default, and later
# also forward message without body (chat state notification...) data = {"from": message['from'],
mess_body = "" "to": message['to'],
"body": "",
"extra": {}}
for e in message.elements(): for e in message.elements():
if e.name == "body": if e.name == "body":
mess_body = e.children[0] if e.children else "" data['body'] = e.children[0] if e.children else ""
break break
mess_type = message['type'] if message.hasAttribute('type') else 'normal' data['type'] = message['type'] if message.hasAttribute('type') else 'normal'
try:
_delay = delay.Delay.fromElement(filter(lambda elm: elm.name == 'delay', message.elements())[0]) def after_treatments(data):
timestamp = timegm(_delay.stamp.utctimetuple()) # set message body to empty string by default, and later
extra = {"archive": str(timestamp)} # also forward message without body (chat state notification...)
if mess_type != 'groupchat': # XXX: we don't save delayed messages in history for groupchats try:
#TODO: add delayed messages to history if they aren't already in it _delay = delay.Delay.fromElement(filter(lambda elm: elm.name == 'delay', message.elements())[0])
self.host.memory.addToHistory(jid.JID(message["from"]), jid.JID(message["to"]), mess_body, mess_type, timestamp, profile=self.parent.profile) timestamp = timegm(_delay.stamp.utctimetuple())
except IndexError: data['extra']['archive'] = str(timestamp)
extra = {} if data['type'] != 'groupchat': # XXX: we don't save delayed messages in history for groupchats
self.host.memory.addToHistory(jid.JID(message["from"]), jid.JID(message["to"]), mess_body, mess_type, profile=self.parent.profile) #TODO: add delayed messages to history if they aren't already in it
self.host.bridge.newMessage(message["from"], mess_body, mess_type, message['to'], extra, profile=self.parent.profile) self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], timestamp, profile=self.parent.profile)
except IndexError:
self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], profile=self.parent.profile)
self.host.bridge.newMessage(data['from'], data['body'], data['type'], data['to'], data['extra'], profile=self.parent.profile)
post_treat.addCallback(after_treatments)
post_treat.callback(data)
class SatRosterProtocol(xmppim.RosterClientProtocol): class SatRosterProtocol(xmppim.RosterClientProtocol):

View file

@ -100,7 +100,7 @@ class CommandExport(object):
except ValueError: except ValueError:
pass pass
def MessageReceivedTrigger(self, message, profile): def MessageReceivedTrigger(self, message, post_treat, profile):
""" Check if source is linked and repeat message, else do nothing """ """ Check if source is linked and repeat message, else do nothing """
from_jid = jid.JID(message["from"]) from_jid = jid.JID(message["from"])
spawned_key = (from_jid.userhostJID(), profile) spawned_key = (from_jid.userhostJID(), profile)

View file

@ -60,7 +60,7 @@ class Exp_Parrot(object):
# debug("Parrot link detected, skipping other triggers") # debug("Parrot link detected, skipping other triggers")
# raise SkipOtherTriggers # raise SkipOtherTriggers
def MessageReceivedTrigger(self, message, profile): def MessageReceivedTrigger(self, message, post_treat, profile):
""" Check if source is linked and repeat message, else do nothing """ """ Check if source is linked and repeat message, else do nothing """
client = self.host.getClient(profile) client = self.host.getClient(profile)
from_jid = jid.JID(message["from"]) from_jid = jid.JID(message["from"])

View file

@ -86,7 +86,7 @@ class MaildirBox(object):
del self.__mailboxes[profile] del self.__mailboxes[profile]
del self.data[profile] del self.data[profile]
def messageReceivedTrigger(self, message, profile): def messageReceivedTrigger(self, message, post_treat, profile):
"""This trigger catch normal message and put the in the Maildir box. """This trigger catch normal message and put the in the Maildir box.
If the message is not of "normal" type, do nothing If the message is not of "normal" type, do nothing
@param message: message xmlstrem @param message: message xmlstrem

View file

@ -134,7 +134,7 @@ class XEP_0085(object):
if (category, name) == (PARAM_KEY, PARAM_NAME): if (category, name) == (PARAM_KEY, PARAM_NAME):
self.updateEntityData("@ALL@", True if value == "true" else "@NONE@", profile) self.updateEntityData("@ALL@", True if value == "true" else "@NONE@", profile)
def messageReceivedTrigger(self, message, profile): def messageReceivedTrigger(self, message, post_treat, profile):
""" """
Update the entity cache when we receive a message with body. Update the entity cache when we receive a message with body.
Check for a check state in the incoming message and broadcast signal. Check for a check state in the incoming message and broadcast signal.