Bump to 1.3.1
[slixmpp.git] / sleekxmpp / plugins / xep_0231 / bob.py
blob5e1f590bd457a2fe7b2282da262e4eacf9fd286d
1 """
2 SleekXMPP: The Sleek XMPP Library
3 Copyright (C) 2012 Nathanael C. Fritz,
4 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
5 This file is part of SleekXMPP.
7 See the file LICENSE for copying permission.
8 """
10 import logging
11 import hashlib
13 from sleekxmpp.stanza import Iq, Message, Presence
14 from sleekxmpp.exceptions import XMPPError
15 from sleekxmpp.xmlstream.handler import Callback
16 from sleekxmpp.xmlstream.matcher import StanzaPath
17 from sleekxmpp.xmlstream import register_stanza_plugin
18 from sleekxmpp.plugins.base import BasePlugin
19 from sleekxmpp.plugins.xep_0231 import stanza, BitsOfBinary
22 log = logging.getLogger(__name__)
25 class XEP_0231(BasePlugin):
27 """
28 XEP-0231 Bits of Binary
29 """
31 name = 'xep_0231'
32 description = 'XEP-0231: Bits of Binary'
33 dependencies = set(['xep_0030'])
35 def plugin_init(self):
36 self._cids = {}
38 register_stanza_plugin(Iq, BitsOfBinary)
39 register_stanza_plugin(Message, BitsOfBinary)
40 register_stanza_plugin(Presence, BitsOfBinary)
42 self.xmpp.register_handler(
43 Callback('Bits of Binary - Iq',
44 StanzaPath('iq/bob'),
45 self._handle_bob_iq))
47 self.xmpp.register_handler(
48 Callback('Bits of Binary - Message',
49 StanzaPath('message/bob'),
50 self._handle_bob))
52 self.xmpp.register_handler(
53 Callback('Bits of Binary - Presence',
54 StanzaPath('presence/bob'),
55 self._handle_bob))
57 self.api.register(self._get_bob, 'get_bob', default=True)
58 self.api.register(self._set_bob, 'set_bob', default=True)
59 self.api.register(self._del_bob, 'del_bob', default=True)
61 def plugin_end(self):
62 self.xmpp['xep_0030'].del_feature(feature='urn:xmpp:bob')
63 self.xmpp.remove_handler('Bits of Binary - Iq')
64 self.xmpp.remove_handler('Bits of Binary - Message')
65 self.xmpp.remove_handler('Bits of Binary - Presence')
67 def session_bind(self, jid):
68 self.xmpp['xep_0030'].add_feature('urn:xmpp:bob')
70 def set_bob(self, data, mtype, cid=None, max_age=None):
71 if cid is None:
72 cid = 'sha1+%s@bob.xmpp.org' % hashlib.sha1(data).hexdigest()
74 bob = BitsOfBinary()
75 bob['data'] = data
76 bob['type'] = mtype
77 bob['cid'] = cid
78 bob['max_age'] = max_age
80 self.api['set_bob'](args=bob)
82 return cid
84 def get_bob(self, jid=None, cid=None, cached=True, ifrom=None,
85 block=True, timeout=None, callback=None):
86 if cached:
87 data = self.api['get_bob'](None, None, ifrom, args=cid)
88 if data is not None:
89 if not isinstance(data, Iq):
90 iq = self.xmpp.Iq()
91 iq.append(data)
92 return iq
93 return data
95 iq = self.xmpp.Iq()
96 iq['to'] = jid
97 iq['from'] = ifrom
98 iq['type'] = 'get'
99 iq['bob']['cid'] = cid
100 return iq.send(block=block, timeout=timeout, callback=callback)
102 def del_bob(self, cid):
103 self.api['del_bob'](args=cid)
105 def _handle_bob_iq(self, iq):
106 cid = iq['bob']['cid']
108 if iq['type'] == 'result':
109 self.api['set_bob'](iq['from'], None, iq['to'], args=iq['bob'])
110 self.xmpp.event('bob', iq)
111 elif iq['type'] == 'get':
112 data = self.api['get_bob'](iq['to'], None, iq['from'], args=cid)
113 if isinstance(data, Iq):
114 data['id'] = iq['id']
115 data.send()
116 return
118 iq.reply()
119 iq.append(data)
120 iq.send()
122 def _handle_bob(self, stanza):
123 self.api['set_bob'](stanza['from'], None,
124 stanza['to'], args=stanza['bob'])
125 self.xmpp.event('bob', stanza)
127 # =================================================================
129 def _set_bob(self, jid, node, ifrom, bob):
130 self._cids[bob['cid']] = bob
132 def _get_bob(self, jid, node, ifrom, cid):
133 if cid in self._cids:
134 return self._cids[cid]
135 else:
136 raise XMPPError('item-not-found')
138 def _del_bob(self, jid, node, ifrom, cid):
139 if cid in self._cids:
140 del self._cids[cid]