Philipp Hörist pushed to branch master at gajim / python-nbxmpp

Commits:

3 changed files:

Changes:

  • nbxmpp/modules/blocking.py
    ... ... @@ -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)

  • nbxmpp/protocol.py
    ... ... @@ -1571,6 +1571,12 @@ class Iq(Protocol):
    1571 1571
             iq.setQuery(self.getQuery().getName()).setNamespace(self.getQueryNS())
    
    1572 1572
             return iq
    
    1573 1573
     
    
    1574
    +    def buildSimpleReply(self, typ):
    
    1575
    +        return Iq(typ,
    
    1576
    +                  to=self.getFrom(),
    
    1577
    +                  attrs={'id': self.getID()})
    
    1578
    +
    
    1579
    +
    
    1574 1580
     class Hashes(Node):
    
    1575 1581
         """
    
    1576 1582
         Hash elements for various XEPs as defined in XEP-300
    

  • nbxmpp/structs.py
    ... ... @@ -75,6 +75,8 @@ PubSubEventData.__new__.__defaults__ = (None, None, None, False, False, False)
    75 75
     
    
    76 76
     MoodData = namedtuple('MoodData', 'mood text')
    
    77 77
     
    
    78
    +BlockingPush = namedtuple('BlockingPush', 'block unblock unblock_all')
    
    79
    +
    
    78 80
     ActivityData = namedtuple('ActivityData', 'activity subactivity text')
    
    79 81
     
    
    80 82
     LocationData = namedtuple('LocationData', LOCATION_DATA)
    
    ... ... @@ -83,8 +85,6 @@ LocationData.__new__.__defaults__ = (None,) * len(LocationData._fields)
    83 85
     BookmarkData = namedtuple('BookmarkData', 'jid name nick autojoin password')
    
    84 86
     BookmarkData.__new__.__defaults__ = (None, None, None, None)
    
    85 87
     
    
    86
    -BlockingListResult = namedtuple('BlockingListResult', 'blocking_list')
    
    87
    -
    
    88 88
     PGPPublicKey = namedtuple('PGPPublicKey', 'jid key date')
    
    89 89
     
    
    90 90
     PGPKeyMetadata = namedtuple('PGPKeyMetadata', 'jid fingerprint date')
    
    ... ... @@ -844,6 +844,7 @@ class IqProperties:
    844 844
             self.payload = None
    
    845 845
             self.http_auth = None
    
    846 846
             self.ibb = None
    
    847
    +        self.blocking = None
    
    847 848
     
    
    848 849
         @property
    
    849 850
         def is_http_auth(self):
    
    ... ... @@ -853,6 +854,10 @@ class IqProperties:
    853 854
         def is_ibb(self):
    
    854 855
             return self.ibb is not None
    
    855 856
     
    
    857
    +    @property
    
    858
    +    def is_blocking(self):
    
    859
    +        return self.blocking is not None
    
    860
    +
    
    856 861
     
    
    857 862
     class PresenceProperties:
    
    858 863
         def __init__(self):