[Git][gajim/python-nbxmpp][master] 4 commits: Add self bare property

Philipp Hörist gitlab at dev.gajim.org
Thu Jan 3 00:21:20 CET 2019


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


Commits:
0b6dc8c9 by Philipp Hörist at 2019-01-01T18:15:07Z
Add self bare property

- - - - -
fbe98a3e by Philipp Hörist at 2019-01-02T15:55:17Z
Add BaseIq handler

- - - - -
29d1e2c0 by Philipp Hörist at 2019-01-02T15:56:06Z
Use namedtuple for HTTPAuth data

- - - - -
2c8f6455 by Philipp Hörist at 2019-01-02T23:10:16Z
Add string representation for error enum

- - - - -


7 changed files:

- nbxmpp/const.py
- nbxmpp/dispatcher_nb.py
- nbxmpp/modules/http_auth.py
- + nbxmpp/modules/iq.py
- nbxmpp/modules/message.py
- nbxmpp/modules/presence.py
- nbxmpp/structs.py


Changes:

=====================================
nbxmpp/const.py
=====================================
@@ -47,6 +47,29 @@ class GSSAPIState(IntEnum):
     WRAP = 1
 
 
+class IqType(Enum):
+    GET = 'get'
+    SET = 'set'
+    RESULT = 'result'
+    ERROR = 'error'
+
+    @property
+    def is_get(self):
+        return self == IqType.GET
+
+    @property
+    def is_set(self):
+        return self == IqType.SET
+
+    @property
+    def is_result(self):
+        return self == IqType.RESULT
+
+    @property
+    def is_error(self):
+        return self == IqType.ERROR
+
+
 class MessageType(Enum):
     NORMAL = 'normal'
     CHAT = 'chat'
@@ -54,6 +77,26 @@ class MessageType(Enum):
     HEADLINE = 'headline'
     ERROR = 'error'
 
+    @property
+    def is_normal(self):
+        return self == MessageType.NORMAL
+
+    @property
+    def is_chat(self):
+        return self == MessageType.CHAT
+
+    @property
+    def is_groupchat(self):
+        return self == MessageType.GROUPCHAT
+
+    @property
+    def is_headline(self):
+        return self == MessageType.HEADLINE
+
+    @property
+    def is_error(self):
+        return self == MessageType.ERROR
+
 
 class PresenceType(Enum):
     PROBE = 'probe'
@@ -237,3 +280,6 @@ class Error(Enum):
     UNEXPECTED_REQUEST = 'unexpected-request'
     POLICY_VIOLATION = 'policy-violation'
     UNKNOWN_ERROR = 'unknown-error'
+
+    def __str__(self):
+        return self.value


=====================================
nbxmpp/dispatcher_nb.py
=====================================
@@ -45,6 +45,7 @@ from nbxmpp.modules.eme import EME
 from nbxmpp.modules.http_auth import HTTPAuth
 from nbxmpp.modules.presence import BasePresence
 from nbxmpp.modules.message import BaseMessage
+from nbxmpp.modules.iq import BaseIq
 from nbxmpp.modules.nickname import Nickname
 from nbxmpp.modules.delay import Delay
 from nbxmpp.modules.muc import MUC
@@ -163,6 +164,7 @@ class XMPPDispatcher(PlugIn):
     def _register_modules(self):
         self._modules['BasePresence'] = BasePresence(self._owner)
         self._modules['BaseMessage'] = BaseMessage(self._owner)
+        self._modules['BaseIq'] = BaseIq(self._owner)
         self._modules['EME'] = EME(self._owner)
         self._modules['HTTPAuth'] = HTTPAuth(self._owner)
         self._modules['Nickname'] = Nickname(self._owner)


=====================================
nbxmpp/modules/http_auth.py
=====================================
@@ -19,6 +19,7 @@ import logging
 
 from nbxmpp.protocol import NS_HTTP_AUTH
 from nbxmpp.structs import StanzaHandler
+from nbxmpp.structs import HTTPAuthData
 
 log = logging.getLogger('nbxmpp.m.http_auth')
 
@@ -46,5 +47,5 @@ class HTTPAuth:
 
         http_auth = confirm.getAttrs().copy()
         http_auth['body'] = stanza.getTagData('body')
-        properties.http_auth = http_auth
+        properties.http_auth = HTTPAuthData(**http_auth)
         log.info('Found data: %s', http_auth)


=====================================
nbxmpp/modules/iq.py
=====================================
@@ -0,0 +1,60 @@
+# 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/>.
+
+import logging
+
+from nbxmpp.protocol import Error as ErrorStanza
+from nbxmpp.protocol import ERR_BAD_REQUEST
+from nbxmpp.protocol import NodeProcessed
+from nbxmpp.structs import StanzaHandler
+from nbxmpp.structs import ErrorProperties
+from nbxmpp.const import IqType
+
+log = logging.getLogger('nbxmpp.m.iq')
+
+
+class BaseIq:
+    def __init__(self, client):
+        self._client = client
+        self.handlers = [
+            StanzaHandler(name='iq',
+                          callback=self._process_iq_base,
+                          priority=10),
+        ]
+
+    def _process_iq_base(self, _con, stanza, properties):
+        try:
+            properties.type = IqType(stanza.getType())
+        except ValueError:
+            log.warning('Message with invalid type: %s', stanza.getType())
+            log.warning(stanza)
+            self._client.send(ErrorStanza(stanza, ERR_BAD_REQUEST))
+            raise NodeProcessed
+
+        properties.jid = stanza.getFrom()
+        properties.id = stanza.getID()
+
+        payloads = stanza.getPayload()
+        for payload in payloads:
+            if payload.getName() != 'error':
+                properties.payload = payload
+                break
+
+        properties.query = stanza.getQuery()
+
+        if properties.type.is_error:
+            properties.error = ErrorProperties(stanza)


=====================================
nbxmpp/modules/message.py
=====================================
@@ -19,6 +19,7 @@ import logging
 
 from nbxmpp.protocol import NodeProcessed
 from nbxmpp.structs import StanzaHandler
+from nbxmpp.structs import ErrorProperties
 from nbxmpp.const import MessageType
 
 log = logging.getLogger('nbxmpp.m.message')
@@ -41,9 +42,8 @@ class BaseMessage:
         properties.thread = stanza.getThread()
         properties.subject = stanza.getSubject()
 
-        if properties.type == MessageType.ERROR:
-            properties.error_code = stanza.getErrorCode()
-            properties.error_message = stanza.getErrorMsg()
+        if properties.type.is_error:
+            properties.error = ErrorProperties(stanza)
 
     @staticmethod
     def _parse_type(stanza):


=====================================
nbxmpp/modules/presence.py
=====================================
@@ -45,12 +45,12 @@ class BasePresence:
         properties.id = stanza.getID()
         properties.status = stanza.getStatus()
 
-        if properties.type == PresenceType.ERROR:
+        if properties.type.is_error:
             properties.error = ErrorProperties(stanza)
 
         own_jid = self._client.get_bound_jid()
-        if own_jid == stanza.getFrom():
-            properties.self_presence = True
+        properties.self_presence = own_jid == properties.jid
+        properties.self_bare = properties.jid.bareMatch(own_jid)
 
     @staticmethod
     def _parse_priority(stanza):


=====================================
nbxmpp/structs.py
=====================================
@@ -50,6 +50,9 @@ MucDestroyed.__new__.__defaults__ = (None, None, None)
 EntityCapsData = namedtuple('EntityCapsData', 'hash node ver')
 EntityCapsData.__new__.__defaults__ = (None, None, None)
 
+HTTPAuthData = namedtuple('HTTPAuthData', 'id method url body')
+HTTPAuthData.__new__.__defaults__ = (None, None, None, None)
+
 class Properties:
     pass
 
@@ -65,8 +68,7 @@ class MessageProperties:
         self.thread = None
         self.user_timestamp = None
         self.timestamp = time.time()
-        self.error_code = None
-        self.error_message = None
+        self.error = None
         self.eme = None
         self.http_auth = None
         self.nickname = None
@@ -113,6 +115,12 @@ class MessageProperties:
 
 class IqProperties:
     def __init__(self):
+        self.type = None
+        self.jid = None
+        self.id = None
+        self.error = None
+        self.query = None
+        self.payload = None
         self.http_auth = None
 
     @property
@@ -130,6 +138,7 @@ class PresenceProperties:
         self.id = None
         self.nickname = None
         self.self_presence = False
+        self.self_bare = False
         self.from_muc = False
         self.status = ''
         self.timestamp = time.time()
@@ -149,6 +158,10 @@ class PresenceProperties:
     def is_self_presence(self):
         return self.self_presence
 
+    @property
+    def is_self_bare(self):
+        return self.self_bare
+
     @property
     def is_muc_destroyed(self):
         return self.muc_destroyed is not None



View it on GitLab: https://dev.gajim.org/gajim/python-nbxmpp/compare/da5f7ab1da8e8c139fdcc0bad250fa8ccd79ae07...2c8f64557619fcc2d7065fbaedddad4ed700cdb7

-- 
View it on GitLab: https://dev.gajim.org/gajim/python-nbxmpp/compare/da5f7ab1da8e8c139fdcc0bad250fa8ccd79ae07...2c8f64557619fcc2d7065fbaedddad4ed700cdb7
You're receiving this email because of your account on dev.gajim.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.gajim.org/pipermail/commits/attachments/20190103/2845b534/attachment-0001.html>


More information about the Commits mailing list