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 contents (XEP 0166)
|
|
|
|
"""
|
2009-11-15 20:47:06 +01:00
|
|
|
|
2013-12-04 18:43:28 +01:00
|
|
|
import os
|
2013-01-02 13:54:02 +01:00
|
|
|
from common import gajim
|
2012-12-09 21:37:51 +01:00
|
|
|
import nbxmpp
|
2013-01-02 13:54:02 +01:00
|
|
|
from common.jingle_transport import JingleTransportIBB
|
2013-12-04 18:43:28 +01:00
|
|
|
from jingle_xtls import SELF_SIGNED_CERTIFICATE
|
|
|
|
from jingle_xtls import load_cert_file
|
2009-12-20 13:37:56 +01:00
|
|
|
|
2009-11-15 20:47:06 +01:00
|
|
|
contents = {}
|
|
|
|
|
|
|
|
def get_jingle_content(node):
|
2010-02-08 15:08:40 +01:00
|
|
|
namespace = node.getNamespace()
|
|
|
|
if namespace in contents:
|
|
|
|
return contents[namespace](node)
|
2009-11-15 20:47:06 +01:00
|
|
|
|
|
|
|
|
2009-12-01 22:39:02 +01:00
|
|
|
class JingleContentSetupException(Exception):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Exception that should be raised when a content fails to setup.
|
|
|
|
"""
|
2009-12-01 22:15:50 +01:00
|
|
|
|
|
|
|
|
2009-11-15 20:47:06 +01:00
|
|
|
class JingleContent(object):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
An abstraction of content in Jingle sessions
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, session, transport):
|
|
|
|
self.session = session
|
|
|
|
self.transport = transport
|
|
|
|
# will be filled by JingleSession.add_content()
|
|
|
|
# don't uncomment these lines, we will catch more buggy code then
|
|
|
|
# (a JingleContent not added to session shouldn't send anything)
|
|
|
|
#self.creator = None
|
|
|
|
#self.name = None
|
|
|
|
self.accepted = False
|
|
|
|
self.sent = False
|
|
|
|
self.negotiated = False
|
|
|
|
|
|
|
|
self.media = None
|
|
|
|
|
|
|
|
self.senders = 'both' #FIXME
|
|
|
|
self.allow_sending = True # Used for stream direction, attribute 'senders'
|
|
|
|
|
|
|
|
self.callbacks = {
|
|
|
|
# these are called when *we* get stanzas
|
2010-08-26 16:56:08 +02:00
|
|
|
'content-accept': [self.__on_transport_info,
|
|
|
|
self.__on_content_accept],
|
2010-02-08 15:08:40 +01:00
|
|
|
'content-add': [self.__on_transport_info],
|
|
|
|
'content-modify': [],
|
|
|
|
'content-reject': [],
|
|
|
|
'content-remove': [],
|
|
|
|
'description-info': [],
|
|
|
|
'security-info': [],
|
2010-08-26 16:56:08 +02:00
|
|
|
'session-accept': [self.__on_transport_info,
|
|
|
|
self.__on_content_accept],
|
2010-02-08 15:08:40 +01:00
|
|
|
'session-info': [],
|
|
|
|
'session-initiate': [self.__on_transport_info],
|
|
|
|
'session-terminate': [],
|
|
|
|
'transport-info': [self.__on_transport_info],
|
2011-06-29 00:31:07 +02:00
|
|
|
'transport-replace': [self.__on_transport_replace],
|
2010-02-08 15:08:40 +01:00
|
|
|
'transport-accept': [],
|
|
|
|
'transport-reject': [],
|
|
|
|
'iq-result': [],
|
|
|
|
'iq-error': [],
|
|
|
|
# these are called when *we* sent these stanzas
|
2010-08-26 16:56:08 +02:00
|
|
|
'content-accept-sent': [self.__fill_jingle_stanza,
|
|
|
|
self.__on_content_accept],
|
2010-02-08 15:08:40 +01:00
|
|
|
'content-add-sent': [self.__fill_jingle_stanza],
|
|
|
|
'session-initiate-sent': [self.__fill_jingle_stanza],
|
2010-08-26 16:56:08 +02:00
|
|
|
'session-accept-sent': [self.__fill_jingle_stanza,
|
|
|
|
self.__on_content_accept],
|
2010-02-08 15:08:40 +01:00
|
|
|
'session-terminate-sent': [],
|
|
|
|
}
|
|
|
|
|
|
|
|
def is_ready(self):
|
|
|
|
return self.accepted and not self.sent
|
|
|
|
|
2010-08-26 16:56:08 +02:00
|
|
|
def __on_content_accept(self, stanza, content, error, action):
|
|
|
|
self.on_negotiated()
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def on_negotiated(self):
|
|
|
|
if self.accepted:
|
|
|
|
self.negotiated = True
|
|
|
|
self.session.content_negotiated(self.media)
|
|
|
|
|
|
|
|
def add_remote_candidates(self, candidates):
|
|
|
|
"""
|
|
|
|
Add a list of candidates to the list of remote candidates
|
|
|
|
"""
|
2011-07-22 22:15:34 +02:00
|
|
|
self.transport.remote_candidates = candidates
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def on_stanza(self, stanza, content, error, action):
|
|
|
|
"""
|
|
|
|
Called when something related to our content was sent by peer
|
|
|
|
"""
|
|
|
|
if action in self.callbacks:
|
|
|
|
for callback in self.callbacks[action]:
|
|
|
|
callback(stanza, content, error, action)
|
|
|
|
|
2011-06-29 00:31:07 +02:00
|
|
|
def __on_transport_replace(self, stanza, content, error, action):
|
|
|
|
content.addChild(node=self.transport.make_transport())
|
2011-10-29 06:09:45 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def __on_transport_info(self, stanza, content, error, action):
|
|
|
|
"""
|
|
|
|
Got a new transport candidate
|
|
|
|
"""
|
|
|
|
candidates = self.transport.parse_transport_stanza(
|
2011-10-29 06:09:45 +02:00
|
|
|
content.getTag('transport'))
|
2010-02-08 15:08:40 +01:00
|
|
|
if candidates:
|
|
|
|
self.add_remote_candidates(candidates)
|
|
|
|
|
|
|
|
def __content(self, payload=[]):
|
|
|
|
"""
|
|
|
|
Build a XML content-wrapper for our data
|
|
|
|
"""
|
2012-12-09 21:37:51 +01:00
|
|
|
return nbxmpp.Node('content',
|
2010-02-08 15:08:40 +01:00
|
|
|
attrs={'name': self.name, 'creator': self.creator},
|
|
|
|
payload=payload)
|
|
|
|
|
|
|
|
def send_candidate(self, candidate):
|
|
|
|
"""
|
|
|
|
Send a transport candidate for a previously defined transport.
|
|
|
|
"""
|
|
|
|
content = self.__content()
|
|
|
|
content.addChild(node=self.transport.make_transport([candidate]))
|
|
|
|
self.session.send_transport_info(content)
|
2011-10-29 06:09:45 +02:00
|
|
|
|
2011-07-05 20:05:16 +02:00
|
|
|
def send_error_candidate(self):
|
|
|
|
"""
|
|
|
|
Sends a candidate-error when we can't connect to a candidate.
|
|
|
|
"""
|
|
|
|
content = self.__content()
|
2011-07-23 00:00:29 +02:00
|
|
|
tp = self.transport.make_transport(add_candidates=False)
|
2011-07-05 20:05:16 +02:00
|
|
|
tp.addChild(name='candidate-error')
|
|
|
|
content.addChild(node=tp)
|
|
|
|
self.session.send_transport_info(content)
|
2011-10-29 06:09:45 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2010-05-04 10:31:05 +02:00
|
|
|
def send_description_info(self):
|
|
|
|
content = self.__content()
|
|
|
|
self._fill_content(content)
|
|
|
|
self.session.send_description_info(content)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def __fill_jingle_stanza(self, stanza, content, error, action):
|
|
|
|
"""
|
|
|
|
Add our things to session-initiate stanza
|
|
|
|
"""
|
|
|
|
self._fill_content(content)
|
|
|
|
self.sent = True
|
|
|
|
content.addChild(node=self.transport.make_transport())
|
|
|
|
|
2012-05-29 01:54:59 +02:00
|
|
|
def _fill_content(self, content):
|
2012-12-09 21:37:51 +01:00
|
|
|
description_node = nbxmpp.simplexml.Node(
|
|
|
|
tag=nbxmpp.NS_JINGLE_FILE_TRANSFER + ' description')
|
2012-05-28 03:05:43 +02:00
|
|
|
if self.session.werequest:
|
2012-12-09 21:37:51 +01:00
|
|
|
simode = nbxmpp.simplexml.Node(tag='request')
|
2012-05-28 03:05:43 +02:00
|
|
|
else:
|
2012-12-09 21:37:51 +01:00
|
|
|
simode = nbxmpp.simplexml.Node(tag='offer')
|
|
|
|
file_tag = simode.setTag('file', namespace=nbxmpp.NS_FILE)
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props.name:
|
2012-12-09 21:37:51 +01:00
|
|
|
node = nbxmpp.simplexml.Node(tag='name')
|
2012-06-14 18:27:23 +02:00
|
|
|
node.addData(self.file_props.name)
|
2012-05-28 03:05:43 +02:00
|
|
|
file_tag.addChild(node=node)
|
2012-08-31 19:54:40 +02:00
|
|
|
if self.file_props.date:
|
2012-12-09 21:37:51 +01:00
|
|
|
node = nbxmpp.simplexml.Node(tag='date')
|
2012-08-31 19:54:40 +02:00
|
|
|
node.addData(self.file_props.date)
|
|
|
|
file_tag.addChild(node=node)
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props.size:
|
2012-12-09 21:37:51 +01:00
|
|
|
node = nbxmpp.simplexml.Node(tag='size')
|
2012-06-14 18:27:23 +02:00
|
|
|
node.addData(self.file_props.size)
|
2012-05-28 03:05:43 +02:00
|
|
|
file_tag.addChild(node=node)
|
2012-08-29 16:12:14 +02:00
|
|
|
if self.file_props.type_ == 'r':
|
|
|
|
if self.file_props.hash_:
|
|
|
|
h = file_tag.addChild('hash', attrs={
|
2012-12-09 21:37:51 +01:00
|
|
|
'algo': self.file_props.algo}, namespace=nbxmpp.NS_HASHES,
|
2012-08-29 16:12:14 +02:00
|
|
|
payload=self.file_props.hash_)
|
|
|
|
else:
|
|
|
|
# if the file is less than 10 mb, then it is small
|
|
|
|
# lets calculate it right away
|
2013-11-02 09:00:16 +01:00
|
|
|
if self.file_props.size < 10000000 and not \
|
2012-08-29 21:06:04 +02:00
|
|
|
self.file_props.hash_:
|
2012-08-29 16:12:14 +02:00
|
|
|
h = self._calcHash()
|
|
|
|
file_tag.addChild(node=h)
|
2012-09-22 19:25:55 +02:00
|
|
|
pjid = gajim.get_jid_without_resource(self.session.peerjid)
|
2012-08-30 02:31:27 +02:00
|
|
|
file_info = {'name' : self.file_props.name,
|
2012-09-02 18:59:55 +02:00
|
|
|
'file-name' : self.file_props.file_name,
|
2012-08-30 02:31:27 +02:00
|
|
|
'hash' : self.file_props.hash_,
|
|
|
|
'size' : self.file_props.size,
|
2012-09-22 19:25:55 +02:00
|
|
|
'date' : self.file_props.date,
|
|
|
|
'peerjid' : pjid
|
2012-08-30 02:31:27 +02:00
|
|
|
}
|
2012-09-23 04:16:03 +02:00
|
|
|
self.session.connection.set_file_info(file_info)
|
2012-05-28 03:05:43 +02:00
|
|
|
desc = file_tag.setTag('desc')
|
2012-06-14 18:27:23 +02:00
|
|
|
if self.file_props.desc:
|
|
|
|
desc.setData(self.file_props.desc)
|
2012-05-28 03:05:43 +02:00
|
|
|
description_node.addChild(node=simode)
|
|
|
|
if self.use_security:
|
2012-12-09 21:37:51 +01:00
|
|
|
security = nbxmpp.simplexml.Node(
|
|
|
|
tag=nbxmpp.NS_JINGLE_XTLS + ' security')
|
2013-12-04 18:43:28 +01:00
|
|
|
certpath = os.path.join(gajim.MY_CERT_DIR, SELF_SIGNED_CERTIFICATE)\
|
|
|
|
+ '.cert'
|
|
|
|
cert = load_cert_file(certpath)
|
|
|
|
if cert:
|
|
|
|
digest_algo = cert.get_signature_algorithm().split('With')[0]
|
|
|
|
security.addChild('fingerprint').addData(cert.digest(
|
|
|
|
digest_algo))
|
|
|
|
for m in ('x509', ): # supported authentication methods
|
|
|
|
method = nbxmpp.simplexml.Node(tag='method')
|
|
|
|
method.setAttr('name', m)
|
|
|
|
security.addChild(node=method)
|
|
|
|
content.addChild(node=security)
|
2012-05-28 03:05:43 +02:00
|
|
|
content.addChild(node=description_node)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
def destroy(self):
|
|
|
|
self.callbacks = None
|
|
|
|
del self.session.contents[(self.creator, self.name)]
|
2012-08-30 02:31:27 +02:00
|
|
|
|
|
|
|
|