Fix the iq.send() function, and a bunch of places where it is called
[slixmpp.git] / slixmpp / plugins / xep_0013 / offline.py
blob51840e7b672dfc1d013097073b8f1008bfff1847
1 """
2 Slixmpp: The Slick XMPP Library
3 Copyright (C) 2012 Nathanael C. Fritz, Lance J.T. Stout
4 This file is part of Slixmpp.
6 See the file LICENSE for copying permissio
7 """
9 import logging
11 import slixmpp
12 from slixmpp.stanza import Message, Iq
13 from slixmpp.exceptions import XMPPError
14 from slixmpp.xmlstream.handler import Collector
15 from slixmpp.xmlstream.matcher import StanzaPath
16 from slixmpp.xmlstream import register_stanza_plugin
17 from slixmpp.plugins import BasePlugin
18 from slixmpp.plugins.xep_0013 import stanza
21 log = logging.getLogger(__name__)
24 class XEP_0013(BasePlugin):
26 """
27 XEP-0013 Flexible Offline Message Retrieval
28 """
30 name = 'xep_0013'
31 description = 'XEP-0013: Flexible Offline Message Retrieval'
32 dependencies = set(['xep_0030'])
33 stanza = stanza
35 def plugin_init(self):
36 register_stanza_plugin(Iq, stanza.Offline)
37 register_stanza_plugin(Message, stanza.Offline)
39 def get_count(self, **kwargs):
40 return self.xmpp['xep_0030'].get_info(
41 node='http://jabber.org/protocol/offline',
42 local=False,
43 **kwargs)
45 def get_headers(self, **kwargs):
46 return self.xmpp['xep_0030'].get_items(
47 node='http://jabber.org/protocol/offline',
48 local=False,
49 **kwargs)
51 def view(self, nodes, ifrom=None, timeout=None, callback=None,
52 timeout_callback=None):
53 if not isinstance(nodes, (list, set)):
54 nodes = [nodes]
56 iq = self.xmpp.Iq()
57 iq['type'] = 'get'
58 iq['from'] = ifrom
59 offline = iq['offline']
60 for node in nodes:
61 item = stanza.Item()
62 item['node'] = node
63 item['action'] = 'view'
64 offline.append(item)
66 collector = Collector(
67 'Offline_Results_%s' % iq['id'],
68 StanzaPath('message/offline'))
69 self.xmpp.register_handler(collector)
71 def wrapped_cb(iq):
72 results = collector.stop()
73 if iq['type'] == 'result':
74 iq['offline']['results'] = results
75 callback(iq)
76 iq.send(timeout=timeout, callback=wrapped_cb,
77 timeout_callback=timeout_callback)
79 def remove(self, nodes, ifrom=None, timeout=None, callback=None,
80 timeout_callback=None):
81 if not isinstance(nodes, (list, set)):
82 nodes = [nodes]
84 iq = self.xmpp.Iq()
85 iq['type'] = 'set'
86 iq['from'] = ifrom
87 offline = iq['offline']
88 for node in nodes:
89 item = stanza.Item()
90 item['node'] = node
91 item['action'] = 'remove'
92 offline.append(item)
94 iq.send(timeout=timeout, callback=callback,
95 timeout_callback=timeout_callback)
97 def fetch(self, ifrom=None, timeout=None, callback=None,
98 timeout_callback=None):
99 iq = self.xmpp.Iq()
100 iq['type'] = 'set'
101 iq['from'] = ifrom
102 iq['offline']['fetch'] = True
104 collector = Collector(
105 'Offline_Results_%s' % iq['id'],
106 StanzaPath('message/offline'))
107 self.xmpp.register_handler(collector)
109 def wrapped_cb(iq):
110 results = collector.stop()
111 if iq['type'] == 'result':
112 iq['offline']['results'] = results
113 callback(iq)
114 iq.send(timeout=timeout, callback=wrapped_cb,
115 timeout_callback=timeout_callback)
117 def purge(self, ifrom=None, timeout=None, callback=None,
118 timeout_callback=None):
119 iq = self.xmpp.Iq()
120 iq['type'] = 'set'
121 iq['from'] = ifrom
122 iq['offline']['purge'] = True
123 iq.send(timeout=timeout, callback=callback,
124 timeout_callback=timeout_callback)