comparison src/plugins/plugin_xep_0100.py @ 544:899bd6fc7580

plugin XEP-0100: fixed lackings profile arguments in some methods
author Goffi <goffi@goffi.org>
date Wed, 14 Nov 2012 21:29:38 +0100
parents 2a072735e459
children ca13633d3b6b
comparison
equal deleted inserted replaced
543:a7f53e778d8e 544:899bd6fc7580
18 You should have received a copy of the GNU Affero General Public License 18 You should have received a copy of the GNU Affero General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. 19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """ 20 """
21 21
22 from logging import debug, info, error 22 from logging import debug, info, error
23 from twisted.internet import protocol 23 from twisted.words.protocols.jabber import client as jabber_client, jid
24 from twisted.words.protocols.jabber import client, jid
25 from twisted.words.protocols.jabber import error as jab_error 24 from twisted.words.protocols.jabber import error as jab_error
26 import twisted.internet.error 25 import twisted.internet.error
27 import pdb
28
29 from wokkel import disco, iwokkel
30 26
31 PLUGIN_INFO = { 27 PLUGIN_INFO = {
32 "name": "Gateways Plugin", 28 "name": "Gateways Plugin",
33 "import_name": "XEP-0100", 29 "import_name": "XEP-0100",
34 "type": "XEP", 30 "type": "XEP",
45 self.host = host 41 self.host = host
46 self.__gateways = {} #dict used to construct the answer to findGateways. Key = target jid 42 self.__gateways = {} #dict used to construct the answer to findGateways. Key = target jid
47 host.bridge.addMethod("findGateways", ".plugin", in_sign='ss', out_sign='s', method=self.findGateways) 43 host.bridge.addMethod("findGateways", ".plugin", in_sign='ss', out_sign='s', method=self.findGateways)
48 host.bridge.addMethod("gatewayRegister", ".plugin", in_sign='ssa(ss)s', out_sign='s', method=self.gatewayRegister) 44 host.bridge.addMethod("gatewayRegister", ".plugin", in_sign='ssa(ss)s', out_sign='s', method=self.gatewayRegister)
49 45
50 def __inc_handled_items(self, request_id): 46 def __inc_handled_items(self, request_id, profile):
51 self.__gateways[request_id]['__handled_items']+=1 47 self.__gateways[request_id]['__handled_items']+=1
52 48
53 if self.__gateways[request_id]['__total_items'] == self.__gateways[request_id]['__handled_items']: 49 if self.__gateways[request_id]['__total_items'] == self.__gateways[request_id]['__handled_items']:
54 debug (_("All items checked for id [%s]") % str(request_id)) 50 debug (_("All items checked for id [%s]") % str(request_id))
55 51
56 del self.__gateways[request_id]['__total_items'] 52 del self.__gateways[request_id]['__total_items']
57 del self.__gateways[request_id]['__handled_items'] 53 del self.__gateways[request_id]['__handled_items']
58 self.host.actionResultExt(request_id,"DICT_DICT",self.__gateways[request_id]) 54 self.host.actionResultExt(request_id,"DICT_DICT",self.__gateways[request_id], profile)
59 55
60 def discoInfo(self, disco, entity, request_id): 56 def discoInfo(self, disco, entity, request_id, profile):
61 """Find disco infos about entity, to check if it is a gateway""" 57 """Find disco infos about entity, to check if it is a gateway"""
62 58
63 for identity in disco.identities: 59 for identity in disco.identities:
64 if identity[0] == 'gateway': 60 if identity[0] == 'gateway':
65 print (_("Found gateway (%(jid)s): %(identity)s") % {'jid':entity.full(), 'identity':disco.identities[identity]}) 61 print (_("Found gateway (%(jid)s): %(identity)s") % {'jid':entity.full(), 'identity':disco.identities[identity]})
66 self.__gateways[request_id][entity.full()] = { 62 self.__gateways[request_id][entity.full()] = {
67 'name':disco.identities[identity], 63 'name':disco.identities[identity],
68 'type':identity[1] 64 'type':identity[1]
69 } 65 }
70 66
71 self.__inc_handled_items(request_id) 67 self.__inc_handled_items(request_id, profile)
72 68
73 def discoInfoErr(self, failure, entity, request_id): 69 def discoInfoErr(self, failure, entity, request_id, profile):
74 """Something is going wrong with disco""" 70 """Something is going wrong with disco"""
75 failure.trap(jab_error.StanzaError,twisted.internet.error.ConnectionLost) 71 failure.trap(jab_error.StanzaError,twisted.internet.error.ConnectionLost)
76 error(_("Error when discovering [%(jid)s]: %(error)s") % {'jid':entity.full(), 'error':failure.getErrorMessage()}) 72 error(_("Error when discovering [%(jid)s]: %(error)s") % {'jid':entity.full(), 'error':failure.getErrorMessage()})
77 self.__inc_handled_items(request_id) 73 self.__inc_handled_items(request_id, profile)
78 74
79 75
80 def discoItems(self, disco, request_id, target, client): 76 def discoItems(self, disco, request_id, target, client):
81 """Look for items with disco protocol, and ask infos for each one""" 77 """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) 78 #FIXME: target is used as we can't find the original iq node (parent is None)
88 84
89 self.__gateways[request_id] = {'__total_items':len(disco._items), '__handled_items':0, '__private__':{'target':target.full()}} 85 self.__gateways[request_id] = {'__total_items':len(disco._items), '__handled_items':0, '__private__':{'target':target.full()}}
90 for item in disco._items: 86 for item in disco._items:
91 #TODO: need to set a timeout for theses requests 87 #TODO: need to set a timeout for theses requests
92 debug (_("item found: %s"), item.name) 88 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) 89 client.disco.requestInfo(item.entity).addCallback(self.discoInfo, entity=item.entity, request_id=request_id, profile=client.profile).addErrback(self.discoInfoErr, entity=item.entity, request_id=request_id, profile=client.profile)
94 90
95 def discoItemsErr(self, failure, request_id, target, client): 91 def discoItemsErr(self, failure, request_id, target, client):
96 """Something is going wrong with disco""" 92 """Something is going wrong with disco"""
97 error(_("Error when discovering [%(target)s]: %(condition)s") % {'target':target.full(), 'condition':unicode(failure.value)}) 93 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)}} 94 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)}}
111 assert(profile) #FIXME: return an error here 107 assert(profile) #FIXME: return an error here
112 if action == 'SUBMIT': 108 if action == 'SUBMIT':
113 self.host.plugins["XEP-0077"].addTrigger(target, self.registrationSuccessful, profile) 109 self.host.plugins["XEP-0077"].addTrigger(target, self.registrationSuccessful, profile)
114 return self.host.plugins["XEP-0077"].in_band_submit(action, target, fields, profile) 110 return self.host.plugins["XEP-0077"].in_band_submit(action, target, fields, profile)
115 111
116 def findGateways(self, target, profile_key='@DEFAULT@'): 112 def findGateways(self, target, profile_key):
117 """Find gateways in the target JID, using discovery protocol 113 """Find gateways in the target JID, using discovery protocol
118 Return an id used for retrieving the list of gateways 114 Return an id used for retrieving the list of gateways
119 """ 115 """
120 profile = self.host.memory.getProfileName(profile_key) 116 profile = self.host.memory.getProfileName(profile_key)
121 client = self.host.getClient(profile_key) 117 client = self.host.getClient(profile_key)