comparison sat/plugins/plugin_xep_0106.py @ 3028:ab2696e34d29

Python 3 port: /!\ this is a huge commit /!\ starting from this commit, SàT is needs Python 3.6+ /!\ SàT maybe be instable or some feature may not work anymore, this will improve with time This patch port backend, bridge and frontends to Python 3. Roughly this has been done this way: - 2to3 tools has been applied (with python 3.7) - all references to python2 have been replaced with python3 (notably shebangs) - fixed files not handled by 2to3 (notably the shell script) - several manual fixes - fixed issues reported by Python 3 that where not handled in Python 2 - replaced "async" with "async_" when needed (it's a reserved word from Python 3.7) - replaced zope's "implements" with @implementer decorator - temporary hack to handle data pickled in database, as str or bytes may be returned, to be checked later - fixed hash comparison for password - removed some code which is not needed anymore with Python 3 - deactivated some code which needs to be checked (notably certificate validation) - tested with jp, fixed reported issues until some basic commands worked - ported Primitivus (after porting dependencies like urwid satext) - more manual fixes
author Goffi <goffi@goffi.org>
date Tue, 13 Aug 2019 19:08:41 +0200
parents 28c969432557
children 9d0df638c8b4
comparison
equal deleted inserted replaced
3027:ff5bcb12ae60 3028:ab2696e34d29
1 #!/usr/bin/env python2 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*- 2 # -*- coding: utf-8 -*-
3 3
4 # SAT plugin for Explicit Message Encryption 4 # SAT plugin for Explicit Message Encryption
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
6 6
19 19
20 from sat.core.i18n import _ 20 from sat.core.i18n import _
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from sat.core.log import getLogger 22 from sat.core.log import getLogger
23 from twisted.words.protocols.jabber import xmlstream 23 from twisted.words.protocols.jabber import xmlstream
24 from zope.interface import implements 24 from zope.interface import implementer
25 from wokkel import disco 25 from wokkel import disco
26 26
27 log = getLogger(__name__) 27 log = getLogger(__name__)
28 28
29 PLUGIN_INFO = { 29 PLUGIN_INFO = {
30 C.PI_NAME: u"JID Escaping", 30 C.PI_NAME: "JID Escaping",
31 C.PI_IMPORT_NAME: u"XEP-0106", 31 C.PI_IMPORT_NAME: "XEP-0106",
32 C.PI_TYPE: u"XEP", 32 C.PI_TYPE: "XEP",
33 C.PI_MODES: C.PLUG_MODE_BOTH, 33 C.PI_MODES: C.PLUG_MODE_BOTH,
34 C.PI_PROTOCOLS: [u"XEP-0106"], 34 C.PI_PROTOCOLS: ["XEP-0106"],
35 C.PI_DEPENDENCIES: [], 35 C.PI_DEPENDENCIES: [],
36 C.PI_MAIN: u"XEP_0106", 36 C.PI_MAIN: "XEP_0106",
37 C.PI_HANDLER: u"yes", 37 C.PI_HANDLER: "yes",
38 C.PI_DESCRIPTION: _(u"""(Un)escape JID to use disallowed chars in local parts"""), 38 C.PI_DESCRIPTION: _("""(Un)escape JID to use disallowed chars in local parts"""),
39 } 39 }
40 40
41 NS_JID_ESCAPING = ur"jid\20escaping" 41 NS_JID_ESCAPING = r"jid\20escaping"
42 ESCAPE_MAP = { 42 ESCAPE_MAP = {
43 ' ': r'\20', 43 ' ': r'\20',
44 '"': r'\22', 44 '"': r'\22',
45 '&': r'\26', 45 '&': r'\26',
46 "'": r'\27', 46 "'": r'\27',
54 54
55 55
56 class XEP_0106(object): 56 class XEP_0106(object):
57 57
58 def __init__(self, host): 58 def __init__(self, host):
59 self.reverse_map = {v:k for k,v in ESCAPE_MAP.iteritems()} 59 self.reverse_map = {v:k for k,v in ESCAPE_MAP.items()}
60 60
61 def getHandler(self, client): 61 def getHandler(self, client):
62 return XEP_0106_handler() 62 return XEP_0106_handler()
63 63
64 def escape(self, text): 64 def escape(self, text):
67 @param text(unicode): text to escape 67 @param text(unicode): text to escape
68 @return (unicode): escaped text 68 @return (unicode): escaped text
69 @raise ValueError: text can't be escaped 69 @raise ValueError: text can't be escaped
70 """ 70 """
71 if not text or text[0] == ' ' or text[-1] == ' ': 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") 72 raise ValueError("text must not be empty, or start or end with a whitespace")
73 escaped = [] 73 escaped = []
74 for c in text: 74 for c in text:
75 if c in ESCAPE_MAP: 75 if c in ESCAPE_MAP:
76 escaped.append(ESCAPE_MAP[c]) 76 escaped.append(ESCAPE_MAP[c])
77 else: 77 else:
78 escaped.append(c) 78 escaped.append(c)
79 return u''.join(escaped) 79 return ''.join(escaped)
80 80
81 def unescape(self, escaped): 81 def unescape(self, escaped):
82 """Unescape text 82 """Unescape text
83 83
84 @param escaped(unicode): text to unescape 84 @param escaped(unicode): text to unescape
85 @return (unicode): unescaped text 85 @return (unicode): unescaped text
86 @raise ValueError: text can't be unescaped 86 @raise ValueError: text can't be unescaped
87 """ 87 """
88 if not escaped or escaped.startswith(r'\27') or escaped.endswith(r'\27'): 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 " 89 raise ValueError("escaped value must not be empty, or start or end with a "
90 u"whitespace") 90 "whitespace")
91 unescaped = [] 91 unescaped = []
92 idx = 0 92 idx = 0
93 while idx < len(escaped): 93 while idx < len(escaped):
94 char_seq = escaped[idx:idx+3] 94 char_seq = escaped[idx:idx+3]
95 if char_seq in self.reverse_map: 95 if char_seq in self.reverse_map:
96 unescaped.append(self.reverse_map[char_seq]) 96 unescaped.append(self.reverse_map[char_seq])
97 idx += 3 97 idx += 3
98 else: 98 else:
99 unescaped.append(escaped[idx]) 99 unescaped.append(escaped[idx])
100 idx += 1 100 idx += 1
101 return u''.join(unescaped) 101 return ''.join(unescaped)
102 102
103 103
104 @implementer(disco.IDisco)
104 class XEP_0106_handler(xmlstream.XMPPHandler): 105 class XEP_0106_handler(xmlstream.XMPPHandler):
105 implements(disco.IDisco)
106 106
107 def getDiscoInfo(self, requestor, target, nodeIdentifier=""): 107 def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
108 return [disco.DiscoFeature(NS_JID_ESCAPING)] 108 return [disco.DiscoFeature(NS_JID_ESCAPING)]
109 109
110 def getDiscoItems(self, requestor, target, nodeIdentifier=""): 110 def getDiscoItems(self, requestor, target, nodeIdentifier=""):