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.
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
):
28 XEP-0231 Bits of Binary
32 description
= 'XEP-0231: Bits of Binary'
33 dependencies
= set(['xep_0030'])
35 def plugin_init(self
):
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',
47 self
.xmpp
.register_handler(
48 Callback('Bits of Binary - Message',
49 StanzaPath('message/bob'),
52 self
.xmpp
.register_handler(
53 Callback('Bits of Binary - Presence',
54 StanzaPath('presence/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)
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):
72 cid
= 'sha1+%s@bob.xmpp.org' % hashlib
.sha1(data
).hexdigest()
78 bob
['max_age'] = max_age
80 self
.api
['set_bob'](args
=bob
)
84 def get_bob(self
, jid
=None, cid
=None, cached
=True, ifrom
=None,
85 block
=True, timeout
=None, callback
=None):
87 data
= self
.api
['get_bob'](None, None, ifrom
, args
=cid
)
89 if not isinstance(data
, Iq
):
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']
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
]
136 raise XMPPError('item-not-found')
138 def _del_bob(self
, jid
, node
, ifrom
, cid
):
139 if cid
in self
._cids
: