Rename to slixmpp
[slixmpp.git] / slixmpp / plugins / xep_0191 / blocking.py
blob92a5781b4ff01a81db1b851dd8eddb02754e2e60
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 permission.
7 """
9 import logging
11 from slixmpp import Iq
12 from slixmpp.plugins import BasePlugin
13 from slixmpp.xmlstream.handler import Callback
14 from slixmpp.xmlstream.matcher import StanzaPath
15 from slixmpp.xmlstream import register_stanza_plugin, JID
16 from slixmpp.plugins.xep_0191 import stanza, Block, Unblock, BlockList
19 log = logging.getLogger(__name__)
22 class XEP_0191(BasePlugin):
24 name = 'xep_0191'
25 description = 'XEP-0191: Blocking Command'
26 dependencies = set(['xep_0030'])
27 stanza = stanza
29 def plugin_init(self):
30 register_stanza_plugin(Iq, BlockList)
31 register_stanza_plugin(Iq, Block)
32 register_stanza_plugin(Iq, Unblock)
34 self.xmpp.register_handler(
35 Callback('Blocked Contact',
36 StanzaPath('iq@type=set/block'),
37 self._handle_blocked))
39 self.xmpp.register_handler(
40 Callback('Unblocked Contact',
41 StanzaPath('iq@type=set/unblock'),
42 self._handle_unblocked))
44 def plugin_end(self):
45 self.xmpp.remove_handler('Blocked Contact')
46 self.xmpp.remove_handler('Unblocked Contact')
48 def get_blocked(self, ifrom=None, block=True, timeout=None, callback=None):
49 iq = self.xmpp.Iq()
50 iq['type'] = 'get'
51 iq['from'] = ifrom
52 iq.enable('blocklist')
53 return iq.send(block=block, timeout=timeout, callback=callback)
55 def block(self, jids, ifrom=None, block=True, timeout=None, callback=None):
56 iq = self.xmpp.Iq()
57 iq['type'] = 'set'
58 iq['from'] = ifrom
60 if not isinstance(jids, (set, list)):
61 jids = [jids]
63 iq['block']['items'] = jids
64 return iq.send(block=block, timeout=timeout, callback=callback)
66 def unblock(self, jids=None, ifrom=None, block=True, timeout=None, callback=None):
67 iq = self.xmpp.Iq()
68 iq['type'] = 'set'
69 iq['from'] = ifrom
71 if jids is None:
72 jids = []
73 if not isinstance(jids, (set, list)):
74 jids = [jids]
76 iq['unblock']['items'] = jids
77 return iq.send(block=block, timeout=timeout, callback=callback)
79 def _handle_blocked(self, iq):
80 self.xmpp.event('blocked', iq)
82 def _handle_unblocked(self, iq):
83 self.xmpp.event('unblocked', iq)