comparison src/core/xmpp.py @ 1053:71d63750963e

core (XMPP): message received (onMessage) refactoring: - better separation of actions - use of failure.trap and generic exception (CancelError/SkipHistory)
author Goffi <goffi@goffi.org>
date Sat, 07 Jun 2014 16:38:53 +0200
parents 6e975c6b0faf
children aa15453ec54d
comparison
equal deleted inserted replaced
1052:e88bff4c8b77 1053:71d63750963e
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
17 # You should have received a copy of the GNU Affero General Public License 17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 from sat.core.i18n import _, D_ 20 from sat.core.i18n import _
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from twisted.internet import task, defer 22 from twisted.internet import task, defer
23 from twisted.words.protocols.jabber import jid, xmlstream 23 from twisted.words.protocols.jabber import jid, xmlstream
24 from wokkel import client, disco, xmppim, generic, compat, delay, iwokkel 24 from wokkel import client, disco, xmppim, generic, compat, delay, iwokkel
25 from sat.core.log import getLogger 25 from sat.core.log import getLogger
128 data['body'] = e.children[0] if e.children else "" 128 data['body'] = e.children[0] if e.children else ""
129 break 129 break
130 130
131 data['type'] = message['type'] if message.hasAttribute('type') else 'normal' 131 data['type'] = message['type'] if message.hasAttribute('type') else 'normal'
132 132
133 def after_treatments(data): 133 def bridgeSignal(data):
134 if data is not None:
135 self.host.bridge.newMessage(data['from'], data['body'], data['type'], data['to'], data['extra'], profile=self.parent.profile)
136 return data
137
138 def addToHistory(data):
134 # set message body to empty string by default, and later 139 # set message body to empty string by default, and later
135 # also forward message without body (chat state notification...) 140 # also forward message without body (chat state notification...)
136 try: 141 try:
137 _delay = delay.Delay.fromElement(filter(lambda elm: elm.name == 'delay', message.elements())[0]) 142 _delay = delay.Delay.fromElement(filter(lambda elm: elm.name == 'delay', message.elements())[0])
138 timestamp = timegm(_delay.stamp.utctimetuple()) 143 timestamp = timegm(_delay.stamp.utctimetuple())
140 if data['type'] != 'groupchat': # XXX: we don't save delayed messages in history for groupchats 145 if data['type'] != 'groupchat': # XXX: we don't save delayed messages in history for groupchats
141 #TODO: add delayed messages to history if they aren't already in it 146 #TODO: add delayed messages to history if they aren't already in it
142 self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], data['extra'], timestamp, profile=self.parent.profile) 147 self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], data['extra'], timestamp, profile=self.parent.profile)
143 except IndexError: 148 except IndexError:
144 self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], data['extra'], profile=self.parent.profile) 149 self.host.memory.addToHistory(jid.JID(data['from']), jid.JID(data['to']), data['body'], data['type'], data['extra'], profile=self.parent.profile)
145 self.host.bridge.newMessage(data['from'], data['body'], data['type'], data['to'], data['extra'], profile=self.parent.profile) 150 return data
146 151
147 post_treat.addCallback(after_treatments) 152 def treatmentsEb(failure):
153 failure.trap(exceptions.SkipHistory)
154 return data
155
156 def cancelErrorTrap(failure):
157 """A message sending can be cancelled by a plugin treatment"""
158 failure.trap(exceptions.CancelError)
159
160 post_treat.addCallback(addToHistory)
161 post_treat.addErrback(treatmentsEb)
162 post_treat.addCallback(bridgeSignal)
163 post_treat.addErrback(cancelErrorTrap)
148 post_treat.callback(data) 164 post_treat.callback(data)
149 165
150 166
151 class SatRosterProtocol(xmppim.RosterClientProtocol): 167 class SatRosterProtocol(xmppim.RosterClientProtocol):
152 168