2018-09-05 02:59:34 +02:00
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published
|
|
|
|
# by the Free Software Foundation; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# 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 the jingle signalling protocol
|
|
|
|
"""
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
#TODO:
|
2009-09-16 13:55:54 +02:00
|
|
|
# * things in XEP 0176, including:
|
|
|
|
# - http://xmpp.org/extensions/xep-0176.html#protocol-restarts
|
|
|
|
# - http://xmpp.org/extensions/xep-0176.html#fallback
|
|
|
|
# * XEP 0177 (raw udp)
|
|
|
|
|
2009-09-13 11:02:49 +02:00
|
|
|
# * UI:
|
|
|
|
# - make state and codec informations available to the user
|
2009-09-18 20:17:35 +02:00
|
|
|
# - video integration
|
2009-09-13 11:02:49 +02:00
|
|
|
# * config:
|
|
|
|
# - codecs
|
2009-09-18 20:17:35 +02:00
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
import logging
|
|
|
|
|
2012-12-09 21:37:51 +01:00
|
|
|
import nbxmpp
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import helpers
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2007-08-06 17:53:01 +02:00
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.jingle_session import JingleSession, JingleStates
|
|
|
|
from gajim.common.jingle_ft import JingleFileTransfer
|
|
|
|
from gajim.common.jingle_transport import JingleTransportSocks5, JingleTransportIBB
|
2018-04-24 19:36:33 +02:00
|
|
|
if app.is_installed('FARSTREAM'):
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common.jingle_rtp import JingleAudio, JingleVideo
|
2009-09-13 11:02:49 +02:00
|
|
|
|
2010-06-06 17:22:52 +02:00
|
|
|
logger = logging.getLogger('gajim.c.jingle')
|
2009-09-17 23:36:26 +02:00
|
|
|
|
2017-02-08 03:49:33 +01:00
|
|
|
|
2018-09-16 11:56:56 +02:00
|
|
|
class ConnectionJingle:
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
This object depends on that it is a part of Connection class.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2010-03-14 21:51:04 +01:00
|
|
|
# dictionary: sessionid => JingleSession object
|
2010-10-27 01:16:05 +02:00
|
|
|
self._sessions = {}
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# dictionary: (jid, iq stanza id) => JingleSession object,
|
|
|
|
# one time callbacks
|
|
|
|
self.__iq_responses = {}
|
2012-08-29 21:06:04 +02:00
|
|
|
self.files = []
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-03-14 21:51:04 +01:00
|
|
|
def delete_jingle_session(self, sid):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Remove a jingle session from a jingle stanza dispatcher
|
|
|
|
"""
|
2010-10-27 01:16:05 +02:00
|
|
|
if sid in self._sessions:
|
2010-02-08 15:08:40 +01:00
|
|
|
#FIXME: Move this elsewhere?
|
2014-03-25 21:26:57 +01:00
|
|
|
for content in list(self._sessions[sid].contents.values()):
|
2010-02-08 15:08:40 +01:00
|
|
|
content.destroy()
|
2010-10-27 01:16:05 +02:00
|
|
|
self._sessions[sid].callbacks = []
|
|
|
|
del self._sessions[sid]
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def _JingleCB(self, con, stanza):
|
|
|
|
"""
|
|
|
|
The jingle stanza dispatcher
|
|
|
|
|
|
|
|
Route jingle stanza to proper JingleSession object, or create one if it
|
|
|
|
is a new session.
|
|
|
|
|
|
|
|
TODO: Also check if the stanza isn't an error stanza, if so route it
|
2018-06-21 01:46:16 +02:00
|
|
|
adequately.
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
# get data
|
2013-08-13 14:37:56 +02:00
|
|
|
try:
|
|
|
|
jid = helpers.get_full_jid_from_iq(stanza)
|
|
|
|
except helpers.InvalidFormat:
|
2017-02-08 03:49:33 +01:00
|
|
|
logger.warning('Invalid JID: %s, ignoring it', stanza.getFrom())
|
2013-08-13 14:37:56 +02:00
|
|
|
return
|
2012-01-08 20:48:03 +01:00
|
|
|
id_ = stanza.getID()
|
|
|
|
if (jid, id_) in self.__iq_responses.keys():
|
|
|
|
self.__iq_responses[(jid, id_)].on_stanza(stanza)
|
|
|
|
del self.__iq_responses[(jid, id_)]
|
2012-12-09 21:37:51 +01:00
|
|
|
raise nbxmpp.NodeProcessed
|
2010-02-08 15:08:40 +01:00
|
|
|
jingle = stanza.getTag('jingle')
|
2010-07-03 10:22:47 +02:00
|
|
|
# a jingle element is not necessary in iq-result stanza
|
|
|
|
# don't check for that
|
|
|
|
if jingle:
|
|
|
|
sid = jingle.getAttr('sid')
|
|
|
|
else:
|
|
|
|
sid = None
|
2011-06-20 23:03:06 +02:00
|
|
|
for sesn in self._sessions.values():
|
2012-01-08 20:48:03 +01:00
|
|
|
if id_ in sesn.iq_ids:
|
2010-07-03 10:22:47 +02:00
|
|
|
sesn.on_stanza(stanza)
|
|
|
|
return
|
2010-02-08 15:08:40 +01:00
|
|
|
# do we need to create a new jingle object
|
2010-10-27 01:16:05 +02:00
|
|
|
if sid not in self._sessions:
|
2010-02-08 15:08:40 +01:00
|
|
|
#TODO: tie-breaking and other things...
|
2012-06-10 23:08:54 +02:00
|
|
|
newjingle = JingleSession(con=self, weinitiate=False, jid=jid,
|
2017-02-08 03:49:33 +01:00
|
|
|
iq_id=id_, sid=sid)
|
2010-10-27 01:16:05 +02:00
|
|
|
self._sessions[sid] = newjingle
|
2010-02-08 15:08:40 +01:00
|
|
|
# we already have such session in dispatcher...
|
2012-01-08 20:48:03 +01:00
|
|
|
self._sessions[sid].collect_iq_id(id_)
|
2010-10-27 01:16:05 +02:00
|
|
|
self._sessions[sid].on_stanza(stanza)
|
2010-03-21 21:45:45 +01:00
|
|
|
# Delete invalid/unneeded sessions
|
2012-01-08 20:48:03 +01:00
|
|
|
if sid in self._sessions and \
|
2017-02-08 03:49:33 +01:00
|
|
|
self._sessions[sid].state == JingleStates.ENDED:
|
2010-03-21 21:45:45 +01:00
|
|
|
self.delete_jingle_session(sid)
|
2012-12-09 21:37:51 +01:00
|
|
|
raise nbxmpp.NodeProcessed
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def start_audio(self, jid):
|
|
|
|
if self.get_jingle_session(jid, media='audio'):
|
|
|
|
return self.get_jingle_session(jid, media='audio').sid
|
|
|
|
jingle = self.get_jingle_session(jid, media='video')
|
|
|
|
if jingle:
|
|
|
|
jingle.add_content('voice', JingleAudio(jingle))
|
|
|
|
else:
|
|
|
|
jingle = JingleSession(self, weinitiate=True, jid=jid)
|
2010-10-27 01:16:05 +02:00
|
|
|
self._sessions[jingle.sid] = jingle
|
2010-02-08 15:08:40 +01:00
|
|
|
jingle.add_content('voice', JingleAudio(jingle))
|
|
|
|
jingle.start_session()
|
|
|
|
return jingle.sid
|
|
|
|
|
2012-12-30 18:59:48 +01:00
|
|
|
def start_video(self, jid, in_xid, out_xid):
|
2010-02-08 15:08:40 +01:00
|
|
|
if self.get_jingle_session(jid, media='video'):
|
|
|
|
return self.get_jingle_session(jid, media='video').sid
|
|
|
|
jingle = self.get_jingle_session(jid, media='audio')
|
|
|
|
if jingle:
|
2012-12-30 18:59:48 +01:00
|
|
|
jingle.add_content('video', JingleVideo(jingle, in_xid=in_xid,
|
2017-02-08 03:49:33 +01:00
|
|
|
out_xid=out_xid))
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
jingle = JingleSession(self, weinitiate=True, jid=jid)
|
2010-10-27 01:16:05 +02:00
|
|
|
self._sessions[jingle.sid] = jingle
|
2012-12-30 18:59:48 +01:00
|
|
|
jingle.add_content('video', JingleVideo(jingle, in_xid=in_xid,
|
2017-02-08 03:49:33 +01:00
|
|
|
out_xid=out_xid))
|
2010-02-08 15:08:40 +01:00
|
|
|
jingle.start_session()
|
|
|
|
return jingle.sid
|
|
|
|
|
2012-05-28 03:05:43 +02:00
|
|
|
def start_file_transfer(self, jid, file_props, request=False):
|
2017-02-08 03:49:33 +01:00
|
|
|
logger.info("start file transfer with file: %s", file_props)
|
2017-08-13 13:18:56 +02:00
|
|
|
contact = app.contacts.get_contact_with_highest_priority(self.name,
|
|
|
|
app.get_jid_without_resource(jid))
|
|
|
|
if app.contacts.is_gc_contact(self.name, jid):
|
2012-06-17 00:41:33 +02:00
|
|
|
gcc = jid.split('/')
|
|
|
|
if len(gcc) == 2:
|
2017-08-13 13:18:56 +02:00
|
|
|
contact = app.contacts.get_gc_contact(self.name, gcc[0], gcc[1])
|
2010-07-22 09:05:06 +02:00
|
|
|
if contact is None:
|
|
|
|
return
|
2012-12-09 21:37:51 +01:00
|
|
|
use_security = contact.supports(nbxmpp.NS_JINGLE_XTLS)
|
2012-06-10 23:08:54 +02:00
|
|
|
jingle = JingleSession(self, weinitiate=True, jid=jid, werequest=request)
|
2012-03-22 05:56:11 +01:00
|
|
|
# this is a file transfer
|
2017-04-29 20:39:58 +02:00
|
|
|
jingle.session_type_ft = True
|
2011-12-28 05:47:00 +01:00
|
|
|
self._sessions[jingle.sid] = jingle
|
2012-06-14 18:27:23 +02:00
|
|
|
file_props.sid = jingle.sid
|
2012-12-09 21:37:51 +01:00
|
|
|
if contact.supports(nbxmpp.NS_JINGLE_BYTESTREAM):
|
2012-01-08 20:57:05 +01:00
|
|
|
transport = JingleTransportSocks5()
|
2012-12-09 21:37:51 +01:00
|
|
|
elif contact.supports(nbxmpp.NS_JINGLE_IBB):
|
2012-01-08 20:57:05 +01:00
|
|
|
transport = JingleTransportIBB()
|
2017-08-07 17:41:32 +02:00
|
|
|
|
|
|
|
senders = 'initiator'
|
|
|
|
if request:
|
|
|
|
senders = 'responder'
|
2012-01-08 20:57:05 +01:00
|
|
|
c = JingleFileTransfer(jingle, transport=transport,
|
2017-02-08 03:49:33 +01:00
|
|
|
file_props=file_props,
|
2017-08-07 17:41:32 +02:00
|
|
|
use_security=use_security,
|
|
|
|
senders=senders)
|
2017-08-16 15:27:35 +02:00
|
|
|
file_props.transport_sid = transport.sid
|
2012-09-01 20:42:57 +02:00
|
|
|
file_props.algo = self.__hash_support(contact)
|
2011-12-28 05:47:00 +01:00
|
|
|
jingle.add_content('file' + helpers.get_random_string_16(), c)
|
|
|
|
jingle.start_session()
|
2010-08-25 12:05:14 +02:00
|
|
|
return c.transport.sid
|
|
|
|
|
2012-01-16 01:37:00 +01:00
|
|
|
def __hash_support(self, contact):
|
2017-06-14 21:29:52 +02:00
|
|
|
if contact.supports(nbxmpp.NS_HASHES_2):
|
2017-04-29 01:02:18 +02:00
|
|
|
if contact.supports(nbxmpp.NS_HASHES_BLAKE2B_512):
|
|
|
|
return 'blake2b-512'
|
|
|
|
elif contact.supports(nbxmpp.NS_HASHES_BLAKE2B_256):
|
|
|
|
return 'blake2b-256'
|
|
|
|
elif contact.supports(nbxmpp.NS_HASHES_SHA3_512):
|
|
|
|
return 'sha3-512'
|
|
|
|
elif contact.supports(nbxmpp.NS_HASHES_SHA3_256):
|
|
|
|
return 'sha3-256'
|
|
|
|
elif contact.supports(nbxmpp.NS_HASHES_SHA512):
|
2013-11-02 21:22:41 +01:00
|
|
|
return 'sha-512'
|
2012-12-09 21:37:51 +01:00
|
|
|
elif contact.supports(nbxmpp.NS_HASHES_SHA256):
|
2012-01-16 01:37:00 +01:00
|
|
|
return 'sha-256'
|
|
|
|
return None
|
2010-03-15 21:34:28 +01:00
|
|
|
|
|
|
|
def iter_jingle_sessions(self, jid, sid=None, media=None):
|
|
|
|
if sid:
|
2012-01-08 20:48:03 +01:00
|
|
|
return (session for session in self._sessions.values() if \
|
|
|
|
session.sid == sid)
|
|
|
|
sessions = (session for session in self._sessions.values() if \
|
|
|
|
session.peerjid == jid)
|
2010-03-15 21:34:28 +01:00
|
|
|
if media:
|
2010-08-26 10:36:58 +02:00
|
|
|
if media not in ('audio', 'video', 'file'):
|
2010-03-15 21:34:28 +01:00
|
|
|
return tuple()
|
|
|
|
else:
|
2012-01-08 20:48:03 +01:00
|
|
|
return (session for session in sessions if \
|
|
|
|
session.get_content(media))
|
2010-03-15 21:34:28 +01:00
|
|
|
else:
|
|
|
|
return sessions
|
|
|
|
|
2012-09-23 04:16:03 +02:00
|
|
|
def set_file_info(self, file_):
|
2018-06-21 01:46:16 +02:00
|
|
|
# Saves information about the files we have transferred in case they need
|
2012-08-29 21:06:04 +02:00
|
|
|
# to be requested again.
|
|
|
|
self.files.append(file_)
|
|
|
|
|
2012-10-10 04:48:01 +02:00
|
|
|
def get_file_info(self, peerjid, hash_=None, name=None, account=None):
|
2012-08-29 21:06:04 +02:00
|
|
|
if hash_:
|
2012-09-07 16:01:11 +02:00
|
|
|
for f in self.files: # DEBUG
|
2012-09-18 06:13:31 +02:00
|
|
|
#if f['hash'] == '1294809248109223':
|
2012-09-27 02:46:09 +02:00
|
|
|
if f['hash'] == hash_ and f['peerjid'] == peerjid:
|
2012-08-29 21:06:04 +02:00
|
|
|
return f
|
|
|
|
elif name:
|
|
|
|
for f in self.files:
|
2012-09-27 02:46:09 +02:00
|
|
|
if f['name'] == name and f['peerjid'] == peerjid:
|
2012-08-29 21:06:04 +02:00
|
|
|
return f
|
2010-03-15 21:34:28 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def get_jingle_session(self, jid, sid=None, media=None):
|
|
|
|
if sid:
|
2010-10-27 01:16:05 +02:00
|
|
|
if sid in self._sessions:
|
|
|
|
return self._sessions[sid]
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
return None
|
|
|
|
elif media:
|
2010-08-26 10:36:58 +02:00
|
|
|
if media not in ('audio', 'video', 'file'):
|
2010-02-08 15:08:40 +01:00
|
|
|
return None
|
2010-10-27 01:16:05 +02:00
|
|
|
for session in self._sessions.values():
|
2010-02-08 15:08:40 +01:00
|
|
|
if session.peerjid == jid and session.get_content(media):
|
|
|
|
return session
|
|
|
|
return None
|