comparison sat/plugins/plugin_xep_0106.py @ 2922:28c969432557

plugin XEP-0106: jid escaping implementation
author Goffi <goffi@goffi.org>
date Fri, 26 Apr 2019 11:57:26 +0200
parents
children ab2696e34d29
comparison
equal deleted inserted replaced
2921:a8c2d8b3453f 2922:28c969432557
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT plugin for Explicit Message Encryption
5 # Copyright (C) 2009-2019 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 sat.core.i18n import _
21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger
23 from twisted.words.protocols.jabber import xmlstream
24 from zope.interface import implements
25 from wokkel import disco
26
27 log = getLogger(__name__)
28
29 PLUGIN_INFO = {
30 C.PI_NAME: u"JID Escaping",
31 C.PI_IMPORT_NAME: u"XEP-0106",
32 C.PI_TYPE: u"XEP",
33 C.PI_MODES: C.PLUG_MODE_BOTH,
34 C.PI_PROTOCOLS: [u"XEP-0106"],
35 C.PI_DEPENDENCIES: [],
36 C.PI_MAIN: u"XEP_0106",
37 C.PI_HANDLER: u"yes",
38 C.PI_DESCRIPTION: _(u"""(Un)escape JID to use disallowed chars in local parts"""),
39 }
40
41 NS_JID_ESCAPING = ur"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.iteritems()}
60
61 def getHandler(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(u"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 u''.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(u"escaped value must not be empty, or start or end with a "
90 u"whitespace")
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 u''.join(unescaped)
102
103
104 class XEP_0106_handler(xmlstream.XMPPHandler):
105 implements(disco.IDisco)
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 []