view src/browser/sat_browser/jid.py @ 571:f0c8a4d51890

browser_side: print a warning when trying to access a non existing contact box: This occurs when you receive updatePresence of a MUC occupant before Libervia knows that this is a MUC. Actually you shouldn't receive presence update for the occupant before the MUC widget is displayed, this means a loss of information. One way to fix it is to modify (un)availableReceived in sat.core.xmpp: check for the entity data "type" or make a disco request if it doesn't exist, and an attribute to presenceUpdate to tell the frontend if we have a MUC occupant or a roster contact.
author souliane <souliane@mailoo.org>
date Fri, 17 Oct 2014 10:42:10 +0200
parents b38629924602
children f8a355bede6a
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)

    @property
    def node(self):
        return self.__node

    @property
    def domain(self):
        return self.__domain

    @property
    def resource(self):
        return self.__resource

    @property
    def bare(self):
        return self.domain if not self.node else (self.node + "@" + self.domain)

    # XXX: Avoid property setters, Pyjamas doesn't seem to handle them well. It seems
    # that it will just naively assign the value without actually calling the method.
    # FIXME: find a way to raise an error if the (undefined!) setter is called.
    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()

    def setBare(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)