From f0abb8aaf9d06106235ca5e0e6b3ca2e425c4422 Mon Sep 17 00:00:00 2001 From: fiaxh Date: Thu, 18 Jul 2019 02:03:42 +0200 Subject: Refactor file receive/send interfaces and UI --- .../openpgp/src/file_transfer/file_decryptor.vala | 43 ++++++++++++++++++++++ .../openpgp/src/file_transfer/file_encryptor.vala | 41 +++++++++++++++++++++ plugins/openpgp/src/in_file_processor.vala | 34 ----------------- plugins/openpgp/src/out_file_processor.vala | 32 ---------------- plugins/openpgp/src/plugin.vala | 4 +- 5 files changed, 86 insertions(+), 68 deletions(-) create mode 100644 plugins/openpgp/src/file_transfer/file_decryptor.vala create mode 100644 plugins/openpgp/src/file_transfer/file_encryptor.vala delete mode 100644 plugins/openpgp/src/in_file_processor.vala delete mode 100644 plugins/openpgp/src/out_file_processor.vala (limited to 'plugins/openpgp/src') diff --git a/plugins/openpgp/src/file_transfer/file_decryptor.vala b/plugins/openpgp/src/file_transfer/file_decryptor.vala new file mode 100644 index 00000000..7668023e --- /dev/null +++ b/plugins/openpgp/src/file_transfer/file_decryptor.vala @@ -0,0 +1,43 @@ +using Dino.Entities; + +namespace Dino.Plugins.OpenPgp { + +public class PgpFileDecryptor : FileDecryptor, Object { + + public FileReceiveData prepare_get_meta_info(Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data) { + return receive_data; + } + + public FileMeta prepare_download_file(Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta) { + return file_meta; + } + + public bool can_decrypt_file(Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data) { + return file_transfer.file_name.has_suffix("pgp") || file_transfer.mime_type == "application/pgp-encrypted"; + } + + public async InputStream decrypt_file(InputStream encrypted_stream, Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data) throws FileReceiveError { + try { + uint8[] buf = new uint8[256]; + Array data = new Array(false, true, 0); + size_t len = -1; + do { + len = encrypted_stream.read(buf); + data.append_vals(buf, (uint) len); + } while(len > 0); + + GPGHelper.DecryptedData clear_data = GPGHelper.decrypt_data(data.data); + file_transfer.encryption = Encryption.PGP; + if (clear_data.filename != null && clear_data.filename != "") { + file_transfer.file_name = clear_data.filename; + } else if (file_transfer.file_name.has_suffix(".pgp")) { + file_transfer.file_name = file_transfer.file_name.substring(0, file_transfer.file_name.length - 4); + } + return new MemoryInputStream.from_data(clear_data.data, GLib.free); + } catch (Error e) { + throw new FileReceiveError.DECRYPTION_FAILED("PGP file decrypt error: %s".printf(e.message)); + } + } +} + +} diff --git a/plugins/openpgp/src/file_transfer/file_encryptor.vala b/plugins/openpgp/src/file_transfer/file_encryptor.vala new file mode 100644 index 00000000..7d51be60 --- /dev/null +++ b/plugins/openpgp/src/file_transfer/file_encryptor.vala @@ -0,0 +1,41 @@ +using Dino.Entities; + +namespace Dino.Plugins.OpenPgp { + +public class PgpFileEncryptor : Dino.FileEncryptor, Object { + + StreamInteractor stream_interactor; + + public PgpFileEncryptor(StreamInteractor stream_interactor) { + this.stream_interactor = stream_interactor; + } + + public bool can_encrypt_file(Conversation conversation, FileTransfer file_transfer) { + return conversation.encryption == Encryption.PGP; + } + + public FileMeta encrypt_file(Conversation conversation, FileTransfer file_transfer) throws FileSendError { + try { + GPG.Key[] keys = stream_interactor.get_module(Manager.IDENTITY).get_key_fprs(conversation); + uint8[] enc_content = GPGHelper.encrypt_file(file_transfer.get_file().get_path(), keys, GPG.EncryptFlags.ALWAYS_TRUST, file_transfer.file_name); + file_transfer.input_stream = new MemoryInputStream.from_data(enc_content, GLib.free); + file_transfer.encryption = Encryption.PGP; + file_transfer.server_file_name = Xmpp.random_uuid() + ".pgp"; + } catch (Error e) { + throw new FileSendError.ENCRYPTION_FAILED("PGP file encryption error: %s".printf(e.message)); + } + + return new FileMeta(); + } + + public FileSendData? preprocess_send_file(Conversation conversation, FileTransfer file_transfer, FileSendData file_send_data, FileMeta file_meta) { + HttpFileSendData? send_data = file_send_data as HttpFileSendData; + if (send_data == null) return null; + + send_data.encrypt_message = false; + + return file_send_data; + } +} + +} diff --git a/plugins/openpgp/src/in_file_processor.vala b/plugins/openpgp/src/in_file_processor.vala deleted file mode 100644 index 918f824a..00000000 --- a/plugins/openpgp/src/in_file_processor.vala +++ /dev/null @@ -1,34 +0,0 @@ -using Dino.Entities; - -namespace Dino.Plugins.OpenPgp { - -public class InFileProcessor : IncomingFileProcessor, Object { - public bool can_process(FileTransfer file_transfer) { - return file_transfer.file_name.has_suffix("pgp") || file_transfer.mime_type == "application/pgp-encrypted"; - } - - public void process(FileTransfer file_transfer) { - try { - uint8[] buf = new uint8[256]; - Array data = new Array(false, true, 0); - size_t len = -1; - do { - len = file_transfer.input_stream.read(buf); - data.append_vals(buf, (uint) len); - } while(len > 0); - - GPGHelper.DecryptedData clear_data = GPGHelper.decrypt_data(data.data); - file_transfer.input_stream = new MemoryInputStream.from_data(clear_data.data, GLib.free); - file_transfer.encryption = Encryption.PGP; - if (clear_data.filename != null && clear_data.filename != "") { - file_transfer.file_name = clear_data.filename; - } else if (file_transfer.file_name.has_suffix(".pgp")) { - file_transfer.file_name = file_transfer.file_name.substring(0, file_transfer.file_name.length - 4); - } - } catch (Error e) { - file_transfer.state = FileTransfer.State.FAILED; - } - } -} - -} diff --git a/plugins/openpgp/src/out_file_processor.vala b/plugins/openpgp/src/out_file_processor.vala deleted file mode 100644 index 40eef7f5..00000000 --- a/plugins/openpgp/src/out_file_processor.vala +++ /dev/null @@ -1,32 +0,0 @@ -using Dino.Entities; - -namespace Dino.Plugins.OpenPgp { - -public class OutFileProcessor : OutgoingFileProcessor, Object { - - StreamInteractor stream_interactor; - - public OutFileProcessor(StreamInteractor stream_interactor) { - this.stream_interactor = stream_interactor; - } - - public bool can_process(Conversation conversation, FileTransfer file_transfer) { - return conversation.encryption == Encryption.PGP; - } - - public void process(Conversation conversation, FileTransfer file_transfer) { - string path = file_transfer.get_file().get_path(); - try { - GPG.Key[] keys = stream_interactor.get_module(Manager.IDENTITY).get_key_fprs(conversation); - uint8[] enc_content = GPGHelper.encrypt_file(path, keys, GPG.EncryptFlags.ALWAYS_TRUST, file_transfer.file_name); - file_transfer.input_stream = new MemoryInputStream.from_data(enc_content, GLib.free); - file_transfer.encryption = Encryption.PGP; - file_transfer.server_file_name = Xmpp.random_uuid() + ".pgp"; - } catch (Error e) { - warning(@"PGP file encryption error: $(e.message)\n"); - file_transfer.state = FileTransfer.State.FAILED; - } - } -} - -} diff --git a/plugins/openpgp/src/plugin.vala b/plugins/openpgp/src/plugin.vala index 1d4f5203..35ede01e 100644 --- a/plugins/openpgp/src/plugin.vala +++ b/plugins/openpgp/src/plugin.vala @@ -29,8 +29,8 @@ public class Plugin : Plugins.RootInterface, Object { app.stream_interactor.module_manager.initialize_account_modules.connect(on_initialize_account_modules); Manager.start(app.stream_interactor, db); - app.stream_interactor.get_module(FileManager.IDENTITY).add_outgoing_processor(new OutFileProcessor(app.stream_interactor)); - app.stream_interactor.get_module(FileManager.IDENTITY).add_incoming_processor(new InFileProcessor()); + app.stream_interactor.get_module(FileManager.IDENTITY).add_file_encryptor(new PgpFileEncryptor(app.stream_interactor)); + app.stream_interactor.get_module(FileManager.IDENTITY).add_file_decryptor(new PgpFileDecryptor()); internationalize(GETTEXT_PACKAGE, app.search_path_generator.get_locale_path(GETTEXT_PACKAGE, LOCALE_INSTALL_DIR)); } -- cgit v1.2.3-70-g09d2