annotate frontends/src/tools/jid.py @ 2181:968b0d13bcc7

plugin account, tools: some cleaning account + email and password tools: - plugin misc account has been reordered and cleaned a bit. - new xmpp_domain setting is used to get the domain name of main XMPP server - password generation method has been moved to tools.utils - email sending method has been moved to tools.email
author Goffi <goffi@goffi.org>
date Sun, 12 Mar 2017 19:32:59 +0100
parents 2daf7b4c6756
children 8b37a62336c3
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
1766
d17772b0fe22 copyright update
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
5 # Copyright (C) 2009-2016 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:
1378
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
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"""
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
36 node_end = self.find('@')
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
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
39 domain_end = self.find('/')
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
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
45 self.domain = self[(node_end + 1) if node_end else 0:domain_end]
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
46 self.resource = self[domain_end + 1:] or None
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
47
1378
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
48 except (TypeError, AttributeError): # Error raised is not the same depending on pyjsbuild options
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
49
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
50 # pyjamas version
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
51 class BaseJID(object):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
52 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
53 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
54
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
55 def __str__(self):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
56 return self.__internal_str
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
57
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
58 def __getattr__(self, name):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
59 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
60
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
61 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
62 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
63 return False
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
64 return (self.node == other.node
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
65 and self.domain == other.domain
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
66 and self.resource == other.resource)
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
67
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
68 def __hash__(self):
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
69 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
70
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
71 def find(self, *args):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
72 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
73
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
74 def _parse(self):
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
75 """Find node domain and resource"""
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
76 node_end = self.__internal_str.find('@')
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
77 if node_end < 0:
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
78 node_end = 0
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
79 domain_end = self.__internal_str.find('/')
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
80 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
81 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
82 if domain_end == -1:
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
83 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
84 self.node = self.__internal_str[:node_end] or None
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
85 self.domain = self.__internal_str[(node_end + 1) if node_end else 0:domain_end]
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
86 self.resource = self.__internal_str[domain_end + 1:] or None
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
87
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
88
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
89 class JID(BaseJID):
0
goffi@necton2
parents:
diff changeset
90 """This class help manage JID (Node@Domaine/Resource)"""
goffi@necton2
parents:
diff changeset
91
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
92 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
93 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
94 self._parse()
0
goffi@necton2
parents:
diff changeset
95
1290
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
96 @staticmethod
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
97 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
98 """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
99 if not jid_str:
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
100 return jid_str
faa1129559b8 core, frontends: refactoring to base Libervia on QuickFrontend (big mixed commit):
Goffi <goffi@goffi.org>
parents: 1265
diff changeset
101 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
102 tokens[0] = tokens[0].lower() # force node and domain to lower-case
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
103 return '/'.join(tokens)
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
104
1265
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1140
diff changeset
105 @property
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1140
diff changeset
106 def bare(self):
e3a9ea76de35 quick_frontend, primitivus: multi-profiles refactoring part 1 (big commit, sorry :p):
Goffi <goffi@goffi.org>
parents: 1140
diff changeset
107 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
108 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
109 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
110
0
goffi@necton2
parents:
diff changeset
111 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
112 """
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
113 @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
114 """
7f32371568e4 sat_frontends (tools): force JID's node and domain to lower-case:
souliane <souliane@mailoo.org>
parents: 1139
diff changeset
115 # 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
116 return self.domain != ""
1378
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
117
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
118
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
119 def newResource(entity, resource):
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
120 """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
121
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
122 @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
123 @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
124 @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
125 """
3dae6964c071 quick_frontends, primitivus: move the chat states logic to quick_frontend
souliane <souliane@mailoo.org>
parents: 1311
diff changeset
126 return JID(u"%s/%s" % (entity.bare, resource))