Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0020.py @ 609:84a6e83157c2
fixed licences in docstrings (they are now in comments)
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 08 Mar 2013 00:36:22 +0100 |
parents | e629371a28d3 |
children | 69a8bfd266a5 |
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 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
4 # SAT plugin for managing xep-0020 |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013 Jérôme Poisson (goffi@goffi.org) |
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
10 # (at your option) any later version. |
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
15 # GNU Affero General Public License for more details. |
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
609
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
84a6e83157c2
fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents:
594
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from logging import debug, info, warning, error |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from twisted.words.protocols.jabber import client, jid |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from twisted.words.xish import domish |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from zope.interface import implements |
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 try: |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from twisted.words.protocols.xmlstream import XMPPHandler |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 except ImportError: |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from wokkel.subprotocols import XMPPHandler |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 from wokkel import disco, iwokkel, data_form |
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 NS_FEATURE_NEG = 'http://jabber.org/protocol/feature-neg' |
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 PLUGIN_INFO = { |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
36 "name": "XEP 0020 Plugin", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
37 "import_name": "XEP-0020", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
38 "type": "XEP", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
39 "protocols": ["XEP-0020"], |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
40 "main": "XEP_0020", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
41 "handler": "yes", |
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
42 "description": _("""Implementation of Feature Negotiation""") |
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 } |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
45 |
588
beaf6bec2fcd
Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
587
diff
changeset
|
46 class XEP_0020(object): |
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 def __init__(self, host): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 info(_("Plugin XEP_0020 initialization")) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
50 |
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 def getHandler(self, profile): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 return XEP_0020_handler() |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 def getFeatureElt(self, elt): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 """Check element's children to find feature elements |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 @param elt: domish.Element |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 @return: feature elements""" |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 return filter(lambda elt: elt.name == 'feature', elt.elements()) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 def getChoosedOptions(self, elt): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 """Return choosed feature for feature element |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 @param elt: feature domish element |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 @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
|
64 form = data_form.Form.fromElement(elt.firstChildElement()) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 result = {} |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 for field in form.fields: |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 values = form.fields[field].values |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 result[field] = values[0] if values else None |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
69 if len(values) > 1: |
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 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
|
71 return result |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 def negociate(self, feature_elt, form_type, negociable_values): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 """Negociate the feature options |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 @param feature_elt: feature domish element |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 @param form_type: the option to negociate |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 @param negociable_values: acceptable values for this negociation""" |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 form = data_form.Form.fromElement(feature_elt.firstChildElement()) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 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
|
80 for value in negociable_values: |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 if value in options: |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 return value |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 return None |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 def chooseOption(self, options_dict): |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 """Build a feature element with choosed options |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 @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
|
88 feature_elt = domish.Element((NS_FEATURE_NEG, 'feature')) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 x_form = data_form.Form('submit') |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 x_form.makeFields(options_dict) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 feature_elt.addChild(x_form.toElement()) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 return feature_elt |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
94 def proposeFeatures(self, options_dict, namespace=None): |
383
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 """Build a feature element with options to propose |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 @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
|
97 @param namespace: feature namespace""" |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 feature_elt = domish.Element((NS_FEATURE_NEG, 'feature')) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 x_form = data_form.Form('form', formNamespace=namespace) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 for field in options_dict: |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 x_form.addField(data_form.Field('list-single', field, |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 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
|
103 feature_elt.addChild(x_form.toElement()) |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 return feature_elt |
98e1d44d5cd4
plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 |
594
e629371a28d3
Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
588
diff
changeset
|
106 |
383
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) |
587
952322b1d490
Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
572
diff
changeset
|
109 |
383
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 [] |