annotate src/plugins/plugin_xep_0020.py @ 1055:abcac1ac27a7

plugin otr: first draft
author Goffi <goffi@goffi.org>
date Sat, 07 Jun 2014 16:39:08 +0200
parents 301b342c697a
children 069ad98b360d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
811
1fe00f0c9a91 dates update
Goffi <goffi@goffi.org>
parents: 771
diff changeset
5 # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014 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
771
bfabeedbf32e core: i18n refactoring:
Goffi <goffi@goffi.org>
parents: 660
diff changeset
20 from sat.core.i18n import _
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
21 from sat.core.log import getLogger
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
22 log = getLogger(__name__)
383
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 = {
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
38 "name": "XEP 0020 Plugin",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
39 "import_name": "XEP-0020",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
40 "type": "XEP",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
41 "protocols": ["XEP-0020"],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
42 "main": "XEP_0020",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
43 "handler": "yes",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
44 "description": _("""Implementation of Feature Negotiation""")
383
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
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
47
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
48 class XEP_0020(object):
383
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
49
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
50 def __init__(self, host):
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
51 log.info(_("Plugin XEP_0020 initialization"))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
52
383
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
53 def getHandler(self, profile):
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
54 return XEP_0020_handler()
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
55
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
56 def getFeatureElt(self, elt):
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
57 """Check element's children to find feature elements
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
58 @param elt: domish.Element
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
59 @return: feature elements"""
660
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 609
diff changeset
60 return [child for child in elt.elements() if child.name == 'feature']
383
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
61
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
62 def getChoosedOptions(self, elt):
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
63 """Return choosed feature for feature element
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
64 @param elt: feature domish element
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
65 @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
66 form = data_form.Form.fromElement(elt.firstChildElement())
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
67 result = {}
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
68 for field in form.fields:
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
69 values = form.fields[field].values
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
70 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
71 if len(values) > 1:
993
301b342c697a core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents: 811
diff changeset
72 log.warning(_("More than one value choosed for %s, keeping the first one") % field)
383
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
73 return result
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
74
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
75 def negociate(self, feature_elt, form_type, negociable_values):
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
76 """Negociate the feature options
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
77 @param feature_elt: feature domish element
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
78 @param form_type: the option to negociate
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
79 @param negociable_values: acceptable values for this negociation"""
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
80 form = data_form.Form.fromElement(feature_elt.firstChildElement())
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
81 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
82 for value in negociable_values:
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
83 if value in options:
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
84 return value
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
85 return None
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
86
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
87 def chooseOption(self, options_dict):
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
88 """Build a feature element with choosed options
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
89 @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
90 feature_elt = domish.Element((NS_FEATURE_NEG, 'feature'))
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
91 x_form = data_form.Form('submit')
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
92 x_form.makeFields(options_dict)
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
93 feature_elt.addChild(x_form.toElement())
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
94 return feature_elt
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
95
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
96 def proposeFeatures(self, options_dict, namespace=None):
383
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
97 """Build a feature element with options to propose
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
98 @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
99 @param namespace: feature namespace"""
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
100 feature_elt = domish.Element((NS_FEATURE_NEG, 'feature'))
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
101 x_form = data_form.Form('form', formNamespace=namespace)
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
102 for field in options_dict:
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
103 x_form.addField(data_form.Field('list-single', field,
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
104 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
105 feature_elt.addChild(x_form.toElement())
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
106 return feature_elt
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
107
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
108
383
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
109 class XEP_0020_handler(XMPPHandler):
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
110 implements(iwokkel.IDisco)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
111
383
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
112 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
113 return [disco.DiscoFeature(NS_FEATURE_NEG)]
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
114
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
115 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
116 return []