comparison src/plugins/plugin_xep_0100.py @ 594:e629371a28d3

Fix pep8 support in src/plugins.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Fri, 18 Jan 2013 17:55:35 +0100
parents beaf6bec2fcd
children 84a6e83157c2
comparison
equal deleted inserted replaced
593:70bae685d05c 594:e629371a28d3
23 from twisted.words.protocols.jabber import client as jabber_client, jid 23 from twisted.words.protocols.jabber import client as jabber_client, jid
24 from twisted.words.protocols.jabber import error as jab_error 24 from twisted.words.protocols.jabber import error as jab_error
25 import twisted.internet.error 25 import twisted.internet.error
26 26
27 PLUGIN_INFO = { 27 PLUGIN_INFO = {
28 "name": "Gateways Plugin", 28 "name": "Gateways Plugin",
29 "import_name": "XEP-0100", 29 "import_name": "XEP-0100",
30 "type": "XEP", 30 "type": "XEP",
31 "protocols": ["XEP-0100"], 31 "protocols": ["XEP-0100"],
32 "dependencies": ["XEP-0077"], 32 "dependencies": ["XEP-0077"],
33 "main": "XEP_0100", 33 "main": "XEP_0100",
34 "description": _("""Implementation of Gateways protocol""") 34 "description": _("""Implementation of Gateways protocol""")
35 } 35 }
36
36 37
37 class XEP_0100(object): 38 class XEP_0100(object):
38 39
39 def __init__(self, host): 40 def __init__(self, host):
40 info(_("Gateways plugin initialization")) 41 info(_("Gateways plugin initialization"))
41 self.host = host 42 self.host = host
42 self.__gateways = {} #dict used to construct the answer to findGateways. Key = target jid 43 self.__gateways = {} # dict used to construct the answer to findGateways. Key = target jid
43 host.bridge.addMethod("findGateways", ".plugin", in_sign='ss', out_sign='s', method=self.findGateways) 44 host.bridge.addMethod("findGateways", ".plugin", in_sign='ss', out_sign='s', method=self.findGateways)
44 host.bridge.addMethod("gatewayRegister", ".plugin", in_sign='ssa(ss)s', out_sign='s', method=self.gatewayRegister) 45 host.bridge.addMethod("gatewayRegister", ".plugin", in_sign='ssa(ss)s', out_sign='s', method=self.gatewayRegister)
45 46
46 def __inc_handled_items(self, request_id, profile): 47 def __inc_handled_items(self, request_id, profile):
47 self.__gateways[request_id]['__handled_items']+=1 48 self.__gateways[request_id]['__handled_items'] += 1
48 49
49 if self.__gateways[request_id]['__total_items'] == self.__gateways[request_id]['__handled_items']: 50 if self.__gateways[request_id]['__total_items'] == self.__gateways[request_id]['__handled_items']:
50 debug (_("All items checked for id [%s]") % str(request_id)) 51 debug(_("All items checked for id [%s]") % str(request_id))
51 52
52 del self.__gateways[request_id]['__total_items'] 53 del self.__gateways[request_id]['__total_items']
53 del self.__gateways[request_id]['__handled_items'] 54 del self.__gateways[request_id]['__handled_items']
54 self.host.actionResultExt(request_id,"DICT_DICT",self.__gateways[request_id], profile) 55 self.host.actionResultExt(request_id, "DICT_DICT", self.__gateways[request_id], profile)
55 56
56 def discoInfo(self, disco, entity, request_id, profile): 57 def discoInfo(self, disco, entity, request_id, profile):
57 """Find disco infos about entity, to check if it is a gateway""" 58 """Find disco infos about entity, to check if it is a gateway"""
58 59
59 for identity in disco.identities: 60 for identity in disco.identities:
60 if identity[0] == 'gateway': 61 if identity[0] == 'gateway':
61 print (_("Found gateway (%(jid)s): %(identity)s") % {'jid':entity.full(), 'identity':disco.identities[identity]}) 62 print (_("Found gateway (%(jid)s): %(identity)s") % {'jid': entity.full(), 'identity': disco.identities[identity]})
62 self.__gateways[request_id][entity.full()] = { 63 self.__gateways[request_id][entity.full()] = {
63 'name':disco.identities[identity], 64 'name': disco.identities[identity],
64 'type':identity[1] 65 'type': identity[1]
65 } 66 }
66 67
67 self.__inc_handled_items(request_id, profile) 68 self.__inc_handled_items(request_id, profile)
68 69
69 def discoInfoErr(self, failure, entity, request_id, profile): 70 def discoInfoErr(self, failure, entity, request_id, profile):
70 """Something is going wrong with disco""" 71 """Something is going wrong with disco"""
71 failure.trap(jab_error.StanzaError,twisted.internet.error.ConnectionLost) 72 failure.trap(jab_error.StanzaError, twisted.internet.error.ConnectionLost)
72 error(_("Error when discovering [%(jid)s]: %(error)s") % {'jid':entity.full(), 'error':failure.getErrorMessage()}) 73 error(_("Error when discovering [%(jid)s]: %(error)s") % {'jid': entity.full(), 'error': failure.getErrorMessage()})
73 self.__inc_handled_items(request_id, profile) 74 self.__inc_handled_items(request_id, profile)
74
75 75
76 def discoItems(self, disco, request_id, target, client): 76 def discoItems(self, disco, request_id, target, client):
77 """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"""
78 #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)
79 # an other way would avoid this useless parameter (is there a way with wokkel ?) 79 # an other way would avoid this useless parameter (is there a way with wokkel ?)
80 if len(disco._items) == 0: 80 if len(disco._items) == 0:
81 debug (_("No gateway found")) 81 debug(_("No gateway found"))
82 self.host.actionResultExt(request_id,"DICT_DICT",{}) 82 self.host.actionResultExt(request_id, "DICT_DICT", {})
83 return 83 return
84 84
85 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()}}
86 for item in disco._items: 86 for item in disco._items:
87 #TODO: need to set a timeout for theses requests 87 #TODO: need to set a timeout for theses requests
88 debug (_("item found: %s"), item.name) 88 debug(_("item found: %s"), item.name)
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) 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)
90 90
91 def discoItemsErr(self, failure, request_id, target, client): 91 def discoItemsErr(self, failure, request_id, target, client):
92 """Something is going wrong with disco""" 92 """Something is going wrong with disco"""
93 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)})
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)}} 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)}}
95 self.host.bridge.actionResult("ERROR", request_id, message_data) 95 self.host.bridge.actionResult("ERROR", request_id, message_data)
96
97 96
98 def registrationSuccessful(self, target, profile): 97 def registrationSuccessful(self, target, profile):
99 """Called when in_band registration is ok, we must now follow the rest of procedure""" 98 """Called when in_band registration is ok, we must now follow the rest of procedure"""
100 debug (_("Registration successful, doing the rest")) 99 debug(_("Registration successful, doing the rest"))
101 self.host.addContact(target, profile) 100 self.host.addContact(target, profile)
102 self.host.setPresence(target, profile) 101 self.host.setPresence(target, profile)
103 102
104 def gatewayRegister(self, action, target, fields, profile_key='@DEFAULT@'): 103 def gatewayRegister(self, action, target, fields, profile_key='@DEFAULT@'):
105 """Register gateway using in-band registration, then log-in to gateway""" 104 """Register gateway using in-band registration, then log-in to gateway"""
106 profile = self.host.memory.getProfileName(profile_key) 105 profile = self.host.memory.getProfileName(profile_key)
107 assert(profile) #FIXME: return an error here 106 assert(profile) # FIXME: return an error here
108 if action == 'SUBMIT': 107 if action == 'SUBMIT':
109 self.host.plugins["XEP-0077"].addTrigger(target, self.registrationSuccessful, profile) 108 self.host.plugins["XEP-0077"].addTrigger(target, self.registrationSuccessful, profile)
110 return self.host.plugins["XEP-0077"].in_band_submit(action, target, fields, profile) 109 return self.host.plugins["XEP-0077"].in_band_submit(action, target, fields, profile)
111 110
112 def findGateways(self, target, profile_key): 111 def findGateways(self, target, profile_key):
115 """ 114 """
116 profile = self.host.memory.getProfileName(profile_key) 115 profile = self.host.memory.getProfileName(profile_key)
117 client = self.host.getClient(profile_key) 116 client = self.host.getClient(profile_key)
118 assert(client) 117 assert(client)
119 to_jid = jid.JID(target) 118 to_jid = jid.JID(target)
120 debug (_("find gateways (target = %(target)s, profile = %(profile)s)") % {'target':to_jid.full(), 'profile':profile}) 119 debug(_("find gateways (target = %(target)s, profile = %(profile)s)") % {'target': to_jid.full(), 'profile': profile})
121 request_id = self.host.get_next_id() 120 request_id = self.host.get_next_id()
122 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) 121 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)
123 return request_id 122 return request_id