view plugins/plugin_xep_0100.py @ 37:a61beb21d16d

Gateway registration, unregistration & edition - default values added in form - DBus bridge: fixed array of struct management when adding dynamically a method - fixed doc for some methods - ugly fix for presence status - added dependency for XEP-0077 in XEP-0100 plugin - Wix: added unregister button in gateways manager - Wix: added privacy warning in gateways manager
author Goffi <goffi@goffi.org>
date Tue, 15 Dec 2009 01:27:32 +1100
parents d6b613764dd7
children 2e3411a6baad
line wrap: on
line source

#!/usr/bin/python
# -*- coding: utf-8 -*-

"""
SAT plugin for managing gateways (xep-0100)
Copyright (C) 2009  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 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 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 <http://www.gnu.org/licenses/>.
"""

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

from wokkel import disco, iwokkel

PLUGIN_INFO = {
"name": "Gateways Plugin",
"import_name": "XEP_0100",
"type": "XEP",
"dependencies": ["XEP_0077"],
"main": "XEP_0100",
"description": """Implementation of Gateways protocol"""
}

class XEP_0100():

    def __init__(self, host):
        info("Gateways plugin initialization")
        self.host = host
        self.__gateways = {}  #dict used to construct the answer to findGateways. Key = target jid
        host.bridge.addMethod("findGateways", ".communication", in_sign='s', out_sign='s', method=self.findGateways)
        host.bridge.addMethod("gatewayRegister", ".request", in_sign='ssa(ss)', out_sign='s', method=self.gatewayRegister)
    
    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 (%s): %s" % (entity.full(), disco.identities[identity]))
                self.__gateways[request_id][entity.full()] = {
                    'name':disco.identities[identity],
                    'type':identity[1]
                }

        self.__gateways[request_id]['__handled_items']+=1

        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']
            self.host.actionResultExt(request_id,"DICT_DICT",self.__gateways[request_id])
    
    def discoItems(self, disco, request_id):
        """Look for items with disco protocol, and ask infos for each one"""
        
        if len(disco._items) == 0:
            debug ("No gateway found")
            self.host.actionResultExt(request_id,"DICT_DICT",{})
            return

        self.__gateways[request_id] = {'__total_items':len(disco._items), '__handled_items':0}
        for item in disco._items:
            debug ("item found: %s", item.name)
            self.host.disco.requestInfo(item.entity).addCallback(self.discoInfo, 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"""
        print "Registration successful, doing the rest"
        self.host.addContact(target)
        self.host.setPresence(target)
    
    def gatewayRegister(self, action, target, fields):
        """Register gateway using in-band registration, then log-in to gateway"""
        if action == 'SUBMIT':
            self.host.plugins["XEP_0077"].addTrigger(target, self.registrationSuccessful)
        return self.host.plugins["XEP_0077"].in_band_submit(action, target, fields)

    def findGateways(self, target):
        """Find gateways in the target JID, using discovery protocol
        Return an id used for retrieving the list of gateways
        """
        debug ("find gateways (target = %s)" % target)
        request_id = self.host.get_next_id()
        self.host.disco.requestItems(jid.JID(target)).addCallback(self.discoItems, request_id=request_id)
        return request_id