comparison libervia/frontends/tools/jid.py @ 4074:26b7ed2817da

refactoring: rename `sat_frontends` to `libervia.frontends`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 14:12:38 +0200
parents sat_frontends/tools/jid.py@199473ffe4ea
children
comparison
equal deleted inserted replaced
4073:7c5654c54fed 4074:26b7ed2817da
1 #!/usr/bin/env python3
2
3 # Libervia XMPP
4 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
5
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU Affero General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
10
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU Affero General Public License for more details.
15
16 # You should have received a copy of the GNU Affero General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19 from typing import Optional, Tuple
20
21
22 class JID(str):
23 """This class helps manage JID (<local>@<domain>/<resource>)"""
24
25 def __new__(cls, jid_str: str) -> "JID":
26 return str.__new__(cls, cls._normalize(jid_str))
27
28 def __init__(self, jid_str: str):
29 self._node, self._domain, self._resource = self._parse()
30
31 @staticmethod
32 def _normalize(jid_str: str) -> str:
33 """Naive normalization before instantiating and parsing the JID"""
34 if not jid_str:
35 return jid_str
36 tokens = jid_str.split("/")
37 tokens[0] = tokens[0].lower() # force node and domain to lower-case
38 return "/".join(tokens)
39
40 def _parse(self) -> Tuple[Optional[str], str, Optional[str]]:
41 """Find node, domain, and resource from JID"""
42 node_end = self.find("@")
43 if node_end < 0:
44 node_end = 0
45 domain_end = self.find("/")
46 if domain_end == 0:
47 raise ValueError("a jid can't start with '/'")
48 if domain_end == -1:
49 domain_end = len(self)
50 node = self[:node_end] or None
51 domain = self[(node_end + 1) if node_end else 0 : domain_end]
52 resource = self[domain_end + 1 :] or None
53 return node, domain, resource
54
55 @property
56 def node(self) -> Optional[str]:
57 return self._node
58
59 @property
60 def local(self) -> Optional[str]:
61 return self._node
62
63 @property
64 def domain(self) -> str:
65 return self._domain
66
67 @property
68 def resource(self) -> Optional[str]:
69 return self._resource
70
71 @property
72 def bare(self) -> "JID":
73 if not self.node:
74 return JID(self.domain)
75 return JID(f"{self.node}@{self.domain}")
76
77 def change_resource(self, resource: str) -> "JID":
78 """Build a new JID with the same node and domain but a different resource.
79
80 @param resource: The new resource for the JID.
81 @return: A new JID instance with the updated resource.
82 """
83 return JID(f"{self.bare}/{resource}")
84
85 def is_valid(self) -> bool:
86 """
87 @return: True if the JID is XMPP compliant
88 """
89 # Simple check for domain part
90 if not self.domain or self.domain.startswith(".") or self.domain.endswith("."):
91 return False
92 if ".." in self.domain:
93 return False
94 return True
95
96
97 def new_resource(entity: JID, resource: str) -> JID:
98 """Build a new JID from the given entity and resource.
99
100 @param entity: original JID
101 @param resource: new resource
102 @return: a new JID instance
103 """
104 return entity.change_resource(resource)