comparison libervia/backend/plugins/plugin_xep_0020.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_xep_0020.py@524856bd7b19
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SAT plugin for managing xep-0020
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from libervia.backend.core.i18n import _
21 from libervia.backend.core.constants import Const as C
22 from libervia.backend.core.log import getLogger
23
24 log = getLogger(__name__)
25 from libervia.backend.core import exceptions
26 from twisted.words.xish import domish
27
28 from zope.interface import implementer
29
30 try:
31 from twisted.words.protocols.xmlstream import XMPPHandler
32 except ImportError:
33 from wokkel.subprotocols import XMPPHandler
34
35 from wokkel import disco, iwokkel, data_form
36
37 NS_FEATURE_NEG = "http://jabber.org/protocol/feature-neg"
38
39 PLUGIN_INFO = {
40 C.PI_NAME: "XEP 0020 Plugin",
41 C.PI_IMPORT_NAME: "XEP-0020",
42 C.PI_TYPE: "XEP",
43 C.PI_PROTOCOLS: ["XEP-0020"],
44 C.PI_MAIN: "XEP_0020",
45 C.PI_HANDLER: "yes",
46 C.PI_DESCRIPTION: _("""Implementation of Feature Negotiation"""),
47 }
48
49
50 class XEP_0020(object):
51 def __init__(self, host):
52 log.info(_("Plugin XEP_0020 initialization"))
53
54 def get_handler(self, client):
55 return XEP_0020_handler()
56
57 def get_feature_elt(self, elt):
58 """Check element's children to find feature elements
59
60 @param elt(domish.Element): parent element of the feature element
61 @return: feature elements
62 @raise exceptions.NotFound: no feature element found
63 """
64 try:
65 feature_elt = next(elt.elements(NS_FEATURE_NEG, "feature"))
66 except StopIteration:
67 raise exceptions.NotFound
68 return feature_elt
69
70 def _get_form(self, elt, namespace):
71 """Return the first child data form
72
73 @param elt(domish.Element): parent of the data form
74 @param namespace (None, unicode): form namespace or None to ignore
75 @return (None, data_form.Form): data form or None is nothing is found
76 """
77 if namespace is None:
78 try:
79 form_elt = next(elt.elements(data_form.NS_X_DATA))
80 except StopIteration:
81 return None
82 else:
83 return data_form.Form.fromElement(form_elt)
84 else:
85 return data_form.findForm(elt, namespace)
86
87 def get_choosed_options(self, feature_elt, namespace):
88 """Return choosed feature for feature element
89
90 @param feature_elt(domish.Element): feature domish element
91 @param namespace (None, unicode): form namespace or None to ignore
92 @return (dict): feature name as key, and choosed option as value
93 @raise exceptions.NotFound: not data form is found
94 """
95 form = self._get_form(feature_elt, namespace)
96 if form is None:
97 raise exceptions.NotFound
98 result = {}
99 for field in form.fields:
100 values = form.fields[field].values
101 result[field] = values[0] if values else None
102 if len(values) > 1:
103 log.warning(
104 _(
105 "More than one value choosed for {}, keeping the first one"
106 ).format(field)
107 )
108 return result
109
110 def negotiate(self, feature_elt, name, negotiable_values, namespace):
111 """Negotiate the feature options
112
113 @param feature_elt(domish.Element): feature element
114 @param name: the option name (i.e. field's var attribute) to negotiate
115 @param negotiable_values(iterable): acceptable values for this negotiation
116 first corresponding value will be returned
117 @param namespace (None, unicode): form namespace or None to ignore
118 @raise KeyError: name is not found in data form fields
119 """
120 form = self._get_form(feature_elt, namespace)
121 options = [option.value for option in form.fields[name].options]
122 for value in negotiable_values:
123 if value in options:
124 return value
125 return None
126
127 def choose_option(self, options, namespace):
128 """Build a feature element with choosed options
129
130 @param options(dict): dict with feature as key and choosed option as value
131 @param namespace (None, unicode): form namespace or None to ignore
132 """
133 feature_elt = domish.Element((NS_FEATURE_NEG, "feature"))
134 x_form = data_form.Form("submit", formNamespace=namespace)
135 x_form.makeFields(options)
136 feature_elt.addChild(x_form.toElement())
137 return feature_elt
138
139 def propose_features(self, options_dict, namespace):
140 """Build a feature element with options to propose
141
142 @param options_dict(dict): dict with feature as key and iterable of acceptable options as value
143 @param namespace(None, unicode): feature namespace
144 """
145 feature_elt = domish.Element((NS_FEATURE_NEG, "feature"))
146 x_form = data_form.Form("form", formNamespace=namespace)
147 for field in options_dict:
148 x_form.addField(
149 data_form.Field(
150 "list-single",
151 field,
152 options=[data_form.Option(option) for option in options_dict[field]],
153 )
154 )
155 feature_elt.addChild(x_form.toElement())
156 return feature_elt
157
158
159 @implementer(iwokkel.IDisco)
160 class XEP_0020_handler(XMPPHandler):
161
162 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
163 return [disco.DiscoFeature(NS_FEATURE_NEG)]
164
165 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
166 return []