diff options
Diffstat (limited to 'libdino/src')
-rw-r--r-- | libdino/src/service/file_manager.vala | 101 | ||||
-rw-r--r-- | libdino/src/service/jingle_file_transfers.vala | 107 | ||||
-rw-r--r-- | libdino/src/service/module_manager.vala | 1 |
3 files changed, 142 insertions, 67 deletions
diff --git a/libdino/src/service/file_manager.vala b/libdino/src/service/file_manager.vala index e0a417ee..841a6b53 100644 --- a/libdino/src/service/file_manager.vala +++ b/libdino/src/service/file_manager.vala @@ -71,45 +71,44 @@ public class FileManager : StreamInteractionModule, Object { file_meta.size = file_transfer.size; file_meta.mime_type = file_transfer.mime_type; - bool encrypted = false; - foreach (FileEncryptor file_encryptor in file_encryptors) { - if (file_encryptor.can_encrypt_file(conversation, file_transfer)) { - file_meta = file_encryptor.encrypt_file(conversation, file_transfer); - encrypted = true; - break; + FileSender file_sender = null; + FileEncryptor file_encryptor = null; + foreach (FileSender sender in file_senders) { + if (sender.can_send(conversation, file_transfer)) { + if (file_transfer.encryption == Encryption.NONE || sender.can_encrypt(conversation, file_transfer)) { + file_sender = sender; + break; + } else { + foreach (FileEncryptor encryptor in file_encryptors) { + if (encryptor.can_encrypt_file(conversation, file_transfer)) { + file_encryptor = encryptor; + break; + } + } + if (file_encryptor != null) { + file_sender = sender; + break; + } + } } } - if (conversation.encryption != Encryption.NONE && !encrypted) { - throw new FileSendError.ENCRYPTION_FAILED("File was not encrypted"); - } - FileSendData file_send_data = null; - foreach (FileSender file_sender in file_senders) { - if (file_sender.can_send(conversation, file_transfer)) { - file_send_data = yield file_sender.prepare_send_file(conversation, file_transfer, file_meta); - break; - } + if (file_sender == null) { + throw new FileSendError.UPLOAD_FAILED("No sender/encryptor combination available"); } - foreach (FileEncryptor file_encryptor in file_encryptors) { - if (file_encryptor.can_encrypt_file(conversation, file_transfer)) { - file_send_data = file_encryptor.preprocess_send_file(conversation, file_transfer, file_send_data, file_meta); - break; - } + if (file_encryptor != null) { + file_meta = file_encryptor.encrypt_file(conversation, file_transfer); } - bool sent = false; - foreach (FileSender file_sender in file_senders) { - if (file_sender.can_send(conversation, file_transfer)) { - yield file_sender.send_file(conversation, file_transfer, file_send_data); - sent = true; - break; - } - } - if (!sent) { - throw new FileSendError.UPLOAD_FAILED("File was not sent"); + FileSendData file_send_data = yield file_sender.prepare_send_file(conversation, file_transfer, file_meta); + + if (file_encryptor != null) { + file_send_data = file_encryptor.preprocess_send_file(conversation, file_transfer, file_send_data, file_meta); } + yield file_sender.send_file(conversation, file_transfer, file_send_data, file_meta); + conversation.last_active = file_transfer.time; } catch (Error e) { warning("Send file error: %s", e.message); @@ -130,7 +129,9 @@ public class FileManager : StreamInteractionModule, Object { yield download_file_internal(file_provider, file_transfer, conversation); } - public bool is_upload_available(Conversation conversation) { + public bool is_upload_available(Conversation? conversation) { + if (conversation == null) return false; + foreach (FileSender file_sender in file_senders) { if (file_sender.is_upload_available(conversation)) return true; } @@ -230,12 +231,18 @@ public class FileManager : StreamInteractionModule, Object { try { // Get meta info FileReceiveData receive_data = file_provider.get_file_receive_data(file_transfer); - foreach (FileDecryptor file_decryptor in file_decryptors) { - if (file_decryptor.can_decrypt_file(conversation, file_transfer, receive_data)) { - receive_data = file_decryptor.prepare_get_meta_info(conversation, file_transfer, receive_data); + FileDecryptor? file_decryptor = null; + foreach (FileDecryptor decryptor in file_decryptors) { + if (decryptor.can_decrypt_file(conversation, file_transfer, receive_data)) { + file_decryptor = decryptor; break; } } + + if (file_decryptor != null) { + receive_data = file_decryptor.prepare_get_meta_info(conversation, file_transfer, receive_data); + } + FileMeta file_meta = yield get_file_meta(file_provider, file_transfer, conversation, receive_data); @@ -244,34 +251,21 @@ public class FileManager : StreamInteractionModule, Object { // Download and decrypt file file_transfer.state = FileTransfer.State.IN_PROGRESS; - foreach (FileDecryptor file_decryptor in file_decryptors) { - if (file_decryptor.can_decrypt_file(conversation, file_transfer, receive_data)) { - file_meta = file_decryptor.prepare_download_file(conversation, file_transfer, receive_data, file_meta); - break; - } + if (file_decryptor != null) { + file_meta = file_decryptor.prepare_download_file(conversation, file_transfer, receive_data, file_meta); } input_stream = yield file_provider.download(file_transfer, receive_data, file_meta); - - foreach (FileDecryptor file_decryptor in file_decryptors) { - if (file_decryptor.can_decrypt_file(conversation, file_transfer, receive_data)) { - input_stream = yield file_decryptor.decrypt_file(input_stream, conversation, file_transfer, receive_data); - break; - } + if (file_decryptor != null) { + input_stream = yield file_decryptor.decrypt_file(input_stream, conversation, file_transfer, receive_data); } // Save file - string filename = Random.next_int().to_string("%x") + "_" + file_meta.file_name; + string filename = Random.next_int().to_string("%x") + "_" + file_transfer.file_name; File file = File.new_for_path(Path.build_filename(get_storage_dir(), filename)); - if (file_transfer.encryption == Encryption.PGP || file.get_path().has_suffix(".pgp")) { - file = File.new_for_path(file.get_path().substring(0, file.get_path().length - 4)); - } - OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION); yield os.splice_async(input_stream, OutputStreamSpliceFlags.CLOSE_SOURCE|OutputStreamSpliceFlags.CLOSE_TARGET); - file_transfer.size = (int)file_meta.size; - file_transfer.file_name = file_meta.file_name; file_transfer.path = file.get_basename(); file_transfer.input_stream = yield file.read_async(); @@ -392,7 +386,8 @@ public interface FileSender : Object { public abstract bool is_upload_available(Conversation conversation); public abstract bool can_send(Conversation conversation, FileTransfer file_transfer); public abstract async FileSendData? prepare_send_file(Conversation conversation, FileTransfer file_transfer, FileMeta file_meta) throws FileSendError; - public abstract async void send_file(Conversation conversation, FileTransfer file_transfer, FileSendData file_send_data) throws FileSendError; + public abstract async void send_file(Conversation conversation, FileTransfer file_transfer, FileSendData file_send_data, FileMeta file_meta) throws FileSendError; + public abstract bool can_encrypt(Conversation conversation, FileTransfer file_transfer); public abstract int get_id(); public abstract float get_priority(); diff --git a/libdino/src/service/jingle_file_transfers.vala b/libdino/src/service/jingle_file_transfers.vala index 0a93979b..182213bb 100644 --- a/libdino/src/service/jingle_file_transfers.vala +++ b/libdino/src/service/jingle_file_transfers.vala @@ -6,6 +6,56 @@ using Dino.Entities; namespace Dino { +public interface JingleFileEncryptionHelper : Object { + public abstract bool can_transfer(Conversation conversation); + public abstract bool can_encrypt(Conversation conversation, FileTransfer file_transfer, Jid? full_jid = null); + public abstract string? get_precondition_name(Conversation conversation, FileTransfer file_transfer); + public abstract Object? get_precondition_options(Conversation conversation, FileTransfer file_transfer); + public abstract FileMeta complete_meta(FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta, Xmpp.Xep.JingleFileTransfer.FileTransfer jingle_transfer); +} + +public class JingleFileEncryptionHelperTransferOnly : JingleFileEncryptionHelper, Object { + public bool can_transfer(Conversation conversation) { + return true; + } + public bool can_encrypt(Conversation conversation, FileTransfer file_transfer, Jid? full_jid) { + return false; + } + public string? get_precondition_name(Conversation conversation, FileTransfer file_transfer) { + return null; + } + public Object? get_precondition_options(Conversation conversation, FileTransfer file_transfer) { + return null; + } + public FileMeta complete_meta(FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta, Xmpp.Xep.JingleFileTransfer.FileTransfer jingle_transfer) { + return file_meta; + } +} + +public class JingleFileHelperRegistry { + private static JingleFileHelperRegistry INSTANCE; + public static JingleFileHelperRegistry instance { get { + if (INSTANCE == null) { + INSTANCE = new JingleFileHelperRegistry(); + INSTANCE.add_encryption_helper(Encryption.NONE, new JingleFileEncryptionHelperTransferOnly()); + } + return INSTANCE; + } } + + internal HashMap<Encryption, JingleFileEncryptionHelper> encryption_helpers = new HashMap<Encryption, JingleFileEncryptionHelper>(); + + public void add_encryption_helper(Encryption encryption, JingleFileEncryptionHelper helper) { + encryption_helpers[encryption] = helper; + } + + public JingleFileEncryptionHelper? get_encryption_helper(Encryption encryption) { + if (encryption_helpers.has_key(encryption)) { + return encryption_helpers[encryption]; + } + return null; + } +} + public class JingleFileProvider : FileProvider, Object { private StreamInteractor stream_interactor; @@ -29,7 +79,15 @@ public class JingleFileProvider : FileProvider, Object { } public async FileMeta get_meta_info(FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta) throws FileReceiveError { - return file_meta; + Xmpp.Xep.JingleFileTransfer.FileTransfer? jingle_file_transfer = file_transfers[file_transfer.info]; + if (jingle_file_transfer == null) { + throw new FileReceiveError.DOWNLOAD_FAILED("Transfer data not available anymore"); + } + FileMeta meta = file_meta; + foreach (JingleFileEncryptionHelper helper in JingleFileHelperRegistry.instance.encryption_helpers.values) { + meta = helper.complete_meta(file_transfer, receive_data, meta, jingle_file_transfer); + } + return meta; } public async InputStream download(FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta) throws FileReceiveError { @@ -39,6 +97,9 @@ public class JingleFileProvider : FileProvider, Object { if (jingle_file_transfer == null) { throw new FileReceiveError.DOWNLOAD_FAILED("Transfer data not available anymore"); } + foreach (JingleFileEncryptionHelper helper in JingleFileHelperRegistry.instance.encryption_helpers.values) { + helper.complete_meta(file_transfer, receive_data, file_meta, jingle_file_transfer); + } try { jingle_file_transfer.accept(stream); } catch (IOError e) { @@ -83,6 +144,10 @@ public class JingleFileSender : FileSender, Object { } public bool is_upload_available(Conversation conversation) { + JingleFileEncryptionHelper? helper = JingleFileHelperRegistry.instance.get_encryption_helper(conversation.encryption); + if (helper == null) return false; + if (!helper.can_transfer(conversation)) return false; + XmppStream? stream = stream_interactor.get_stream(conversation.account); if (stream == null) return false; @@ -98,32 +163,46 @@ public class JingleFileSender : FileSender, Object { } public bool can_send(Conversation conversation, FileTransfer file_transfer) { - if (conversation.encryption != Encryption.NONE) return false; - - XmppStream? stream = stream_interactor.get_stream(file_transfer.account); - if (stream == null) return false; + // No file specific restrictions apply to Jingle file transfers + return is_upload_available(conversation); + } - foreach (Jid full_jid in stream.get_flag(Presence.Flag.IDENTITY).get_resources(conversation.counterpart)) { - if (stream.get_module(Xep.JingleFileTransfer.Module.IDENTITY).is_available(stream, full_jid)) { - return true; - } - } - return false; + public bool can_encrypt(Conversation conversation, FileTransfer file_transfer) { + JingleFileEncryptionHelper? helper = JingleFileHelperRegistry.instance.get_encryption_helper(file_transfer.encryption); + if (helper == null) return false; + return helper.can_encrypt(conversation, file_transfer); } public async FileSendData? prepare_send_file(Conversation conversation, FileTransfer file_transfer, FileMeta file_meta) throws FileSendError { + if (file_meta is HttpFileMeta) { + throw new FileSendError.UPLOAD_FAILED("Cannot upload http file meta over Jingle"); + } return new FileSendData(); } - public async void send_file(Conversation conversation, FileTransfer file_transfer, FileSendData file_send_data) throws FileSendError { - // TODO(hrxi) What should happen if `stream == null`? + public async void send_file(Conversation conversation, FileTransfer file_transfer, FileSendData file_send_data, FileMeta file_meta) throws FileSendError { XmppStream? stream = stream_interactor.get_stream(file_transfer.account); + if (stream == null) throw new FileSendError.UPLOAD_FAILED("No stream available"); + JingleFileEncryptionHelper? helper = JingleFileHelperRegistry.instance.get_encryption_helper(file_transfer.encryption); + bool must_encrypt = helper != null && helper.can_encrypt(conversation, file_transfer); foreach (Jid full_jid in stream.get_flag(Presence.Flag.IDENTITY).get_resources(conversation.counterpart)) { // TODO(hrxi): Prioritization of transports (and resources?). if (!stream.get_module(Xep.JingleFileTransfer.Module.IDENTITY).is_available(stream, full_jid)) { continue; } - stream.get_module(Xep.JingleFileTransfer.Module.IDENTITY).offer_file_stream.begin(stream, full_jid, file_transfer.input_stream, file_transfer.file_name, file_transfer.size); + if (must_encrypt && !helper.can_encrypt(conversation, file_transfer, full_jid)) { + continue; + } + string? precondition_name = null; + Object? precondition_options = null; + if (must_encrypt) { + precondition_name = helper.get_precondition_name(conversation, file_transfer); + precondition_options = helper.get_precondition_options(conversation, file_transfer); + if (precondition_name == null) { + throw new FileSendError.ENCRYPTION_FAILED("Should have created a precondition, but did not"); + } + } + yield stream.get_module(Xep.JingleFileTransfer.Module.IDENTITY).offer_file_stream(stream, full_jid, file_transfer.input_stream, file_transfer.server_file_name, file_meta.size, precondition_name, precondition_options); return; } } diff --git a/libdino/src/service/module_manager.vala b/libdino/src/service/module_manager.vala index 6a07a146..2b1567a0 100644 --- a/libdino/src/service/module_manager.vala +++ b/libdino/src/service/module_manager.vala @@ -84,6 +84,7 @@ public class ModuleManager { module_map[account].add(new Xep.JingleSocks5Bytestreams.Module()); module_map[account].add(new Xep.JingleInBandBytestreams.Module()); module_map[account].add(new Xep.JingleFileTransfer.Module()); + module_map[account].add(new Xep.Jet.Module()); initialize_account_modules(account, module_map[account]); } } |