comparison libervia/web/pages/_browser/jid.py @ 1530:b338c31d5251

browser: integrate the `jid` module
author Goffi <goffi@goffi.org>
date Thu, 08 Jun 2023 23:32:47 +0200
parents
children 7006b55001a4
comparison
equal deleted inserted replaced
1529:de7e468e2d44 1530:b338c31d5251
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 # XXX: this is the same `jid` class as in `libervia.frontends.tools`, but typing hints
20 # have been removed as they are a bit slow to load with Brython, and this class is used
21 # a lot.
22
23
24 class JID(str):
25 """This class helps manage JID (<local>@<domain>/<resource>)"""
26
27 def __new__(cls, jid_str: str) -> "JID":
28 return str.__new__(cls, cls._normalize(jid_str))
29
30 def __init__(self, jid_str: str):
31 self._node, self._domain, self._resource = self._parse()
32
33 @staticmethod
34 def _normalize(jid_str: str) -> str:
35 """Naive normalization before instantiating and parsing the JID"""
36 if not jid_str:
37 return jid_str
38 tokens = jid_str.split("/")
39 tokens[0] = tokens[0].lower() # force node and domain to lower-case
40 return "/".join(tokens)
41
42 def _parse(self):
43 """Find node, domain, and resource from JID"""
44 node_end = self.find("@")
45 if node_end < 0:
46 node_end = 0
47 domain_end = self.find("/")
48 if domain_end == 0:
49 raise ValueError("a jid can't start with '/'")
50 if domain_end == -1:
51 domain_end = len(self)
52 node = self[:node_end] or None
53 domain = self[(node_end + 1) if node_end else 0 : domain_end]
54 resource = self[domain_end + 1 :] or None
55 return node, domain, resource
56
57 @property
58 def node(self):
59 return self._node
60
61 @property
62 def local(self):
63 return self._node
64
65 @property
66 def domain(self) -> str:
67 return self._domain
68
69 @property
70 def resource(self):
71 return self._resource
72
73 @property
74 def bare(self) -> "JID":
75 if not self.node:
76 return JID(self.domain)
77 return JID(f"{self.node}@{self.domain}")
78
79 def change_resource(self, resource: str) -> "JID":
80 """Build a new JID with the same node and domain but a different resource.
81
82 @param resource: The new resource for the JID.
83 @return: A new JID instance with the updated resource.
84 """
85 return JID(f"{self.bare}/{resource}")
86
87 def is_valid(self) -> bool:
88 """
89 @return: True if the JID is XMPP compliant
90 """
91 # Simple check for domain part
92 if not self.domain or self.domain.startswith(".") or self.domain.endswith("."):
93 return False
94 if ".." in self.domain:
95 return False
96 return True
97
98
99 def new_resource(entity: JID, resource: str) -> JID:
100 """Build a new JID from the given entity and resource.
101
102 @param entity: original JID
103 @param resource: new resource
104 @return: a new JID instance
105 """
106 return entity.change_resource(resource)