comparison src/plugins/plugin_xep_0166.py @ 1567:268fda4236ca

plugins XE0166, XEP-0234, XEP-0260, XEP-0261: renamed session key managing other peer's jid to "peer_jid" instead of "to_jid"
author Goffi <goffi@goffi.org>
date Sun, 08 Nov 2015 14:44:33 +0100
parents cbfbe028d099
children 846a39900fa6
comparison
equal deleted inserted replaced
1566:ec3848916ee8 1567:268fda4236ca
113 ## helpers methods to build stanzas ## 113 ## helpers methods to build stanzas ##
114 114
115 def _buildJingleElt(self, client, session, action): 115 def _buildJingleElt(self, client, session, action):
116 iq_elt = client.IQ('set') 116 iq_elt = client.IQ('set')
117 iq_elt['from'] = client.jid.full() 117 iq_elt['from'] = client.jid.full()
118 iq_elt['to'] = session['to_jid'].full() 118 iq_elt['to'] = session['peer_jid'].full()
119 jingle_elt = iq_elt.addElement("jingle", NS_JINGLE) 119 jingle_elt = iq_elt.addElement("jingle", NS_JINGLE)
120 jingle_elt["sid"] = session['id'] 120 jingle_elt["sid"] = session['id']
121 jingle_elt['action'] = action 121 jingle_elt['action'] = action
122 return iq_elt, jingle_elt 122 return iq_elt, jingle_elt
123 123
257 raise exceptions.InternalError(u"unmanaged action {}".format(action)) 257 raise exceptions.InternalError(u"unmanaged action {}".format(action))
258 258
259 return iq_elt, context_elt 259 return iq_elt, context_elt
260 260
261 @defer.inlineCallbacks 261 @defer.inlineCallbacks
262 def initiate(self, to_jid, contents, profile=C.PROF_KEY_NONE): 262 def initiate(self, peer_jid, contents, profile=C.PROF_KEY_NONE):
263 """Send a session initiation request 263 """Send a session initiation request
264 264
265 @param to_jid(jid.JID): jid to establith session with 265 @param peer_jid(jid.JID): jid to establith session with
266 @param contents(list[dict]): list of contents to use: 266 @param contents(list[dict]): list of contents to use:
267 The dict must have the following keys: 267 The dict must have the following keys:
268 - app_ns(unicode): namespace of the application 268 - app_ns(unicode): namespace of the application
269 the following keys are optional: 269 the following keys are optional:
270 - transport_type(unicode): type of transport to use (see XEP-0166 §8) 270 - transport_type(unicode): type of transport to use (see XEP-0166 §8)
283 # TODO: session cleaning after timeout ? 283 # TODO: session cleaning after timeout ?
284 session = client.jingle_sessions[sid] = {'id': sid, 284 session = client.jingle_sessions[sid] = {'id': sid,
285 'state': STATE_PENDING, 285 'state': STATE_PENDING,
286 'initiator': initiator, 286 'initiator': initiator,
287 'role': XEP_0166.ROLE_INITIATOR, 287 'role': XEP_0166.ROLE_INITIATOR,
288 'to_jid': to_jid, 288 'peer_jid': peer_jid,
289 'started': time.time(), 289 'started': time.time(),
290 'contents': {} 290 'contents': {}
291 } 291 }
292 iq_elt, jingle_elt = self._buildJingleElt(client, session, XEP_0166.A_SESSION_INITIATE) 292 iq_elt, jingle_elt = self._buildJingleElt(client, session, XEP_0166.A_SESSION_INITIATE)
293 jingle_elt["initiator"] = initiator.full() 293 jingle_elt["initiator"] = initiator.full()
367 ## defaults methods called when plugin doesn't have them ## 367 ## defaults methods called when plugin doesn't have them ##
368 368
369 def jingleRequestConfirmationDefault(self, action, session, content_name, desc_elt, profile): 369 def jingleRequestConfirmationDefault(self, action, session, content_name, desc_elt, profile):
370 """This method request confirmation for a jingle session""" 370 """This method request confirmation for a jingle session"""
371 log.debug(u"Using generic jingle confirmation method") 371 log.debug(u"Using generic jingle confirmation method")
372 return xml_tools.deferConfirm(self.host, _(CONFIRM_TXT).format(entity=session['to_jid'].full()), _('Confirm Jingle session'), profile=profile) 372 return xml_tools.deferConfirm(self.host, _(CONFIRM_TXT).format(entity=session['peer_jid'].full()), _('Confirm Jingle session'), profile=profile)
373 373
374 ## jingle events ## 374 ## jingle events ##
375 375
376 def _onJingleRequest(self, request, profile): 376 def _onJingleRequest(self, request, profile):
377 """Called when any jingle request is received 377 """Called when any jingle request is received
403 except KeyError: 403 except KeyError:
404 log.warning(u"Received jingle request has no action") 404 log.warning(u"Received jingle request has no action")
405 self.sendError('bad-request', None, request, profile) 405 self.sendError('bad-request', None, request, profile)
406 return 406 return
407 407
408 to_jid = jid.JID(request['from']) 408 peer_jid = jid.JID(request['from'])
409 409
410 # we get or create the session 410 # we get or create the session
411 try: 411 try:
412 session = client.jingle_sessions[sid] 412 session = client.jingle_sessions[sid]
413 except KeyError: 413 except KeyError:
414 session = client.jingle_sessions[sid] = {'id': sid, 414 session = client.jingle_sessions[sid] = {'id': sid,
415 'state': STATE_PENDING, 415 'state': STATE_PENDING,
416 'initiator': to_jid, 416 'initiator': peer_jid,
417 'role': XEP_0166.ROLE_RESPONDER, 417 'role': XEP_0166.ROLE_RESPONDER,
418 'to_jid': to_jid, 418 'peer_jid': peer_jid,
419 'started': time.time(), 419 'started': time.time(),
420 } 420 }
421 else: 421 else:
422 if session['to_jid'] != to_jid: 422 if session['peer_jid'] != peer_jid:
423 log.warning(u"sid conflict ({}), the jid doesn't match. Can be a collision, a hack attempt, or a bad sid generation".format(sid)) 423 log.warning(u"sid conflict ({}), the jid doesn't match. Can be a collision, a hack attempt, or a bad sid generation".format(sid))
424 self.sendError('service-unavailable', sid, request, profile) 424 self.sendError('service-unavailable', sid, request, profile)
425 return 425 return
426 if session['id'] != sid: 426 if session['id'] != sid:
427 log.error(u"session id doesn't match") 427 log.error(u"session id doesn't match")