Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0315.py @ 2773:b7974814dd5b
jp (pubsub/search): fixed name for node_max
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 12 Jan 2019 16:49:23 +0100 |
parents | 30d95dc3c22d |
children | 95321f233387 |
rev | line source |
---|---|
2772
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin to implement XEP-0315 (Data Forms XML Element) |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.i18n import _ |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core.constants import Const as C |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.log import getLogger |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from wokkel import data_form |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from twisted.words.xish import domish |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 log = getLogger(__name__) |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 PLUGIN_INFO = { |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 C.PI_NAME: "Data Forms XML Element", |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 C.PI_IMPORT_NAME: "XEP-0315", |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 C.PI_TYPE: "XEP", |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 C.PI_PROTOCOLS: ["XEP-0315"], |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 C.PI_DEPENDENCIES: [], |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 C.PI_MAIN: "XEP_0315", |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 C.PI_HANDLER: "no", |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 C.PI_DESCRIPTION: _("""Implementation of Data Forms XML Element"""), |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 } |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 NS_XML_ELEMENT = "urn:xmpp:xml-element" |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 class Field(data_form.Field): |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 """Field class adding implementation for XML Element""" |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 def __init__(self, fieldType='text-single', var=None, value=None, |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 values=None, options=None, label=None, desc=None, |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 required=False, ext_type=None): |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 """ |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 @ext_type(None, unicode): extended type, can be: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 - xml: field is an XML Element |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 - None: field is a base (XEP-0004) type |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 """ |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 super(Field, self).__init__(fieldType, var, value, values, options, label, desc, |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 required) |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 self.ext_type = ext_type |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 def typeCheck(self): |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 if self.ext_type is not None: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 # we use an extended type |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 if self.fieldType not in (None, 'hidden'): |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 raise ValueError('Invalid main type for a extended type field') |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 if len(self.values) != 1: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 raise ValueError('extended type must have one and only one value') |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 if self.options: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 raise ValueError('options must not be set for extented type') |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 if self.ext_type == u'xml': |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 if not isinstance(self.value, domish.Element): |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 raise ValueError('invalid value, a domish.Element is expected') |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 else: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 raise ValueError( |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 'Unknown extended type: {ext_type}'.format(ext_type=self.ext_type)) |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 return super(Field, self).typeCheck() |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 def toElement(self, asForm=False): |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 if self.ext_type is None: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 return super(Field, self).toElement(asForm) |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 else: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 self.typeCheck() |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 field_elt = domish.Element((data_form.NS_X_DATA, 'field')) |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 if self.fieldType: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 field_elt['type'] = self.fieldType |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 if self.var is not None: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 field_elt['var'] = self.var |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 wrapper_elt = field_elt.addElement((NS_XML_ELEMENT, u'wrapper')) |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 wrapper_elt.addChild(self.value) |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 if asForm: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 if self.label is not None: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 field_elt['label'] = self.label |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 if self.desc is not None: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 field_elt.addElement('desc', content=self.desc) |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 if self.required: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 field_elt.addElement('required') |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 return field_elt |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 @staticmethod |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 def fromElement(element): |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 field = super(Field, Field).fromElement(element) |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 if element.wrapper and element.wrapper.uri == NS_XML_ELEMENT: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 field.ext_type = u"xml" |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 field.value = element.wrapper.firstChildElement() |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 return field |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 class XEP_0315(object): |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 def __init__(self, host): |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 self.host = host |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 if data_form.Field is not Field: |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 log.debug(u"installing data_form data extension") |
30d95dc3c22d
plugin XEP-0315: implementation of Data Forms XML Element
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 data_form.Field = Field |