2009-11-15 20:47:06 +01:00
|
|
|
##
|
|
|
|
## Copyright (C) 2006 Gajim Team
|
|
|
|
##
|
2013-08-16 09:33:15 +02:00
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2009-11-15 20:47:06 +01:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2013-08-16 09:33:15 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2009-11-15 20:47:06 +01:00
|
|
|
##
|
2013-08-16 09:33:15 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2009-11-15 20:47:06 +01:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2013-08-16 09:33:15 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2009-11-15 20:47:06 +01:00
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2013-08-16 09:33:15 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2009-11-26 12:58:12 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
Handles Jingle sessions (XEP 0166)
|
|
|
|
"""
|
2009-11-15 20:47:06 +01:00
|
|
|
|
|
|
|
#TODO:
|
|
|
|
# * 'senders' attribute of 'content' element
|
|
|
|
# * security preconditions
|
|
|
|
# * actions:
|
|
|
|
# - content-modify
|
2009-11-29 22:45:34 +01:00
|
|
|
# - session-info
|
2009-11-15 20:47:06 +01:00
|
|
|
# - security-info
|
|
|
|
# - transport-accept, transport-reject
|
|
|
|
# - Tie-breaking
|
|
|
|
# * timeout
|
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
import logging
|
2017-03-04 21:22:46 +01:00
|
|
|
from enum import Enum, unique
|
2012-12-09 21:37:51 +01:00
|
|
|
import nbxmpp
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.jingle_transport import get_jingle_transport, JingleTransportIBB
|
|
|
|
from gajim.common.jingle_content import get_jingle_content, JingleContentSetupException, JingleContent
|
|
|
|
from gajim.common.jingle_ft import State
|
|
|
|
from gajim.common.connection_handlers_events import (
|
2017-02-08 03:49:33 +01:00
|
|
|
FilesProp, JingleRequestReceivedEvent, JingleDisconnectedReceivedEvent,
|
|
|
|
JingleTransferCancelledEvent, JingleConnectedReceivedEvent,
|
|
|
|
JingleErrorReceivedEvent)
|
|
|
|
|
2017-08-13 13:18:56 +02:00
|
|
|
log = logging.getLogger("app.c.jingle_session")
|
2009-11-15 20:47:06 +01:00
|
|
|
|
2009-11-26 12:58:12 +01:00
|
|
|
# FIXME: Move it to JingleSession.States?
|
2017-03-04 21:22:46 +01:00
|
|
|
@unique
|
2017-02-08 03:49:33 +01:00
|
|
|
class JingleStates(Enum):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
States in which jingle session may exist
|
|
|
|
"""
|
2017-02-08 03:49:33 +01:00
|
|
|
ENDED = 0
|
|
|
|
PENDING = 1
|
|
|
|
ACTIVE = 2
|
2009-11-15 20:47:06 +01:00
|
|
|
|
|
|
|
class OutOfOrder(Exception):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Exception that should be raised when an action is received when in the wrong
|
|
|
|
state
|
|
|
|
"""
|
2009-11-15 20:47:06 +01:00
|
|
|
|
|
|
|
class TieBreak(Exception):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Exception that should be raised in case of a tie, when we overrule the other
|
|
|
|
action
|
|
|
|
"""
|
2009-11-15 20:47:06 +01:00
|
|
|
|
2016-01-03 22:26:15 +01:00
|
|
|
class FailedApplication(Exception):
|
|
|
|
"""
|
2017-02-08 03:49:33 +01:00
|
|
|
Exception that should be raised in case responder supports none of the
|
|
|
|
payload-types offered by the initiator
|
2016-01-03 22:26:15 +01:00
|
|
|
"""
|
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
class JingleSession:
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
This represents one jingle session, that is, one or more content types
|
|
|
|
negotiated between an initiator and a responder.
|
|
|
|
"""
|
|
|
|
|
2012-06-10 23:08:54 +02:00
|
|
|
def __init__(self, con, weinitiate, jid, iq_id=None, sid=None,
|
2017-02-08 03:49:33 +01:00
|
|
|
werequest=False):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
con -- connection object,
|
|
|
|
weinitiate -- boolean, are we the initiator?
|
|
|
|
jid - jid of the other entity
|
|
|
|
"""
|
|
|
|
self.contents = {} # negotiated contents
|
|
|
|
self.connection = con # connection to use
|
|
|
|
# our full jid
|
2017-08-13 13:18:56 +02:00
|
|
|
self.ourjid = app.get_jid_from_account(self.connection.name)
|
2010-03-14 22:12:10 +01:00
|
|
|
if con.server_resource:
|
|
|
|
self.ourjid = self.ourjid + '/' + con.server_resource
|
2010-02-08 15:08:40 +01:00
|
|
|
self.peerjid = jid # jid we connect to
|
|
|
|
# jid we use as the initiator
|
2017-02-08 03:49:33 +01:00
|
|
|
self.initiator = self.ourjid if weinitiate else self.peerjid
|
2010-02-08 15:08:40 +01:00
|
|
|
# jid we use as the responder
|
2017-02-08 03:49:33 +01:00
|
|
|
self.responder = self.peerjid if weinitiate else self.ourjid
|
2010-02-08 15:08:40 +01:00
|
|
|
# are we an initiator?
|
|
|
|
self.weinitiate = weinitiate
|
2012-05-28 03:05:43 +02:00
|
|
|
# Are we requesting or offering a file?
|
|
|
|
self.werequest = werequest
|
2012-09-06 01:39:07 +02:00
|
|
|
self.request = False
|
2010-02-08 15:08:40 +01:00
|
|
|
# what state is session in? (one from JingleStates)
|
2017-02-08 03:49:33 +01:00
|
|
|
self.state = JingleStates.ENDED
|
2010-02-08 15:08:40 +01:00
|
|
|
if not sid:
|
|
|
|
sid = con.connection.getAnID()
|
|
|
|
self.sid = sid # sessionid
|
2010-07-03 10:22:47 +02:00
|
|
|
# iq stanza id, used to determine which sessions to summon callback
|
|
|
|
# later on when iq-result stanza arrives
|
|
|
|
if iq_id is not None:
|
|
|
|
self.iq_ids = [iq_id]
|
|
|
|
else:
|
|
|
|
self.iq_ids = []
|
2010-02-08 15:08:40 +01:00
|
|
|
self.accepted = True # is this session accepted by user
|
2012-03-22 05:56:11 +01:00
|
|
|
# Tells whether this session is a file transfer or not
|
2017-02-08 03:49:33 +01:00
|
|
|
self.session_type_ft = False
|
2010-02-08 15:08:40 +01:00
|
|
|
# callbacks to call on proper contents
|
|
|
|
# use .prepend() to add new callbacks, especially when you're going
|
|
|
|
# to send error instead of ack
|
|
|
|
self.callbacks = {
|
2017-02-08 03:49:33 +01:00
|
|
|
'content-accept': [self.__ack, self.__on_content_accept,
|
|
|
|
self.__broadcast],
|
|
|
|
'content-add': [self.__ack,
|
|
|
|
self.__on_content_add, self.__broadcast
|
|
|
|
], #TODO
|
|
|
|
'content-modify': [self.__ack], #TODO
|
|
|
|
'content-reject': [self.__ack, self.__on_content_remove],
|
|
|
|
'content-remove': [self.__ack, self.__on_content_remove],
|
|
|
|
'description-info': [self.__ack, self.__broadcast], #TODO
|
|
|
|
'security-info': [self.__ack], #TODO
|
|
|
|
'session-accept': [self.__ack, self.__on_session_accept,
|
|
|
|
self.__on_content_accept,
|
|
|
|
self.__broadcast],
|
|
|
|
'session-info': [self.__ack, self.__broadcast,
|
|
|
|
self.__on_session_info],
|
|
|
|
'session-initiate': [self.__ack, self.__on_session_initiate,
|
|
|
|
self.__broadcast],
|
|
|
|
'session-terminate': [self.__ack, self.__on_session_terminate,
|
|
|
|
self.__broadcast_all],
|
|
|
|
'transport-info': [self.__ack, self.__broadcast],
|
|
|
|
'transport-replace': [self.__ack, self.__broadcast,
|
|
|
|
self.__on_transport_replace], #TODO
|
|
|
|
'transport-accept': [self.__ack], #TODO
|
|
|
|
'transport-reject': [self.__ack], #TODO
|
|
|
|
'iq-result': [self.__broadcast],
|
|
|
|
'iq-error': [self.__on_error],
|
2010-02-08 15:08:40 +01:00
|
|
|
}
|
|
|
|
|
2010-07-03 10:22:47 +02:00
|
|
|
def collect_iq_id(self, iq_id):
|
|
|
|
if iq_id is not None:
|
|
|
|
self.iq_ids.append(iq_id)
|
2010-08-26 10:36:58 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def approve_session(self):
|
|
|
|
"""
|
|
|
|
Called when user accepts session in UI (when we aren't the initiator)
|
|
|
|
"""
|
|
|
|
self.accept_session()
|
|
|
|
|
|
|
|
def decline_session(self):
|
|
|
|
"""
|
|
|
|
Called when user declines session in UI (when we aren't the initiator)
|
|
|
|
"""
|
2012-12-09 21:37:51 +01:00
|
|
|
reason = nbxmpp.Node('reason')
|
2010-02-08 15:08:40 +01:00
|
|
|
reason.addChild('decline')
|
|
|
|
self._session_terminate(reason)
|
2012-12-09 21:37:51 +01:00
|
|
|
|
2012-03-19 20:52:33 +01:00
|
|
|
def cancel_session(self):
|
|
|
|
"""
|
|
|
|
Called when user declines session in UI (when we aren't the initiator)
|
|
|
|
"""
|
2012-12-09 21:37:51 +01:00
|
|
|
reason = nbxmpp.Node('reason')
|
2012-03-19 20:52:33 +01:00
|
|
|
reason.addChild('cancel')
|
|
|
|
self._session_terminate(reason)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-08-26 11:09:35 +02:00
|
|
|
def approve_content(self, media, name=None):
|
|
|
|
content = self.get_content(media, name)
|
2010-02-08 15:08:40 +01:00
|
|
|
if content:
|
|
|
|
content.accepted = True
|
|
|
|
self.on_session_state_changed(content)
|
|
|
|
|
2010-08-26 11:09:35 +02:00
|
|
|
def reject_content(self, media, name=None):
|
|
|
|
content = self.get_content(media, name)
|
2010-02-08 15:08:40 +01:00
|
|
|
if content:
|
2017-02-08 03:49:33 +01:00
|
|
|
if self.state == JingleStates.ACTIVE:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.__content_reject(content)
|
|
|
|
content.destroy()
|
|
|
|
self.on_session_state_changed()
|
|
|
|
|
|
|
|
def end_session(self):
|
|
|
|
"""
|
|
|
|
Called when user stops or cancel session in UI
|
|
|
|
"""
|
2012-12-09 21:37:51 +01:00
|
|
|
reason = nbxmpp.Node('reason')
|
2017-02-08 03:49:33 +01:00
|
|
|
if self.state == JingleStates.ACTIVE:
|
2010-02-08 15:08:40 +01:00
|
|
|
reason.addChild('success')
|
|
|
|
else:
|
|
|
|
reason.addChild('cancel')
|
|
|
|
self._session_terminate(reason)
|
|
|
|
|
2010-08-26 11:09:35 +02:00
|
|
|
def get_content(self, media=None, name=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
if media is None:
|
|
|
|
return
|
|
|
|
for content in self.contents.values():
|
|
|
|
if content.media == media:
|
2010-08-26 11:09:35 +02:00
|
|
|
if name is None or content.name == name:
|
|
|
|
return content
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def add_content(self, name, content, creator='we'):
|
|
|
|
"""
|
|
|
|
Add new content to session. If the session is active, this will send
|
|
|
|
proper stanza to update session
|
|
|
|
|
|
|
|
Creator must be one of ('we', 'peer', 'initiator', 'responder')
|
|
|
|
"""
|
|
|
|
assert creator in ('we', 'peer', 'initiator', 'responder')
|
|
|
|
if (creator == 'we' and self.weinitiate) or (creator == 'peer' and \
|
|
|
|
not self.weinitiate):
|
|
|
|
creator = 'initiator'
|
|
|
|
elif (creator == 'peer' and self.weinitiate) or (creator == 'we' and \
|
|
|
|
not self.weinitiate):
|
|
|
|
creator = 'responder'
|
|
|
|
content.creator = creator
|
|
|
|
content.name = name
|
|
|
|
self.contents[(creator, name)] = content
|
|
|
|
if (creator == 'initiator') == self.weinitiate:
|
|
|
|
# The content is from us, accept it
|
|
|
|
content.accepted = True
|
|
|
|
|
2010-05-04 10:04:53 +02:00
|
|
|
def remove_content(self, creator, name, reason=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2010-05-04 10:04:53 +02:00
|
|
|
Remove the content `name` created by `creator`
|
|
|
|
by sending content-remove, or by sending session-terminate if
|
|
|
|
there is no content left.
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
if (creator, name) in self.contents:
|
|
|
|
content = self.contents[(creator, name)]
|
2010-08-17 18:22:25 +02:00
|
|
|
self.__content_remove(content, reason)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.contents[(creator, name)].destroy()
|
2010-05-04 10:04:53 +02:00
|
|
|
if not self.contents:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.end_session()
|
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
def modify_content(self, creator, name, transport=None):
|
2012-12-09 21:37:51 +01:00
|
|
|
'''
|
2011-06-29 00:31:07 +02:00
|
|
|
Currently used for transport replacement
|
|
|
|
'''
|
2017-02-08 03:49:33 +01:00
|
|
|
content = self.contents[(creator, name)]
|
2011-06-29 00:31:07 +02:00
|
|
|
transport.set_sid(content.transport.sid)
|
|
|
|
transport.set_file_props(content.transport.file_props)
|
|
|
|
content.transport = transport
|
|
|
|
# The content will have to be resend now that it is modified
|
|
|
|
content.sent = False
|
|
|
|
content.accepted = True
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def on_session_state_changed(self, content=None):
|
2017-02-08 03:49:33 +01:00
|
|
|
if self.state == JingleStates.ENDED:
|
2010-02-08 15:08:40 +01:00
|
|
|
# Session not yet started, only one action possible: session-initiate
|
|
|
|
if self.is_ready() and self.weinitiate:
|
|
|
|
self.__session_initiate()
|
2017-02-08 03:49:33 +01:00
|
|
|
elif self.state == JingleStates.PENDING:
|
2010-02-08 15:08:40 +01:00
|
|
|
# We can either send a session-accept or a content-add
|
|
|
|
if self.is_ready() and not self.weinitiate:
|
|
|
|
self.__session_accept()
|
|
|
|
elif content and (content.creator == 'initiator') == self.weinitiate:
|
|
|
|
self.__content_add(content)
|
|
|
|
elif content and self.weinitiate:
|
|
|
|
self.__content_accept(content)
|
2017-02-08 03:49:33 +01:00
|
|
|
elif self.state == JingleStates.ACTIVE:
|
2011-12-27 03:18:45 +01:00
|
|
|
# We can either send a content-add or a content-accept. However, if
|
|
|
|
# we are sending a file we can only use session_initiate.
|
2010-02-08 15:08:40 +01:00
|
|
|
if not content:
|
|
|
|
return
|
2011-12-27 03:18:45 +01:00
|
|
|
we_created_content = (content.creator == 'initiator') \
|
|
|
|
== self.weinitiate
|
|
|
|
if we_created_content and content.media == 'file':
|
|
|
|
self.__session_initiate()
|
|
|
|
if we_created_content:
|
2010-02-08 15:08:40 +01:00
|
|
|
# We initiated this content. It's a pending content-add.
|
|
|
|
self.__content_add(content)
|
|
|
|
else:
|
|
|
|
# The other side created this content, we accept it.
|
|
|
|
self.__content_accept(content)
|
|
|
|
|
|
|
|
def is_ready(self):
|
|
|
|
"""
|
|
|
|
Return True when all codecs and candidates are ready (for all contents)
|
|
|
|
"""
|
2013-01-05 09:52:09 +01:00
|
|
|
return (any((content.is_ready() for content in self.contents.values()))
|
2017-02-08 03:49:33 +01:00
|
|
|
and self.accepted)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def accept_session(self):
|
|
|
|
"""
|
|
|
|
Mark the session as accepted
|
|
|
|
"""
|
|
|
|
self.accepted = True
|
|
|
|
self.on_session_state_changed()
|
|
|
|
|
|
|
|
def start_session(self):
|
|
|
|
"""
|
|
|
|
Mark the session as ready to be started
|
|
|
|
"""
|
|
|
|
self.accepted = True
|
|
|
|
self.on_session_state_changed()
|
|
|
|
|
|
|
|
def send_session_info(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def send_content_accept(self, content):
|
2017-02-08 03:49:33 +01:00
|
|
|
assert self.state != JingleStates.ENDED
|
2010-02-08 15:08:40 +01:00
|
|
|
stanza, jingle = self.__make_jingle('content-accept')
|
|
|
|
jingle.addChild(node=content)
|
|
|
|
self.connection.connection.send(stanza)
|
|
|
|
|
|
|
|
def send_transport_info(self, content):
|
2017-02-08 03:49:33 +01:00
|
|
|
assert self.state != JingleStates.ENDED
|
2010-02-08 15:08:40 +01:00
|
|
|
stanza, jingle = self.__make_jingle('transport-info')
|
|
|
|
jingle.addChild(node=content)
|
|
|
|
self.connection.connection.send(stanza)
|
2010-07-15 07:38:53 +02:00
|
|
|
self.collect_iq_id(stanza.getID())
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-05-04 10:31:05 +02:00
|
|
|
def send_description_info(self, content):
|
2017-02-08 03:49:33 +01:00
|
|
|
assert self.state != JingleStates.ENDED
|
2010-05-04 10:31:05 +02:00
|
|
|
stanza, jingle = self.__make_jingle('description-info')
|
|
|
|
jingle.addChild(node=content)
|
|
|
|
self.connection.connection.send(stanza)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def on_stanza(self, stanza):
|
|
|
|
"""
|
|
|
|
A callback for ConnectionJingle. It gets stanza, then tries to send it to
|
|
|
|
all internally registered callbacks. First one to raise
|
2012-12-09 21:37:51 +01:00
|
|
|
nbxmpp.NodeProcessed breaks function
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
jingle = stanza.getTag('jingle')
|
|
|
|
error = stanza.getTag('error')
|
|
|
|
if error:
|
|
|
|
# it's an iq-error stanza
|
|
|
|
action = 'iq-error'
|
|
|
|
elif jingle:
|
|
|
|
# it's a jingle action
|
|
|
|
action = jingle.getAttr('action')
|
|
|
|
if action not in self.callbacks:
|
2010-04-21 20:25:27 +02:00
|
|
|
self.__send_error(stanza, 'bad-request')
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
# FIXME: If we aren't initiated and it's not a session-initiate...
|
2017-02-08 03:49:33 +01:00
|
|
|
if action not in ['session-initiate', 'session-terminate'] \
|
|
|
|
and self.state == JingleStates.ENDED:
|
2010-02-08 15:08:40 +01:00
|
|
|
self.__send_error(stanza, 'item-not-found', 'unknown-session')
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
# it's an iq-result (ack) stanza
|
|
|
|
action = 'iq-result'
|
|
|
|
callables = self.callbacks[action]
|
|
|
|
try:
|
2012-08-31 05:09:23 +02:00
|
|
|
for call in callables:
|
|
|
|
call(stanza=stanza, jingle=jingle, error=error, action=action)
|
2012-12-09 21:37:51 +01:00
|
|
|
except nbxmpp.NodeProcessed:
|
2010-02-08 15:08:40 +01:00
|
|
|
pass
|
|
|
|
except TieBreak:
|
|
|
|
self.__send_error(stanza, 'conflict', 'tiebreak')
|
|
|
|
except OutOfOrder:
|
|
|
|
# FIXME
|
|
|
|
self.__send_error(stanza, 'unexpected-request', 'out-of-order')
|
2016-01-03 22:26:15 +01:00
|
|
|
except FailedApplication:
|
|
|
|
reason = nbxmpp.Node('reason')
|
|
|
|
reason.addChild('failed-application')
|
|
|
|
self._session_terminate(reason)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __ack(self, stanza, jingle, error, action):
|
|
|
|
"""
|
|
|
|
Default callback for action stanzas -- simple ack and stop processing
|
|
|
|
"""
|
|
|
|
response = stanza.buildReply('result')
|
2011-12-29 14:08:35 +01:00
|
|
|
response.delChild(response.getQuery())
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.connection.send(response)
|
|
|
|
|
|
|
|
def __on_error(self, stanza, jingle, error, action):
|
|
|
|
# FIXME
|
|
|
|
text = error.getTagData('text')
|
2010-07-01 18:30:35 +02:00
|
|
|
error_name = None
|
2010-02-08 15:08:40 +01:00
|
|
|
for child in error.getChildren():
|
2012-12-09 21:37:51 +01:00
|
|
|
if child.getNamespace() == nbxmpp.NS_JINGLE_ERRORS:
|
2010-07-01 18:30:35 +02:00
|
|
|
error_name = child.getName()
|
|
|
|
break
|
2012-12-09 21:37:51 +01:00
|
|
|
elif child.getNamespace() == nbxmpp.NS_STANZAS:
|
2010-07-01 18:30:35 +02:00
|
|
|
error_name = child.getName()
|
2010-08-26 10:36:58 +02:00
|
|
|
self.__dispatch_error(error_name, text, error.getAttr('type'))
|
2012-08-25 02:13:25 +02:00
|
|
|
|
2011-06-29 00:31:07 +02:00
|
|
|
def transport_replace(self):
|
|
|
|
transport = JingleTransportIBB()
|
|
|
|
# For debug only, delete this and replace for a function
|
|
|
|
# that will identify contents by its sid
|
2017-02-08 03:49:33 +01:00
|
|
|
for creator, name in self.contents:
|
2011-06-29 00:31:07 +02:00
|
|
|
self.modify_content(creator, name, transport)
|
|
|
|
cont = self.contents[(creator, name)]
|
|
|
|
cont.transport = transport
|
|
|
|
stanza, jingle = self.__make_jingle('transport-replace')
|
|
|
|
self.__append_contents(jingle)
|
|
|
|
self.__broadcast(stanza, jingle, None, 'transport-replace')
|
|
|
|
self.connection.connection.send(stanza)
|
2017-02-08 03:49:33 +01:00
|
|
|
self.state = JingleStates.PENDING
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __on_transport_replace(self, stanza, jingle, error, action):
|
|
|
|
for content in jingle.iterTags('content'):
|
|
|
|
creator = content['creator']
|
|
|
|
name = content['name']
|
|
|
|
if (creator, name) in self.contents:
|
|
|
|
transport_ns = content.getTag('transport').getNamespace()
|
2012-12-09 21:37:51 +01:00
|
|
|
if transport_ns == nbxmpp.NS_JINGLE_ICE_UDP:
|
2010-02-08 15:08:40 +01:00
|
|
|
# FIXME: We don't manage anything else than ICE-UDP now...
|
|
|
|
# What was the previous transport?!?
|
|
|
|
# Anyway, content's transport is not modifiable yet
|
|
|
|
pass
|
2012-12-09 21:37:51 +01:00
|
|
|
elif transport_ns == nbxmpp.NS_JINGLE_IBB:
|
2011-06-29 00:31:07 +02:00
|
|
|
transport = JingleTransportIBB()
|
|
|
|
self.modify_content(creator, name, transport)
|
2017-02-08 03:49:33 +01:00
|
|
|
self.state = JingleStates.PENDING
|
|
|
|
self.contents[(creator, name)].state = State.TRANSPORT_REPLACE
|
2011-06-29 00:31:07 +02:00
|
|
|
self.__ack(stanza, jingle, error, action)
|
|
|
|
self.__session_accept()
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
stanza, jingle = self.__make_jingle('transport-reject')
|
|
|
|
content = jingle.setTag('content', attrs={'creator': creator,
|
2017-02-08 03:49:33 +01:00
|
|
|
'name': name})
|
2010-02-08 15:08:40 +01:00
|
|
|
content.setTag('transport', namespace=transport_ns)
|
|
|
|
self.connection.connection.send(stanza)
|
2012-12-09 21:37:51 +01:00
|
|
|
raise nbxmpp.NodeProcessed
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2018-06-22 00:47:29 +02:00
|
|
|
# FIXME: This resource is unknown to us, what should we do?
|
2010-02-08 15:08:40 +01:00
|
|
|
# For now, reject the transport
|
|
|
|
stanza, jingle = self.__make_jingle('transport-reject')
|
2017-02-08 03:49:33 +01:00
|
|
|
content = jingle.setTag('content', attrs={'creator': creator,
|
|
|
|
'name': name})
|
|
|
|
content.setTag('transport', namespace=transport_ns)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.connection.connection.send(stanza)
|
2012-12-09 21:37:51 +01:00
|
|
|
raise nbxmpp.NodeProcessed
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __on_session_info(self, stanza, jingle, error, action):
|
2016-01-03 20:43:32 +01:00
|
|
|
# TODO: active, (un)hold, (un)mute
|
|
|
|
payload = jingle.getPayload()
|
|
|
|
if payload[0].getName() == 'ringing':
|
|
|
|
# ignore ringing
|
|
|
|
raise nbxmpp.NodeProcessed
|
2017-02-08 03:49:33 +01:00
|
|
|
if self.state != JingleStates.ACTIVE:
|
2012-08-25 02:13:25 +02:00
|
|
|
raise OutOfOrder
|
2017-02-08 03:49:33 +01:00
|
|
|
for child in payload:
|
|
|
|
if child.getName() == 'checksum':
|
|
|
|
hash_ = child.getTag('file').getTag(name='hash',
|
2017-06-14 21:29:52 +02:00
|
|
|
namespace=nbxmpp.NS_HASHES_2)
|
2012-05-29 01:54:59 +02:00
|
|
|
algo = hash_.getAttr('algo')
|
2017-06-14 21:29:52 +02:00
|
|
|
if algo in nbxmpp.Hashes2.supported:
|
2012-08-25 02:13:25 +02:00
|
|
|
file_props = FilesProp.getFileProp(self.connection.name,
|
|
|
|
self.sid)
|
|
|
|
file_props.algo = algo
|
|
|
|
file_props.hash_ = hash_.getData()
|
2012-12-09 21:37:51 +01:00
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
self.__send_error(stanza, 'feature-not-implemented', 'unsupported-info',
|
2012-08-25 02:13:25 +02:00
|
|
|
type_='modify')
|
2012-12-09 21:37:51 +01:00
|
|
|
raise nbxmpp.NodeProcessed
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __on_content_remove(self, stanza, jingle, error, action):
|
|
|
|
for content in jingle.iterTags('content'):
|
|
|
|
creator = content['creator']
|
|
|
|
name = content['name']
|
|
|
|
if (creator, name) in self.contents:
|
|
|
|
content = self.contents[(creator, name)]
|
|
|
|
# TODO: this will fail if content is not an RTP content
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(JingleDisconnectedReceivedEvent(
|
2010-10-29 16:49:53 +02:00
|
|
|
None, conn=self.connection, jingle_session=self,
|
|
|
|
media=content.media, reason='removed'))
|
2010-02-08 15:08:40 +01:00
|
|
|
content.destroy()
|
|
|
|
if not self.contents:
|
2012-12-09 21:37:51 +01:00
|
|
|
reason = nbxmpp.Node('reason')
|
2010-02-08 15:08:40 +01:00
|
|
|
reason.setTag('success')
|
|
|
|
self._session_terminate(reason)
|
|
|
|
|
|
|
|
def __on_session_accept(self, stanza, jingle, error, action):
|
|
|
|
# FIXME
|
2017-02-08 03:49:33 +01:00
|
|
|
if self.state != JingleStates.PENDING:
|
2010-02-08 15:08:40 +01:00
|
|
|
raise OutOfOrder
|
2017-02-08 03:49:33 +01:00
|
|
|
self.state = JingleStates.ACTIVE
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
@staticmethod
|
|
|
|
def __on_content_accept(stanza, jingle, error, action):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Called when we get content-accept stanza or equivalent one (like
|
|
|
|
session-accept)
|
|
|
|
"""
|
|
|
|
# check which contents are accepted
|
|
|
|
for content in jingle.iterTags('content'):
|
|
|
|
creator = content['creator']
|
|
|
|
# TODO
|
|
|
|
name = content['name']
|
|
|
|
|
|
|
|
def __on_content_add(self, stanza, jingle, error, action):
|
2017-02-08 03:49:33 +01:00
|
|
|
if self.state == JingleStates.ENDED:
|
2010-02-08 15:08:40 +01:00
|
|
|
raise OutOfOrder
|
|
|
|
parse_result = self.__parse_contents(jingle)
|
|
|
|
contents = parse_result[0]
|
|
|
|
rejected_contents = parse_result[1]
|
2017-08-24 23:20:35 +02:00
|
|
|
# for name, creator in rejected_contents:
|
2010-02-08 15:08:40 +01:00
|
|
|
# TODO
|
2017-08-24 23:20:35 +02:00
|
|
|
# content = JingleContent()
|
|
|
|
# self.add_content(name, content, creator)
|
|
|
|
# self.__content_reject(content)
|
|
|
|
# self.contents[(content.creator, content.name)].destroy()
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(JingleRequestReceivedEvent(None,
|
2017-02-08 03:49:33 +01:00
|
|
|
conn=self.connection,
|
|
|
|
jingle_session=self,
|
|
|
|
contents=contents))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __on_session_initiate(self, stanza, jingle, error, action):
|
|
|
|
"""
|
|
|
|
We got a jingle session request from other entity, therefore we are the
|
|
|
|
receiver... Unpack the data, inform the user
|
|
|
|
"""
|
2017-02-08 03:49:33 +01:00
|
|
|
if self.state != JingleStates.ENDED:
|
2010-02-08 15:08:40 +01:00
|
|
|
raise OutOfOrder
|
|
|
|
self.initiator = jingle['initiator']
|
|
|
|
self.responder = self.ourjid
|
|
|
|
self.peerjid = self.initiator
|
|
|
|
self.accepted = False # user did not accept this session yet
|
|
|
|
# TODO: If the initiator is unknown to the receiver (e.g., via presence
|
|
|
|
# subscription) and the receiver has a policy of not communicating via
|
|
|
|
# Jingle with unknown entities, it SHOULD return a <service-unavailable/>
|
|
|
|
# error.
|
|
|
|
# Lets check what kind of jingle session does the peer want
|
2010-03-15 19:25:27 +01:00
|
|
|
contents, contents_rejected, reason_txt = self.__parse_contents(jingle)
|
2018-04-29 23:30:08 +02:00
|
|
|
|
|
|
|
# If there's no content we understand...
|
|
|
|
if not contents:
|
|
|
|
# TODO: http://xmpp.org/extensions/xep-0166.html#session-terminate
|
|
|
|
reason = nbxmpp.Node('reason')
|
|
|
|
reason.setTag(reason_txt)
|
|
|
|
self.__ack(stanza, jingle, error, action)
|
|
|
|
self._session_terminate(reason)
|
|
|
|
raise nbxmpp.NodeProcessed
|
|
|
|
|
2018-06-22 00:47:29 +02:00
|
|
|
# If we are not receiving a file
|
2011-12-28 05:47:00 +01:00
|
|
|
# Check if there's already a session with this user:
|
2017-08-07 17:41:32 +02:00
|
|
|
if contents[0].media != 'file':
|
2011-12-28 05:47:00 +01:00
|
|
|
for session in self.connection.iter_jingle_sessions(self.peerjid):
|
2017-02-08 13:07:12 +01:00
|
|
|
if session is not self:
|
2012-12-09 21:37:51 +01:00
|
|
|
reason = nbxmpp.Node('reason')
|
2011-12-28 05:47:00 +01:00
|
|
|
alternative_session = reason.setTag('alternative-session')
|
|
|
|
alternative_session.setTagData('sid', session.sid)
|
|
|
|
self.__ack(stanza, jingle, error, action)
|
|
|
|
self._session_terminate(reason)
|
2012-12-09 21:37:51 +01:00
|
|
|
raise nbxmpp.NodeProcessed
|
2012-08-29 21:06:04 +02:00
|
|
|
else:
|
2012-09-22 19:25:55 +02:00
|
|
|
# Stop if we don't have the requested file or the peer is not
|
|
|
|
# allowed to request the file
|
2017-08-07 17:41:32 +02:00
|
|
|
request = contents[0].senders == 'responder'
|
2012-08-29 21:06:04 +02:00
|
|
|
if request:
|
2012-09-06 01:39:07 +02:00
|
|
|
self.request = True
|
2017-02-08 03:49:33 +01:00
|
|
|
hash_tag = request.getTag('file').getTag('hash')
|
|
|
|
hash_data = hash_tag.getData() if hash_tag else None
|
2012-08-29 21:06:04 +02:00
|
|
|
n = request.getTag('file').getTag('name')
|
2012-10-10 04:48:01 +02:00
|
|
|
n = n.getData() if n else None
|
2017-08-13 13:18:56 +02:00
|
|
|
pjid = app.get_jid_without_resource(self.peerjid)
|
2017-02-08 03:49:33 +01:00
|
|
|
file_info = self.connection.get_file_info(pjid, hash_data, n,
|
|
|
|
self.connection.name)
|
2012-09-27 02:46:09 +02:00
|
|
|
if not file_info:
|
2017-02-08 03:49:33 +01:00
|
|
|
log.warning('The peer %s is requesting a ' \
|
|
|
|
'file that we dont have or ' \
|
|
|
|
'it is not allowed to request', pjid)
|
2012-08-30 02:31:27 +02:00
|
|
|
self.decline_session()
|
2012-12-09 21:37:51 +01:00
|
|
|
raise nbxmpp.NodeProcessed
|
2018-04-29 23:30:08 +02:00
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
self.state = JingleStates.PENDING
|
2010-02-08 15:08:40 +01:00
|
|
|
# Send event about starting a session
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(JingleRequestReceivedEvent(None,
|
2017-02-08 03:49:33 +01:00
|
|
|
conn=self.connection,
|
|
|
|
jingle_session=self,
|
2017-08-07 17:41:32 +02:00
|
|
|
contents=contents[0]))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __broadcast(self, stanza, jingle, error, action):
|
|
|
|
"""
|
|
|
|
Broadcast the stanza contents to proper content handlers
|
|
|
|
"""
|
2010-07-03 10:22:47 +02:00
|
|
|
#if jingle is None: # it is a iq-result stanza
|
|
|
|
# for cn in self.contents.values():
|
|
|
|
# cn.on_stanza(stanza, None, error, action)
|
|
|
|
# return
|
|
|
|
# special case: iq-result stanza does not come with a jingle element
|
|
|
|
if action == 'iq-result':
|
|
|
|
for cn in self.contents.values():
|
|
|
|
cn.on_stanza(stanza, None, error, action)
|
|
|
|
return
|
2010-02-08 15:08:40 +01:00
|
|
|
for content in jingle.iterTags('content'):
|
|
|
|
name = content['name']
|
|
|
|
creator = content['creator']
|
2010-05-03 15:17:02 +02:00
|
|
|
if (creator, name) not in self.contents:
|
|
|
|
text = 'Content %s (created by %s) does not exist' % (name, creator)
|
|
|
|
self.__send_error(stanza, 'bad-request', text=text, type_='_modify')
|
2012-12-09 21:37:51 +01:00
|
|
|
raise nbxmpp.NodeProcessed
|
2010-05-03 15:17:02 +02:00
|
|
|
else:
|
|
|
|
cn = self.contents[(creator, name)]
|
|
|
|
cn.on_stanza(stanza, content, error, action)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __on_session_terminate(self, stanza, jingle, error, action):
|
2010-03-14 21:51:04 +01:00
|
|
|
self.connection.delete_jingle_session(self.sid)
|
2010-02-08 15:08:40 +01:00
|
|
|
reason, text = self.__reason_from_stanza(jingle)
|
|
|
|
if reason not in ('success', 'cancel', 'decline'):
|
2010-07-01 18:30:35 +02:00
|
|
|
self.__dispatch_error(reason, text)
|
2010-02-08 15:08:40 +01:00
|
|
|
if text:
|
|
|
|
text = '%s (%s)' % (reason, text)
|
|
|
|
else:
|
|
|
|
# TODO
|
|
|
|
text = reason
|
2017-02-08 03:49:33 +01:00
|
|
|
if reason == 'cancel' and self.session_type_ft:
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(JingleTransferCancelledEvent(None,
|
2017-02-08 03:49:33 +01:00
|
|
|
conn=self.connection,
|
|
|
|
jingle_session=self,
|
|
|
|
media=None,
|
|
|
|
reason=text))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __broadcast_all(self, stanza, jingle, error, action):
|
|
|
|
"""
|
|
|
|
Broadcast the stanza to all content handlers
|
|
|
|
"""
|
2013-01-05 09:52:09 +01:00
|
|
|
for content in self.contents.values():
|
2010-02-08 15:08:40 +01:00
|
|
|
content.on_stanza(stanza, None, error, action)
|
|
|
|
|
|
|
|
def __parse_contents(self, jingle):
|
|
|
|
# TODO: Needs some reworking
|
|
|
|
contents = []
|
|
|
|
contents_rejected = []
|
|
|
|
reasons = set()
|
|
|
|
for element in jingle.iterTags('content'):
|
2017-08-07 17:41:32 +02:00
|
|
|
senders = element.getAttr('senders')
|
2010-02-08 15:08:40 +01:00
|
|
|
transport = get_jingle_transport(element.getTag('transport'))
|
2010-06-19 16:52:17 +02:00
|
|
|
if transport:
|
|
|
|
transport.ourjid = self.ourjid
|
2010-02-08 15:08:40 +01:00
|
|
|
content_type = get_jingle_content(element.getTag('description'))
|
|
|
|
if content_type:
|
|
|
|
try:
|
|
|
|
if transport:
|
2018-04-06 23:34:26 +02:00
|
|
|
content = content_type(self, transport=transport)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.add_content(element['name'],
|
2017-02-08 03:49:33 +01:00
|
|
|
content, 'peer')
|
2017-08-07 17:41:32 +02:00
|
|
|
contents.append(content)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
reasons.add('unsupported-transports')
|
|
|
|
contents_rejected.append((element['name'], 'peer'))
|
|
|
|
except JingleContentSetupException:
|
|
|
|
reasons.add('failed-application')
|
|
|
|
else:
|
|
|
|
contents_rejected.append((element['name'], 'peer'))
|
2010-03-14 15:59:01 +01:00
|
|
|
reasons.add('unsupported-applications')
|
2010-02-08 15:08:40 +01:00
|
|
|
failure_reason = None
|
|
|
|
# Store the first reason of failure
|
|
|
|
for reason in ('failed-application', 'unsupported-transports',
|
2017-02-08 03:49:33 +01:00
|
|
|
'unsupported-applications'):
|
2010-02-08 15:08:40 +01:00
|
|
|
if reason in reasons:
|
|
|
|
failure_reason = reason
|
|
|
|
break
|
|
|
|
return (contents, contents_rejected, failure_reason)
|
|
|
|
|
2010-07-01 18:30:35 +02:00
|
|
|
def __dispatch_error(self, error=None, text=None, type_=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
if text:
|
|
|
|
text = '%s (%s)' % (error, text)
|
2010-07-01 18:30:35 +02:00
|
|
|
if type_ != 'modify':
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(JingleErrorReceivedEvent(None,
|
2017-02-08 03:49:33 +01:00
|
|
|
conn=self.connection,
|
|
|
|
jingle_session=self,
|
|
|
|
reason=text or error))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
@staticmethod
|
|
|
|
def __reason_from_stanza(stanza):
|
2010-03-15 21:34:28 +01:00
|
|
|
# TODO: Move to GUI?
|
2010-02-08 15:08:40 +01:00
|
|
|
reason = 'success'
|
2017-02-08 03:49:33 +01:00
|
|
|
reasons = [
|
|
|
|
'success', 'busy', 'cancel', 'connectivity-error', 'decline',
|
|
|
|
'expired', 'failed-application', 'failed-transport',
|
|
|
|
'general-error', 'gone', 'incompatible-parameters', 'media-error',
|
|
|
|
'security-error', 'timeout', 'unsupported-applications',
|
|
|
|
'unsupported-transports'
|
|
|
|
]
|
2010-02-08 15:08:40 +01:00
|
|
|
tag = stanza.getTag('reason')
|
2010-08-18 08:35:32 +02:00
|
|
|
text = ''
|
2010-02-08 15:08:40 +01:00
|
|
|
if tag:
|
|
|
|
text = tag.getTagData('text')
|
|
|
|
for r in reasons:
|
|
|
|
if tag.getTag(r):
|
|
|
|
reason = r
|
|
|
|
break
|
|
|
|
return (reason, text)
|
|
|
|
|
2010-05-04 10:04:53 +02:00
|
|
|
def __make_jingle(self, action, reason=None):
|
2012-12-09 21:37:51 +01:00
|
|
|
stanza = nbxmpp.Iq(typ='set', to=nbxmpp.JID(self.peerjid),
|
2017-02-08 03:49:33 +01:00
|
|
|
frm=self.ourjid)
|
|
|
|
attrs = {
|
|
|
|
'action': action,
|
|
|
|
'sid': self.sid,
|
|
|
|
'initiator' : self.initiator
|
|
|
|
}
|
2012-12-09 21:37:51 +01:00
|
|
|
jingle = stanza.addChild('jingle', attrs=attrs,
|
2017-02-08 03:49:33 +01:00
|
|
|
namespace=nbxmpp.NS_JINGLE)
|
2010-05-04 10:04:53 +02:00
|
|
|
if reason is not None:
|
|
|
|
jingle.addChild(node=reason)
|
2010-02-08 15:08:40 +01:00
|
|
|
return stanza, jingle
|
|
|
|
|
2010-04-21 20:25:27 +02:00
|
|
|
def __send_error(self, stanza, error, jingle_error=None, text=None, type_=None):
|
2012-12-09 21:37:51 +01:00
|
|
|
err_stanza = nbxmpp.Error(stanza, '%s %s' % (nbxmpp.NS_STANZAS, error))
|
2010-04-21 20:25:27 +02:00
|
|
|
err = err_stanza.getTag('error')
|
|
|
|
if type_:
|
|
|
|
err.setAttr('type', type_)
|
2010-02-08 15:08:40 +01:00
|
|
|
if jingle_error:
|
2012-12-09 21:37:51 +01:00
|
|
|
err.setTag(jingle_error, namespace=nbxmpp.NS_JINGLE_ERRORS)
|
2010-02-08 15:08:40 +01:00
|
|
|
if text:
|
|
|
|
err.setTagData('text', text)
|
2010-04-21 20:25:27 +02:00
|
|
|
self.connection.connection.send(err_stanza)
|
2010-07-01 18:30:35 +02:00
|
|
|
self.__dispatch_error(jingle_error or error, text, type_)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
@staticmethod
|
|
|
|
def __append_content(jingle, content):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2017-08-07 17:41:32 +02:00
|
|
|
Append <content/> element to <jingle/> element
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
jingle.addChild('content',
|
2017-02-08 03:49:33 +01:00
|
|
|
attrs={'name': content.name,
|
2017-08-07 17:41:32 +02:00
|
|
|
'creator': content.creator,
|
|
|
|
'senders': content.senders})
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __append_contents(self, jingle):
|
|
|
|
"""
|
|
|
|
Append all <content/> elements to <jingle/>
|
|
|
|
"""
|
|
|
|
# TODO: integrate with __appendContent?
|
|
|
|
# TODO: parameters 'name', 'content'?
|
|
|
|
for content in self.contents.values():
|
2010-08-26 13:18:16 +02:00
|
|
|
if content.is_ready():
|
|
|
|
self.__append_content(jingle, content)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __session_initiate(self):
|
2017-02-08 03:49:33 +01:00
|
|
|
assert self.state == JingleStates.ENDED
|
2010-02-08 15:08:40 +01:00
|
|
|
stanza, jingle = self.__make_jingle('session-initiate')
|
|
|
|
self.__append_contents(jingle)
|
|
|
|
self.__broadcast(stanza, jingle, None, 'session-initiate-sent')
|
|
|
|
self.connection.connection.send(stanza)
|
2010-07-03 10:22:47 +02:00
|
|
|
self.collect_iq_id(stanza.getID())
|
2017-02-08 03:49:33 +01:00
|
|
|
self.state = JingleStates.PENDING
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __session_accept(self):
|
2017-02-08 03:49:33 +01:00
|
|
|
assert self.state == JingleStates.PENDING
|
2010-02-08 15:08:40 +01:00
|
|
|
stanza, jingle = self.__make_jingle('session-accept')
|
|
|
|
self.__append_contents(jingle)
|
|
|
|
self.__broadcast(stanza, jingle, None, 'session-accept-sent')
|
|
|
|
self.connection.connection.send(stanza)
|
2010-07-03 10:22:47 +02:00
|
|
|
self.collect_iq_id(stanza.getID())
|
2017-02-08 03:49:33 +01:00
|
|
|
self.state = JingleStates.ACTIVE
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __session_info(self, payload=None):
|
2017-02-08 03:49:33 +01:00
|
|
|
assert self.state != JingleStates.ENDED
|
2010-02-08 15:08:40 +01:00
|
|
|
stanza, jingle = self.__make_jingle('session-info')
|
|
|
|
if payload:
|
|
|
|
jingle.addChild(node=payload)
|
|
|
|
self.connection.connection.send(stanza)
|
2012-12-09 21:37:51 +01:00
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
def _JingleFileTransfer__session_info(self, payload):
|
2012-01-16 01:37:00 +01:00
|
|
|
# For some strange reason when I call
|
2017-02-08 03:49:33 +01:00
|
|
|
# self.session.__session_info(payload) from the jingleFileTransfer object
|
2012-01-16 01:37:00 +01:00
|
|
|
# within a thread, this method gets called instead. Even though, it
|
|
|
|
# isn't being called explicitly.
|
2017-02-08 03:49:33 +01:00
|
|
|
self.__session_info(payload)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _session_terminate(self, reason=None):
|
2010-05-04 10:04:53 +02:00
|
|
|
stanza, jingle = self.__make_jingle('session-terminate', reason=reason)
|
2010-02-08 15:08:40 +01:00
|
|
|
self.__broadcast_all(stanza, jingle, None, 'session-terminate-sent')
|
2010-06-16 10:13:23 +02:00
|
|
|
if self.connection.connection and self.connection.connected >= 2:
|
|
|
|
self.connection.connection.send(stanza)
|
2010-03-15 21:34:28 +01:00
|
|
|
# TODO: Move to GUI?
|
2010-02-08 15:08:40 +01:00
|
|
|
reason, text = self.__reason_from_stanza(jingle)
|
|
|
|
if reason not in ('success', 'cancel', 'decline'):
|
2010-07-01 18:30:35 +02:00
|
|
|
self.__dispatch_error(reason, text)
|
2010-02-08 15:08:40 +01:00
|
|
|
if text:
|
|
|
|
text = '%s (%s)' % (reason, text)
|
|
|
|
else:
|
|
|
|
text = reason
|
2010-03-14 21:51:04 +01:00
|
|
|
self.connection.delete_jingle_session(self.sid)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(JingleDisconnectedReceivedEvent(None,
|
2017-02-08 03:49:33 +01:00
|
|
|
conn=self.connection,
|
|
|
|
jingle_session=self,
|
|
|
|
media=None,
|
|
|
|
reason=text))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __content_add(self, content):
|
|
|
|
# TODO: test
|
2017-02-08 03:49:33 +01:00
|
|
|
assert self.state != JingleStates.ENDED
|
2010-02-08 15:08:40 +01:00
|
|
|
stanza, jingle = self.__make_jingle('content-add')
|
|
|
|
self.__append_content(jingle, content)
|
|
|
|
self.__broadcast(stanza, jingle, None, 'content-add-sent')
|
2010-08-26 10:36:58 +02:00
|
|
|
id_ = self.connection.connection.send(stanza)
|
|
|
|
self.collect_iq_id(id_)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __content_accept(self, content):
|
|
|
|
# TODO: test
|
2017-02-08 03:49:33 +01:00
|
|
|
assert self.state != JingleStates.ENDED
|
2010-02-08 15:08:40 +01:00
|
|
|
stanza, jingle = self.__make_jingle('content-accept')
|
|
|
|
self.__append_content(jingle, content)
|
|
|
|
self.__broadcast(stanza, jingle, None, 'content-accept-sent')
|
2010-08-26 10:36:58 +02:00
|
|
|
id_ = self.connection.connection.send(stanza)
|
|
|
|
self.collect_iq_id(id_)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __content_reject(self, content):
|
2017-02-08 03:49:33 +01:00
|
|
|
assert self.state != JingleStates.ENDED
|
2010-02-08 15:08:40 +01:00
|
|
|
stanza, jingle = self.__make_jingle('content-reject')
|
|
|
|
self.__append_content(jingle, content)
|
|
|
|
self.connection.connection.send(stanza)
|
|
|
|
# TODO: this will fail if content is not an RTP content
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(JingleDisconnectedReceivedEvent(None,
|
2017-02-08 03:49:33 +01:00
|
|
|
conn=self.connection,
|
|
|
|
jingle_session=self,
|
|
|
|
media=content.media,
|
|
|
|
reason='rejected'))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def __content_modify(self):
|
2017-02-08 03:49:33 +01:00
|
|
|
assert self.state != JingleStates.ENDED
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-05-04 10:04:53 +02:00
|
|
|
def __content_remove(self, content, reason=None):
|
2017-02-08 03:49:33 +01:00
|
|
|
assert self.state != JingleStates.ENDED
|
2011-10-12 21:07:02 +02:00
|
|
|
if self.connection.connection and self.connection.connected > 1:
|
|
|
|
stanza, jingle = self.__make_jingle('content-remove', reason=reason)
|
|
|
|
self.__append_content(jingle, content)
|
|
|
|
self.connection.connection.send(stanza)
|
2010-02-08 15:08:40 +01:00
|
|
|
# TODO: this will fail if content is not an RTP content
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(JingleDisconnectedReceivedEvent(None,
|
2017-02-08 03:49:33 +01:00
|
|
|
conn=self.connection,
|
|
|
|
jingle_session=self,
|
|
|
|
media=content.media,
|
|
|
|
reason='removed'))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def content_negotiated(self, media):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.nec.push_incoming_event(JingleConnectedReceivedEvent(None,
|
2017-02-08 03:49:33 +01:00
|
|
|
conn=self.connection,
|
|
|
|
jingle_session=self,
|
|
|
|
media=media))
|