comparison plugins/plugin_xep_0100.py @ 25:53e921c8a357

new plugin: gateways plugin, and first implementation of findGateways - test menu in Wix - new actionResultExt method, for sending dictionary of dictionaries - new getNextId method, for accessing sat ids from plugins.
author Goffi <goffi@goffi.org>
date Fri, 04 Dec 2009 08:47:44 +0100
parents
children d6b613764dd7
comparison
equal deleted inserted replaced
24:61124cb82fb7 25:53e921c8a357
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 SAT plugin for managing gateways (xep-0100)
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 from logging import debug, info, error
23 from twisted.internet import protocol
24 from twisted.words.protocols.jabber import client, jid, xmlstream, error
25 import pdb
26
27 from wokkel import disco, iwokkel
28
29 PLUGIN_INFO = {
30 "name": "Gateways Plugin",
31 "import_name": "XEP_0100",
32 "type": "XEP",
33 "dependencies": [],
34 "main": "XEP_0100",
35 "description": """Implementation of Gateways protocol"""
36 }
37
38 class XEP_0100():
39
40 def __init__(self, host):
41 info("Gateways plugin initialization")
42 self.host = host
43 self.__gateways = {} #dict used to construct the answer to findGateways. Key = target jid
44 host.bridge.addMethod("findGateways", ".communication", in_sign='s', out_sign='s', method=self.findGateways)
45
46 def discoInfo(self, disco, entity, request_id):
47 """Find disco infos about entity, to check if it is a gateway"""
48
49 for identity in disco.identities:
50 if identity[0] == 'gateway':
51 print ("Found gateway (%s): %s" % (entity, disco.identities[identity]))
52 self.__gateways[request_id][entity.full()] = {
53 'name':disco.identities[identity],
54 'type':identity[1]
55 }
56
57 self.__gateways[request_id]['__handled_items']+=1
58
59 if self.__gateways[request_id]['__total_items'] == self.__gateways[request_id]['__handled_items']:
60 debug ("All items checked for id [%s]" % str(request_id))
61
62 del self.__gateways[request_id]['__total_items']
63 del self.__gateways[request_id]['__handled_items']
64 self.host.actionResultExt(request_id,"DICT_DICT",self.__gateways[request_id])
65
66 def discoItems(self, disco, request_id):
67 """Look for items with disco protocol, and ask infos for each one"""
68
69 if len(disco._items) == 0:
70 debug ("No gateway found")
71 self.host.actionResultExt(request_id,"DICT_DICT",{})
72 return
73
74 self.__gateways[request_id] = {'__total_items':len(disco._items), '__handled_items':0}
75 for item in disco._items:
76 debug ("item found: %s", item.name)
77 self.host.disco.requestInfo(item.entity).addCallback(self.discoInfo, entity=item.entity, request_id=request_id)
78
79 def findGateways(self, target):
80 """Find gateways in the target JID, using discovery protocol
81 Return an id used for retrieving the list of gateways
82 """
83 debug ("find gateways (target = %s)" % target)
84 request_id = self.host.get_next_id()
85 self.host.disco.requestItems(jid.JID(target)).addCallback(self.discoItems, request_id=request_id)
86 return request_id