comparison src/plugins/plugin_xep_0100.py @ 993:301b342c697a

core: use of the new core.log module: /!\ this is a massive refactoring and was largely automated, it probably did bring some bugs /!\
author Goffi <goffi@goffi.org>
date Sat, 19 Apr 2014 19:19:19 +0200
parents c6d8fc63b1db
children 9e24ca275ddd
comparison
equal deleted inserted replaced
992:f51a1895275c 993:301b342c697a
19 19
20 from sat.core.i18n import _, D_ 20 from sat.core.i18n import _, D_
21 from sat.core.constants import Const as C 21 from sat.core.constants import Const as C
22 from sat.core import exceptions 22 from sat.core import exceptions
23 from sat.tools import xml_tools 23 from sat.tools import xml_tools
24 from logging import debug, info, warning, error 24 from sat.core.log import getLogger
25 log = getLogger(__name__)
25 from twisted.words.protocols.jabber import jid 26 from twisted.words.protocols.jabber import jid
26 from twisted.internet import reactor, defer 27 from twisted.internet import reactor, defer
27 28
28 PLUGIN_INFO = { 29 PLUGIN_INFO = {
29 "name": "Gateways Plugin", 30 "name": "Gateways Plugin",
53 54
54 55
55 class XEP_0100(object): 56 class XEP_0100(object):
56 57
57 def __init__(self, host): 58 def __init__(self, host):
58 info(_("Gateways plugin initialization")) 59 log.info(_("Gateways plugin initialization"))
59 self.host = host 60 self.host = host
60 self.__gateways = {} # dict used to construct the answer to findGateways. Key = target jid 61 self.__gateways = {} # dict used to construct the answer to findGateways. Key = target jid
61 host.bridge.addMethod("findGateways", ".plugin", in_sign='ss', out_sign='s', method=self._findGateways) 62 host.bridge.addMethod("findGateways", ".plugin", in_sign='ss', out_sign='s', method=self._findGateways)
62 host.bridge.addMethod("gatewayRegister", ".plugin", in_sign='ss', out_sign='s', method=self._gatewayRegister) 63 host.bridge.addMethod("gatewayRegister", ".plugin", in_sign='ss', out_sign='s', method=self._gatewayRegister)
63 self.__menu_id = host.registerCallback(self._gatewaysMenu, with_data=True) 64 self.__menu_id = host.registerCallback(self._gatewaysMenu, with_data=True)
109 110
110 def _gatewaySelectedCb(self, data, profile): 111 def _gatewaySelectedCb(self, data, profile):
111 try: 112 try:
112 target_jid = jid.JID(data['index']) 113 target_jid = jid.JID(data['index'])
113 except (KeyError, RuntimeError): 114 except (KeyError, RuntimeError):
114 warning(_("No gateway index selected")) 115 log.warning(_("No gateway index selected"))
115 return {} 116 return {}
116 117
117 d = self.gatewayRegister(target_jid, profile) 118 d = self.gatewayRegister(target_jid, profile)
118 d.addCallback(lambda xmlui: {'xmlui': xmlui.toXml()}) 119 d.addCallback(lambda xmlui: {'xmlui': xmlui.toXml()})
119 return d 120 return d
123 @param identity: tuple as returned by Disco identities (category, type) 124 @param identity: tuple as returned by Disco identities (category, type)
124 125
125 """ 126 """
126 category, type_ = identity 127 category, type_ = identity
127 if category != 'gateway': 128 if category != 'gateway':
128 error(_('INTERNAL ERROR: identity category should always be "gateway" in _getTypeString, got "%s"') % category) 129 log.error(_('INTERNAL ERROR: identity category should always be "gateway" in _getTypeString, got "%s"') % category)
129 try: 130 try:
130 return _(TYPE_DESCRIPTIONS[type_]) 131 return _(TYPE_DESCRIPTIONS[type_])
131 except KeyError: 132 except KeyError:
132 return _("Unknown IM") 133 return _("Unknown IM")
133 134
134 def _registrationSuccessful(self, jid_, profile): 135 def _registrationSuccessful(self, jid_, profile):
135 """Called when in_band registration is ok, we must now follow the rest of procedure""" 136 """Called when in_band registration is ok, we must now follow the rest of procedure"""
136 debug(_("Registration successful, doing the rest")) 137 log.debug(_("Registration successful, doing the rest"))
137 self.host.addContact(jid_, profile_key=profile) 138 self.host.addContact(jid_, profile_key=profile)
138 self.host.setPresence(jid_, profile_key=profile) 139 self.host.setPresence(jid_, profile_key=profile)
139 140
140 def _gatewayRegister(self, target_jid_s, profile_key=C.PROF_KEY_NONE): 141 def _gatewayRegister(self, target_jid_s, profile_key=C.PROF_KEY_NONE):
141 d = self.gatewayRegister(jid.JID(target_jid_s), profile_key) 142 d = self.gatewayRegister(jid.JID(target_jid_s), profile_key)
165 ret.append((success, (msg, items[idx]))) 166 ret.append((success, (msg, items[idx])))
166 else: 167 else:
167 entity = items[idx].entity 168 entity = items[idx].entity
168 gateways = [(identity, result.identities[identity]) for identity in result.identities if identity[0] == 'gateway'] 169 gateways = [(identity, result.identities[identity]) for identity in result.identities if identity[0] == 'gateway']
169 if gateways: 170 if gateways:
170 info (_("Found gateway [%(jid)s]: %(identity_name)s") % {'jid': entity.full(), 'identity_name': ' - '.join([gateway[1] for gateway in gateways])}) 171 log.info (_("Found gateway [%(jid)s]: %(identity_name)s") % {'jid': entity.full(), 'identity_name': ' - '.join([gateway[1] for gateway in gateways])})
171 ret.append((success, (entity, gateways))) 172 ret.append((success, (entity, gateways)))
172 else: 173 else:
173 info(_("Skipping [%(jid)s] which is not a gateway") % {'jid': entity.full()}) 174 log.info(_("Skipping [%(jid)s] which is not a gateway") % {'jid': entity.full()})
174 return ret 175 return ret
175 176
176 def _itemsReceived(self, disco, target, client): 177 def _itemsReceived(self, disco, target, client):
177 """Look for items with disco protocol, and ask infos for each one""" 178 """Look for items with disco protocol, and ask infos for each one"""
178 179
179 if len(disco._items) == 0: 180 if len(disco._items) == 0:
180 debug(_("No gateway found")) 181 log.debug(_("No gateway found"))
181 return [] 182 return []
182 183
183 _defers = [] 184 _defers = []
184 for item in disco._items: 185 for item in disco._items:
185 debug(_("item found: %s"), item.entity) 186 log.debug(_("item found: %s"), item.entity)
186 _defers.append(client.disco.requestInfo(item.entity)) 187 _defers.append(client.disco.requestInfo(item.entity))
187 dl = defer.DeferredList(_defers) 188 dl = defer.DeferredList(_defers)
188 dl.addCallback(self._infosReceived, items=disco._items, target=target, client=client) 189 dl.addCallback(self._infosReceived, items=disco._items, target=target, client=client)
189 reactor.callLater(GATEWAY_TIMEOUT, dl.cancel) 190 reactor.callLater(GATEWAY_TIMEOUT, dl.cancel)
190 return dl 191 return dl
203 204
204 def findGateways(self, target, profile): 205 def findGateways(self, target, profile):
205 """Find gateways in the target JID, using discovery protocol 206 """Find gateways in the target JID, using discovery protocol
206 """ 207 """
207 client = self.host.getClient(profile) 208 client = self.host.getClient(profile)
208 debug(_("find gateways (target = %(target)s, profile = %(profile)s)") % {'target': target.full(), 'profile': profile}) 209 log.debug(_("find gateways (target = %(target)s, profile = %(profile)s)") % {'target': target.full(), 'profile': profile})
209 d = client.disco.requestItems(target) 210 d = client.disco.requestItems(target)
210 d.addCallback(self._itemsReceived , target=target, client=client) 211 d.addCallback(self._itemsReceived , target=target, client=client)
211 return d 212 return d