annotate libervia/frontends/tools/jid.py @ 4242:8acf46ed7f36

frontends: remote control implementation: This is the frontends common part of remote control implementation. It handle the creation of WebRTC session, and management of inputs. For now the reception use freedesktop.org Desktop portal, and works mostly with Wayland based Desktop Environments. rel 436
author Goffi <goffi@goffi.org>
date Sat, 11 May 2024 13:52:43 +0200
parents 26b7ed2817da
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3137
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
1 #!/usr/bin/env python3
559a625a236b fixed shebangs
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
2
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
3 # Libervia XMPP
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
4 # Copyright (C) 2009-2023 Jérôme Poisson (goffi@goffi.org)
0
goffi@necton2
parents:
diff changeset
5
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
6 # This program is free software: you can redistribute it and/or modify
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
9 # (at your option) any later version.
0
goffi@necton2
parents:
diff changeset
10
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
11 # This program is distributed in the hope that it will be useful,
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
14 # GNU Affero General Public License for more details.
0
goffi@necton2
parents:
diff changeset
15
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0
goffi@necton2
parents:
diff changeset
18
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
19 from typing import Optional, Tuple
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
20
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
21
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
22 class JID(str):
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
23 """This class helps manage JID (<local>@<domain>/<resource>)"""
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
24
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
25 def __new__(cls, jid_str: str) -> "JID":
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
26 return str.__new__(cls, cls._normalize(jid_str))
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
27
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
28 def __init__(self, jid_str: str):
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
29 self._node, self._domain, self._resource = self._parse()
0
goffi@necton2
parents:
diff changeset
30
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
31 @staticmethod
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
32 def _normalize(jid_str: str) -> str:
1140
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
33 """Naive normalization before instantiating and parsing the JID"""
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
34 if not jid_str:
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
35 return jid_str
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
36 tokens = jid_str.split("/")
1140
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
37 tokens[0] = tokens[0].lower() # force node and domain to lower-case
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
38 return "/".join(tokens)
1140
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
39
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
40 def _parse(self) -> Tuple[Optional[str], str, Optional[str]]:
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
41 """Find node, domain, and resource from JID"""
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
42 node_end = self.find("@")
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
43 if node_end < 0:
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
44 node_end = 0
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
45 domain_end = self.find("/")
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
46 if domain_end == 0:
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
47 raise ValueError("a jid can't start with '/'")
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
48 if domain_end == -1:
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
49 domain_end = len(self)
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
50 node = self[:node_end] or None
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
51 domain = self[(node_end + 1) if node_end else 0 : domain_end]
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
52 resource = self[domain_end + 1 :] or None
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
53 return node, domain, resource
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
54
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1140
diff changeset
55 @property
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
56 def node(self) -> Optional[str]:
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
57 return self._node
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
58
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
59 @property
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
60 def local(self) -> Optional[str]:
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
61 return self._node
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
62
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
63 @property
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
64 def domain(self) -> str:
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
65 return self._domain
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
66
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
67 @property
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
68 def resource(self) -> Optional[str]:
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
69 return self._resource
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
70
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
71 @property
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
72 def bare(self) -> "JID":
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1140
diff changeset
73 if not self.node:
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1140
diff changeset
74 return JID(self.domain)
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
75 return JID(f"{self.node}@{self.domain}")
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
76
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
77 def change_resource(self, resource: str) -> "JID":
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
78 """Build a new JID with the same node and domain but a different resource.
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1140
diff changeset
79
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
80 @param resource: The new resource for the JID.
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
81 @return: A new JID instance with the updated resource.
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
82 """
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
83 return JID(f"{self.bare}/{resource}")
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
84
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
85 def is_valid(self) -> bool:
1140
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
86 """
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
87 @return: True if the JID is XMPP compliant
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
88 """
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
89 # Simple check for domain part
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
90 if not self.domain or self.domain.startswith(".") or self.domain.endswith("."):
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
91 return False
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
92 if ".." in self.domain:
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
93 return False
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
94 return True
1378
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
95
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
96
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
97 def new_resource(entity: JID, resource: str) -> JID:
1378
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
98 """Build a new JID from the given entity and resource.
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
99
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
100 @param entity: original JID
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
101 @param resource: new resource
1378
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
102 @return: a new JID instance
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
103 """
4050
199473ffe4ea frontends (tools/jid): remove old pyjamas code + type hints
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
104 return entity.change_resource(resource)