Mercurial > libervia-web
view src/browser/sat_browser/jid.py @ 538:3317e5d0ac1d
browser_side (plugin OTR): put all the text messages into constants
author | souliane <souliane@mailoo.org> |
---|---|
date | Sun, 07 Sep 2014 22:33:28 +0200 |
parents | 138336986bd0 |
children | b38629924602 |
line wrap: on
line source
#!/usr/bin/python # -*- coding: utf-8 -*- # Libervia: a Salut à Toi frontend # Copyright (C) 2011, 2012, 2013, 2014 Jérôme Poisson <goffi@goffi.org> # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. # FIXME: Libervia should use sat_frontends.tools.jid but pyjamas doesn't # know the unicode type and also experiences issues with __new__. class JID(object): """This class help manage JID (Node@Domaine/Resource)""" def __init__(self, jid): try: assert(isinstance(jid, str)) except AssertionError: raise AssertionError('Expected a string but got a %(class)s: %(inst)s' % {'class': jid.__class__, 'inst': jid}) self.__raw = str(JID.__normalize(jid)) self.__parse() @classmethod def __normalize(cls, jid): """Naive normalization before instantiating and parsing the JID""" if not jid: return jid tokens = jid.split('/') tokens[0] = tokens[0].lower() # force node and domain to lower-case return '/'.join(tokens) # XXX: I defined node, domain and resource as properties here, and renamed # the class attributes with the '__' prefix, but it appeared that the setter # methods were not called! Maybe pyjamas doesn't handle them well. def setNode(self, node): self.node = node self.__build() def setDomain(self, domain): self.domain = domain self.__build() def setResource(self, resource): self.resource = resource self.__build() @property def bare(self): return self.domain if not self.node else (self.node + "@" + self.domain) @bare.setter def bare(self, bare): self.__parseBare(bare) self.build() def __build(self): """Build the JID string from the node, domain and resource""" self.__raw = self.bare if not self.resource else (self.bare + '/' + self.resource) def __parse(self): """Parse the JID string to extract the node, domain and resource""" tokens = self.__raw.split('/') bare, self.resource = (tokens[0], tokens[1]) if len(tokens) > 1 else (self.__raw, '') self.__parseBare(bare) def __parseBare(self, bare): """Parse the given JID bare to extract the node and domain @param bare (str): JID bare to parse """ tokens = bare.split('@') self.node, self.domain = (tokens[0], tokens[1]) if len(tokens) > 1 else ('', bare) def __str__(self): try: return self.__raw except AttributeError: raise AttributeError("Trying to output a JID which has not been parsed yet") def is_valid(self): """ @return: True if the JID is XMPP compliant """ return self.domain != '' def __eq__(self, other): """Redefine equality operator to implement the naturally expected behavior""" return self.__raw == other.__raw def __hash__(self): """Redefine hash to implement the naturally expected behavior""" return hash(self.__raw) def full(self): return str(self)