diff options
author | Philipp Hörist <philipp@hoerist.com> | 2019-10-14 21:50:58 +0300 |
---|---|---|
committer | Philipp Hörist <philipp@hoerist.com> | 2019-10-14 21:53:27 +0300 |
commit | 16305759af54f1043a975ffd3effcb9b1e147d54 (patch) | |
tree | b082d443ec9f975aabd068e2887eec95942f34e1 | |
parent | bae3cfd112d2cb8fd5aba91ce17dcff1021a24b9 (diff) |
Add Chat State Notifications (XEP-0085) support
-rw-r--r-- | nbxmpp/const.py | 9 | ||||
-rw-r--r-- | nbxmpp/dispatcher.py | 2 | ||||
-rw-r--r-- | nbxmpp/modules/chatstates.py | 62 | ||||
-rw-r--r-- | nbxmpp/structs.py | 5 |
4 files changed, 78 insertions, 0 deletions
diff --git a/nbxmpp/const.py b/nbxmpp/const.py index 32bbf2e..3d5431f 100644 --- a/nbxmpp/const.py +++ b/nbxmpp/const.py @@ -545,3 +545,12 @@ TUNE_DATA = [ 'title', 'track', 'uri'] + + +CHATSTATES = [ + 'active', + 'inactive', + 'gone', + 'composing', + 'paused' +]
\ No newline at end of file diff --git a/nbxmpp/dispatcher.py b/nbxmpp/dispatcher.py index 39f72f6..c465ae9 100644 --- a/nbxmpp/dispatcher.py +++ b/nbxmpp/dispatcher.py @@ -77,6 +77,7 @@ 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.chatstates import Chatstates from nbxmpp.modules.misc import unwrap_carbon from nbxmpp.modules.misc import unwrap_mam from nbxmpp.util import get_properties_struct @@ -221,6 +222,7 @@ class XMPPDispatcher(PlugIn): self._modules['Correction'] = Correction(self._owner) self._modules['Attention'] = Attention(self._owner) self._modules['SecurityLabels'] = SecurityLabels(self._owner) + self._modules['Chatstates'] = Chatstates(self._owner) for instance in self._modules.values(): for handler in instance.handlers: diff --git a/nbxmpp/modules/chatstates.py b/nbxmpp/modules/chatstates.py new file mode 100644 index 0000000..8304310 --- /dev/null +++ b/nbxmpp/modules/chatstates.py @@ -0,0 +1,62 @@ +# 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_CHATSTATES +from nbxmpp.protocol import NS_DELAY2 +from nbxmpp.structs import StanzaHandler +from nbxmpp.const import CHATSTATES + +log = logging.getLogger('nbxmpp.m.chatstates') + + +class Chatstates: + def __init__(self, client): + self._client = client + self.handlers = [ + StanzaHandler(name='message', + callback=self._process_message_chatstate, + ns=NS_CHATSTATES, + priority=15), + ] + + def _process_message_chatstate(self, _con, stanza, properties): + chatstate = parse_chatstate(stanza) + if chatstate is None: + return + + if properties.is_mam_message: + return + + if stanza.getTag('delay', namespace=NS_DELAY2) is not None: + return + + if chatstate not in CHATSTATES: + log.warning('Invalid chatstate: %s', chatstate) + log.warning(stanza) + return + + properties.chatstate = chatstate + + +def parse_chatstate(stanza): + children = stanza.getChildren() + for child in children: + if child.getNamespace() == NS_CHATSTATES: + return child.getName() + return None diff --git a/nbxmpp/structs.py b/nbxmpp/structs.py index eeea660..b735d48 100644 --- a/nbxmpp/structs.py +++ b/nbxmpp/structs.py @@ -558,6 +558,7 @@ class MessageProperties: self.forms = None self.xhtml = None self.security_label = None + self.chatstate = None @property def has_user_delay(self): @@ -662,6 +663,10 @@ class MessageProperties: def has_security_label(self): return self.security_label is not None + @property + def has_chatstate(self): + return self.chatstate is not None + class IqProperties: def __init__(self): |