comparison libervia/backend/plugins/plugin_xep_0106.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/plugins/plugin_xep_0106.py@524856bd7b19
children 0d7bb4df2343
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SAT plugin for Explicit Message Encryption
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 from libervia.backend.core.i18n import _
21 from libervia.backend.core.constants import Const as C
22 from libervia.backend.core.log import getLogger
23 from twisted.words.protocols.jabber import xmlstream
24 from zope.interface import implementer
25 from wokkel import disco
26
27 log = getLogger(__name__)
28
29 PLUGIN_INFO = {
30 C.PI_NAME: "JID Escaping",
31 C.PI_IMPORT_NAME: "XEP-0106",
32 C.PI_TYPE: "XEP",
33 C.PI_MODES: C.PLUG_MODE_BOTH,
34 C.PI_PROTOCOLS: ["XEP-0106"],
35 C.PI_DEPENDENCIES: [],
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"""),
39 }
40
41 NS_JID_ESCAPING = r"jid\20escaping"
42 ESCAPE_MAP = {
43 ' ': r'\20',
44 '"': r'\22',
45 '&': r'\26',
46 "'": r'\27',
47 '/': r'\2f',
48 ':': r'\3a',
49 '<': r'\3c',
50 '>': r'\3e',
51 '@': r'\40',
52 '\\': r'\5c',
53 }
54
55
56 class XEP_0106(object):
57
58 def __init__(self, host):
59 self.reverse_map = {v:k for k,v in ESCAPE_MAP.items()}
60
61 def get_handler(self, client):
62 return XEP_0106_handler()
63
64 def escape(self, text):
65 """Escape text
66
67 @param text(unicode): text to escape
68 @return (unicode): escaped text
69 @raise ValueError: text can't be escaped
70 """
71 if not text or text[0] == ' ' or text[-1] == ' ':
72 raise ValueError("text must not be empty, or start or end with a whitespace")
73 escaped = []
74 for c in text:
75 if c in ESCAPE_MAP:
76 escaped.append(ESCAPE_MAP[c])
77 else:
78 escaped.append(c)
79 return ''.join(escaped)
80
81 def unescape(self, escaped):
82 """Unescape text
83
84 @param escaped(unicode): text to unescape
85 @return (unicode): unescaped text
86 @raise ValueError: text can't be unescaped
87 """
88 if not escaped or escaped.startswith(r'\27') or escaped.endswith(r'\27'):
89 raise ValueError("escaped value must not be empty, or start or end with a "
90 f"whitespace: rejected value is {escaped!r}")
91 unescaped = []
92 idx = 0
93 while idx < len(escaped):
94 char_seq = escaped[idx:idx+3]
95 if char_seq in self.reverse_map:
96 unescaped.append(self.reverse_map[char_seq])
97 idx += 3
98 else:
99 unescaped.append(escaped[idx])
100 idx += 1
101 return ''.join(unescaped)
102
103
104 @implementer(disco.IDisco)
105 class XEP_0106_handler(xmlstream.XMPPHandler):
106
107 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
108 return [disco.DiscoFeature(NS_JID_ESCAPING)]
109
110 def getDiscoItems(self, requestor, target, nodeIdentifier=""):
111 return []