view plugins/ @ 69:86f1f7f6d332

i18n first draft - gettext support added in SàT - first draft of french translation - added README with a HOWTO for translators
author Goffi <>
date Wed, 03 Mar 2010 17:12:23 +1100
parents d35c5edab53f
children 94011f553cd0
line wrap: on
line source

# -*- coding: utf-8 -*-

SAT plugin for managing gateways (xep-0100)
Copyright (C) 2009, 2010  Jérôme Poisson (

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <>.

from logging import debug, info, error
from twisted.internet import protocol
from twisted.words.protocols.jabber import client, jid
from twisted.words.protocols.jabber import error as jab_error
import pdb

from wokkel import disco, iwokkel

"name": "Gateways Plugin",
"import_name": "XEP_0100",
"type": "XEP",
"protocols": ["XEP-0100"],
"dependencies": ["XEP_0077"],
"main": "XEP_0100",
"description": _("""Implementation of Gateways protocol""")

class XEP_0100():

    def __init__(self, host):
        info(_("Gateways plugin initialization")) = host
        self.__gateways = {}  #dict used to construct the answer to findGateways. Key = target jid
        host.bridge.addMethod("findGateways", ".communication", in_sign='ss', out_sign='s', method=self.findGateways)
        host.bridge.addMethod("gatewayRegister", ".request", in_sign='ssa(ss)', out_sign='s', method=self.gatewayRegister)

    def __inc_handled_items(self, request_id):

        if self.__gateways[request_id]['__total_items'] == self.__gateways[request_id]['__handled_items']:
            debug (_("All items checked for id [%s]") % str(request_id))
            del self.__gateways[request_id]['__total_items']
            del self.__gateways[request_id]['__handled_items']

    def discoInfo(self, disco, entity, request_id):
        """Find disco infos about entity, to check if it is a gateway"""

        for identity in disco.identities:
            if identity[0] == 'gateway':
                print (_("Found gateway (%(jid)s): %(identity)s") % {'jid':entity.full(), 'identity':disco.identities[identity]})
                self.__gateways[request_id][entity.full()] = {

    def discoInfoErr(self, failure, entity, request_id):
        """Something is going wrong with disco"""
        error(_("Error when discovering [%(jid)s]: %(condition)s") % {'jid':entity.full(), 'condition':failure.value.condition})
    def discoItems(self, disco, request_id, target, client):
        """Look for items with disco protocol, and ask infos for each one"""
        #FIXME: target is used as we can't find the original iq node (parent is None)
        #       an other way would avoid this useless parameter (is there a way with wokkel ?)
        if len(disco._items) == 0:
            debug (_("No gateway found"))

        self.__gateways[request_id] = {'__total_items':len(disco._items), '__handled_items':0, '__private__':{'target':target.full()}}
        for item in disco._items:
            debug (_("item found: %s"),
            client.disco.requestInfo(item.entity).addCallback(self.discoInfo, entity=item.entity, request_id=request_id)
            client.disco.requestInfo(item.entity).addErrback(self.discoInfoErr, entity=item.entity, request_id=request_id)

    def registrationSuccessful(self, target):
        """Called when in_band registration is ok, we must now follow the rest of procedure"""
        debug (_("Registration successful, doing the rest"))
    def gatewayRegister(self, action, target, fields):
        """Register gateway using in-band registration, then log-in to gateway"""
        if action == 'SUBMIT':
  ["XEP_0077"].addTrigger(target, self.registrationSuccessful)
        return["XEP_0077"].in_band_submit(action, target, fields)

    def findGateways(self, target, profile_key='@DEFAULT@'):
        """Find gateways in the target JID, using discovery protocol
        Return an id used for retrieving the list of gateways
        client =
        to_jid = jid.JID(target)
        debug (_("find gateways (target = %s)") % to_jid.full())
        request_id =
        client.disco.requestItems(to_jid).addCallback(self.discoItems, request_id=request_id, target = to_jid, client = client)
        return request_id