annotate src/plugins/plugin_xep_0020.py @ 817:c39117d00f35

jp: refactoring: - imports from sat_frontends.jp instead of local imports - added __init__.py - commands now inherits from a base class: each base.CommandBase instance is a subcommand - new arguments are added in CommandBase.add_parser_options methods, starting point si CommandBase.run or CommandBase.connected if a profile connection is needed - commands are exported using a __commands__ variable at the top of the module - sub-subcommand are easily added by using an other CommandBase instance as parent instead of using a Jp instance. In this case, the parent subcommand must be the one exported, and have a subcommands iterable (see cmd_file or cmd_pipe for examples). - options which are often used (like --profile) are automatically added on demand (use_profile=True, use_progress=True) - commands are automatically loaded when there are in a module named cmd_XXX - restored --connect option - restored progress bar - restored getVersion bridge call on jp --version - fixed file and pipe commands - fixed forgotten translations - fixed non SàT compliant docstrings - better about/version dialog
author Goffi <goffi@goffi.org>
date Mon, 10 Feb 2014 13:44:09 +0100
parents 1fe00f0c9a91
children 301b342c697a
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 _
383
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from logging import debug, info, warning, error
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.words.protocols.jabber import client, jid
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.xish import domish
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
24
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from zope.interface import implements
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
26
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
27 try:
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from twisted.words.protocols.xmlstream import XMPPHandler
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
29 except ImportError:
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from wokkel.subprotocols import XMPPHandler
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
31
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from wokkel import disco, iwokkel, data_form
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
33
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
34 NS_FEATURE_NEG = 'http://jabber.org/protocol/feature-neg'
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
35
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
36 PLUGIN_INFO = {
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
37 "name": "XEP 0020 Plugin",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
38 "import_name": "XEP-0020",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
39 "type": "XEP",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
40 "protocols": ["XEP-0020"],
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
41 "main": "XEP_0020",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
42 "handler": "yes",
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
43 "description": _("""Implementation of Feature Negotiation""")
383
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
44 }
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
45
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
46
588
beaf6bec2fcd Remove every old-style class.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 587
diff changeset
47 class XEP_0020(object):
383
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"))
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
51
383
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"""
660
69a8bfd266a5 core, plugins: fixed bad use of children instead of elements() for domish.Element instances.
Goffi <goffi@goffi.org>
parents: 609
diff changeset
59 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
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
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
70 if len(values) > 1:
383
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
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
95 def proposeFeatures(self, options_dict, namespace=None):
383
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
594
e629371a28d3 Fix pep8 support in src/plugins.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 588
diff changeset
107
383
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
108 class XEP_0020_handler(XMPPHandler):
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
109 implements(iwokkel.IDisco)
587
952322b1d490 Remove trailing whitespaces.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 572
diff changeset
110
383
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
111 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
112 return [disco.DiscoFeature(NS_FEATURE_NEG)]
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
113
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
114 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
98e1d44d5cd4 plugins: feature negociation (XEP-0020) implementation
Goffi <goffi@goffi.org>
parents:
diff changeset
115 return []