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

Commits:

6 changed files:

Changes:

  • nbxmpp/dispatcher_nb.py
    ... ... @@ -41,8 +41,10 @@ from nbxmpp.protocol import Protocol
    41 41
     from nbxmpp.protocol import Node
    
    42 42
     from nbxmpp.protocol import Error
    
    43 43
     from nbxmpp.protocol import ERR_FEATURE_NOT_IMPLEMENTED
    
    44
    +from nbxmpp.modules.eme import EME
    
    45
    +from nbxmpp.modules.http_auth import HTTPAuth
    
    44 46
     from nbxmpp.misc import unwrap_carbon
    
    45
    -from nbxmpp.util import PropertyDict
    
    47
    +from nbxmpp.util import get_property_dict
    
    46 48
     
    
    47 49
     
    
    48 50
     log = logging.getLogger('nbxmpp.dispatcher_nb')
    
    ... ... @@ -96,6 +98,7 @@ class XMPPDispatcher(PlugIn):
    96 98
         def __init__(self):
    
    97 99
             PlugIn.__init__(self)
    
    98 100
             self.handlers = {}
    
    101
    +        self._modules = {}
    
    99 102
             self._expected = {}
    
    100 103
             self._defaultHandler = None
    
    101 104
             self._pendingExceptions = []
    
    ... ... @@ -144,6 +147,14 @@ class XMPPDispatcher(PlugIn):
    144 147
             """
    
    145 148
             self.handlers = handlers
    
    146 149
     
    
    150
    +    def _register_modules(self):
    
    151
    +        self._modules['EME'] = EME(self._owner)
    
    152
    +        self._modules['HTTPAuth'] = HTTPAuth(self._owner)
    
    153
    +
    
    154
    +        for instance in self._modules.values():
    
    155
    +            for handler in instance.handlers:
    
    156
    +                self.RegisterHandler(*handler)
    
    157
    +
    
    147 158
         def _init(self):
    
    148 159
             """
    
    149 160
             Register default namespaces/protocols/handlers. Used internally
    
    ... ... @@ -158,6 +169,7 @@ class XMPPDispatcher(PlugIn):
    158 169
             self.RegisterProtocol('message', Message)
    
    159 170
             self.RegisterDefaultHandler(self.returnStanzaHandler)
    
    160 171
             self.RegisterEventHandler(self._owner._caller._event_dispatcher)
    
    172
    +        self._register_modules()
    
    161 173
             self.on_responses = {}
    
    162 174
     
    
    163 175
         def plugin(self, owner):
    
    ... ... @@ -178,6 +190,7 @@ class XMPPDispatcher(PlugIn):
    178 190
             """
    
    179 191
             Prepare instance to be destructed
    
    180 192
             """
    
    193
    +        self._modules = {}
    
    181 194
             self.Stream.dispatch = None
    
    182 195
             self.Stream.features = None
    
    183 196
             self.Stream.destroy()
    
    ... ... @@ -464,8 +477,8 @@ class XMPPDispatcher(PlugIn):
    464 477
             # Convert simplexml to Protocol object
    
    465 478
             stanza = self.handlers[xmlns][name]['type'](node=stanza)
    
    466 479
     
    
    467
    -        properties = PropertyDict()
    
    468
    -        if stanza.getName() == 'message':
    
    480
    +        properties = get_property_dict(name)
    
    481
    +        if name == 'message':
    
    469 482
                 # https://tools.ietf.org/html/rfc6120#section-8.1.1.1
    
    470 483
                 # If the stanza does not include a 'to' address then the client MUST
    
    471 484
                 # treat it as if the 'to' address were included with a value of the
    

  • nbxmpp/modules/__init__.py

  • nbxmpp/modules/eme.py
    1
    +# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
    
    2
    +#
    
    3
    +# This file is part of nbxmpp.
    
    4
    +#
    
    5
    +# This program is free software; you can redistribute it and/or
    
    6
    +# modify it under the terms of the GNU General Public License
    
    7
    +# as published by the Free Software Foundation; either version 2
    
    8
    +# of the License, or (at your option) any later version.
    
    9
    +#
    
    10
    +# This program is distributed in the hope that it will be useful,
    
    11
    +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    12
    +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    
    13
    +# GNU General Public License for more details.
    
    14
    +#
    
    15
    +# You should have received a copy of the GNU General Public License
    
    16
    +# along with this program; If not, see <http://www.gnu.org/licenses/>.
    
    17
    +
    
    18
    +import logging
    
    19
    +
    
    20
    +from nbxmpp.protocol import NS_EME
    
    21
    +from nbxmpp.util import StanzaHandler
    
    22
    +
    
    23
    +log = logging.getLogger('nbxmpp.m.eme')
    
    24
    +
    
    25
    +
    
    26
    +class EME:
    
    27
    +    def __init__(self, client):
    
    28
    +        self._client = client
    
    29
    +        self.handlers = [
    
    30
    +            StanzaHandler(name='message',
    
    31
    +                          callback=self._process_eme,
    
    32
    +                          ns=NS_EME,
    
    33
    +                          priority=40)
    
    34
    +        ]
    
    35
    +
    
    36
    +    @staticmethod
    
    37
    +    def _process_eme(_con, stanza, properties):
    
    38
    +        enc_tag = stanza.getTag('encryption', namespace=NS_EME)
    
    39
    +        if enc_tag is None:
    
    40
    +            return
    
    41
    +
    
    42
    +        ns = enc_tag.getAttr('namespace')
    
    43
    +        if ns is None:
    
    44
    +            log.warning('No namespace on message')
    
    45
    +            return
    
    46
    +
    
    47
    +        properties.eme = {}
    
    48
    +        properties.eme['namespace'] = ns
    
    49
    +        properties.eme['name'] = enc_tag.getAttr('name')
    
    50
    +        log.info('Found data: %s', properties.eme)

  • nbxmpp/modules/http_auth.py
    1
    +# Copyright (C) 2018 Philipp Hörist <philipp AT hoerist.com>
    
    2
    +#
    
    3
    +# This file is part of nbxmpp.
    
    4
    +#
    
    5
    +# This program is free software; you can redistribute it and/or
    
    6
    +# modify it under the terms of the GNU General Public License
    
    7
    +# as published by the Free Software Foundation; either version 2
    
    8
    +# of the License, or (at your option) any later version.
    
    9
    +#
    
    10
    +# This program is distributed in the hope that it will be useful,
    
    11
    +# but WITHOUT ANY WARRANTY; without even the implied warranty of
    
    12
    +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    
    13
    +# GNU General Public License for more details.
    
    14
    +#
    
    15
    +# You should have received a copy of the GNU General Public License
    
    16
    +# along with this program; If not, see <http://www.gnu.org/licenses/>.
    
    17
    +
    
    18
    +import logging
    
    19
    +
    
    20
    +from nbxmpp.protocol import NS_HTTP_AUTH
    
    21
    +from nbxmpp.util import StanzaHandler
    
    22
    +
    
    23
    +log = logging.getLogger('nbxmpp.m.http_auth')
    
    24
    +
    
    25
    +
    
    26
    +class HTTPAuth:
    
    27
    +    def __init__(self, client):
    
    28
    +        self._client = client
    
    29
    +        self.handlers = [
    
    30
    +            StanzaHandler(name='message',
    
    31
    +                          callback=self._process_http_auth,
    
    32
    +                          ns=NS_HTTP_AUTH,
    
    33
    +                          priority=40),
    
    34
    +            StanzaHandler(name='iq',
    
    35
    +                          callback=self._process_http_auth,
    
    36
    +                          typ='get',
    
    37
    +                          ns=NS_HTTP_AUTH,
    
    38
    +                          priority=40)
    
    39
    +        ]
    
    40
    +
    
    41
    +    @staticmethod
    
    42
    +    def _process_http_auth(_con, stanza, properties):
    
    43
    +        confirm = stanza.getTag('confirm', namespace=NS_HTTP_AUTH)
    
    44
    +        if confirm is None:
    
    45
    +            return
    
    46
    +
    
    47
    +        http_auth = confirm.getAttrs().copy()
    
    48
    +        http_auth['body'] = stanza.getTagData('body')
    
    49
    +        properties.http_auth = http_auth
    
    50
    +        log.info('Found data: %s', http_auth)

  • nbxmpp/util.py
    ... ... @@ -16,6 +16,10 @@
    16 16
     # along with this program; If not, see <http://www.gnu.org/licenses/>.
    
    17 17
     
    
    18 18
     import base64
    
    19
    +from collections import namedtuple
    
    20
    +
    
    21
    +StanzaHandler = namedtuple('StanzaHandler', 'name callback typ ns xmlns system priority')
    
    22
    +StanzaHandler.__new__.__defaults__ = ('', '', None, False, 50)
    
    19 23
     
    
    20 24
     
    
    21 25
     def b64decode(data, return_type=str):
    
    ... ... @@ -36,15 +40,47 @@ def b64encode(data, return_type=str):
    36 40
         return result.decode()
    
    37 41
     
    
    38 42
     
    
    39
    -class PropertyDict:
    
    43
    +class PropertyBase:
    
    40 44
         def __init__(self):
    
    41
    -        self.__data = {}
    
    45
    +        self._data = {}
    
    42 46
     
    
    43 47
         def __getattr__(self, key):
    
    44
    -        return self.__data[key]
    
    48
    +        return self._data[key]
    
    45 49
     
    
    46 50
         def __setattr__(self, key, value):
    
    47
    -        if '__' in key:
    
    51
    +        if '_data' in key:
    
    48 52
                 super().__setattr__(key, value)
    
    49 53
             else:
    
    50
    -            self.__data[key] = value
    54
    +            self._data[key] = value
    
    55
    +
    
    56
    +
    
    57
    +class MessagePropertyDict(PropertyBase):
    
    58
    +    def __init__(self):
    
    59
    +        self._data = {
    
    60
    +            'carbon_type': None,
    
    61
    +            'eme': None,
    
    62
    +            'http_auth': None,
    
    63
    +        }
    
    64
    +
    
    65
    +    @property
    
    66
    +    def is_http_auth(self):
    
    67
    +        return self._data['http_auth'] is not None
    
    68
    +
    
    69
    +
    
    70
    +class IqPropertyDict(PropertyBase):
    
    71
    +    def __init__(self):
    
    72
    +        self._data = {
    
    73
    +            'http_auth': None,
    
    74
    +        }
    
    75
    +
    
    76
    +    @property
    
    77
    +    def is_http_auth(self):
    
    78
    +        return self._data['http_auth'] is not None
    
    79
    +
    
    80
    +
    
    81
    +def get_property_dict(name):
    
    82
    +    if name == 'message':
    
    83
    +        return MessagePropertyDict()
    
    84
    +    if name == 'iq':
    
    85
    +        return IqPropertyDict()
    
    86
    +    return PropertyBase()

  • setup.py
    ... ... @@ -8,5 +8,5 @@ setup(name='nbxmpp',
    8 8
           author='Yann Leboulanger',
    
    9 9
           author_email='asterix@lagaule.org',
    
    10 10
           url='http://dev.gajim.org/gajim/python-nbxmpp',
    
    11
    -      packages=['nbxmpp'],
    
    11
    +      packages=['nbxmpp', 'nbxmpp.modules'],
    
    12 12
           )