comparison src/core/xmpp.py @ 663:8004c7d4aba7

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.
author Goffi <goffi@goffi.org>
date Thu, 31 Oct 2013 17:18:04 +0100
parents 7ea6d5a86e58
children 2a7185b8452c
comparison
equal deleted inserted replaced
662:4f747d7fde8c 663:8004c7d4aba7
104 xmppim.MessageProtocol.__init__(self) 104 xmppim.MessageProtocol.__init__(self)
105 self.host = host 105 self.host = host
106 106
107 def onMessage(self, message): 107 def onMessage(self, message):
108 debug(_(u"got message from: %s"), message["from"]) 108 debug(_(u"got message from: %s"), message["from"])
109 if not self.host.trigger.point("MessageReceived", message, profile=self.parent.profile): 109 post_treat = defer.Deferred() # XXX: plugin can add there treatments to this deferred
110 return 110
111 # set message body to empty string by default, and later 111 if not self.host.trigger.point("MessageReceived", message, post_treat, profile=self.parent.profile):
112 # also forward message without body (chat state notification...) 112 return
113 mess_body = "" 113
114 data = {"from": message['from'],
115 "to": message['to'],
116 "body": "",
117 "extra": {}}
118
114 for e in message.elements(): 119 for e in message.elements():
115 if e.name == "body": 120 if e.name == "body":
116 mess_body = e.children[0] if e.children else "" 121 data['body'] = e.children[0] if e.children else ""
117 break 122 break
118 123
119 mess_type = message['type'] if message.hasAttribute('type') else 'normal' 124 data['type'] = message['type'] if message.hasAttribute('type') else 'normal'
120 try: 125
121 _delay = delay.Delay.fromElement(filter(lambda elm: elm.name == 'delay', message.elements())[0]) 126 def after_treatments(data):
122 timestamp = timegm(_delay.stamp.utctimetuple()) 127 # set message body to empty string by default, and later
123 extra = {"archive": str(timestamp)} 128 # also forward message without body (chat state notification...)
124 if mess_type != 'groupchat': # XXX: we don't save delayed messages in history for groupchats 129 try:
125 #TODO: add delayed messages to history if they aren't already in it 130 _delay = delay.Delay.fromElement(filter(lambda elm: elm.name == 'delay', message.elements())[0])
126 self.host.memory.addToHistory(jid.JID(message["from"]), jid.JID(message["to"]), mess_body, mess_type, timestamp, profile=self.parent.profile) 131 timestamp = timegm(_delay.stamp.utctimetuple())
127 except IndexError: 132 data['extra']['archive'] = str(timestamp)
128 extra = {} 133 if data['type'] != 'groupchat': # XXX: we don't save delayed messages in history for groupchats
129 self.host.memory.addToHistory(jid.JID(message["from"]), jid.JID(message["to"]), mess_body, mess_type, profile=self.parent.profile) 134 #TODO: add delayed messages to history if they aren't already in it
130 self.host.bridge.newMessage(message["from"], mess_body, mess_type, message['to'], extra, profile=self.parent.profile) 135 self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], timestamp, profile=self.parent.profile)
136 except IndexError:
137 self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], profile=self.parent.profile)
138 self.host.bridge.newMessage(data['from'], data['body'], data['type'], data['to'], data['extra'], profile=self.parent.profile)
139
140 post_treat.addCallback(after_treatments)
141 post_treat.callback(data)
131 142
132 143
133 class SatRosterProtocol(xmppim.RosterClientProtocol): 144 class SatRosterProtocol(xmppim.RosterClientProtocol):
134 145
135 def __init__(self, host): 146 def __init__(self, host):