1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
#
# This file is part of nbxmpp.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 3
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; If not, see <http://www.gnu.org/licenses/>.
from nbxmpp.namespaces import Namespace
from nbxmpp.protocol import Iq
from nbxmpp.protocol import JID
from nbxmpp.modules.base import BaseModule
from nbxmpp.errors import StanzaError
from nbxmpp.errors import MalformedStanzaError
from nbxmpp.task import iq_request_task
from nbxmpp.structs import BlockingPush
from nbxmpp.structs import StanzaHandler
from nbxmpp.modules.util import process_response
class Blocking(BaseModule):
def __init__(self, client):
BaseModule.__init__(self, client)
self._client = client
self.handlers = [
StanzaHandler(name='iq',
priority=15,
callback=self._process_blocking_push,
typ='set',
ns=Namespace.BLOCKING),
]
@iq_request_task
def request_blocking_list(self):
_task = yield
result = yield _make_blocking_list_request()
if result.isError():
raise StanzaError(result)
blocklist = result.getTag('blocklist', namespace=Namespace.BLOCKING)
if blocklist is None:
raise MalformedStanzaError('blocklist node missing', result)
blocked = []
for item in blocklist.getTags('item'):
blocked.append(item.getAttr('jid'))
self._log.info('Received blocking list: %s', blocked)
yield blocked
@iq_request_task
def block(self, jids, report=None):
_task = yield
self._log.info('Block: %s', jids)
response = yield _make_block_request(jids, report)
yield process_response(response)
@iq_request_task
def unblock(self, jids):
_task = yield
self._log.info('Unblock: %s', jids)
response = yield _make_unblock_request(jids)
yield process_response(response)
@staticmethod
def _process_blocking_push(client, stanza, properties):
unblock = stanza.getTag('unblock', namespace=Namespace.BLOCKING)
if unblock is not None:
properties.blocking = _parse_push(unblock)
return
block = stanza.getTag('block', namespace=Namespace.BLOCKING)
if block is not None:
properties.blocking = _parse_push(block)
reply = stanza.buildSimpleReply('result')
client.send_stanza(reply)
def _make_blocking_list_request():
iq = Iq('get', Namespace.BLOCKING)
iq.setQuery('blocklist')
return iq
def _make_block_request(jids, report):
iq = Iq('set', Namespace.BLOCKING)
query = iq.setQuery(name='block')
for jid in jids:
item = query.addChild(name='item', attrs={'jid': jid})
if report in ('spam', 'abuse'):
action = item.addChild(name='report',
namespace=Namespace.REPORTING)
action.setTag(report)
return iq
def _make_unblock_request(jids):
iq = Iq('set', Namespace.BLOCKING)
query = iq.setQuery(name='unblock')
for jid in jids:
query.addChild(name='item', attrs={'jid': jid})
return iq
def _parse_push(node):
items = node.getTags('item')
if not items:
return BlockingPush(block=[], unblock=[], unblock_all=True)
jids = []
for item in items:
jid = item.getAttr('jid')
if not jid:
continue
try:
jid = JID.from_string(jid)
except Exception:
continue
jids.append(jid)
block, unblock = [], []
if node.getName() == 'block':
block = jids
else:
unblock = jids
return BlockingPush(block=block, unblock=unblock, unblock_all=False)
|