comparison libervia/backend/plugins/plugin_xep_0106.py @ 4270:0d7bb4df2343

Reformatted code base using black.
author Goffi <goffi@goffi.org>
date Wed, 19 Jun 2024 18:44:57 +0200
parents 4b842c1fb686
children
comparison
equal deleted inserted replaced
4269:64a85ce8be70 4270:0d7bb4df2343
38 C.PI_DESCRIPTION: _("""(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 = r"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",
47 '/': r'\2f', 47 "/": r"\2f",
48 ':': r'\3a', 48 ":": r"\3a",
49 '<': r'\3c', 49 "<": r"\3c",
50 '>': r'\3e', 50 ">": r"\3e",
51 '@': r'\40', 51 "@": r"\40",
52 '\\': r'\5c', 52 "\\": r"\5c",
53 } 53 }
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.items()} 59 self.reverse_map = {v: k for k, v in ESCAPE_MAP.items()}
60 60
61 def get_handler(self, client): 61 def get_handler(self, client):
62 return XEP_0106_handler() 62 return XEP_0106_handler()
63 63
64 def escape(self, text): 64 def escape(self, text):
66 66
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("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 ''.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("escaped value must not be empty, or start or end with a " 89 raise ValueError(
90 f"whitespace: rejected value is {escaped!r}") 90 "escaped value must not be empty, or start or end with a "
91 f"whitespace: rejected value is {escaped!r}"
92 )
91 unescaped = [] 93 unescaped = []
92 idx = 0 94 idx = 0
93 while idx < len(escaped): 95 while idx < len(escaped):
94 char_seq = escaped[idx:idx+3] 96 char_seq = escaped[idx : idx + 3]
95 if char_seq in self.reverse_map: 97 if char_seq in self.reverse_map:
96 unescaped.append(self.reverse_map[char_seq]) 98 unescaped.append(self.reverse_map[char_seq])
97 idx += 3 99 idx += 3
98 else: 100 else:
99 unescaped.append(escaped[idx]) 101 unescaped.append(escaped[idx])
100 idx += 1 102 idx += 1
101 return ''.join(unescaped) 103 return "".join(unescaped)
102 104
103 105
104 @implementer(disco.IDisco) 106 @implementer(disco.IDisco)
105 class XEP_0106_handler(xmlstream.XMPPHandler): 107 class XEP_0106_handler(xmlstream.XMPPHandler):
106 108