view sat/core/ @ 2994:94708a7d3ecf

core, plugin XEP-0045: fixed message type autodetection + ENTITY_TYPE_MUC constant: an old hardcoded value was used in several places to detect if an entity is a MUC, but this value was not valid anymore. This has been fixed, and ENTITY_TYPE_MUC constant is now used instead. This fixes message type autodetection for "groupchat" messages. fixes 300
author Goffi <>
date Tue, 09 Jul 2019 09:06:45 +0200
parents 1d92ad95185f
children ab2696e34d29
line wrap: on
line source

#!/usr/bin/env python2
# -*- coding: utf-8 -*-

# SAT: a jabber client
# Copyright (C) 2009-2019 Jérôme Poisson (

# 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
# 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 <>.

import sys
import time
import calendar
import uuid
from functools import partial
from sat.core.i18n import _
from sat.core.constants import Const as C
from sat.memory import cache
from twisted.internet import defer, error as internet_error
from twisted.words.protocols.jabber.xmlstream import XMPPHandler
from twisted.words.protocols.jabber import xmlstream
from twisted.words.protocols.jabber import error
from twisted.words.protocols.jabber import jid
from twisted.words.xish import domish
from twisted.python import failure
from wokkel import client as wokkel_client, disco, xmppim, generic, iwokkel
from wokkel import component
from wokkel import delay
from sat.core.log import getLogger
from sat.core import exceptions
from sat.memory import encryption
from sat.memory import persistent
from import xml_tools
from zope.interface import implements

log = getLogger(__name__)

NS_X_DATA = u"jabber:x:data"
NS_XML_ELEMENT = u"urn:xmpp:xml-element"
NS_ROSTER_VER = u"urn:xmpp:features:rosterver"
# we use 2 "@" which is illegal in a jid, to be sure we are not mixing keys
# with roster jids
ROSTER_VER_KEY = u"@version@"

class SatXMPPEntity(object):
    """Common code for Client and Component"""

    def __init__(self, host_app, profile, max_retries):
        factory = self.factory

        # we monkey patch clientConnectionLost to handle networkEnabled/networkDisabled
        # and to allow plugins to tune reconnection mechanism
        clientConnectionFailed_ori = factory.clientConnectionFailed
        clientConnectionLost_ori = factory.clientConnectionLost
        factory.clientConnectionFailed = partial(
            self.connectionTerminated, term_type=u"failed", cb=clientConnectionFailed_ori)
        factory.clientConnectionLost = partial(
            self.connectionTerminated, term_type=u"lost", cb=clientConnectionLost_ori)

        factory.maxRetries = max_retries
        factory.maxDelay = 30
        # when self._connected_d is None, we are not connected
        # else, it's a deferred which fire on disconnection
        self._connected_d = None
        self.profile = profile
        self.host_app = host_app
        self.cache = cache.Cache(host_app, profile)
        self.mess_id2uid = {}  # map from message id to uid used in history.
                               # Key: (full_jid, message_id) Value: uid
        # this Deferred fire when entity is connected
        self.conn_deferred = defer.Deferred()
        self._progress_cb = {}  # callback called when a progress is requested
                                # (key = progress id)
        self.actions = {}  # used to keep track of actions for retrieval (key = action_id)
        self.encryption = encryption.EncryptionHandler(self)

    def __unicode__(self):
        return u"Client instance for profile {profile}".format(profile=self.profile)

    def __str__(self):
        return self.__unicode__.encode('utf-8')

    ## initialisation ##

    def _callConnectionTriggers(self):
        """Call conneting trigger prepare connected trigger

        @param plugins(iterable): plugins to use
        @return (list[object, callable]): plugin to trigger tuples with:
            - plugin instance
            - profileConnected* triggers (to call after connection)
        plugin_conn_cb = []
        for plugin in self._getPluginsList():
            # we check if plugin handle client mode
            if plugin.is_handler:

            # profileConnecting/profileConnected methods handling

            # profile connecting is called right now (before actually starting client)
            connecting_cb = getattr(plugin, "profileConnecting", None)
            if connecting_cb is not None:
                yield connecting_cb(self)

            # profile connected is called after client is ready and roster is got
            connected_cb = getattr(plugin, "profileConnected", None)
            if connected_cb is not None:
                plugin_conn_cb.append((plugin, connected_cb))


    def _getPluginsList(self):
        """Return list of plugin to use

        need to be implemented by subclasses
        this list is used to call profileConnect* triggers
        @return(iterable[object]): plugins to use
        raise NotImplementedError

    def _createSubProtocols(self):

    def entityConnected(self):
        """Called once connection is done

        may return a Deferred, to perform initialisation tasks

    def startConnection(cls, host, profile, max_retries):
        """instantiate the entity and start the connection"""
        # FIXME: reconnection doesn't seems to be handled correclty
        #        (client is deleted then recreated from scratch)
        #        most of methods called here should be called once on first connection
        #        (e.g. adding subprotocols)
        #        but client should not be deleted except if session is finished
        #        (independently of connection/deconnection)
            port = int(
                    C.FORCE_PORT_PARAM, "Connection", profile_key=profile
        except ValueError:
            log.debug(_("Can't parse port value, using default value"))
            port = (
            )  # will use default value 5222 or be retrieved from a DNS SRV record

        password = yield host.memory.asyncGetParamA(
            "Password", "Connection", profile_key=profile
        entity = host.profiles[profile] = cls(
            host, profile, jid.JID(host.memory.getParamA("JabberID", "Connection",
            profile_key=profile)), password,
            host.memory.getParamA(C.FORCE_SERVER_PARAM, "Connection",
                                  profile_key=profile) or None,
            port, max_retries,


        entity.fallBack = SatFallbackHandler(host)

        entity.versionHandler = SatVersionHandler(C.APP_NAME_FULL, host.full_version)

        entity.identityHandler = SatIdentityHandler()

        log.debug(_("setting plugins parents"))

        plugin_conn_cb = yield entity._callConnectionTriggers()


        yield entity.conn_deferred

        yield defer.maybeDeferred(entity.entityConnected)

        # Call profileConnected callback for all plugins,
        # and print error message if any of them fails
        conn_cb_list = []
        for __, callback in plugin_conn_cb:
            conn_cb_list.append(defer.maybeDeferred(callback, entity))
        list_d = defer.DeferredList(conn_cb_list)

        def logPluginResults(results):
            all_succeed = all([success for success, result in results])
            if not all_succeed:
                log.error(_(u"Plugins initialisation error"))
                for idx, (success, result) in enumerate(results):
                    if not success:
                            u"error (plugin %(name)s): %(failure)s"
                            % {
                                "name": plugin_conn_cb[idx][0]._info["import_name"],
                                "failure": result,

        yield list_d.addCallback(
        )  # FIXME: we should have a timeout here, and a way to know if a plugin freeze
        # TODO: mesure launch time of each plugin

    def _disconnectionCb(self, __):
        self._connected_d = None

    def _disconnectionEb(self, failure_):
        log.error(_(u"Error while disconnecting: {}".format(failure_)))

    def _authd(self, xmlstream):
        super(SatXMPPEntity, self)._authd(xmlstream)
        log.debug(_(u"{profile} identified").format(profile=self.profile))

    def _finish_connection(self, __):

    def streamInitialized(self):
        """Called after _authd"""
        log.debug(_(u"XML stream is initialized"))
        if not self.host_app.trigger.point("xml_init", self):

    def postStreamInit(self):
        """Workflow after stream initalisation."""
            _(u"********** [{profile}] CONNECTED **********").format(profile=self.profile)

        # the following Deferred is used to know when we are connected
        # so we need to be set it to None when connection is lost
        self._connected_d = defer.Deferred()

        # we send the signal to the clients
        self.host_app.bridge.connected(self.jid.full(), self.profile)

        self.disco = SatDiscoProtocol(self)
        self.discoHandler = disco.DiscoHandler()
        disco_d = defer.succeed(None)

        if not self.host_app.trigger.point("Disco handled", disco_d, self.profile):


    def initializationFailed(self, reason):
                u"ERROR: XMPP connection failed for profile '%(profile)s': %(reason)s"
                % {"profile": self.profile, "reason": reason}
            super(SatXMPPEntity, self).initializationFailed(reason)
            # we already chained an errback, no need to raise an exception

    ## connection ##

    def connectionTerminated(self, connector, reason, term_type, cb):
        """Display disconnection reason, and call factory method

        This method is monkey patched to factory, allowing plugins to handle finely
        reconnection with the triggers.
        @param connector(twisted.internet.base.BaseConnector): current connector
        @param reason(failure.Failure): why connection has been terminated
        @param term_type(unicode): on of 'failed' or 'lost'
        @param cb(callable): original factory method

        @trigger connection_failed(connector, reason): connection can't be established
        @trigger connection_lost(connector, reason): connection was available but it not
        # we save connector because it may be deleted when connection will be dropped
        # if reconnection is disabled
        self._saved_connector = connector
        if reason is not None and not isinstance(reason.value,
                reason_str = unicode(reason.value)
            except Exception:
                # FIXME: workaround for Android were p4a strips docstrings
                #        while Twisted use docstring in __str__
                # TODO: create a ticket upstream, Twisted should work when optimization
                #       is used
                reason_str = unicode(reason.value.__class__)
            log.warning(u"Connection {term_type}: {reason}".format(
                term_type = term_type,
        if not self.host_app.trigger.point(u"connection_" + term_type, connector, reason):
        return cb(connector, reason)

    def networkDisabled(self):
        """Indicate that network has been completely disabled

        In other words, internet is not available anymore and transport must be stopped.
        Retrying is disabled too, as it makes no sense to try without network, and it may
        use resources (notably battery on mobiles).
        """"stopping connection because of network disabled"))
        self.factory.continueTrying = 0
        self._network_disabled = True
        if self.xmlstream is not None:

    def networkEnabled(self):
        """Indicate that network has been (re)enabled

        This happens when e.g. user activate WIFI connection.
            connector = self._saved_connector
            network_disabled = self._network_disabled
        except AttributeError:
            # connection has not been stopped by networkDisabled
            # we don't have to restart it
            log.debug(u"no connection to restart")
            del self._network_disabled
            if not network_disabled:
                raise exceptions.InternalError(u"network_disabled should be True")"network is available, trying to connect"))
        # we want to be sure to start fresh
        # we have a saved connector, meaning the connection has been stopped previously
        # we can now try to reconnect

    def _connected(self, xs):
        send_hooks = []
        receive_hooks = []
            "stream_hooks", self, receive_hooks, send_hooks)
        for hook in receive_hooks:
            xs.addHook(C.STREAM_HOOK_RECEIVE, hook)
        for hook in send_hooks:
            xs.addHook(C.STREAM_HOOK_SEND, hook)
        super(SatXMPPEntity, self)._connected(xs)

    def disconnectProfile(self, reason):
        if self._connected_d is not None:
            )  # we send the signal to the clients
            )  # and we remove references to this client
                _(u"********** [{profile}] DISCONNECTED **********").format(
        if not self.conn_deferred.called:
            if reason is None:
                err = error.StreamError(u"Server unexpectedly closed the connection")
                err = reason
                    if err.value.args[0][0][2] == "certificate verify failed":
                        err = exceptions.InvalidCertificate(
                            _(u"Your server certificate is not valid "
                              u"(its identity can't be checked).\n\n"
                              u"This should never happen and may indicate that "
                              u"somebody is trying to spy on you.\n"
                              u"Please contact your server administrator."))
                            # with invalid certificate, we should not retry to connect
                            # so we delete saved connector to avoid reconnection if
                            # networkEnabled is called.
                            del self._saved_connector
                        except AttributeError:
                except (IndexError, TypeError):

    def _disconnected(self, reason):
        super(SatXMPPEntity, self)._disconnected(reason)
        if not self.host_app.trigger.point("disconnected", self, reason):

    def _cleanConnection(self, __):
        """method called on disconnection

        used to call profileDisconnected* triggers
        trigger_name = "profileDisconnected"
        for plugin in self._getPluginsList():
            disconnected_cb = getattr(plugin, trigger_name, None)
            if disconnected_cb is not None:
                yield disconnected_cb(self)

    def isConnected(self):
            return bool(self.xmlstream.transport.connected)
        except AttributeError:
            return False

    def entityDisconnect(self):
        if not self.host_app.trigger.point("disconnecting", self):
        if self._connected_d is not None:
            return self._connected_d
            return defer.succeed(None)

    ## sending ##

    def IQ(self, type_=u"set", timeout=60):
        """shortcut to create an IQ element managing deferred

        @param type_(unicode): IQ type ('set' or 'get')
        @param timeout(None, int): timeout in seconds
        @return((D)domish.Element: result stanza
            errback is called if and error stanza is returned
        iq_elt = xmlstream.IQ(self.xmlstream, type_)
        iq_elt.timeout = timeout
        return iq_elt

    def sendError(self, iq_elt, condition):
        """Send error stanza build from iq_elt

        @param iq_elt(domish.Element): initial IQ element
        @param condition(unicode): error condition
        iq_error_elt = error.StanzaError(condition).toResponse(iq_elt)

    def generateMessageXML(self, data):
        """Generate <message/> stanza from message data

        @param data(dict): message data
            domish element will be put in data['xml']
            following keys are needed:
                - from
                - to
                - uid: can be set to '' if uid attribute is not wanted
                - message
                - type
                - subject
                - extra
        @return (dict) message data
        data["xml"] = message_elt = domish.Element((None, "message"))
        message_elt["to"] = data["to"].full()
        message_elt["from"] = data["from"].full()
        message_elt["type"] = data["type"]
        if data["uid"]:  # key must be present but can be set to ''
            # by a plugin to avoid id on purpose
            message_elt["id"] = data["uid"]
        for lang, subject in data["subject"].iteritems():
            subject_elt = message_elt.addElement("subject", content=subject)
            if lang:
                subject_elt[(C.NS_XML, "lang")] = lang
        for lang, message in data["message"].iteritems():
            body_elt = message_elt.addElement("body", content=message)
            if lang:
                body_elt[(C.NS_XML, "lang")] = lang
            thread = data["extra"]["thread"]
        except KeyError:
            if "thread_parent" in data["extra"]:
                raise exceptions.InternalError(
                    u"thread_parent found while there is not associated thread"
            thread_elt = message_elt.addElement("thread", content=thread)
                thread_elt["parent"] = data["extra"]["thread_parent"]
            except KeyError:
        return data

    def addPostXmlCallbacks(self, post_xml_treatments):
        """Used to add class level callbacks at the end of the workflow

        @param post_xml_treatments(D): the same Deferred as in sendMessage trigger
        raise NotImplementedError

    def sendMessage(self, to_jid, message, subject=None, mess_type="auto", extra=None,
                    uid=None, no_trigger=False,):
        r"""Send a message to an entity

        @param to_jid(jid.JID): destinee of the message
        @param message(dict): message body, key is the language (use '' when unknown)
        @param subject(dict): message subject, key is the language (use '' when unknown)
        @param mess_type(str): one of standard message type (cf RFC 6121 §5.2.2) or:
            - auto: for automatic type detection
            - info: for information ("info_type" can be specified in extra)
        @param extra(dict, None): extra data. Key can be:
            - info_type: information type, can be
        @param uid(unicode, None): unique id:
            should be unique at least in this XMPP session
            if None, an uuid will be generated
        @param no_trigger (bool): if True, sendMessage[suffix] trigger will no be used
            useful when a message need to be sent without any modification
            /!\ this will also skip encryption methods!
        if subject is None:
            subject = {}
        if extra is None:
            extra = {}

        assert mess_type in C.MESS_TYPE_ALL

        data = {  # dict is similar to the one used in client.onMessage
            "from": self.jid,
            "to": to_jid,
            "uid": uid or unicode(uuid.uuid4()),
            "message": message,
            "subject": subject,
            "type": mess_type,
            "extra": extra,
            "timestamp": time.time(),
        # XXX: plugin can add their pre XML treatments to this deferred
        pre_xml_treatments = defer.Deferred()
        # XXX: plugin can add their post XML treatments to this deferred
        post_xml_treatments = defer.Deferred()

        if data["type"] == C.MESS_TYPE_AUTO:
            # we try to guess the type
            if data["subject"]:
                data["type"] = C.MESS_TYPE_NORMAL
            elif not data["to"].resource:  # if to JID has a resource,
                                           # the type is not 'groupchat'
                # we may have a groupchat message, we check if the we know this jid
                    entity_type = self.host_app.memory.getEntityDatum(
                        data["to"], C.ENTITY_TYPE, self.profile
                    # FIXME: should entity_type manage resources ?
                except (exceptions.UnknownEntityError, KeyError):
                    entity_type = "contact"

                if entity_type == C.ENTITY_TYPE_MUC:
                    data["type"] = C.MESS_TYPE_GROUPCHAT
                    data["type"] = C.MESS_TYPE_CHAT
                data["type"] == C.MESS_TYPE_CHAT
            data["type"] == C.MESS_TYPE_CHAT if data["subject"] else C.MESS_TYPE_NORMAL

        # FIXME: send_only is used by libervia's OTR plugin to avoid
        #        the triggers from frontend, and no_trigger do the same
        #        thing internally, this could be unified
        send_only = data["extra"].get("send_only", False)

        if not no_trigger and not send_only:
            # is the session encrypted? If so we indicate it in data

            if not self.host_app.trigger.point(
                "sendMessage" + self.trigger_suffix,
                return defer.succeed(None)

        log.debug(_(u"Sending message (type {type}, to {to})")
                    .format(type=data["type"], to=to_jid.full()))

        pre_xml_treatments.addCallback(lambda __: self.generateMessageXML(data))
        if send_only:
            log.debug(_(u"Triggers, storage and echo have been inhibited by the "
                        u"'send_only' parameter"))
        return pre_xml_treatments

    def _cancelErrorTrap(self, failure):
        """A message sending can be cancelled by a plugin treatment"""

    def messageAddToHistory(self, data):
        """Store message into database (for local history)

        @param data: message data dictionnary
        @param client: profile's client
        if data[u"type"] != C.MESS_TYPE_GROUPCHAT:
            # we don't add groupchat message to history, as we get them back
            # and they will be added then
            if data[u"message"] or data[u"subject"]:  # we need a message to store
                self.host_app.memory.addToHistory(self, data)
                    u"No message found"
                )  # empty body should be managed by plugins before this point
        return data

    def messageGetBridgeArgs(self, data):
        """Generate args to use with bridge from data dict"""
        return (data[u"uid"], data[u"timestamp"], data[u"from"].full(),
                data[u"to"].full(), data[u"message"], data[u"subject"],
                data[u"type"], data[u"extra"])

    def messageSendToBridge(self, data):
        """Send message to bridge, so frontends can display it

        @param data: message data dictionnary
        @param client: profile's client
        if data[u"type"] != C.MESS_TYPE_GROUPCHAT:
            # we don't send groupchat message to bridge, as we get them back
            # and they will be added the
            if (data[u"message"] or data[u"subject"]):  # we need a message to send
                                                        # something

                # We send back the message, so all frontends are aware of it
                log.warning(_(u"No message found"))
        return data

class SatXMPPClient(SatXMPPEntity, wokkel_client.XMPPClient):
    trigger_suffix = ""
    is_component = False

    def __init__(self, host_app, profile, user_jid, password, host=None,
                 port=C.XMPP_C2S_PORT, max_retries=C.XMPP_MAX_RETRIES):
        # XXX: DNS SRV records are checked when the host is not specified.
        # If no SRV record is found, the host is directly extracted from the JID.
        self.started = time.time()

        # Currently, we use "client/pc/Salut à Toi", but as
        # SàT is multi-frontends and can be used on mobile devices, as a bot,
        # with a web frontend,
        # etc., we should implement a way to dynamically update identities through the
        # bridge
        self.identities = [disco.DiscoIdentity(u"client", u"pc", C.APP_NAME)]
        if sys.platform == "android":
            # FIXME: temporary hack as SRV is not working on android
            # TODO: remove this hack and fix SRV
  "FIXME: Android hack, ignoring SRV")
            if host is None:
                host =
            # for now we consider Android devices to be always phones
            self.identities = [disco.DiscoIdentity(u"client", u"phone", C.APP_NAME)]

        hosts_map = host_app.memory.getConfig(None, "hosts_dict", {})
        if host is None and in hosts_map:
            host_data = hosts_map[]
            if isinstance(host_data, basestring):
                host = host_data
            elif isinstance(host_data, dict):
                if u"host" in host_data:
                    host = host_data[u"host"]
                if u"port" in host_data:
                    port = host_data[u"port"]
                    _(u"invalid data used for host: {data}").format(data=host_data)
                host_data = None
            if host_data is not None:
                    u"using {host}:{port} for host {host_ori} as requested in config"
                    .format(, host=host, port=port)

        self.check_certificate = host_app.memory.getParamA(
            "check_certificate", "Connection", profile_key=profile)

            self, user_jid, password, host or None, port or C.XMPP_C2S_PORT,
            check_certificate = self.check_certificate
        SatXMPPEntity.__init__(self, host_app, profile, max_retries)

        if not self.check_certificate:
            msg = (_(u"Certificate validation is deactivated, this is unsecure and "
                u"somebody may be spying on you. If you have no good reason to disable "
                u"certificate validation, please activate \"Check certificate\" in your "
                u"settings in \"Connection\" tab."))
            xml_tools.quickNote(host_app, self, msg, _(u"Security notice"),
                level = C.XMLUI_DATA_LVL_WARNING)

    def _getPluginsList(self):
        for p in self.host_app.plugins.itervalues():
            if C.PLUG_MODE_CLIENT in p._info[u"modes"]:
                yield p

    def _createSubProtocols(self):
        self.messageProt = SatMessageProtocol(self.host_app)

        self.roster = SatRosterProtocol(self.host_app)

        self.presence = SatPresenceProtocol(self.host_app)

    def startConnection(cls, host, profile, max_retries):
        yield super(SatXMPPClient, cls).startConnection(host, profile, max_retries)
        entity = host.profiles[profile]
        # we finally send our presence

    def entityConnected(self):
        # we want to be sure that we got the roster
        return self.roster.got_roster

    def addPostXmlCallbacks(self, post_xml_treatments):

    def send(self, obj):
        # original send method accept string
        # but we restrict to domish.Element to make trigger treatments easier
        assert isinstance(obj, domish.Element)
        # XXX: this trigger is the last one before sending stanza on wire
        #      it is intended for things like end 2 end encryption.
        #      *DO NOT* cancel (i.e. return False) without very good reason
        #      (out of band transmission for instance).
        #      e2e should have a priority of 0 here, and out of band transmission
        #      a lower priority
        #  FIXME: trigger not used yet, can be uncommented when e2e full stanza
        #         encryption is implemented
        #  if not self.host_app.trigger.point("send", self, obj):
        #      return
        super(SatXMPPClient, self).send(obj)

    def sendMessageData(self, mess_data):
        """Convenient method to send message data to stream

        This method will send mess_data[u'xml'] to stream, but a trigger is there
        The trigger can't be cancelled, it's a good place for e2e encryption which
        don't handle full stanza encryption
        This trigger can return a Deferred (it's an asyncPoint)
        @param mess_data(dict): message data as constructed by onMessage workflow
        @return (dict): mess_data (so it can be used in a deferred chain)
        # XXX: This is the last trigger before u"send" (last but one globally)
        #      for sending message.
        #      This is intented for e2e encryption which doesn't do full stanza
        #      encryption (e.g. OTR)
        #      This trigger point can't cancel the method
        yield self.host_app.trigger.asyncPoint("sendMessageData", self, mess_data,

    def feedback(self, to_jid, message, extra=None):
        """Send message to frontends

        This message will be an info message, not recorded in history.
        It can be used to give feedback of a command
        @param to_jid(jid.JID): destinee jid
        @param message(unicode): message to send to frontends
        @param extra(None, dict): extra data to use
            in particular, info subtype can be specified with MESS_EXTRA_INFO
        if extra is None:
            extra = {}
            message={u"": message},

    def _finish_connection(self, __):
        d = self.roster.requestRoster()
        d.addCallback(lambda __: super(SatXMPPClient, self)._finish_connection(__))

class SatXMPPComponent(SatXMPPEntity, component.Component):
    """XMPP component

    This component are similar but not identical to clients.
    An entry point plugin is launched after component is connected.
    Component need to instantiate MessageProtocol itself

    trigger_suffix = (
    )  # used for to distinguish some trigger points set in SatXMPPEntity
    is_component = True
    sendHistory = (
    )  # XXX: set to True from entry plugin to keep messages in history for received
       #      messages

    def __init__(self, host_app, profile, component_jid, password, host=None, port=None,
        self.started = time.time()
        if port is None:
            port = C.XMPP_COMPONENT_PORT

        ## entry point ##
        entry_point = host_app.memory.getEntryPoint(profile)
            self.entry_plugin = host_app.plugins[entry_point]
        except KeyError:
            raise exceptions.NotFound(
                _(u"The requested entry point ({entry_point}) is not available").format(

        self.identities = [disco.DiscoIdentity(u"component", u"generic", C.APP_NAME)]
        # jid is set automatically on bind by Twisted for Client, but not for Component
        self.jid = component_jid
        if host is None:
                host =".", 1)[1]
            except IndexError:
                raise ValueError(u"Can't guess host from jid, please specify a host")
        # XXX: component.Component expect unicode jid, while Client expect jid.JID.
        #      this is not consistent, so we use jid.JID for SatXMPP*
        component.Component.__init__(self, host, port, component_jid.full(), password)
        SatXMPPEntity.__init__(self, host_app, profile, max_retries)

    def _buildDependencies(self, current, plugins, required=True):
        """build recursively dependencies needed for a plugin

        this method build list of plugin needed for a component and raises
        errors if they are not available or not allowed for components
        @param current(object): parent plugin to check
            use entry_point for first call
        @param plugins(list): list of validated plugins, will be filled by the method
            give an empty list for first call
        @param required(bool): True if plugin is mandatory
            for recursive calls only, should not be modified by inital caller
        @raise InternalError: one of the plugin is not handling components
        @raise KeyError: one plugin should be present in self.host_app.plugins but it
                         is not
        if C.PLUG_MODE_COMPONENT not in current._info[u"modes"]:
            if not required:
                        u"Plugin {current_name} is needed for {entry_name}, "
                        u"but it doesn't handle component mode"
                raise exceptions.InternalError(_(u"invalid plugin mode"))

        for import_name in current._info.get(C.PI_DEPENDENCIES, []):
            # plugins are already loaded as dependencies
            # so we know they are in self.host_app.plugins
            dep = self.host_app.plugins[import_name]
            self._buildDependencies(dep, plugins)

        for import_name in current._info.get(C.PI_RECOMMENDATIONS, []):
            # here plugins are only recommendations,
            # so they may not exist in self.host_app.plugins
                dep = self.host_app.plugins[import_name]
            except KeyError:
            self._buildDependencies(dep, plugins, required=False)

        if current not in plugins:
            # current can be required for several plugins and so
            # it can already be present in the list

    def _getPluginsList(self):
        # XXX: for component we don't launch all plugins triggers
        #      but only the ones from which there is a dependency
        plugins = []
        self._buildDependencies(self.entry_plugin, plugins)
        return plugins

    def entityConnected(self):
        # we can now launch entry point
            start_cb = self.entry_plugin.componentStart
        except AttributeError:
            return start_cb(self)

    def addPostXmlCallbacks(self, post_xml_treatments):
        if self.sendHistory:

class SatMessageProtocol(xmppim.MessageProtocol):

    def __init__(self, host):
        xmppim.MessageProtocol.__init__(self) = host

    def parseMessage(self, message_elt):
        """parse a message XML and return message_data

        @param message_elt(domish.Element): raw <message> xml
        @param client(SatXMPPClient, None): client to map message id to uid
            if None, mapping will not be done
        @return(dict): message data
        if != u"message":
                u"parseMessage used with a non <message/> stanza, ignoring: {xml}"
            return {}

        if message_elt.uri is None:
            # wokkel element parsing strip out root namespace
            message_elt.defaultUri = message_elt.uri = C.NS_CLIENT
            for c in message_elt.elements():
                if c.uri is None:
                    c.uri = C.NS_CLIENT
        elif message_elt.uri != C.NS_CLIENT:
                u"received <message> with a wrong namespace: {xml}"

        client = self.parent

        if not message_elt.hasAttribute(u'to'):
            message_elt['to'] = client.jid.full()

        message = {}
        subject = {}
        extra = {}
        data = {
            u"from": jid.JID(message_elt["from"]),
            u"to": jid.JID(message_elt["to"]),
            u"uid": message_elt.getAttribute(
                u"uid", unicode(uuid.uuid4())
            ),  # XXX: uid is not a standard attribute but may be added by plugins
            u"message": message,
            u"subject": subject,
            u"type": message_elt.getAttribute(u"type", u"normal"),
            u"extra": extra,

            message_id = data[u"extra"][u"message_id"] = message_elt[u"id"]
        except KeyError:
            client.mess_id2uid[(data["from"], message_id)] = data["uid"]

        # message
        for e in message_elt.elements(C.NS_CLIENT, "body"):
            message[e.getAttribute((C.NS_XML, "lang"), "")] = unicode(e)

        # subject
        for e in message_elt.elements(C.NS_CLIENT, "subject"):
            subject[e.getAttribute((C.NS_XML, "lang"), "")] = unicode(e)

        # delay and timestamp
            received_timestamp = message_elt._received_timestamp
        except AttributeError:
            # message_elt._received_timestamp should have been set in onMessage
            # but if parseMessage is called directly, it can be missing
            log.debug(u"missing received timestamp for {message_elt}".format(
            received_timestamp = time.time()

            delay_elt = message_elt.elements(delay.NS_DELAY, "delay").next()
        except StopIteration:
            data["timestamp"] = received_timestamp
            parsed_delay = delay.Delay.fromElement(delay_elt)
            data["timestamp"] = calendar.timegm(parsed_delay.stamp.utctimetuple())
            data["received_timestamp"] = received_timestamp
            if parsed_delay.sender:
                data["delay_sender"] = parsed_delay.sender.full()"message_parse", client,  message_elt, data)
        return data

    def _onMessageStartWorkflow(self, cont, client, message_elt, post_treat):
        """Parse message and do post treatments

        It is the first callback called after MessageReceived trigger
        @param cont(bool): workflow will continue only if this is True
        @param message_elt(domish.Element): message stanza
            may have be modified by triggers
        @param post_treat(defer.Deferred): post parsing treatments
        if not cont:
        data = self.parseMessage(message_elt)
        post_treat.addCallback(self.bridgeSignal, data)

    def onMessage(self, message_elt):
        # TODO: handle threads
        message_elt._received_timestamp = time.time()
        client = self.parent
        if not "from" in message_elt.attributes:
            message_elt["from"] =
        log.debug(_(u"got message from: {from_}").format(from_=message_elt["from"]))

        # plugin can add their treatments to this deferred
        post_treat = defer.Deferred()

        d =
            "MessageReceived", client, message_elt, post_treat

        d.addCallback(self._onMessageStartWorkflow, client, message_elt, post_treat)

    def skipEmptyMessage(self, data):
        if not data["message"] and not data["extra"] and not data["subject"]:
            raise failure.Failure(exceptions.CancelError("Cancelled empty message"))
        return data

    def addToHistory(self, data):
        if data.pop(u"history", None) == C.HISTORY_SKIP:
  "history is skipped as requested")
            data[u"extra"][u"history"] = C.HISTORY_SKIP
            if data[u"message"] or data[u"subject"]:  # we need a message to store
                return, data)
                log.debug(u"not storing empty message to history: {data}"

    def bridgeSignal(self, __, data):
            data["extra"]["received_timestamp"] = unicode(data["received_timestamp"])
            data["extra"]["delay_sender"] = data["delay_sender"]
        except KeyError:
        if C.MESS_KEY_ENCRYPTION in data:
            data[u"extra"][u"encrypted"] = C.BOOL_TRUE
        if data is not None:
            if data["message"] or data["subject"] or data["type"] == C.MESS_TYPE_INFO:
                log.debug(u"Discarding bridge signal for empty message: {data}".format(
        return data

    def cancelErrorTrap(self, failure_):
        """A message sending can be cancelled by a plugin treatment"""

class SatRosterProtocol(xmppim.RosterClientProtocol):

    def __init__(self, host):
        xmppim.RosterClientProtocol.__init__(self) = host
        self.got_roster = defer.Deferred()  # called when roster is received and ready
        # XXX: the two following dicts keep a local copy of the roster
        self._jids = {}  # map from jids to RosterItem: key=jid value=RosterItem
        self._groups = {}  # map from groups to jids: key=group value=set of jids

    def versioning(self):
        """True if server support roster versioning"""
        return (NS_ROSTER_VER, u'ver') in self.parent.xmlstream.features

    def roster_cache(self):
        """Cache of roster from storage

        This property return a new PersistentDict on each call, it must be loaded
        manually if necessary
        return persistent.PersistentDict(NS_ROSTER_VER, self.parent.profile)

    def _registerItem(self, item):
        """Register item in local cache

        item must be already registered in self._jids before this method is called
        @param item (RosterIem): item added
        log.debug(u"registering item: {}".format(item.entity.full()))
        if item.entity.resource:
                u"Received a roster item with a resource, this is not common but not "
                u"restricted by RFC 6121, this case may be not well tested."
        if not item.subscriptionTo:
            if not item.subscriptionFrom:
                    _(u"There's no subscription between you and [{}]!").format(
      "You are not subscribed to [{}]!").format(item.entity.full()))
        if not item.subscriptionFrom:
  "[{}] is not subscribed to you!").format(item.entity.full()))

        for group in item.groups:
            self._groups.setdefault(group, set()).add(item.entity)

    def _cacheRoster(self, version):
        """Serialise local roster and save it to storage

        @param version(unicode): version of roster in local cache
        roster_cache = self.roster_cache
        yield roster_cache.clear()
        roster_cache[ROSTER_VER_KEY] = version
        for roster_jid, roster_item in self._jids.iteritems():
            roster_jid_s = roster_jid.full()
            roster_item_elt = roster_item.toElement().toXml()
            roster_cache[roster_jid_s] = roster_item_elt

    def resync(self):
        """Ask full roster to resync database

        this should not be necessary, but may be used if user suspsect roster
        to be somehow corrupted
        roster_cache = self.roster_cache
        yield roster_cache.clear()
        yield self.requestRoster()

    def requestRoster(self):
        """Ask the server for Roster list """
        if self.versioning:
  "our server support roster versioning, we use it"))
            roster_cache = self.roster_cache
            yield roster_cache.load()
                version = roster_cache[ROSTER_VER_KEY]
            except KeyError:
      "no roster in cache, we start fresh"))
                # u"" means we use versioning without valid roster in cache
                version = u""
      "We have roster v{version} in cache").format(version=version))
                # we deserialise cached roster to our local cache
                for roster_jid_s, roster_item_elt_s in roster_cache.iteritems():
                    if roster_jid_s == ROSTER_VER_KEY:
                    roster_jid = jid.JID(roster_jid_s)
                    roster_item_elt = generic.parseXml(roster_item_elt_s.encode('utf-8'))
                    roster_item = xmppim.RosterItem.fromElement(roster_item_elt)
                    self._jids[roster_jid] = roster_item
            log.warning(_(u"our server doesn't support roster versioning"))
            version = None

        log.debug("requesting roster")
        roster = yield self.getRoster(version=version)
        if roster is None:
            log.debug(u"empty roster result received, we'll get roster item with roster "
            # a full roster is received
            self._jids = roster
            for item in roster.itervalues():
                if not item.subscriptionTo and not item.subscriptionFrom and not item.ask:
                    # XXX: current behaviour: we don't want contact in our roster list
                    # if there is no presence subscription
                    # may change in the future
                        u"Removing contact {} from roster because there is no presence "
                    self.removeItem(item.entity)  # FIXME: to be checked
            yield self._cacheRoster(roster.version)

        if not self.got_roster.called:
            # got_roster may already be called if we use resync()

    def removeItem(self, to_jid):
        """Remove a contact from roster list
        @param to_jid: a JID instance
        @return: Deferred
        return xmppim.RosterClientProtocol.removeItem(self, to_jid)

    def getAttributes(self, item):
        """Return dictionary of attributes as used in bridge from a RosterItem

        @param item: RosterItem
        @return: dictionary of attributes
        item_attr = {
            "to": unicode(item.subscriptionTo),
            "from": unicode(item.subscriptionFrom),
            "ask": unicode(item.ask),
            item_attr["name"] =
        return item_attr

    def setReceived(self, request):
        item = request.item
        entity = item.entity"adding {entity} to roster").format(entity=entity.full()))
        if request.version is not None:
            # we update the cache in storage
            roster_cache = self.roster_cache
            roster_cache[entity.full()] = item.toElement().toXml()
            roster_cache[ROSTER_VER_KEY] = request.version

        try:  # update the cache for the groups the contact has been removed from
            left_groups = set(self._jids[entity].groups).difference(item.groups)
            for group in left_groups:
                jids_set = self._groups[group]
                if not jids_set:
                    del self._groups[group]
        except KeyError:
            pass  # no previous item registration (or it's been cleared)
        self._jids[entity] = item
            entity.full(), self.getAttributes(item), item.groups, self.parent.profile

    def removeReceived(self, request):
        entity = request.item.entity"removing {entity} from roster").format(entity=entity.full()))
        if request.version is not None:
            # we update the cache in storage
            roster_cache = self.roster_cache
                del roster_cache[request.item.entity.full()]
            except KeyError:
                # because we don't use load(), cache won't have the key, but it
                # will be deleted from storage anyway
            roster_cache[ROSTER_VER_KEY] = request.version

        # we first remove item from local cache (self._groups and self._jids)
            item = self._jids.pop(entity)
        except KeyError:
                u"Received a roster remove event for an item not in cache ({})".format(
        for group in item.groups:
                jids_set = self._groups[group]
                if not jids_set:
                    del self._groups[group]
            except KeyError:
                    u"there is no cache for the group [{group}] of the removed roster "
                    u"item [{jid_}]".format(group=group, jid=entity)

        # then we send the bridge signal, self.parent.profile)

    def getGroups(self):
        """Return a list of groups"""
        return self._groups.keys()

    def getItem(self, entity_jid):
        """Return RosterItem for a given jid

        @param entity_jid(jid.JID): jid of the contact
        @return(RosterItem, None): RosterItem instance
            None if contact is not in cache
        return self._jids.get(entity_jid, None)

    def getJids(self):
        """Return all jids of the roster"""
        return self._jids.keys()

    def isJidInRoster(self, entity_jid):
        """Return True if jid is in roster"""
        return entity_jid in self._jids

    def isPresenceAuthorised(self, entity_jid):
        """Return True if entity is authorised to see our presence"""
            item = self._jids[entity_jid.userhostJID()]
        except KeyError:
            return False
        return item.subscriptionFrom

    def getItems(self):
        """Return all items of the roster"""
        return self._jids.values()

    def getJidsFromGroup(self, group):
            return self._groups[group]
        except KeyError:
            raise exceptions.UnknownGroupError(group)

    def getJidsSet(self, type_, groups=None):
        """Helper method to get a set of jids

        @param type_(unicode): one of:
            C.ALL: get all jids from roster
            C.GROUP: get jids from groups (listed in "groups")
        @groups(list[unicode]): list of groups used if type_==C.GROUP
        @return (set(jid.JID)): set of selected jids
        if type_ == C.ALL and groups is not None:
            raise ValueError("groups must not be set for {} type".format(C.ALL))

        if type_ == C.ALL:
            return set(self.getJids())
        elif type_ == C.GROUP:
            jids = set()
            for group in groups:
            return jids
            raise ValueError(u"Unexpected type_ {}".format(type_))

    def getNick(self, entity_jid):
        """Return a nick name for an entity

        return nick choosed by user if available
        else return user part of entity_jid
        item = self.getItem(entity_jid)
        if item is None:
            return entity_jid.user
            return or entity_jid.user

class SatPresenceProtocol(xmppim.PresenceClientProtocol):
    def __init__(self, host):
        xmppim.PresenceClientProtocol.__init__(self) = host

    def send(self, obj):
        presence_d = defer.succeed(None)
        if not"Presence send", self.parent, obj, presence_d):
        presence_d.addCallback(lambda __: super(SatPresenceProtocol, self).send(obj))

    def availableReceived(self, entity, show=None, statuses=None, priority=0):
        if not statuses:
            statuses = {}

        if None in statuses:  # we only want string keys
            statuses[C.PRESENCE_STATUSES_DEFAULT] = statuses.pop(None)

        if not
            "presence_received", self.parent, entity, show, priority, statuses
            entity, show or "", int(priority), statuses, self.parent.profile

        # now it's time to notify frontends
            entity.full(), show or "", int(priority), statuses, self.parent.profile

    def unavailableReceived(self, entity, statuses=None):
            _(u"presence update for [%(entity)s] (unavailable, statuses=%(statuses)s)")
            % {"entity": entity, C.PRESENCE_STATUSES: statuses}

        if not statuses:
            statuses = {}

        if None in statuses:  # we only want string keys
            statuses[C.PRESENCE_STATUSES_DEFAULT] = statuses.pop(None)

        if not
            "presence_received", self.parent, entity, C.PRESENCE_UNAVAILABLE, 0, statuses,

        # now it's time to notify frontends
        # if the entity is not known yet in this session or is already unavailable,
        # there is no need to send an unavailable signal
            presence =
                entity, "presence", self.parent.profile
        except (KeyError, exceptions.UnknownEntityError):
            # the entity has not been seen yet in this session
            if != C.PRESENCE_UNAVAILABLE:
            entity, C.PRESENCE_UNAVAILABLE, 0, statuses, self.parent.profile

    def available(self, entity=None, show=None, statuses=None, priority=None):
        """Set a presence and statuses.

        @param entity (jid.JID): entity
        @param show (unicode): value in ('unavailable', '', 'away', 'xa', 'chat', 'dnd')
        @param statuses (dict{unicode: unicode}): multilingual statuses with
            the entry key beeing a language code on 2 characters or "default".
        if priority is None:
                priority = int(
                        "Priority", "Connection", profile_key=self.parent.profile
            except ValueError:
                priority = 0

        if statuses is None:
            statuses = {}

        # default for us is None for wokkel
        # so we must temporarily switch to wokkel's convention...
        if C.PRESENCE_STATUSES_DEFAULT in statuses:
            statuses[None] = statuses.pop(C.PRESENCE_STATUSES_DEFAULT)

        presence_elt = xmppim.AvailablePresence(entity, show, statuses, priority)

        # ... before switching back
        if None in statuses:
            statuses["default"] = statuses.pop(None)

        if not"presence_available", presence_elt, self.parent):

    def subscribed(self, entity):
        yield self.parent.roster.got_roster
        xmppim.PresenceClientProtocol.subscribed(self, entity), self.parent.profile)
        item = self.parent.roster.getItem(entity)
        if (
            not item or not item.subscriptionTo
        ):  # we automatically subscribe to 'to' presence
            log.debug(_('sending automatic "from" subscription request'))

    def unsubscribed(self, entity):
        xmppim.PresenceClientProtocol.unsubscribed(self, entity), self.parent.profile)

    def subscribedReceived(self, entity):
        log.debug(_(u"subscription approved for [%s]") % entity.userhost())"subscribed", entity.userhost(), self.parent.profile)

    def unsubscribedReceived(self, entity):
        log.debug(_(u"unsubscription confirmed for [%s]") % entity.userhost())"unsubscribed", entity.userhost(), self.parent.profile)

    def subscribeReceived(self, entity):
        log.debug(_(u"subscription request from [%s]") % entity.userhost())
        yield self.parent.roster.got_roster
        item = self.parent.roster.getItem(entity)
        if item and item.subscriptionTo:
            # We automatically accept subscription if we are already subscribed to
            # contact presence
            log.debug(_("sending automatic subscription acceptance"))
                "subscribe", entity.userhost(), self.parent.profile
                "subscribe", entity.userhost(), self.parent.profile

    def unsubscribeReceived(self, entity):
        log.debug(_(u"unsubscription asked for [%s]") % entity.userhost())
        yield self.parent.roster.got_roster
        item = self.parent.roster.getItem(entity)
        if item and item.subscriptionFrom:  # we automatically remove contact
            log.debug(_("automatic contact deletion"))
  , self.parent.profile)"unsubscribe", entity.userhost(), self.parent.profile)

class SatDiscoProtocol(disco.DiscoClientProtocol):

    def __init__(self, host):

    def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
        # those features are implemented in Wokkel (or sat_tmp.wokkel)
        # and thus are always available
        return [disco.DiscoFeature(NS_X_DATA),

    def getDiscoItems(self, requestor, target, nodeIdentifier=""):
        return []

class SatFallbackHandler(generic.FallbackHandler):
    def __init__(self, host):

    def iqFallback(self, iq):
        if iq.handled is True:
        log.debug(u"iqFallback: xml = [%s]" % (iq.toXml()))
        generic.FallbackHandler.iqFallback(self, iq)

class SatVersionHandler(generic.VersionHandler):

    def getDiscoInfo(self, requestor, target, node):
        # XXX: We need to work around wokkel's behaviour (namespace not added if there
        #      is a node) as it cause issues with XEP-0115 & PEP (XEP-0163): there is a
        #      node when server ask for disco info, and not when we generate the key, so
        #      the hash is used with different disco features, and when the server (seen
        #      on ejabberd) generate its own hash for security check it reject our
        #      features (resulting in e.g. no notification on PEP)
        return generic.VersionHandler.getDiscoInfo(self, requestor, target, None)

class SatIdentityHandler(XMPPHandler):
    """Manage disco Identity of SàT."""
    # TODO: dynamic identity update (see docstring). Note that a XMPP entity can have
    #       several identities

    def getDiscoInfo(self, requestor, target, nodeIdentifier=""):
        return self.parent.identities

    def getDiscoItems(self, requestor, target, nodeIdentifier=""):
        return []