Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0020.py @ 481:0b671dbc476e
misc: CONTRAT SOCIAL (Social Contract) English translation
author | Matthieu Rakotojaona <matthieu.rakotojaona@gmail.com> |
---|---|
date | Thu, 02 Aug 2012 01:07:45 +0200 |
parents | 2a072735e459 |
children | ca13633d3b6b |
rev | line source |
---|---|
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/python |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 """ |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 SAT plugin for managing xep-0020 |
459 | 6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org) |
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 This program is free software: you can redistribute it and/or modify |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
9 it under the terms of the GNU Affero General Public License as published by |
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 (at your option) any later version. |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 This program is distributed in the hope that it will be useful, |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
16 GNU Affero General Public License for more details. |
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 |
480
2a072735e459
Licence modification: the full project is now under AGPL v3+ instead of GPL v3+
Goffi <goffi@goffi.org>
parents:
459
diff
changeset
|
18 You should have received a copy of the GNU Affero General Public License |
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 along with this program. If not, see <http://www.gnu.org/licenses/>. |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 """ |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from logging import debug, info, warning, error |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.words.protocols.jabber import client, jid |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.words.xish import domish |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from zope.interface import implements |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 try: |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from twisted.words.protocols.xmlstream import XMPPHandler |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 except ImportError: |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 from wokkel.subprotocols import XMPPHandler |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 from wokkel import disco, iwokkel, data_form |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 NS_FEATURE_NEG = 'http://jabber.org/protocol/feature-neg' |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 PLUGIN_INFO = { |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 "name": "XEP 0020 Plugin", |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 "import_name": "XEP-0020", |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 "type": "XEP", |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 "protocols": ["XEP-0020"], |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 "main": "XEP_0020", |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 "handler": "yes", |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 "description": _("""Implementation of Feature Negotiation""") |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 } |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 class XEP_0020(): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 def __init__(self, host): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 info(_("Plugin XEP_0020 initialization")) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 def getHandler(self, profile): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 return XEP_0020_handler() |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 def getFeatureElt(self, elt): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 """Check element's children to find feature elements |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 @param elt: domish.Element |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 @return: feature elements""" |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 return filter(lambda elt: elt.name == 'feature', elt.elements()) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 def getChoosedOptions(self, elt): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 """Return choosed feature for feature element |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 @param elt: feature domish element |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 @return: dict with feature name as key, and choosed option as value""" |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 form = data_form.Form.fromElement(elt.firstChildElement()) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 result = {} |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 for field in form.fields: |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 values = form.fields[field].values |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 result[field] = values[0] if values else None |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 if len(values)>1: |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 warning(_("More than one value choosed for %s, keeping the first one") % field) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 return result |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 def negociate(self, feature_elt, form_type, negociable_values): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 """Negociate the feature options |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 @param feature_elt: feature domish element |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 @param form_type: the option to negociate |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 @param negociable_values: acceptable values for this negociation""" |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 form = data_form.Form.fromElement(feature_elt.firstChildElement()) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 options = [option.value for option in form.fields[form_type].options] |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 for value in negociable_values: |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 if value in options: |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 return value |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 return None |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 def chooseOption(self, options_dict): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 """Build a feature element with choosed options |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 @param options_dict: dict with feature as key and choosed option as value""" |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 feature_elt = domish.Element((NS_FEATURE_NEG, 'feature')) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 x_form = data_form.Form('submit') |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 x_form.makeFields(options_dict) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 feature_elt.addChild(x_form.toElement()) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 return feature_elt |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 def proposeFeatures(self, options_dict, namespace = None): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 """Build a feature element with options to propose |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 @param options_dict: dict with feature as key and list of acceptable options as value |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 @param namespace: feature namespace""" |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 feature_elt = domish.Element((NS_FEATURE_NEG, 'feature')) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 x_form = data_form.Form('form', formNamespace=namespace) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 for field in options_dict: |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 x_form.addField(data_form.Field('list-single', field, |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 options=[data_form.Option(_option) for _option in options_dict[field]])) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 feature_elt.addChild(x_form.toElement()) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 return feature_elt |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 class XEP_0020_handler(XMPPHandler): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 implements(iwokkel.IDisco) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 def getDiscoInfo(self, requestor, target, nodeIdentifier=''): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 return [disco.DiscoFeature(NS_FEATURE_NEG)] |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 def getDiscoItems(self, requestor, target, nodeIdentifier=''): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 return [] |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |