comparison src/browser/sat_browser/jid.py @ 515:da690ef8019e

browser_side: force JID's node and domain to lower-case
author souliane <souliane@mailoo.org>
date Tue, 26 Aug 2014 21:25:01 +0200
parents 97c72fe4a5f2
children 138336986bd0
comparison
equal deleted inserted replaced
514:530c88c1deee 515:da690ef8019e
15 # GNU Affero General Public License for more details. 15 # GNU Affero General Public License for more details.
16 16
17 # You should have received a copy of the GNU Affero General Public License 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/>. 18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
19 19
20 # FIXME: Libervia should use sat_frontends.tools.jid but pyjamas doesn't
21 # know the unicode type and also experiences issues with __new__.
22
20 23
21 class JID(object): 24 class JID(object):
22 """This class help manage JID (Node@Domaine/Resource)""" 25 """This class help manage JID (Node@Domaine/Resource)"""
23 26
24 def __init__(self, jid): 27 def __init__(self, jid):
25 self.__raw = str(jid) 28 self.__raw = str(self.__normalize(jid))
26 self.__parse() 29 self.__parse()
27 30
31 @classmethod
32 def __normalize(cls, jid):
33 """Naive normalization before instantiating and parsing the JID"""
34 if not jid:
35 return jid
36 tokens = jid.split('/')
37 tokens[0] = tokens[0].lower() # force node and domain to lower-case
38 return '/'.join(tokens)
39
28 def __parse(self): 40 def __parse(self):
29 """find node domaine and resource""" 41 """Find node domain and resource"""
30 node_end = self.__raw.find('@') 42 node_end = self.__raw.find('@')
31 if node_end < 0: 43 if node_end < 0:
32 node_end = 0 44 node_end = 0
33 domain_end = self.__raw.find('/') 45 domain_end = self.__raw.find('/')
34 if domain_end < 1: 46 if domain_end < 1:
43 55
44 def __str__(self): 56 def __str__(self):
45 return self.__raw.__str__() 57 return self.__raw.__str__()
46 58
47 def is_valid(self): 59 def is_valid(self):
48 """return True if the jid is xmpp compliant""" 60 """
61 @return: True if the JID is XMPP compliant
62 """
49 #FIXME: always return True for the moment 63 #FIXME: always return True for the moment
50 return True 64 return self.domain != ''
51 65
52 def __eq__(self, other): 66 def __eq__(self, other):
53 """Redefine equality operator to implement the naturally expected test""" 67 """Redefine equality operator to implement the naturally expected test"""
54 return self.node == other.node and self.domain == other.domain and self.resource == other.resource 68 return self.node == other.node and self.domain == other.domain and self.resource == other.resource