From 2c523d1a3b6a97b78b5ee4d688f33fccd71eb83c Mon Sep 17 00:00:00 2001 From: mathieui Date: Wed, 2 Dec 2020 20:56:41 +0100 Subject: XEP-0439: Quick Response --- slixmpp/plugins/__init__.py | 1 + slixmpp/plugins/xep_0439/__init__.py | 13 +++++ slixmpp/plugins/xep_0439/quickresponse.py | 91 +++++++++++++++++++++++++++++++ slixmpp/plugins/xep_0439/stanza.py | 43 +++++++++++++++ 4 files changed, 148 insertions(+) create mode 100644 slixmpp/plugins/xep_0439/__init__.py create mode 100644 slixmpp/plugins/xep_0439/quickresponse.py create mode 100644 slixmpp/plugins/xep_0439/stanza.py diff --git a/slixmpp/plugins/__init__.py b/slixmpp/plugins/__init__.py index 2b02c9e3..02ac3712 100644 --- a/slixmpp/plugins/__init__.py +++ b/slixmpp/plugins/__init__.py @@ -98,5 +98,6 @@ __all__ = [ 'xep_0424', # Message Retraction 'xep_0425', # Message Moderation 'xep_0428', # Message Fallback + 'xep_0439', # Quick Response 'xep_0444', # Message Reactions ] diff --git a/slixmpp/plugins/xep_0439/__init__.py b/slixmpp/plugins/xep_0439/__init__.py new file mode 100644 index 00000000..6fa46cb4 --- /dev/null +++ b/slixmpp/plugins/xep_0439/__init__.py @@ -0,0 +1,13 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2020 Mathieu Pasquet + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" + +from slixmpp.plugins.base import register_plugin +from slixmpp.plugins.xep_0439.stanza import * +from slixmpp.plugins.xep_0439.quickresponse import XEP_0439 + +register_plugin(XEP_0439) diff --git a/slixmpp/plugins/xep_0439/quickresponse.py b/slixmpp/plugins/xep_0439/quickresponse.py new file mode 100644 index 00000000..74e299d6 --- /dev/null +++ b/slixmpp/plugins/xep_0439/quickresponse.py @@ -0,0 +1,91 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2020 Mathieu Pasquet + This file is part of Slixmpp. + + See the file LICENSE for copying permission. +""" +from typing import ( + Iterable, + Optional, + Tuple, +) + +from slixmpp import JID +from slixmpp.plugins import BasePlugin +from slixmpp.plugins.xep_0439 import stanza + + +class XEP_0439(BasePlugin): + '''XEP-0439: Quick Response''' + + name = 'xep_0439' + description = 'Quick Response' + dependencies = set() + stanza = stanza + namespace = stanza.NS + + def plugin_init(self) -> None: + stanza.register_plugins() + + def ask_for_responses(self, mto: JID, body: str, + responses: Iterable[Tuple[str, str]], + mtype: str = 'chat', lang: Optional[str] = None, *, + mfrom: Optional[JID] = None): + """ + Send a message with a set of responses. + + :param JID mto: The JID of the entity which will receive the message + :param str body: The message body of the question + :param Iterable[Tuple[str, str]] responses: A set of tuples containing + (value, label) for each response + :param str mtype: The message type + :param str lang: The lang of the message (if not use, the default + for this session will be used. + """ + if lang is None: + lang = self.xmpp.default_lang + msg = self.xmpp.make_message(mto=mto, mfrom=mfrom, mtype=mtype) + msg['body|%s' % lang] = body + values = set() + for value, label in responses: + if value in values: + raise ValueError("Duplicate values") + values.add(value) + elem = stanza.Response() + elem['lang'] = lang + elem['value'] = value + elem['label'] = label + msg.append(elem) + msg.send() + + def ask_for_actions(self, mto: JID, body: str, + actions: Iterable[Tuple[str, str]], + mtype: str = 'chat', lang: Optional[str] = None, *, + mfrom: Optional[JID] = None): + """ + Send a message with a set of actions. + + :param JID mto: The JID of the entity which will receive the message + :param str body: The message body of the question + :param Iterable[Tuple[str, str]] actions: A set of tuples containing + (action, label) for each action + :param str mtype: The message type + :param str lang: The lang of the message (if not use, the default + for this session will be used. + """ + if lang is None: + lang = self.xmpp.default_lang + msg = self.xmpp.make_message(mto=mto, mfrom=mfrom, mtype=mtype) + msg['body|%s' % lang] = body + ids = set() + for id, label in actions: + if id in ids: + raise ValueError("Duplicate ids") + ids.add(id) + elem = stanza.Action() + elem['lang'] = lang + elem['id'] = id + elem['label'] = label + msg.append(elem) + msg.send() diff --git a/slixmpp/plugins/xep_0439/stanza.py b/slixmpp/plugins/xep_0439/stanza.py new file mode 100644 index 00000000..e00e1f27 --- /dev/null +++ b/slixmpp/plugins/xep_0439/stanza.py @@ -0,0 +1,43 @@ +""" + Slixmpp: The Slick XMPP Library + Copyright (C) 2020 Mathieu Pasquet + This file is part of Slixmpp. + + See the file LICENSE for copying permissio +""" + +from slixmpp.stanza import Message +from slixmpp.xmlstream import ( + ElementBase, + register_stanza_plugin, +) + + +NS = 'urn:xmpp:tmp:quick-response' + + +class Response(ElementBase): + namespace = NS + name = 'response' + plugin_attrib = 'response' + interfaces = {'value', 'label'} + + +class Action(ElementBase): + namespace = NS + name = 'action' + plugin_attrib = 'action' + interfaces = {'id', 'label'} + + +class ActionSelected(ElementBase): + namespace = NS + name = 'action-selected' + plugin_attrib = 'action_selected' + interfaces = {'id'} + + +def register_plugins(): + register_stanza_plugin(Message, Action, iterable=True) + register_stanza_plugin(Message, ActionSelected) + register_stanza_plugin(Message, Response, iterable=True) -- cgit v1.2.3