Move ConnectionJingle code to module

This commit is contained in:
Philipp Hörist 2019-05-19 21:02:13 +02:00
parent 1675161095
commit 275e719da3
17 changed files with 70 additions and 62 deletions

View File

@ -594,7 +594,7 @@ class ChatControl(ChatControlBase):
self._set_jingle_state('video', state, sid=sid, reason=reason) self._set_jingle_state('video', state, sid=sid, reason=reason)
def _get_audio_content(self): def _get_audio_content(self):
session = app.connections[self.account].get_jingle_session( session = app.connections[self.account].get_module('Jingle').get_jingle_session(
self.contact.get_full_jid(), self.audio_sid) self.contact.get_full_jid(), self.audio_sid)
return session.get_content('audio') return session.get_content('audio')
@ -757,7 +757,7 @@ class ChatControl(ChatControlBase):
return return
setattr(self, jingle_type + '_sid', None) setattr(self, jingle_type + '_sid', None)
setattr(self, jingle_type + '_state', self.JINGLE_STATE_NULL) setattr(self, jingle_type + '_state', self.JINGLE_STATE_NULL)
session = app.connections[self.account].get_jingle_session( session = app.connections[self.account].get_module('Jingle').get_jingle_session(
self.contact.get_full_jid(), sid) self.contact.get_full_jid(), sid)
if session: if session:
content = session.get_content(jingle_type) content = session.get_content(jingle_type)
@ -790,7 +790,7 @@ class ChatControl(ChatControlBase):
in_da = self.xml.get_object('incoming_drawingarea') in_da = self.xml.get_object('incoming_drawingarea')
in_da.realize() in_da.realize()
in_xid = in_da.get_window().get_xid() in_xid = in_da.get_window().get_xid()
sid = app.connections[self.account].start_video( sid = app.connections[self.account].get_module('Jingle').start_video(
self.contact.get_full_jid(), in_xid, out_xid) self.contact.get_full_jid(), in_xid, out_xid)
else: else:
sid = getattr(app.connections[self.account], sid = getattr(app.connections[self.account],

View File

@ -189,7 +189,7 @@ class StandardCommonChatCommands(CommandContainer):
for tone in sequence: for tone in sequence:
if not (tone in ("*", "#") or tone.isdigit()): if not (tone in ("*", "#") or tone.isdigit()):
raise CommandError(_("%s is not a valid tone") % tone) raise CommandError(_("%s is not a valid tone") % tone)
gjs = self.connection.get_jingle_session gjs = self.connection.get_module('Jingle').get_jingle_session
session = gjs(self.full_jid, self.audio_sid) session = gjs(self.full_jid, self.audio_sid)
content = session.get_content("audio") content = session.get_content("audio")
content.batch_dtmf(sequence) content.batch_dtmf(sequence)

View File

@ -32,7 +32,6 @@ import nbxmpp
from gajim.common import app from gajim.common import app
from gajim.common import helpers from gajim.common import helpers
from gajim.common import jingle_xtls from gajim.common import jingle_xtls
from gajim.common.jingle import ConnectionJingle
from gajim.common.protocol.bytestream import ConnectionSocks5Bytestream from gajim.common.protocol.bytestream import ConnectionSocks5Bytestream
from gajim.common.connection_handlers_events import StreamReceivedEvent from gajim.common.connection_handlers_events import StreamReceivedEvent
from gajim.common.connection_handlers_events import PresenceReceivedEvent from gajim.common.connection_handlers_events import PresenceReceivedEvent
@ -183,11 +182,9 @@ class ConnectionHandlersBase:
return sess return sess
class ConnectionHandlers(ConnectionSocks5Bytestream, class ConnectionHandlers(ConnectionSocks5Bytestream,
ConnectionHandlersBase, ConnectionHandlersBase):
ConnectionJingle):
def __init__(self): def __init__(self):
ConnectionSocks5Bytestream.__init__(self) ConnectionSocks5Bytestream.__init__(self)
ConnectionJingle.__init__(self)
ConnectionHandlersBase.__init__(self) ConnectionHandlersBase.__init__(self)
app.nec.register_incoming_event(PresenceReceivedEvent) app.nec.register_incoming_event(PresenceReceivedEvent)
@ -221,9 +218,6 @@ class ConnectionHandlers(ConnectionSocks5Bytestream,
nbxmpp.NS_BYTESTREAM) nbxmpp.NS_BYTESTREAM)
con.RegisterHandler('iq', self._bytestreamErrorCB, 'error', con.RegisterHandler('iq', self._bytestreamErrorCB, 'error',
nbxmpp.NS_BYTESTREAM) nbxmpp.NS_BYTESTREAM)
con.RegisterHandler('iq', self._JingleCB, 'result')
con.RegisterHandler('iq', self._JingleCB, 'error')
con.RegisterHandler('iq', self._JingleCB, 'set', nbxmpp.NS_JINGLE)
con.RegisterHandler('iq', self._ResultCB, 'result') con.RegisterHandler('iq', self._ResultCB, 'result')
con.RegisterHandler('unknown', self._StreamCB, con.RegisterHandler('unknown', self._StreamCB,
nbxmpp.NS_XMPP_STREAMS, xmlns=nbxmpp.NS_STREAMS) nbxmpp.NS_XMPP_STREAMS, xmlns=nbxmpp.NS_STREAMS)

View File

@ -209,7 +209,7 @@ class FileRequestReceivedEvent(nec.NetworkIncomingEvent):
n = file_tag.getTag('name') n = file_tag.getTag('name')
n = n.getData() if n else None n = n.getData() if n else None
pjid = app.get_jid_without_resource(self.fjid) pjid = app.get_jid_without_resource(self.fjid)
file_info = self.conn.get_file_info( file_info = self.conn.get_module('Jingle').get_file_info(
pjid, hash_=h, name=n, account=self.conn.name) pjid, hash_=h, name=n, account=self.conn.name)
self.file_props.file_name = file_info['file-name'] self.file_props.file_name = file_info['file-name']
self.file_props.sender = self.conn._ft_get_our_jid() self.file_props.sender = self.conn._ft_get_our_jid()

View File

@ -219,7 +219,7 @@ class JingleContent:
'date' : self.file_props.date, 'date' : self.file_props.date,
'peerjid' : pjid 'peerjid' : pjid
} }
self.session.connection.set_file_info(file_info) self.session.connection.get_module('Jingle').set_file_info(file_info)
desc = file_tag.setTag('desc') desc = file_tag.setTag('desc')
if self.file_props.desc: if self.file_props.desc:
desc.setData(self.file_props.desc) desc.setData(self.file_props.desc)

View File

@ -161,7 +161,7 @@ class JingleFileTransfer(JingleContent):
'date' : self.file_props.date, 'date' : self.file_props.date,
'peerjid' : pjid 'peerjid' : pjid
} }
self.session.connection.set_file_info(file_info) self.session.connection.get_module('Jingle').set_file_info(file_info)
def _compute_hash(self): def _compute_hash(self):
# Caculates the hash and returns a xep-300 hash stanza # Caculates the hash and returns a xep-300 hash stanza

View File

@ -538,7 +538,7 @@ class JingleSession:
# If we are not receiving a file # If we are not receiving a file
# Check if there's already a session with this user: # Check if there's already a session with this user:
if contents[0].media != 'file': if contents[0].media != 'file':
for session in self.connection.iter_jingle_sessions(self.peerjid): for session in self.connection.get_module('Jingle').iter_jingle_sessions(self.peerjid):
if session is not self: if session is not self:
reason = nbxmpp.Node('reason') reason = nbxmpp.Node('reason')
alternative_session = reason.setTag('alternative-session') alternative_session = reason.setTag('alternative-session')
@ -557,8 +557,8 @@ class JingleSession:
n = request.getTag('file').getTag('name') n = request.getTag('file').getTag('name')
n = n.getData() if n else None n = n.getData() if n else None
pjid = app.get_jid_without_resource(self.peerjid) pjid = app.get_jid_without_resource(self.peerjid)
file_info = self.connection.get_file_info(pjid, hash_data, n, file_info = self.connection.get_module('Jingle').get_file_info(
self.connection.name) pjid, hash_data, n, self.connection.name)
if not file_info: if not file_info:
log.warning('The peer %s is requesting a ' \ log.warning('The peer %s is requesting a ' \
'file that we dont have or ' \ 'file that we dont have or ' \
@ -598,7 +598,7 @@ class JingleSession:
cn.on_stanza(stanza, content, error, action) cn.on_stanza(stanza, content, error, action)
def __on_session_terminate(self, stanza, jingle, error, action): def __on_session_terminate(self, stanza, jingle, error, action):
self.connection.delete_jingle_session(self.sid) self.connection.get_module('Jingle').delete_jingle_session(self.sid)
reason, text = self.__reason_from_stanza(jingle) reason, text = self.__reason_from_stanza(jingle)
if reason not in ('success', 'cancel', 'decline'): if reason not in ('success', 'cancel', 'decline'):
self.__dispatch_error(reason, text) self.__dispatch_error(reason, text)
@ -776,7 +776,7 @@ class JingleSession:
text = '%s (%s)' % (reason, text) text = '%s (%s)' % (reason, text)
else: else:
text = reason text = reason
self.connection.delete_jingle_session(self.sid) self.connection.get_module('Jingle').delete_jingle_session(self.sid)
app.nec.push_incoming_event(JingleDisconnectedReceivedEvent(None, app.nec.push_incoming_event(JingleDisconnectedReceivedEvent(None,
conn=self.connection, conn=self.connection,
jingle_session=self, jingle_session=self,

View File

@ -326,8 +326,8 @@ class JingleTransportSocks5(JingleTransport):
self._add_candidates(proxy_cand) self._add_candidates(proxy_cand)
def get_content(self): def get_content(self):
sesn = self.connection.get_jingle_session(self.ourjid, sesn = self.connection.get_module('Jingle').get_jingle_session(
self.file_props.sid) self.ourjid, self.file_props.sid)
for content in sesn.contents.values(): for content in sesn.contents.values():
if content.transport == self: if content.transport == self:
return content return content
@ -337,8 +337,8 @@ class JingleTransportSocks5(JingleTransport):
# send activate request to proxy, send activated confirmation to peer # send activate request to proxy, send activated confirmation to peer
if not self.connection: if not self.connection:
return return
sesn = self.connection.get_jingle_session(self.ourjid, sesn = self.connection.get_module('Jingle').get_jingle_session(
self.file_props.sid) self.ourjid, self.file_props.sid)
if sesn is None: if sesn is None:
return return

View File

@ -75,6 +75,7 @@ MODULES = [
'vcard_temp', 'vcard_temp',
'announce', 'announce',
'ibb', 'ibb',
'jingle',
] ]
_imported_modules = [] # type: List[tuple] _imported_modules = [] # type: List[tuple]

View File

@ -174,7 +174,7 @@ class IBB(BaseModule):
peerjid = file_props.receiver peerjid = file_props.receiver
else: else:
peerjid = file_props.sender peerjid = file_props.sender
session = self._con.get_jingle_session( session = self._con.get_module('Jingle').get_jingle_session(
peerjid, file_props.sid, 'file') peerjid, file_props.sid, 'file')
# According to the xep, the initiator also cancels # According to the xep, the initiator also cancels
# the jingle session if there are no more files to send using IBB # the jingle session if there are no more files to send using IBB

View File

@ -31,24 +31,40 @@ Handles the jingle signalling protocol
import logging import logging
import nbxmpp import nbxmpp
from nbxmpp.structs import StanzaHandler
from gajim.common import helpers from gajim.common import helpers
from gajim.common import app from gajim.common import app
from gajim.common.modules.base import BaseModule
from gajim.common.jingle_session import JingleSession, JingleStates from gajim.common.jingle_session import JingleSession
from gajim.common.jingle_session import JingleStates
from gajim.common.jingle_ft import JingleFileTransfer from gajim.common.jingle_ft import JingleFileTransfer
from gajim.common.jingle_transport import JingleTransportSocks5, JingleTransportIBB from gajim.common.jingle_transport import JingleTransportSocks5
from gajim.common.jingle_transport import JingleTransportIBB
if app.is_installed('FARSTREAM'): if app.is_installed('FARSTREAM'):
from gajim.common.jingle_rtp import JingleAudio, JingleVideo from gajim.common.jingle_rtp import JingleAudio, JingleVideo
logger = logging.getLogger('gajim.c.jingle') logger = logging.getLogger('gajim.c.m.jingle')
class ConnectionJingle: class Jingle(BaseModule):
""" def __init__(self, con):
This object depends on that it is a part of Connection class. BaseModule.__init__(self, con)
"""
self.handlers = [
StanzaHandler(name='iq',
typ='result',
callback=self._on_jingle_iq),
StanzaHandler(name='iq',
typ='error',
callback=self._on_jingle_iq),
StanzaHandler(name='iq',
typ='set',
ns=nbxmpp.NS_JINGLE,
callback=self._on_jingle_iq),
]
def __init__(self):
# dictionary: sessionid => JingleSession object # dictionary: sessionid => JingleSession object
self._sessions = {} self._sessions = {}
@ -68,7 +84,7 @@ class ConnectionJingle:
self._sessions[sid].callbacks = [] self._sessions[sid].callbacks = []
del self._sessions[sid] del self._sessions[sid]
def _JingleCB(self, con, stanza): def _on_jingle_iq(self, con, stanza):
""" """
The jingle stanza dispatcher The jingle stanza dispatcher
@ -103,7 +119,7 @@ class ConnectionJingle:
# do we need to create a new jingle object # do we need to create a new jingle object
if sid not in self._sessions: if sid not in self._sessions:
#TODO: tie-breaking and other things... #TODO: tie-breaking and other things...
newjingle = JingleSession(con=self, weinitiate=False, jid=jid, newjingle = JingleSession(self._con, weinitiate=False, jid=jid,
iq_id=id_, sid=sid) iq_id=id_, sid=sid)
self._sessions[sid] = newjingle self._sessions[sid] = newjingle
# we already have such session in dispatcher... # we already have such session in dispatcher...
@ -122,7 +138,7 @@ class ConnectionJingle:
if jingle: if jingle:
jingle.add_content('voice', JingleAudio(jingle)) jingle.add_content('voice', JingleAudio(jingle))
else: else:
jingle = JingleSession(self, weinitiate=True, jid=jid) jingle = JingleSession(self._con, weinitiate=True, jid=jid)
self._sessions[jingle.sid] = jingle self._sessions[jingle.sid] = jingle
jingle.add_content('voice', JingleAudio(jingle)) jingle.add_content('voice', JingleAudio(jingle))
jingle.start_session() jingle.start_session()
@ -136,7 +152,7 @@ class ConnectionJingle:
jingle.add_content('video', JingleVideo(jingle, in_xid=in_xid, jingle.add_content('video', JingleVideo(jingle, in_xid=in_xid,
out_xid=out_xid)) out_xid=out_xid))
else: else:
jingle = JingleSession(self, weinitiate=True, jid=jid) jingle = JingleSession(self._con, weinitiate=True, jid=jid)
self._sessions[jingle.sid] = jingle self._sessions[jingle.sid] = jingle
jingle.add_content('video', JingleVideo(jingle, in_xid=in_xid, jingle.add_content('video', JingleVideo(jingle, in_xid=in_xid,
out_xid=out_xid)) out_xid=out_xid))
@ -145,16 +161,16 @@ class ConnectionJingle:
def start_file_transfer(self, jid, file_props, request=False): def start_file_transfer(self, jid, file_props, request=False):
logger.info("start file transfer with file: %s", file_props) logger.info("start file transfer with file: %s", file_props)
contact = app.contacts.get_contact_with_highest_priority(self.name, contact = app.contacts.get_contact_with_highest_priority(
app.get_jid_without_resource(jid)) self._account, app.get_jid_without_resource(jid))
if app.contacts.is_gc_contact(self.name, jid): if app.contacts.is_gc_contact(self._account, jid):
gcc = jid.split('/') gcc = jid.split('/')
if len(gcc) == 2: if len(gcc) == 2:
contact = app.contacts.get_gc_contact(self.name, gcc[0], gcc[1]) contact = app.contacts.get_gc_contact(self._account, gcc[0], gcc[1])
if contact is None: if contact is None:
return return
use_security = contact.supports(nbxmpp.NS_JINGLE_XTLS) use_security = contact.supports(nbxmpp.NS_JINGLE_XTLS)
jingle = JingleSession(self, weinitiate=True, jid=jid, werequest=request) jingle = JingleSession(self._con, weinitiate=True, jid=jid, werequest=request)
# this is a file transfer # this is a file transfer
jingle.session_type_ft = True jingle.session_type_ft = True
self._sessions[jingle.sid] = jingle self._sessions[jingle.sid] = jingle
@ -233,3 +249,7 @@ class ConnectionJingle:
if session.peerjid == jid and session.get_content(media): if session.peerjid == jid and session.get_content(media):
return session return session
return None return None
def get_instance(*args, **kwargs):
return Jingle(*args, **kwargs), 'Jingle'

View File

@ -111,8 +111,8 @@ class ConnectionBytestream:
# file transfer initiated by a jingle session # file transfer initiated by a jingle session
log.info("send_file_approval: jingle session accept") log.info("send_file_approval: jingle session accept")
session = self.get_jingle_session(file_props.sender, session = self.get_module('Jingle').get_jingle_session(
file_props.sid) file_props.sender, file_props.sid)
if not session: if not session:
return return
content = None content = None

View File

@ -333,10 +333,6 @@ class P2PClient(IdleObject):
nbxmpp.NS_BYTESTREAM) nbxmpp.NS_BYTESTREAM)
self.RegisterHandler('iq', self._caller._bytestreamErrorCB, 'error', self.RegisterHandler('iq', self._caller._bytestreamErrorCB, 'error',
nbxmpp.NS_BYTESTREAM) nbxmpp.NS_BYTESTREAM)
self.RegisterHandler('iq', self._caller._JingleCB, 'result')
self.RegisterHandler('iq', self._caller._JingleCB, 'error')
self.RegisterHandler('iq', self._caller._JingleCB, 'set',
nbxmpp.NS_JINGLE)
self._caller._register_new_handlers(self) self._caller._register_new_handlers(self)

View File

@ -57,11 +57,9 @@ class DecryptedMessageReceivedEvent(NetworkIncomingEvent):
class ConnectionHandlersZeroconf(ConnectionSocks5BytestreamZeroconf, class ConnectionHandlersZeroconf(ConnectionSocks5BytestreamZeroconf,
connection_handlers.ConnectionHandlersBase, connection_handlers.ConnectionHandlersBase):
connection_handlers.ConnectionJingle):
def __init__(self): def __init__(self):
ConnectionSocks5BytestreamZeroconf.__init__(self) ConnectionSocks5BytestreamZeroconf.__init__(self)
connection_handlers.ConnectionJingle.__init__(self)
connection_handlers.ConnectionHandlersBase.__init__(self) connection_handlers.ConnectionHandlersBase.__init__(self)
def _messageCB(self, con, stanza, properties): def _messageCB(self, con, stanza, properties):

View File

@ -1344,8 +1344,8 @@ class VoIPCallReceivedDialog:
def on_voip_call_received_messagedialog_response(self, dialog, response): def on_voip_call_received_messagedialog_response(self, dialog, response):
# we've got response from user, either stop connecting or accept the call # we've got response from user, either stop connecting or accept the call
session = app.connections[self.account].get_jingle_session(self.fjid, session = app.connections[self.account].get_module('Jingle').get_jingle_session(
self.sid) self.fjid, self.sid)
if not session: if not session:
dialog.destroy() dialog.destroy()
return return

View File

@ -310,9 +310,8 @@ class FileTransfersWindow:
new_file_props.date = file_props.date new_file_props.date = file_props.date
new_file_props.hash_ = file_props.hash_ new_file_props.hash_ = file_props.hash_
new_file_props.type_ = 'r' new_file_props.type_ = 'r'
tsid = app.connections[account].start_file_transfer(fjid, tsid = app.connections[account].get_module('Jingle').start_file_transfer(
new_file_props, fjid, new_file_props, True)
True)
new_file_props.transport_sid = tsid new_file_props.transport_sid = tsid
self.add_transfer(account, contact, new_file_props) self.add_transfer(account, contact, new_file_props)
@ -352,8 +351,8 @@ class FileTransfersWindow:
if file_props is None: if file_props is None:
return False return False
app.connections[account].start_file_transfer(contact.get_full_jid(), app.connections[account].get_module('Jingle').start_file_transfer(
file_props) contact.get_full_jid(), file_props)
self.add_transfer(account, contact, file_props) self.add_transfer(account, contact, file_props)
return True return True

View File

@ -742,8 +742,8 @@ class Interface:
file_props.sid, file_props.received_len) file_props.sid, file_props.received_len)
def __compare_hashes(self, account, file_props): def __compare_hashes(self, account, file_props):
session = app.connections[account].get_jingle_session(jid=None, session = app.connections[account].get_module('Jingle').get_jingle_session(
sid=file_props.sid) jid=None, sid=file_props.sid)
ft_win = self.instances['file_transfers'] ft_win = self.instances['file_transfers']
h = Hashes2() h = Hashes2()
try: try:
@ -794,8 +794,8 @@ class Interface:
self.popup_ft_result(account, jid, file_props) self.popup_ft_result(account, jid, file_props)
if file_props.error == 0: if file_props.error == 0:
ft.set_status(file_props, 'ok') ft.set_status(file_props, 'ok')
session = app.connections[account].get_jingle_session(jid=None, session = app.connections[account].get_module('Jingle').get_jingle_session(
sid=file_props.sid) jid=None, sid=file_props.sid)
# End jingle session # End jingle session
# TODO: only if there are no other parallel downloads in this session # TODO: only if there are no other parallel downloads in this session
if session: if session: