diff options
author | Philipp Hörist <philipp@hoerist.com> | 2019-10-14 19:20:19 +0300 |
---|---|---|
committer | Philipp Hörist <philipp@hoerist.com> | 2019-10-14 19:20:19 +0300 |
commit | bae3cfd112d2cb8fd5aba91ce17dcff1021a24b9 (patch) | |
tree | 2c9d9e8316316ca7fc4200f26570585336234648 /nbxmpp | |
parent | 8cc2f1d3e7babb442a259d21bad5635345f8466f (diff) |
Add Security Labels (XEP-0258) support
Diffstat (limited to 'nbxmpp')
-rw-r--r-- | nbxmpp/dispatcher.py | 2 | ||||
-rw-r--r-- | nbxmpp/modules/security_labels.py | 57 | ||||
-rw-r--r-- | nbxmpp/structs.py | 8 |
3 files changed, 67 insertions, 0 deletions
diff --git a/nbxmpp/dispatcher.py b/nbxmpp/dispatcher.py index 86ac7fe..39f72f6 100644 --- a/nbxmpp/dispatcher.py +++ b/nbxmpp/dispatcher.py @@ -76,6 +76,7 @@ from nbxmpp.modules.receipts import Receipts from nbxmpp.modules.oob import OOB from nbxmpp.modules.correction import Correction from nbxmpp.modules.attention import Attention +from nbxmpp.modules.security_labels import SecurityLabels from nbxmpp.modules.misc import unwrap_carbon from nbxmpp.modules.misc import unwrap_mam from nbxmpp.util import get_properties_struct @@ -219,6 +220,7 @@ class XMPPDispatcher(PlugIn): self._modules['OOB'] = OOB(self._owner) self._modules['Correction'] = Correction(self._owner) self._modules['Attention'] = Attention(self._owner) + self._modules['SecurityLabels'] = SecurityLabels(self._owner) for instance in self._modules.values(): for handler in instance.handlers: diff --git a/nbxmpp/modules/security_labels.py b/nbxmpp/modules/security_labels.py new file mode 100644 index 0000000..04e119d --- /dev/null +++ b/nbxmpp/modules/security_labels.py @@ -0,0 +1,57 @@ +# 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 NS_SECLABEL +from nbxmpp.structs import StanzaHandler +from nbxmpp.structs import SecurityLabel +from nbxmpp.structs import DisplayMarking + +log = logging.getLogger('nbxmpp.m.security_labels') + + +class SecurityLabels: + def __init__(self, client): + self._client = client + self.handlers = [ + StanzaHandler(name='message', + callback=self._process_message_security_label, + ns=NS_SECLABEL, + priority=15), + ] + + def _process_message_security_label(self, _con, stanza, properties): + security = stanza.getTag('securitylabel', namespace=NS_SECLABEL) + if security is None: + return + + displaymarking = security.getTag('displaymarking') + if displaymarking is None: + return + + label = displaymarking.getData() + if not label: + log.warning('No label found') + log.warning(stanza) + return + + fgcolor = displaymarking.getAttr('fgcolor') + bgcolor = displaymarking.getAttr('bgcolor') + + properties.security_label = SecurityLabel( + DisplayMarking(label, fgcolor, bgcolor)) diff --git a/nbxmpp/structs.py b/nbxmpp/structs.py index 98b28bc..eeea660 100644 --- a/nbxmpp/structs.py +++ b/nbxmpp/structs.py @@ -125,6 +125,9 @@ OOBData = namedtuple('OOBData', 'url desc') CorrectionData = namedtuple('CorrectionData', 'id') +DisplayMarking = namedtuple('DisplayMarking', 'label fgcolor bgcolor') +SecurityLabel = namedtuple('SecurityLabel', 'displaymarking') + class DiscoInfo(namedtuple('DiscoInfo', 'stanza identities features dataforms timestamp')): @@ -554,6 +557,7 @@ class MessageProperties: self.attention = False self.forms = None self.xhtml = None + self.security_label = None @property def has_user_delay(self): @@ -654,6 +658,10 @@ class MessageProperties: def has_xhtml(self): return self.xhtml is not None + @property + def has_security_label(self): + return self.security_label is not None + class IqProperties: def __init__(self): |