comparison sat/plugins/plugin_xep_0020.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents src/plugins/plugin_xep_0020.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for managing xep-0020
5 # Copyright (C) 2009-2018 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 sat.core.i18n import _
21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger
23 log = getLogger(__name__)
24 from sat.core import exceptions
25 from twisted.words.xish import domish
26
27 from zope.interface import implements
28
29 try:
30 from twisted.words.protocols.xmlstream import XMPPHandler
31 except ImportError:
32 from wokkel.subprotocols import XMPPHandler
33
34 from wokkel import disco, iwokkel, data_form
35
36 NS_FEATURE_NEG = 'http://jabber.org/protocol/feature-neg'
37
38 PLUGIN_INFO = {
39 C.PI_NAME: "XEP 0020 Plugin",
40 C.PI_IMPORT_NAME: "XEP-0020",
41 C.PI_TYPE: "XEP",
42 C.PI_PROTOCOLS: ["XEP-0020"],
43 C.PI_MAIN: "XEP_0020",
44 C.PI_HANDLER: "yes",
45 C.PI_DESCRIPTION: _("""Implementation of Feature Negotiation""")
46 }
47
48
49 class XEP_0020(object):
50
51 def __init__(self, host):
52 log.info(_("Plugin XEP_0020 initialization"))
53
54 def getHandler(self, client):
55 return XEP_0020_handler()
56
57 def getFeatureElt(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 = elt.elements(NS_FEATURE_NEG, 'feature').next()
66 except StopIteration:
67 raise exceptions.NotFound
68 return feature_elt
69
70 def _getForm(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 = elt.elements(data_form.NS_X_DATA).next()
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 getChoosedOptions(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._getForm(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(_(u"More than one value choosed for {}, keeping the first one").format(field))
104 return result
105
106 def negotiate(self, feature_elt, name, negotiable_values, namespace):
107 """Negotiate the feature options
108
109 @param feature_elt(domish.Element): feature element
110 @param name: the option name (i.e. field's var attribute) to negotiate
111 @param negotiable_values(iterable): acceptable values for this negotiation
112 first corresponding value will be returned
113 @param namespace (None, unicode): form namespace or None to ignore
114 @raise KeyError: name is not found in data form fields
115 """
116 form = self._getForm(feature_elt, namespace)
117 options = [option.value for option in form.fields[name].options]
118 for value in negotiable_values:
119 if value in options:
120 return value
121 return None
122
123 def chooseOption(self, options, namespace):
124 """Build a feature element with choosed options
125
126 @param options(dict): dict with feature as key and choosed option as value
127 @param namespace (None, unicode): form namespace or None to ignore
128 """
129 feature_elt = domish.Element((NS_FEATURE_NEG, 'feature'))
130 x_form = data_form.Form('submit', formNamespace=namespace)
131 x_form.makeFields(options)
132 feature_elt.addChild(x_form.toElement())
133 return feature_elt
134
135 def proposeFeatures(self, options_dict, namespace):
136 """Build a feature element with options to propose
137
138 @param options_dict(dict): dict with feature as key and iterable of acceptable options as value
139 @param namespace(None, unicode): feature namespace
140 """
141 feature_elt = domish.Element((NS_FEATURE_NEG, 'feature'))
142 x_form = data_form.Form('form', formNamespace=namespace)
143 for field in options_dict:
144 x_form.addField(data_form.Field('list-single', field,
145 options=[data_form.Option(option) for option in options_dict[field]]))
146 feature_elt.addChild(x_form.toElement())
147 return feature_elt
148
149
150 class XEP_0020_handler(XMPPHandler):
151 implements(iwokkel.IDisco)
152
153 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
154 return [disco.DiscoFeature(NS_FEATURE_NEG)]
155
156 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
157 return []