1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
|
#
# (C) Copyright 2003-2010 Jacek Konieczny <jajcus@jajcus.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License Version
# 2.1 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
"""Handling of XMPP stanzas.
Normative reference:
- `RFC 3920 <http://www.ietf.org/rfc/rfc3920.txt>`__
"""
__docformat__="restructuredtext en"
import libxml2
import logging
import threading
from pyxmpp.expdict import ExpiringDictionary
from pyxmpp.exceptions import ProtocolError, BadRequestProtocolError, FeatureNotImplementedProtocolError
from pyxmpp.stanza import Stanza
class StanzaProcessor:
"""Universal stanza handler/router class.
Provides facilities to set up custom handlers for various types of stanzas.
:Ivariables:
- `lock`: lock object used to synchronize access to the
`StanzaProcessor` object.
- `me`: local JID.
- `peer`: remote stream endpoint JID.
- `process_all_stanzas`: when `True` then all stanzas received are
considered local.
- `initiator`: `True` if local stream endpoint is the initiating entity.
"""
def __init__(self):
"""Initialize a `StanzaProcessor` object."""
self.me=None
self.peer=None
self.initiator=None
self.peer_authenticated=False
self.process_all_stanzas=True
self._iq_response_handlers=ExpiringDictionary()
self._iq_get_handlers={}
self._iq_set_handlers={}
self._message_handlers=[]
self._presence_handlers=[]
self.__logger=logging.getLogger("pyxmpp.Stream")
self.lock=threading.RLock()
def process_response(self, response):
"""Examines out the response returned by a stanza handler and sends all
stanzas provided.
:Returns:
- `True`: if `response` is `Stanza`, iterable or `True` (meaning the stanza was processed).
- `False`: when `response` is `False` or `None`
:returntype: `bool`
"""
if response is None or response is False:
return False
if isinstance(response, Stanza):
self.send(response)
return True
try:
response = iter(response)
except TypeError:
return bool(response)
for stanza in response:
if isinstance(stanza, Stanza):
self.send(stanza)
return True
def process_iq(self, stanza):
"""Process IQ stanza received.
:Parameters:
- `stanza`: the stanza received
If a matching handler is available pass the stanza to it.
Otherwise ignore it if it is "error" or "result" stanza
or return "feature-not-implemented" error."""
sid=stanza.get_id()
fr=stanza.get_from()
typ=stanza.get_type()
if typ in ("result","error"):
if fr:
ufr=fr.as_unicode()
else:
ufr=None
res_handler = err_handler = None
try:
res_handler, err_handler = self._iq_response_handlers.pop((sid,ufr))
except KeyError:
if ( (fr==self.peer or fr==self.me or fr==self.me.bare()) ):
try:
res_handler, err_handler = self._iq_response_handlers.pop((sid,None))
except KeyError:
pass
if None is res_handler is err_handler:
return False
if typ=="result":
response = res_handler(stanza)
else:
response = err_handler(stanza)
self.process_response(response)
return True
q=stanza.get_query()
if not q:
raise BadRequestProtocolError, "Stanza with no child element"
el=q.name
ns=q.ns().getContent()
if typ=="get":
if self._iq_get_handlers.has_key((el,ns)):
response = self._iq_get_handlers[(el,ns)](stanza)
self.process_response(response)
return True
else:
raise FeatureNotImplementedProtocolError, "Not implemented"
elif typ=="set":
if self._iq_set_handlers.has_key((el,ns)):
response = self._iq_set_handlers[(el,ns)](stanza)
self.process_response(response)
return True
else:
raise FeatureNotImplementedProtocolError, "Not implemented"
else:
raise BadRequestProtocolError, "Unknown IQ stanza type"
def __try_handlers(self,handler_list,typ,stanza):
""" Search the handler list for handlers matching
given stanza type and payload namespace. Run the
handlers found ordering them by priority until
the first one which returns `True`.
:Parameters:
- `handler_list`: list of available handlers
- `typ`: stanza type (value of its "type" attribute)
- `stanza`: the stanza to handle
:return: result of the last handler or `False` if no
handler was found."""
namespaces=[]
if stanza.xmlnode.children:
c=stanza.xmlnode.children
while c:
try:
ns=c.ns()
except libxml2.treeError:
ns=None
if ns is None:
c=c.next
continue
ns_uri=ns.getContent()
if ns_uri not in namespaces:
namespaces.append(ns_uri)
c=c.next
for handler_entry in handler_list:
t=handler_entry[1]
ns=handler_entry[2]
handler=handler_entry[3]
if t!=typ:
continue
if ns is not None and ns not in namespaces:
continue
response = handler(stanza)
if self.process_response(response):
return True
return False
def process_message(self,stanza):
"""Process message stanza.
Pass it to a handler of the stanza's type and payload namespace.
If no handler for the actual stanza type succeeds then hadlers
for type "normal" are used.
:Parameters:
- `stanza`: message stanza to be handled
"""
if not self.initiator and not self.peer_authenticated:
self.__logger.debug("Ignoring message - peer not authenticated yet")
return True
typ=stanza.get_type()
if self.__try_handlers(self._message_handlers,typ,stanza):
return True
if typ!="error":
return self.__try_handlers(self._message_handlers,"normal",stanza)
return False
def process_presence(self,stanza):
"""Process presence stanza.
Pass it to a handler of the stanza's type and payload namespace.
:Parameters:
- `stanza`: presence stanza to be handled
"""
if not self.initiator and not self.peer_authenticated:
self.__logger.debug("Ignoring presence - peer not authenticated yet")
return True
typ=stanza.get_type()
if not typ:
typ="available"
return self.__try_handlers(self._presence_handlers,typ,stanza)
def route_stanza(self,stanza):
"""Process stanza not addressed to us.
Return "recipient-unavailable" return if it is not
"error" nor "result" stanza.
This method should be overriden in derived classes if they
are supposed to handle stanzas not addressed directly to local
stream endpoint.
:Parameters:
- `stanza`: presence stanza to be processed
"""
if stanza.get_type() not in ("error","result"):
r = stanza.make_error_response("recipient-unavailable")
self.send(r)
return True
def process_stanza(self,stanza):
"""Process stanza received from the stream.
First "fix" the stanza with `self.fix_in_stanza()`,
then pass it to `self.route_stanza()` if it is not directed
to `self.me` and `self.process_all_stanzas` is not True. Otherwise
stanza is passwd to `self.process_iq()`, `self.process_message()`
or `self.process_presence()` appropriately.
:Parameters:
- `stanza`: the stanza received.
:returns: `True` when stanza was handled
"""
self.fix_in_stanza(stanza)
to=stanza.get_to()
if not self.process_all_stanzas and to and to!=self.me and to.bare()!=self.me.bare():
return self.route_stanza(stanza)
try:
if stanza.stanza_type=="iq":
if self.process_iq(stanza):
return True
elif stanza.stanza_type=="message":
if self.process_message(stanza):
return True
elif stanza.stanza_type=="presence":
if self.process_presence(stanza):
return True
except ProtocolError, e:
typ = stanza.get_type()
if typ != 'error' and (typ != 'result' or stanza.stanza_type != 'iq'):
r = stanza.make_error_response(e.xmpp_name)
self.send(r)
e.log_reported()
else:
e.log_ignored()
self.__logger.debug("Unhandled %r stanza: %r" % (stanza.stanza_type,stanza.serialize()))
return False
def check_to(self,to):
"""Check "to" attribute of received stream header.
:return: `to` if it is equal to `self.me`, None otherwise.
Should be overriden in derived classes which require other logic
for handling that attribute."""
if to!=self.me:
return None
return to
def set_response_handlers(self,iq,res_handler,err_handler,timeout_handler=None,timeout=300):
"""Set response handler for an IQ "get" or "set" stanza.
This should be called before the stanza is sent.
:Parameters:
- `iq`: an IQ stanza
- `res_handler`: result handler for the stanza. Will be called
when matching <iq type="result"/> is received. Its only
argument will be the stanza received. The handler may return
a stanza or list of stanzas which should be sent in response.
- `err_handler`: error handler for the stanza. Will be called
when matching <iq type="error"/> is received. Its only
argument will be the stanza received. The handler may return
a stanza or list of stanzas which should be sent in response
but this feature should rather not be used (it is better not to
respond to 'error' stanzas).
- `timeout_handler`: timeout handler for the stanza. Will be called
when no matching <iq type="result"/> or <iq type="error"/> is
received in next `timeout` seconds. The handler should accept
two arguments and ignore them.
- `timeout`: timeout value for the stanza. After that time if no
matching <iq type="result"/> nor <iq type="error"/> stanza is
received, then timeout_handler (if given) will be called.
"""
self.lock.acquire()
try:
self._set_response_handlers(iq,res_handler,err_handler,timeout_handler,timeout)
finally:
self.lock.release()
def _set_response_handlers(self,iq,res_handler,err_handler,timeout_handler=None,timeout=300):
"""Same as `Stream.set_response_handlers` but assume `self.lock` is acquired."""
self.fix_out_stanza(iq)
to=iq.get_to()
if to:
to=to.as_unicode()
if timeout_handler:
self._iq_response_handlers.set_item((iq.get_id(),to),
(res_handler,err_handler),
timeout,timeout_handler)
else:
self._iq_response_handlers.set_item((iq.get_id(),to),
(res_handler,err_handler),timeout)
def set_iq_get_handler(self,element,namespace,handler):
"""Set <iq type="get"/> handler.
:Parameters:
- `element`: payload element name
- `namespace`: payload element namespace URI
- `handler`: function to be called when a stanza
with defined element is received. Its only argument
will be the stanza received. The handler may return a stanza or
list of stanzas which should be sent in response.
Only one handler may be defined per one namespaced element.
If a handler for the element was already set it will be lost
after calling this method.
"""
self.lock.acquire()
try:
self._iq_get_handlers[(element,namespace)]=handler
finally:
self.lock.release()
def unset_iq_get_handler(self,element,namespace):
"""Remove <iq type="get"/> handler.
:Parameters:
- `element`: payload element name
- `namespace`: payload element namespace URI
"""
self.lock.acquire()
try:
if self._iq_get_handlers.has_key((element,namespace)):
del self._iq_get_handlers[(element,namespace)]
finally:
self.lock.release()
def set_iq_set_handler(self,element,namespace,handler):
"""Set <iq type="set"/> handler.
:Parameters:
- `element`: payload element name
- `namespace`: payload element namespace URI
- `handler`: function to be called when a stanza
with defined element is received. Its only argument
will be the stanza received. The handler may return a stanza or
list of stanzas which should be sent in response.
Only one handler may be defined per one namespaced element.
If a handler for the element was already set it will be lost
after calling this method."""
self.lock.acquire()
try:
self._iq_set_handlers[(element,namespace)]=handler
finally:
self.lock.release()
def unset_iq_set_handler(self,element,namespace):
"""Remove <iq type="set"/> handler.
:Parameters:
- `element`: payload element name.
- `namespace`: payload element namespace URI."""
self.lock.acquire()
try:
if self._iq_set_handlers.has_key((element,namespace)):
del self._iq_set_handlers[(element,namespace)]
finally:
self.lock.release()
def __add_handler(self,handler_list,typ,namespace,priority,handler):
"""Add a handler function to a prioritized handler list.
:Parameters:
- `handler_list`: a handler list.
- `typ`: stanza type.
- `namespace`: stanza payload namespace.
- `priority`: handler priority. Must be >=0 and <=100. Handlers
with lower priority list will be tried first."""
if priority<0 or priority>100:
raise ValueError,"Bad handler priority (must be in 0:100)"
handler_list.append((priority,typ,namespace,handler))
handler_list.sort()
def set_message_handler(self, typ, handler, namespace=None, priority=100):
"""Set a handler for <message/> stanzas.
:Parameters:
- `typ`: message type. `None` will be treated the same as "normal",
and will be the default for unknown types (those that have no
handler associated).
- `namespace`: payload namespace. If `None` that message with any
payload (or even with no payload) will match.
- `priority`: priority value for the handler. Handlers with lower
priority value are tried first.
- `handler`: function to be called when a message stanza
with defined type and payload namespace is received. Its only
argument will be the stanza received. The handler may return a
stanza or list of stanzas which should be sent in response.
Multiple <message /> handlers with the same type/namespace/priority may
be set. Order of calling handlers with the same priority is not defined.
Handlers will be called in priority order until one of them returns True or
any stanza(s) to send (even empty list will do).
"""
self.lock.acquire()
try:
if not typ:
typ = "normal"
self.__add_handler(self._message_handlers,typ,namespace,priority,handler)
finally:
self.lock.release()
def set_presence_handler(self,typ,handler,namespace=None,priority=100):
"""Set a handler for <presence/> stanzas.
:Parameters:
- `typ`: presence type. "available" will be treated the same as `None`.
- `namespace`: payload namespace. If `None` that presence with any
payload (or even with no payload) will match.
- `priority`: priority value for the handler. Handlers with lower
priority value are tried first.
- `handler`: function to be called when a presence stanza
with defined type and payload namespace is received. Its only
argument will be the stanza received. The handler may return a
stanza or list of stanzas which should be sent in response.
Multiple <presence /> handlers with the same type/namespace/priority may
be set. Order of calling handlers with the same priority is not defined.
Handlers will be called in priority order until one of them returns
True or any stanza(s) to send (even empty list will do).
"""
self.lock.acquire()
try:
if not typ:
typ="available"
self.__add_handler(self._presence_handlers,typ,namespace,priority,handler)
finally:
self.lock.release()
def fix_in_stanza(self,stanza):
"""Modify incoming stanza before processing it.
This implementation does nothig. It should be overriden in derived
classes if needed."""
pass
def fix_out_stanza(self,stanza):
"""Modify outgoing stanza before sending into the stream.
This implementation does nothig. It should be overriden in derived
classes if needed."""
pass
def send(self,stanza):
"""Send a stanza somwhere. This one does nothing. Should be overriden
in derived classes.
:Parameters:
- `stanza`: the stanza to send.
:Types:
- `stanza`: `pyxmpp.stanza.Stanza`"""
raise NotImplementedError,"This method must be overriden in derived classes."""
# vi: sts=4 et sw=4
|