Mercurial > libervia-backend
comparison src/plugins/plugin_xep_0100.py @ 223:86d249b6d9b7
Files reorganisation
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 29 Dec 2010 01:06:29 +0100 |
parents | plugins/plugin_xep_0100.py@8537df794f74 |
children | b1794cbb88e5 |
comparison
equal
deleted
inserted
replaced
222:3198bfd66daa | 223:86d249b6d9b7 |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT plugin for managing gateways (xep-0100) | |
6 Copyright (C) 2009, 2010 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 | |
25 from twisted.words.protocols.jabber import error as jab_error | |
26 import twisted.internet.error | |
27 import pdb | |
28 | |
29 from wokkel import disco, iwokkel | |
30 | |
31 PLUGIN_INFO = { | |
32 "name": "Gateways Plugin", | |
33 "import_name": "XEP_0100", | |
34 "type": "XEP", | |
35 "protocols": ["XEP-0100"], | |
36 "dependencies": ["XEP_0077"], | |
37 "main": "XEP_0100", | |
38 "description": _("""Implementation of Gateways protocol""") | |
39 } | |
40 | |
41 class XEP_0100(): | |
42 | |
43 def __init__(self, host): | |
44 info(_("Gateways plugin initialization")) | |
45 self.host = host | |
46 self.__gateways = {} #dict used to construct the answer to findGateways. Key = target jid | |
47 host.bridge.addMethod("findGateways", ".communication", in_sign='ss', out_sign='s', method=self.findGateways) | |
48 host.bridge.addMethod("gatewayRegister", ".request", in_sign='ssa(ss)s', out_sign='s', method=self.gatewayRegister) | |
49 | |
50 def __inc_handled_items(self, request_id): | |
51 self.__gateways[request_id]['__handled_items']+=1 | |
52 | |
53 if self.__gateways[request_id]['__total_items'] == self.__gateways[request_id]['__handled_items']: | |
54 debug (_("All items checked for id [%s]") % str(request_id)) | |
55 | |
56 del self.__gateways[request_id]['__total_items'] | |
57 del self.__gateways[request_id]['__handled_items'] | |
58 self.host.actionResultExt(request_id,"DICT_DICT",self.__gateways[request_id]) | |
59 | |
60 def discoInfo(self, disco, entity, request_id): | |
61 """Find disco infos about entity, to check if it is a gateway""" | |
62 | |
63 for identity in disco.identities: | |
64 if identity[0] == 'gateway': | |
65 print (_("Found gateway (%(jid)s): %(identity)s") % {'jid':entity.full(), 'identity':disco.identities[identity]}) | |
66 self.__gateways[request_id][entity.full()] = { | |
67 'name':disco.identities[identity], | |
68 'type':identity[1] | |
69 } | |
70 | |
71 self.__inc_handled_items(request_id) | |
72 | |
73 def discoInfoErr(self, failure, entity, request_id): | |
74 """Something is going wrong with disco""" | |
75 failure.trap(jab_error.StanzaError,twisted.internet.error.ConnectionLost) | |
76 error(_("Error when discovering [%(jid)s]: %(error)s") % {'jid':entity.full(), 'error':failure.getErrorMessage()}) | |
77 self.__inc_handled_items(request_id) | |
78 | |
79 | |
80 def discoItems(self, disco, request_id, target, client): | |
81 """Look for items with disco protocol, and ask infos for each one""" | |
82 #FIXME: target is used as we can't find the original iq node (parent is None) | |
83 # an other way would avoid this useless parameter (is there a way with wokkel ?) | |
84 if len(disco._items) == 0: | |
85 debug (_("No gateway found")) | |
86 self.host.actionResultExt(request_id,"DICT_DICT",{}) | |
87 return | |
88 | |
89 self.__gateways[request_id] = {'__total_items':len(disco._items), '__handled_items':0, '__private__':{'target':target.full()}} | |
90 for item in disco._items: | |
91 #TODO: need to set a timeout for theses requests | |
92 debug (_("item found: %s"), item.name) | |
93 client.disco.requestInfo(item.entity).addCallback(self.discoInfo, entity=item.entity, request_id=request_id).addErrback(self.discoInfoErr, entity=item.entity, request_id=request_id) | |
94 | |
95 def discoItemsErr(self, failure, request_id, target, client): | |
96 """Something is going wrong with disco""" | |
97 error(_("Error when discovering [%(target)s]: %(condition)s") % {'target':target.full(), 'condition':unicode(failure.value)}) | |
98 message_data={"reason": "connection error", "message":_(u"Error while trying to discover %(target)s gateways: %(error_mess)s") % {'target':target.full(), 'error_mess':unicode(failure.value)}} | |
99 self.host.bridge.actionResult("ERROR", request_id, message_data) | |
100 | |
101 | |
102 def registrationSuccessful(self, target, profile): | |
103 """Called when in_band registration is ok, we must now follow the rest of procedure""" | |
104 debug (_("Registration successful, doing the rest")) | |
105 self.host.addContact(target, profile) | |
106 self.host.setPresence(target, profile) | |
107 | |
108 def gatewayRegister(self, action, target, fields, profile_key='@DEFAULT@'): | |
109 """Register gateway using in-band registration, then log-in to gateway""" | |
110 profile = self.host.memory.getProfileName(profile_key) | |
111 assert(profile) #FIXME: return an error here | |
112 if action == 'SUBMIT': | |
113 self.host.plugins["XEP_0077"].addTrigger(target, self.registrationSuccessful, profile) | |
114 return self.host.plugins["XEP_0077"].in_band_submit(action, target, fields, profile) | |
115 | |
116 def findGateways(self, target, profile_key='@DEFAULT@'): | |
117 """Find gateways in the target JID, using discovery protocol | |
118 Return an id used for retrieving the list of gateways | |
119 """ | |
120 profile = self.host.memory.getProfileName(profile_key) | |
121 client = self.host.getClient(profile_key) | |
122 assert(client) | |
123 to_jid = jid.JID(target) | |
124 debug (_("find gateways (target = %(target)s, profile = %(profile)s)") % {'target':to_jid.full(), 'profile':profile}) | |
125 request_id = self.host.get_next_id() | |
126 client.disco.requestItems(to_jid).addCallback(self.discoItems, request_id=request_id, target = to_jid, client = client).addErrback(self.discoItemsErr, request_id=request_id, target = to_jid, client = client) | |
127 return request_id |