comparison sat_frontends/tools/jid.py @ 2562:26edcf3a30eb

core, setup: huge cleaning: - moved directories from src and frontends/src to sat and sat_frontends, which is the recommanded naming convention - move twisted directory to root - removed all hacks from setup.py, and added missing dependencies, it is now clean - use https URL for website in setup.py - removed "Environment :: X11 Applications :: GTK", as wix is deprecated and removed - renamed sat.sh to sat and fixed its installation - added python_requires to specify Python version needed - replaced glib2reactor which use deprecated code by gtk3reactor sat can now be installed directly from virtualenv without using --system-site-packages anymore \o/
author Goffi <goffi@goffi.org>
date Mon, 02 Apr 2018 19:44:50 +0200
parents frontends/src/tools/jid.py@0046283a285d
children 56f94936df1e
comparison
equal deleted inserted replaced
2561:bd30dc3ffe5a 2562:26edcf3a30eb
1 #!/usr/bin/env python2
2 # -*- coding: utf-8 -*-
3
4 # SAT: a jabber client
5 # Copyright (C) 2009-2018 Jérôme Poisson (goffi@goffi.org)
6
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Affero General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Affero General Public License for more details.
16
17 # You should have received a copy of the GNU Affero General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20
21 # hack to use this module with pyjamas
22 try:
23 unicode('') # XXX: unicode doesn't exist in pyjamas
24
25 # normal version
26 class BaseJID(unicode):
27 def __new__(cls, jid_str):
28 self = unicode.__new__(cls, cls._normalize(jid_str))
29 return self
30
31 def __init__(self, jid_str):
32 pass
33
34 def _parse(self):
35 """Find node domain and resource"""
36 node_end = self.find('@')
37 if node_end < 0:
38 node_end = 0
39 domain_end = self.find('/')
40 if domain_end == 0:
41 raise ValueError("a jid can't start with '/'")
42 if domain_end == -1:
43 domain_end = len(self)
44 self.node = self[:node_end] or None
45 self.domain = self[(node_end + 1) if node_end else 0:domain_end]
46 self.resource = self[domain_end + 1:] or None
47
48 except (TypeError, AttributeError): # Error raised is not the same depending on pyjsbuild options
49
50 # pyjamas version
51 class BaseJID(object):
52 def __init__(self, jid_str):
53 self.__internal_str = JID._normalize(jid_str)
54
55 def __str__(self):
56 return self.__internal_str
57
58 def __getattr__(self, name):
59 return getattr(self.__internal_str, name)
60
61 def __eq__(self, other):
62 if not isinstance(other, JID):
63 return False
64 return (self.node == other.node
65 and self.domain == other.domain
66 and self.resource == other.resource)
67
68 def __hash__(self):
69 return hash('JID<{}>'.format(self.__internal_str))
70
71 def find(self, *args):
72 return self.__internal_str.find(*args)
73
74 def _parse(self):
75 """Find node domain and resource"""
76 node_end = self.__internal_str.find('@')
77 if node_end < 0:
78 node_end = 0
79 domain_end = self.__internal_str.find('/')
80 if domain_end == 0:
81 raise ValueError("a jid can't start with '/'")
82 if domain_end == -1:
83 domain_end = len(self.__internal_str)
84 self.node = self.__internal_str[:node_end] or None
85 self.domain = self.__internal_str[(node_end + 1) if node_end else 0:domain_end]
86 self.resource = self.__internal_str[domain_end + 1:] or None
87
88
89 class JID(BaseJID):
90 """This class help manage JID (Node@Domaine/Resource)"""
91
92 def __init__(self, jid_str):
93 super(JID, self).__init__(jid_str)
94 self._parse()
95
96 @staticmethod
97 def _normalize(jid_str):
98 """Naive normalization before instantiating and parsing the JID"""
99 if not jid_str:
100 return jid_str
101 tokens = jid_str.split('/')
102 tokens[0] = tokens[0].lower() # force node and domain to lower-case
103 return '/'.join(tokens)
104
105 @property
106 def bare(self):
107 if not self.node:
108 return JID(self.domain)
109 return JID(u"{}@{}".format(self.node, self.domain))
110
111 def is_valid(self):
112 """
113 @return: True if the JID is XMPP compliant
114 """
115 # TODO: implement real check, according to the RFC http://tools.ietf.org/html/rfc6122
116 return self.domain != ""
117
118
119 def newResource(entity, resource):
120 """Build a new JID from the given entity and resource.
121
122 @param entity (JID): original JID
123 @param resource (unicode): new resource
124 @return: a new JID instance
125 """
126 return JID(u"%s/%s" % (entity.bare, resource))