annotate sat_frontends/tools/jid.py @ 2796:fdc53c8a5439

plugin XEP-0198: don't use hooks on components
author Goffi <goffi@goffi.org>
date Sat, 02 Feb 2019 01:05:25 +0100
parents 003b8b4b56a7
children ab2696e34d29
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1766
diff changeset
1 #!/usr/bin/env python2
0
goffi@necton2
parents:
diff changeset
2 # -*- coding: utf-8 -*-
goffi@necton2
parents:
diff changeset
3
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
4 # SAT: a jabber client
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
0
goffi@necton2
parents:
diff changeset
6
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
7 # 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
8 # 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
9 # 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
10 # (at your option) any later version.
0
goffi@necton2
parents:
diff changeset
11
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
12 # 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
13 # 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
14 # 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
15 # GNU Affero General Public License for more details.
0
goffi@necton2
parents:
diff changeset
16
609
84a6e83157c2 fixed licences in docstrings (they are now in comments)
Goffi <goffi@goffi.org>
parents: 590
diff changeset
17 # 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
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0
goffi@necton2
parents:
diff changeset
19
goffi@necton2
parents:
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 # hack to use this module with pyjamas
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
22 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
23 unicode("") # XXX: unicode doesn't exist in pyjamas
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
24
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
25 # normal version
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
26 class BaseJID(unicode):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
27 def __new__(cls, jid_str):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
28 self = unicode.__new__(cls, cls._normalize(jid_str))
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
29 return self
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
30
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
31 def __init__(self, jid_str):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
32 pass
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
33
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
34 def _parse(self):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
35 """Find node domain and resource"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
36 node_end = self.find("@")
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
37 if node_end < 0:
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
38 node_end = 0
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
39 domain_end = self.find("/")
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
40 if domain_end == 0:
1378
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
41 raise ValueError("a jid can't start with '/'")
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
42 if domain_end == -1:
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
43 domain_end = len(self)
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
44 self.node = self[:node_end] or None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
45 self.domain = self[(node_end + 1) if node_end else 0 : domain_end]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
46 self.resource = self[domain_end + 1 :] or None
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
47
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
48
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
49 except (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
50 TypeError,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
51 AttributeError,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
52 ): # Error raised is not the same depending on pyjsbuild options
1378
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
53
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
54 # pyjamas version
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
55 class BaseJID(object):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
56 def __init__(self, jid_str):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
57 self.__internal_str = JID._normalize(jid_str)
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
58
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
59 def __str__(self):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
60 return self.__internal_str
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
61
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
62 def __getattr__(self, name):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
63 return getattr(self.__internal_str, name)
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
64
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
65 def __eq__(self, other):
1311
4895e1e092fb frontends (tools): Pyjamas's JID implementation can be compared to an object of a different type:
souliane <souliane@mailoo.org>
parents: 1290
diff changeset
66 if not isinstance(other, JID):
4895e1e092fb frontends (tools): Pyjamas's JID implementation can be compared to an object of a different type:
souliane <souliane@mailoo.org>
parents: 1290
diff changeset
67 return False
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
68 return (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
69 self.node == other.node
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
70 and self.domain == other.domain
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
71 and self.resource == other.resource
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
72 )
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
73
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
74 def __hash__(self):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
75 return hash("JID<{}>".format(self.__internal_str))
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
76
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
77 def find(self, *args):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
78 return self.__internal_str.find(*args)
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
79
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
80 def _parse(self):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
81 """Find node domain and resource"""
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
82 node_end = self.__internal_str.find("@")
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
83 if node_end < 0:
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
84 node_end = 0
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
85 domain_end = self.__internal_str.find("/")
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
86 if domain_end == 0:
1311
4895e1e092fb frontends (tools): Pyjamas's JID implementation can be compared to an object of a different type:
souliane <souliane@mailoo.org>
parents: 1290
diff changeset
87 raise ValueError("a jid can't start with '/'")
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
88 if domain_end == -1:
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
89 domain_end = len(self.__internal_str)
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
90 self.node = self.__internal_str[:node_end] or None
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
91 self.domain = self.__internal_str[
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
92 (node_end + 1) if node_end else 0 : domain_end
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
93 ]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
94 self.resource = self.__internal_str[domain_end + 1 :] or None
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
95
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
96
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
97 class JID(BaseJID):
0
goffi@necton2
parents:
diff changeset
98 """This class help manage JID (Node@Domaine/Resource)"""
goffi@necton2
parents:
diff changeset
99
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
100 def __init__(self, jid_str):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
101 super(JID, self).__init__(jid_str)
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1140
diff changeset
102 self._parse()
0
goffi@necton2
parents:
diff changeset
103
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
104 @staticmethod
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
105 def _normalize(jid_str):
1140
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
106 """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
107 if not jid_str:
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
108 return jid_str
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
109 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
110 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
111 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
112
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1140
diff changeset
113 @property
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1140
diff changeset
114 def bare(self):
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1140
diff changeset
115 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
116 return JID(self.domain)
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1140
diff changeset
117 return JID(u"{}@{}".format(self.node, self.domain))
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1140
diff changeset
118
0
goffi@necton2
parents:
diff changeset
119 def is_valid(self):
1140
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
120 """
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
121 @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
122 """
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
123 # TODO: implement real check, according to the RFC http://tools.ietf.org/html/rfc6122
688
f7878ad3c846 tools: renamed tools.jid.JID attribute "short" to "bare"
souliane <souliane@mailoo.org>
parents: 609
diff changeset
124 return self.domain != ""
1378
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
125
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
126
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
127 def newResource(entity, resource):
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
128 """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
129
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
130 @param entity (JID): original JID
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
131 @param resource (unicode): new resource
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
132 @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
133 """
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
134 return JID(u"%s/%s" % (entity.bare, resource))