| ... |
... |
@@ -17,13 +17,14 @@ |
|
17
|
17
|
|
|
18
|
18
|
from nbxmpp.namespaces import Namespace
|
|
19
|
19
|
from nbxmpp.protocol import Iq
|
|
20
|
|
-from nbxmpp.protocol import isResultNode
|
|
21
|
|
-from nbxmpp.structs import BlockingListResult
|
|
22
|
|
-from nbxmpp.structs import CommonResult
|
|
23
|
|
-from nbxmpp.util import call_on_response
|
|
24
|
|
-from nbxmpp.util import callback
|
|
25
|
|
-from nbxmpp.util import raise_error
|
|
|
20
|
+from nbxmpp.protocol import JID
|
|
26
|
21
|
from nbxmpp.modules.base import BaseModule
|
|
|
22
|
+from nbxmpp.modules.util import raise_if_error
|
|
|
23
|
+from nbxmpp.modules.util import finalize
|
|
|
24
|
+from nbxmpp.task import iq_request_task
|
|
|
25
|
+from nbxmpp.errors import MalformedStanzaError
|
|
|
26
|
+from nbxmpp.structs import BlockingPush
|
|
|
27
|
+from nbxmpp.structs import StanzaHandler
|
|
27
|
28
|
|
|
28
|
29
|
|
|
29
|
30
|
class Blocking(BaseModule):
|
| ... |
... |
@@ -31,54 +32,115 @@ class Blocking(BaseModule): |
|
31
|
32
|
BaseModule.__init__(self, client)
|
|
32
|
33
|
|
|
33
|
34
|
self._client = client
|
|
34
|
|
- self.handlers = []
|
|
|
35
|
+ self.handlers = [
|
|
|
36
|
+ StanzaHandler(name='iq',
|
|
|
37
|
+ priority=15,
|
|
|
38
|
+ callback=self._process_blocking_push,
|
|
|
39
|
+ typ='set',
|
|
|
40
|
+ ns=Namespace.BLOCKING),
|
|
|
41
|
+ ]
|
|
35
|
42
|
|
|
36
|
|
- @call_on_response('_blocking_list_received')
|
|
37
|
|
- def get_blocking_list(self):
|
|
38
|
|
- iq = Iq('get', Namespace.BLOCKING)
|
|
39
|
|
- iq.setQuery('blocklist')
|
|
40
|
|
- return iq
|
|
|
43
|
+ @iq_request_task
|
|
|
44
|
+ def request_blocking_list(self):
|
|
|
45
|
+ _task = yield
|
|
41
|
46
|
|
|
42
|
|
- @callback
|
|
43
|
|
- def _blocking_list_received(self, stanza):
|
|
44
|
|
- blocked = []
|
|
45
|
|
- if not isResultNode(stanza):
|
|
46
|
|
- return raise_error(self._log.info, stanza)
|
|
|
47
|
+ result = yield _make_blocking_list_request()
|
|
|
48
|
+
|
|
|
49
|
+ raise_if_error(result)
|
|
47
|
50
|
|
|
48
|
|
- blocklist = stanza.getTag('blocklist', namespace=Namespace.BLOCKING)
|
|
|
51
|
+ blocklist = result.getTag('blocklist', namespace=Namespace.BLOCKING)
|
|
49
|
52
|
if blocklist is None:
|
|
50
|
|
- return raise_error(self._log.warning, stanza, 'stanza-malformed')
|
|
|
53
|
+ raise MalformedStanzaError('blocklist node missing', result)
|
|
51
|
54
|
|
|
|
55
|
+ blocked = []
|
|
52
|
56
|
for item in blocklist.getTags('item'):
|
|
53
|
57
|
blocked.append(item.getAttr('jid'))
|
|
54
|
58
|
|
|
55
|
59
|
self._log.info('Received blocking list: %s', blocked)
|
|
56
|
|
- return BlockingListResult(blocking_list=blocked)
|
|
|
60
|
+ yield blocked
|
|
57
|
61
|
|
|
58
|
|
- @call_on_response('_default_response')
|
|
|
62
|
+ @iq_request_task
|
|
59
|
63
|
def block(self, jids, report=None):
|
|
|
64
|
+ task = yield
|
|
|
65
|
+
|
|
60
|
66
|
self._log.info('Block: %s', jids)
|
|
61
|
|
- iq = Iq('set', Namespace.BLOCKING)
|
|
62
|
|
- query = iq.setQuery(name='block')
|
|
63
|
|
- for jid in jids:
|
|
64
|
|
- item = query.addChild(name='item', attrs={'jid': jid})
|
|
65
|
|
- if report in ('spam', 'abuse'):
|
|
66
|
|
- action = item.addChild(name='report',
|
|
67
|
|
- namespace=Namespace.REPORTING)
|
|
68
|
|
- action.setTag(report)
|
|
69
|
|
- return iq
|
|
70
|
|
-
|
|
71
|
|
- @call_on_response('_default_response')
|
|
|
67
|
+
|
|
|
68
|
+ result = yield _make_block_request(jids, report)
|
|
|
69
|
+ yield finalize(task, result)
|
|
|
70
|
+
|
|
|
71
|
+ @iq_request_task
|
|
72
|
72
|
def unblock(self, jids):
|
|
|
73
|
+ task = yield
|
|
|
74
|
+
|
|
73
|
75
|
self._log.info('Unblock: %s', jids)
|
|
74
|
|
- iq = Iq('set', Namespace.BLOCKING)
|
|
75
|
|
- query = iq.setQuery(name='unblock')
|
|
76
|
|
- for jid in jids:
|
|
77
|
|
- query.addChild(name='item', attrs={'jid': jid})
|
|
78
|
|
- return iq
|
|
79
|
|
-
|
|
80
|
|
- @callback
|
|
81
|
|
- def _default_response(self, stanza):
|
|
82
|
|
- if not isResultNode(stanza):
|
|
83
|
|
- return raise_error(self._log.info, stanza)
|
|
84
|
|
- return CommonResult(jid=stanza.getFrom())
|
|
|
76
|
+
|
|
|
77
|
+ result = yield _make_unblock_request(jids)
|
|
|
78
|
+ yield finalize(task, result)
|
|
|
79
|
+
|
|
|
80
|
+ @staticmethod
|
|
|
81
|
+ def _process_blocking_push(client, stanza, properties):
|
|
|
82
|
+ unblock = stanza.getTag('unblock', namespace=Namespace.BLOCKING)
|
|
|
83
|
+ if unblock is not None:
|
|
|
84
|
+ properties.blocking = _parse_push(unblock)
|
|
|
85
|
+ return
|
|
|
86
|
+
|
|
|
87
|
+ block = stanza.getTag('block', namespace=Namespace.BLOCKING)
|
|
|
88
|
+ if block is not None:
|
|
|
89
|
+ properties.blocking = _parse_push(block)
|
|
|
90
|
+
|
|
|
91
|
+ reply = stanza.buildSimpleReply('result')
|
|
|
92
|
+ client.send_stanza(reply)
|
|
|
93
|
+
|
|
|
94
|
+
|
|
|
95
|
+def _make_blocking_list_request():
|
|
|
96
|
+ iq = Iq('get', Namespace.BLOCKING)
|
|
|
97
|
+ iq.setQuery('blocklist')
|
|
|
98
|
+ return iq
|
|
|
99
|
+
|
|
|
100
|
+
|
|
|
101
|
+def _make_block_request(jids, report):
|
|
|
102
|
+ iq = Iq('set', Namespace.BLOCKING)
|
|
|
103
|
+ query = iq.setQuery(name='block')
|
|
|
104
|
+ for jid in jids:
|
|
|
105
|
+ item = query.addChild(name='item', attrs={'jid': jid})
|
|
|
106
|
+ if report in ('spam', 'abuse'):
|
|
|
107
|
+ action = item.addChild(name='report',
|
|
|
108
|
+ namespace=Namespace.REPORTING)
|
|
|
109
|
+ action.setTag(report)
|
|
|
110
|
+ return iq
|
|
|
111
|
+
|
|
|
112
|
+
|
|
|
113
|
+def _make_unblock_request(jids):
|
|
|
114
|
+ iq = Iq('set', Namespace.BLOCKING)
|
|
|
115
|
+ query = iq.setQuery(name='unblock')
|
|
|
116
|
+ for jid in jids:
|
|
|
117
|
+ query.addChild(name='item', attrs={'jid': jid})
|
|
|
118
|
+ return iq
|
|
|
119
|
+
|
|
|
120
|
+
|
|
|
121
|
+def _parse_push(node):
|
|
|
122
|
+ items = node.getTags('item')
|
|
|
123
|
+ if not items:
|
|
|
124
|
+ return BlockingPush(block=[], unblock=[], unblock_all=True)
|
|
|
125
|
+
|
|
|
126
|
+ jids = []
|
|
|
127
|
+ for item in items:
|
|
|
128
|
+ jid = item.getAttr('jid')
|
|
|
129
|
+ if not jid:
|
|
|
130
|
+ continue
|
|
|
131
|
+
|
|
|
132
|
+ try:
|
|
|
133
|
+ jid = JID.from_string(jid)
|
|
|
134
|
+ except Exception:
|
|
|
135
|
+ continue
|
|
|
136
|
+
|
|
|
137
|
+ jids.append(jid)
|
|
|
138
|
+
|
|
|
139
|
+
|
|
|
140
|
+ block, unblock = [], []
|
|
|
141
|
+ if node.getName() == 'block':
|
|
|
142
|
+ block = jids
|
|
|
143
|
+ else:
|
|
|
144
|
+ unblock = jids
|
|
|
145
|
+
|
|
|
146
|
+ return BlockingPush(block=block, unblock=unblock, unblock_all=False)
|