Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0106.py @ 2979:f44a95f566d2
core, plugin XEP-0045: fix crashes when changing URIs:
When adding missing URIs in message, the "children" property was used instead of elements(). "children" not only return elements, and this was causing crashes.
This may fix bug 294
rel 294
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 30 Jun 2019 15:52:30 +0200 |
parents | 28c969432557 |
children | ab2696e34d29 |
rev | line source |
---|---|
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python2 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # SAT plugin for Explicit Message Encryption |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # This program is free software: you can redistribute it and/or modify |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # it under the terms of the GNU Affero General Public License as published by |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # the Free Software Foundation, either version 3 of the License, or |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 # (at your option) any later version. |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # This program is distributed in the hope that it will be useful, |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 # GNU Affero General Public License for more details. |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # You should have received a copy of the GNU Affero General Public License |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 from sat.core.i18n import _ |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from sat.core.constants import Const as C |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from sat.core.log import getLogger |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 from twisted.words.protocols.jabber import xmlstream |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 from zope.interface import implements |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from wokkel import disco |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 log = getLogger(__name__) |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 PLUGIN_INFO = { |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 C.PI_NAME: u"JID Escaping", |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 C.PI_IMPORT_NAME: u"XEP-0106", |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 C.PI_TYPE: u"XEP", |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 C.PI_MODES: C.PLUG_MODE_BOTH, |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 C.PI_PROTOCOLS: [u"XEP-0106"], |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 C.PI_DEPENDENCIES: [], |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 C.PI_MAIN: u"XEP_0106", |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 C.PI_HANDLER: u"yes", |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 C.PI_DESCRIPTION: _(u"""(Un)escape JID to use disallowed chars in local parts"""), |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 } |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 NS_JID_ESCAPING = ur"jid\20escaping" |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 ESCAPE_MAP = { |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 ' ': r'\20', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 '"': r'\22', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 '&': r'\26', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 "'": r'\27', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 '/': r'\2f', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 ':': r'\3a', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 '<': r'\3c', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 '>': r'\3e', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 '@': r'\40', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 '\\': r'\5c', |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 } |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 class XEP_0106(object): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 def __init__(self, host): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 self.reverse_map = {v:k for k,v in ESCAPE_MAP.iteritems()} |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 def getHandler(self, client): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 return XEP_0106_handler() |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 def escape(self, text): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 """Escape text |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 @param text(unicode): text to escape |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 @return (unicode): escaped text |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 @raise ValueError: text can't be escaped |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 """ |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 if not text or text[0] == ' ' or text[-1] == ' ': |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 raise ValueError(u"text must not be empty, or start or end with a whitespace") |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 escaped = [] |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 for c in text: |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 if c in ESCAPE_MAP: |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 escaped.append(ESCAPE_MAP[c]) |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 else: |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 escaped.append(c) |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 return u''.join(escaped) |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 def unescape(self, escaped): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 """Unescape text |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 @param escaped(unicode): text to unescape |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 @return (unicode): unescaped text |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 @raise ValueError: text can't be unescaped |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 """ |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 if not escaped or escaped.startswith(r'\27') or escaped.endswith(r'\27'): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 raise ValueError(u"escaped value must not be empty, or start or end with a " |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 u"whitespace") |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 unescaped = [] |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 idx = 0 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 while idx < len(escaped): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 char_seq = escaped[idx:idx+3] |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 if char_seq in self.reverse_map: |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 unescaped.append(self.reverse_map[char_seq]) |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 idx += 3 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 else: |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 unescaped.append(escaped[idx]) |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 idx += 1 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 return u''.join(unescaped) |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 class XEP_0106_handler(xmlstream.XMPPHandler): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 implements(disco.IDisco) |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 return [disco.DiscoFeature(NS_JID_ESCAPING)] |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 def getDiscoItems(self, requestor, target, nodeIdentifier=""): |
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 return [] |