Daniel Brötzmann pushed to branch master at gajim / gajim
Commits: da837340 by wurstsalat at 2022-05-23T01:33:37+02:00 chore: JingleTransport: Add type annotations
- - - - - ddc4e211 by wurstsalat at 2022-05-23T01:33:37+02:00 fix: Socks5: Catch specific exception
Fixes #10265
- - - - - f2459751 by wurstsalat at 2022-05-23T01:33:37+02:00 chore: Jingle: Add type annotations
- - - - - 3429c7bf by wurstsalat at 2022-05-23T01:33:37+02:00 fix: Jingle: Stop early if no transport is available
Fixes #10889
- - - - - 27173321 by wurstsalat at 2022-05-23T01:33:37+02:00 fix: JingleFileTransfer: Fix unbound variable
- - - - - d129668a by wurstsalat at 2022-05-23T01:33:37+02:00 fix: FileTransferWindow: Set file props receiver JID correctly
- - - - -
5 changed files:
- gajim/common/jingle_ft.py - gajim/common/jingle_transport.py - gajim/common/modules/jingle.py - gajim/common/socks5.py - gajim/gtk/filetransfer.py
Changes:
===================================== gajim/common/jingle_ft.py ===================================== @@ -74,13 +74,13 @@ class State(IntEnum):
class JingleFileTransfer(JingleContent): - def __init__(self, session: JingleSession, + file_props: FileProp, transport: Optional[JingleTransport] = None, - file_props: Optional[FileProp] = None, use_security: bool = False, - senders: Optional[str] = None): + senders: Optional[str] = None + ) -> None:
JingleContent.__init__(self, session, transport, senders) log.info("transport value: %s", transport) @@ -99,29 +99,31 @@ def __init__(self, self.callbacks['transport-reject'] += [self.__on_transport_reject] self.callbacks['transport-info'] += [self.__on_transport_info] self.callbacks['iq-result'] += [self.__on_iq_result] + self.use_security = use_security self.x509_fingerprint = None self.file_props = file_props self.weinitiate = self.session.weinitiate self.werequest = self.session.werequest - if self.file_props is not None: - if self.session.werequest: - self.file_props.sender = self.session.peerjid - self.file_props.receiver = self.session.ourjid - else: - self.file_props.sender = self.session.ourjid - self.file_props.receiver = self.session.peerjid - self.file_props.session_type = 'jingle' - self.file_props.sid = session.sid - self.file_props.transfered_size = [] - self.file_props.transport_sid = self.transport.sid - log.info("FT request: %s", file_props) + if transport is None: self.transport = JingleTransportSocks5() self.transport.set_connection(session.connection) self.transport.set_file_props(self.file_props) self.transport.set_our_jid(session.ourjid) log.info('ourjid: %s', session.ourjid) + + if self.session.werequest: + self.file_props.sender = self.session.peerjid + self.file_props.receiver = self.session.ourjid + else: + self.file_props.sender = self.session.ourjid + self.file_props.receiver = self.session.peerjid + self.file_props.session_type = 'jingle' + self.file_props.sid = session.sid + self.file_props.transfered_size = [] + self.file_props.transport_sid = self.transport.sid + self.session = session self.media = 'file' self.nominated_cand = {}
===================================== gajim/common/jingle_transport.py ===================================== @@ -16,8 +16,8 @@ Handles Jingle Transports (currently only ICE-UDP) """
-from typing import Any # pylint: disable=unused-import -from typing import Dict # pylint: disable=unused-import +from typing import Any +from typing import Optional
import logging import socket @@ -29,11 +29,13 @@ from nbxmpp.util import generate_id
from gajim.common import app +from gajim.common.client import Client +from gajim.common.file_props import FileProp
log = logging.getLogger('gajim.c.jingle_transport')
+transports: dict[str, Any] = {}
-transports = {} # type: Dict[str, Any]
def get_jingle_transport(node): namespace = node.getNamespace() @@ -59,15 +61,15 @@ class JingleTransport: __slots__ = ['type_', 'candidates', 'remote_candidates', 'connection', 'file_props', 'ourjid', 'sid']
- def __init__(self, type_): + def __init__(self, type_: TransportType) -> None: self.type_ = type_ self.candidates = [] self.remote_candidates = []
- self.connection = None - self.file_props = None - self.ourjid = None - self.sid = None + self.connection: Optional[Client] = None + self.file_props: Optional[FileProp] = None + self.ourjid: Optional[str] = None + self.sid: Optional[str] = None
def _iter_candidates(self): for candidate in self.candidates: @@ -96,20 +98,21 @@ def parse_transport_stanza(self, transport): """ return []
- def set_connection(self, conn): + def set_connection(self, conn: Client) -> None: self.connection = conn if not self.sid: self.sid = generate_id()
- def set_file_props(self, file_props): + def set_file_props(self, file_props: FileProp) -> None: self.file_props = file_props
- def set_our_jid(self, jid): + def set_our_jid(self, jid: str) -> None: self.ourjid = jid
- def set_sid(self, sid): + def set_sid(self, sid: str) -> None: self.sid = sid
+ class JingleTransportSocks5(JingleTransport): """ Socks5 transport in jingle scenario
===================================== gajim/common/modules/jingle.py ===================================== @@ -28,6 +28,8 @@ # * config: # - codecs
+from typing import Optional + import logging
import nbxmpp @@ -36,8 +38,8 @@
from gajim.common import helpers from gajim.common import jingle_xtls +from gajim.common.file_props import FileProp from gajim.common.modules.base import BaseModule - from gajim.common.jingle_session import JingleSession from gajim.common.jingle_session import JingleStates from gajim.common.jingle_ft import JingleFileTransfer @@ -74,14 +76,14 @@ def __init__(self, con): ]
# dictionary: sessionid => JingleSession object - self._sessions = {} + self._sessions: dict[str, JingleSession] = {}
# dictionary: (jid, iq stanza id) => JingleSession object, # one time callbacks self.__iq_responses = {} self.files = []
- def delete_jingle_session(self, sid): + def delete_jingle_session(self, sid: str) -> None: """ Remove a jingle session from a jingle stanza dispatcher """ @@ -204,7 +206,11 @@ def start_audio_video(self, jid): jingle_session.start_session() return jingle_session.sid
- def start_file_transfer(self, jid, file_props, request=False): + def start_file_transfer(self, + jid: str, + file_props: FileProp, + request: bool = False + ) -> Optional[str]: logger.info("start file transfer with file: %s", file_props) contact = self._con.get_module('Contacts').get_contact(jid) use_security = contact.supports(Namespace.JINGLE_XTLS) @@ -222,7 +228,9 @@ def start_file_transfer(self, jid, file_props, request=False): elif contact.supports(Namespace.JINGLE_IBB): transport = JingleTransportIBB() else: - transport = None + logger.error('No suitable transport method available for %s', + contact.jid) + return
senders = 'initiator' if request:
===================================== gajim/common/socks5.py ===================================== @@ -689,9 +689,12 @@ def write_next(self): raise e except OpenSSL.SSL.SysCallError: return self._on_send_exception() - except Exception as e: + except socket.error as err: if e.errno not in (EINTR, ENOBUFS, EWOULDBLOCK): return self._on_send_exception() + except Exception as err: + return self._on_send_exception() + self.size += lenn current_time = time.time() self.file_props.elapsed_time += current_time - \
===================================== gajim/gtk/filetransfer.py ===================================== @@ -17,6 +17,10 @@ # You should have received a copy of the GNU General Public License # along with Gajim. If not, see http://www.gnu.org/licenses/.
+from __future__ import annotations + +from typing import Optional + import os import time import logging @@ -48,6 +52,7 @@ from gajim.common.modules.bytestream import is_transfer_paused from gajim.common.modules.bytestream import is_transfer_stopped from gajim.common.modules.contacts import BareContact +from gajim.common import types
from .dialogs import DialogButton from .dialogs import ConfirmationDialog @@ -390,12 +395,20 @@ def _on_yes(): text=_('_Download Again'), callback=_on_yes)]).show()
- def show_file_send_request(self, account, contact): + def show_file_send_request(self, + account: str, + contact: types.BareContact + ) -> None: send_callback = partial(self.send_file, account, contact) SendFileDialog(contact, send_callback, self.window)
- def send_file(self, account, contact, resource_jid, file_path, - file_desc=''): + def send_file(self, + account: str, + contact: types.BareContact, + resource_jid: JID, + file_path: str, + file_desc: str = '' + ) -> bool: """ Start the real transfer(upload) of the file """ @@ -403,11 +416,15 @@ def send_file(self, account, contact, resource_jid, file_path, pritext = _('Gajim can not read this file') sextext = _('Another process is using this file.') ErrorDialog(pritext, sextext) - return + return False
file_name = os.path.split(file_path)[1] - file_props = self.get_send_file_props(account, contact, file_path, - file_name, file_desc) + file_props = self.get_send_file_props( + account, + resource_jid, + file_path, + file_name, + file_desc) if file_props is None: return False
@@ -682,13 +699,19 @@ def get_iter_by_sid(self, typ, sid): return iter_ iter_ = self.model.iter_next(iter_)
- def __convert_date(self, epoch): + @staticmethod + def __convert_date(epoch: float) -> str: # Converts date-time from seconds from epoch to iso 8601 dt = datetime.utcfromtimestamp(epoch) return dt.isoformat() + 'Z'
- def get_send_file_props(self, account, contact, file_path, file_name, - file_desc=''): + def get_send_file_props(self, + account: str, + resource_jid: JID, + file_path: str, + file_name: str, + file_desc: str = '' + ) -> Optional[FileProp]: """ Create new file_props object and set initial file transfer properties in it @@ -696,15 +719,20 @@ def get_send_file_props(self, account, contact, file_path, file_name, if os.path.isfile(file_path): stat = os.stat(file_path) else: - ErrorDialog(_('Invalid File'), _('File: ') + file_path) + ErrorDialog( + _('Invalid File'), + _('File: %s') % file_path) return None + if stat[6] == 0: ErrorDialog( _('Invalid File'), _('It is not possible to send empty files')) return None + file_props = FilesProp.getNewFileProp( - account, sid=helpers.get_random_string()) + account, + sid=helpers.get_random_string()) mod_date = os.path.getmtime(file_path) file_props.file_name = file_path file_props.name = file_name @@ -714,7 +742,7 @@ def get_send_file_props(self, account, contact, file_path, file_name, file_props.elapsed_time = 0 file_props.size = stat[6] file_props.sender = account - file_props.receiver = contact + file_props.receiver = str(resource_jid) file_props.tt_account = account return file_props
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/810b140b5420b0cb57baf28b27df551c...