diff options
author | fiaxh <git@lightrise.org> | 2019-07-18 02:03:42 +0200 |
---|---|---|
committer | fiaxh <git@lightrise.org> | 2019-07-18 02:03:42 +0200 |
commit | f0abb8aaf9d06106235ca5e0e6b3ca2e425c4422 (patch) | |
tree | 03fce63f8c37b50b5202690a6801234724ed0021 /plugins/openpgp/src/file_transfer | |
parent | 30353e92d63c033857ad0396c921b3bf5cab44c3 (diff) | |
download | dino-f0abb8aaf9d06106235ca5e0e6b3ca2e425c4422.tar.gz dino-f0abb8aaf9d06106235ca5e0e6b3ca2e425c4422.zip |
Refactor file receive/send interfaces and UI
Diffstat (limited to 'plugins/openpgp/src/file_transfer')
-rw-r--r-- | plugins/openpgp/src/file_transfer/file_decryptor.vala | 43 | ||||
-rw-r--r-- | plugins/openpgp/src/file_transfer/file_encryptor.vala | 41 |
2 files changed, 84 insertions, 0 deletions
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<uint8> data = new Array<uint8>(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; + } +} + +} |