Coverage for slidge / util / jid_escaping.py: 100%
10 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-06-13 04:38 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-06-13 04:38 +0000
1from slixmpp.jid import unescape_node
4class EscapeMixin:
5 async def legacy_id_to_jid_username(self, legacy_id: str) -> str:
6 """
7 Convert a legacy ID to a valid 'user' part of a JID.
9 The default implementation uses :xep:`0106`.
11 Should be overridden for cases where the str conversion of
12 the legacy_id is not enough, e.g., if it is case-sensitive or contains
13 forbidden characters not covered by :xep:`0106`.
15 :param legacy_id: The identifier to convert.
16 :return: A valid username part (or "local part") of a JID.
17 """
18 return str(legacy_id).translate(ESCAPE_TABLE)
20 async def jid_username_to_legacy_id(self, jid_username: str) -> str:
21 """
22 Convert a JID user part to a legacy ID.
24 The default implementation uses :xep:`0106`.
26 Should be overridden in case legacy IDs are not strings, or more
27 generally for any case where the username part of a JID
28 is not enough to identify a contact on the legacy network.
30 :param jid_username: User part of a JID, ie "user" in "user@example.com"
31 :return: The string representation of an identifier on the legacy network.
32 """
33 return unescape_node(jid_username) # type:ignore[no-any-return]
36JID_ESCAPE_SEQUENCES = {
37 "\\20",
38 "\\22",
39 "\\26",
40 "\\27",
41 "\\2f",
42 "\\3a",
43 "\\3c",
44 "\\3e",
45 "\\40",
46 "\\5c",
47}
50JID_UNESCAPE_TRANSFORMATIONS = {
51 "\\20": " ",
52 "\\22": '"',
53 "\\26": "&",
54 "\\27": "'",
55 "\\2f": "/",
56 "\\3a": ":",
57 "\\3c": "<",
58 "\\3e": ">",
59 "\\40": "@",
60 "\\5c": "\\",
61}
64ESCAPE_TABLE = "".maketrans({v: k for k, v in JID_UNESCAPE_TRANSFORMATIONS.items()})
66__all__ = "ESCAPE_TABLE", "unescape_node"