Mercurial > libervia-backend
annotate sat/plugins/plugin_xep_0106.py @ 3742:bf0505d41c09
comp AP: helper methods to get AP objects:
rel 364
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 22 Mar 2022 17:00:42 +0100 |
parents | be6d91572633 |
children | 524856bd7b19 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
3137 | 2 |
2922
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 |
3479 | 5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org) |
2922
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 |
3028 | 24 from zope.interface import implementer |
2922
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 = { |
3028 | 30 C.PI_NAME: "JID Escaping", |
31 C.PI_IMPORT_NAME: "XEP-0106", | |
32 C.PI_TYPE: "XEP", | |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 C.PI_MODES: C.PLUG_MODE_BOTH, |
3028 | 34 C.PI_PROTOCOLS: ["XEP-0106"], |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 C.PI_DEPENDENCIES: [], |
3028 | 36 C.PI_MAIN: "XEP_0106", |
37 C.PI_HANDLER: "yes", | |
38 C.PI_DESCRIPTION: _("""(Un)escape JID to use disallowed chars in local parts"""), | |
2922
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 |
3028 | 41 NS_JID_ESCAPING = r"jid\20escaping" |
2922
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): |
3028 | 59 self.reverse_map = {v:k for k,v in ESCAPE_MAP.items()} |
2922
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] == ' ': |
3028 | 72 raise ValueError("text must not be empty, or start or end with a whitespace") |
2922
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) |
3028 | 79 return ''.join(escaped) |
2922
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'): |
3028 | 89 raise ValueError("escaped value must not be empty, or start or end with a " |
3450
66c4fc1f7c30
plugin XEP-0106: log rejected value when escaping fails.
Goffi <goffi@goffi.org>
parents:
3137
diff
changeset
|
90 f"whitespace: rejected value is {escaped!r}") |
2922
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 |
3028 | 101 return ''.join(unescaped) |
2922
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 |
3028 | 104 @implementer(disco.IDisco) |
2922
28c969432557
plugin XEP-0106: jid escaping implementation
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 class XEP_0106_handler(xmlstream.XMPPHandler): |
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 [] |