aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
Diffstat (limited to 'plugins')
-rw-r--r--plugins/http-files/CMakeLists.txt3
-rw-r--r--plugins/http-files/src/file_provider.vala134
-rw-r--r--plugins/http-files/src/file_sender.vala126
-rw-r--r--plugins/http-files/src/manager.vala147
-rw-r--r--plugins/http-files/src/message_filter.vala23
-rw-r--r--plugins/http-files/src/plugin.vala11
-rw-r--r--plugins/omemo/CMakeLists.txt4
-rw-r--r--plugins/omemo/src/file_transfer/file_decryptor.vala89
-rw-r--r--plugins/omemo/src/file_transfer/file_encryptor.vala74
-rw-r--r--plugins/omemo/src/file_transfer/file_provider.vala168
-rw-r--r--plugins/omemo/src/file_transfer/file_sender.vala108
-rw-r--r--plugins/omemo/src/plugin.vala5
-rw-r--r--plugins/openpgp/CMakeLists.txt5
-rw-r--r--plugins/openpgp/src/file_transfer/file_decryptor.vala (renamed from plugins/openpgp/src/in_file_processor.vala)21
-rw-r--r--plugins/openpgp/src/file_transfer/file_encryptor.vala41
-rw-r--r--plugins/openpgp/src/out_file_processor.vala32
-rw-r--r--plugins/openpgp/src/plugin.vala4
17 files changed, 468 insertions, 527 deletions
diff --git a/plugins/http-files/CMakeLists.txt b/plugins/http-files/CMakeLists.txt
index 93ccbf67..625c215f 100644
--- a/plugins/http-files/CMakeLists.txt
+++ b/plugins/http-files/CMakeLists.txt
@@ -10,7 +10,8 @@ find_packages(HTTP_FILES_PACKAGES REQUIRED
vala_precompile(HTTP_FILES_VALA_C
SOURCES
src/file_provider.vala
- src/manager.vala
+ src/file_sender.vala
+ src/message_filter.vala
src/plugin.vala
src/register_plugin.vala
CUSTOM_VAPIS
diff --git a/plugins/http-files/src/file_provider.vala b/plugins/http-files/src/file_provider.vala
index b7d18e36..38687863 100644
--- a/plugins/http-files/src/file_provider.vala
+++ b/plugins/http-files/src/file_provider.vala
@@ -7,16 +7,15 @@ using Xmpp;
namespace Dino.Plugins.HttpFiles {
public class FileProvider : Dino.FileProvider, Object {
- public string id { get { return "http"; } }
private StreamInteractor stream_interactor;
private Dino.Database dino_db;
- private Regex url_regex;
+ private Regex url_regex = /^(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))$/;
+ private Regex omemo_url_regex = /^aesgcm:\/\/(.*)#(([A-Fa-f0-9]{2}){48}|([A-Fa-f0-9]{2}){44})$/;
public FileProvider(StreamInteractor stream_interactor, Dino.Database dino_db) {
this.stream_interactor = stream_interactor;
this.dino_db = dino_db;
- this.url_regex = /^(?i)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))$/;
stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(new ReceivedMessageListener(this));
}
@@ -38,7 +37,11 @@ public class FileProvider : Dino.FileProvider, Object {
public override async bool run(Entities.Message message, Xmpp.MessageStanza stanza, Conversation conversation) {
if (outer.url_regex.match(message.body)) {
string? oob_url = Xmpp.Xep.OutOfBandData.get_url_from_message(stanza);
- if (oob_url != null && oob_url == message.body) {
+
+ bool normal_file = oob_url != null && oob_url == message.body;
+ bool omemo_file = outer.omemo_url_regex.match(message.body);
+
+ if (normal_file || omemo_file) {
yield outer.on_file_message(message, conversation);
}
}
@@ -47,83 +50,102 @@ public class FileProvider : Dino.FileProvider, Object {
}
private async void on_file_message(Entities.Message message, Conversation conversation) {
- FileTransfer file_transfer = new FileTransfer();
- file_transfer.account = conversation.account;
- file_transfer.counterpart = message.counterpart;
- file_transfer.ourpart = message.ourpart;
- file_transfer.encryption = Encryption.NONE;
- file_transfer.time = message.time;
- file_transfer.local_time = message.local_time;
- file_transfer.direction = message.direction;
- file_transfer.file_name = message.body.substring(message.body.last_index_of("/") + 1);
- file_transfer.size = -1;
- file_transfer.state = FileTransfer.State.NOT_STARTED;
- file_transfer.provider = 0;
- file_transfer.info = message.id.to_string();
-
- if (stream_interactor.get_module(FileManager.IDENTITY).is_sender_trustworthy(file_transfer, conversation)) {
- yield get_meta_info(file_transfer);
- if (file_transfer.size >= 0 && file_transfer.size < 5000000) {
- ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item(conversation, 1, message.id);
- if (content_item != null) {
- stream_interactor.get_module(ContentItemStore.IDENTITY).set_item_hide(content_item, true);
- }
- }
- file_incoming(file_transfer, conversation);
+ // Hide message
+ ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item(conversation, 1, message.id);
+ if (content_item != null) {
+ stream_interactor.get_module(ContentItemStore.IDENTITY).set_item_hide(content_item, true);
}
+
+ var additional_info = message.id.to_string();
+
+ var receive_data = new HttpFileReceiveData();
+ receive_data.url = message.body;
+
+ var file_meta = new HttpFileMeta();
+ file_meta.file_name = Uri.unescape_string(message.body.substring(message.body.last_index_of("/") + 1));
+ file_meta.message = message;
+
+ file_incoming(additional_info, message.from, message.time, message.local_time, conversation, receive_data, file_meta);
}
- public async void get_meta_info(FileTransfer file_transfer) {
- string url_body = dino_db.message.select({dino_db.message.body}).with(dino_db.message.id, "=", int.parse(file_transfer.info))[dino_db.message.body];
+ public async FileMeta get_meta_info(FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta) throws FileReceiveError {
+ HttpFileReceiveData? http_receive_data = receive_data as HttpFileReceiveData;
+ if (http_receive_data == null) return file_meta;
+
var session = new Soup.Session();
- var head_message = new Soup.Message("HEAD", url_body);
+ var head_message = new Soup.Message("HEAD", http_receive_data.url);
+
if (head_message != null) {
- yield session.send_async(head_message, null);
+ try {
+ yield session.send_async(head_message, null);
+ } catch (Error e) {
+ throw new FileReceiveError.GET_METADATA_FAILED("HEAD request failed");
+ }
string? content_type = null, content_length = null;
- print(url_body + ":\n");
head_message.response_headers.foreach((name, val) => {
- print(name + " " + val + "\n");
if (name == "Content-Type") content_type = val;
if (name == "Content-Length") content_length = val;
});
- file_transfer.mime_type = content_type;
+ file_meta.mime_type = content_type;
if (content_length != null) {
- file_transfer.size = int.parse(content_length);
+ file_meta.size = int.parse(content_length);
}
}
+
+ return file_meta;
}
- public async void download(FileTransfer file_transfer, File file_) {
+ public async InputStream download(FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta) throws FileReceiveError {
+ HttpFileReceiveData? http_receive_data = receive_data as HttpFileReceiveData;
+ if (http_receive_data == null) assert(false);
+
try {
- File file = file_;
- string url_body = dino_db.message.select({dino_db.message.body}).with(dino_db.message.id, "=", int.parse(file_transfer.info))[dino_db.message.body];
var session = new Soup.Session();
- Soup.Request request = session.request(url_body);
+ Soup.Request request = session.request(http_receive_data.url);
- file_transfer.input_stream = yield request.send_async(null);
+ return yield request.send_async(null);
+ } catch (Error e) {
+ throw new FileReceiveError.DOWNLOAD_FAILED("Downloading file error: %s".printf(e.message));
+ }
+ }
- foreach (IncomingFileProcessor processor in stream_interactor.get_module(FileManager.IDENTITY).incoming_processors) {
- if (processor.can_process(file_transfer)) {
- processor.process(file_transfer);
- }
- }
+ public FileMeta get_file_meta(FileTransfer file_transfer) throws FileReceiveError {
+ Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(file_transfer.counterpart.bare_jid, file_transfer.account);
+ if (conversation == null) throw new FileReceiveError.GET_METADATA_FAILED("No conversation");
- 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));
- }
+ Message? message = stream_interactor.get_module(MessageStorage.IDENTITY).get_message_by_id(int.parse(file_transfer.info), conversation);
+ if (message == null) throw new FileReceiveError.GET_METADATA_FAILED("No message");
- OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION);
- yield os.splice_async(file_transfer.input_stream, 0);
- os.close();
- file_transfer.path = file.get_basename();
- file_transfer.input_stream = yield file.read_async();
+ var file_meta = new HttpFileMeta();
+ file_meta.size = file_transfer.size;
+ file_meta.mime_type = file_transfer.mime_type;
- file_transfer.state = FileTransfer.State.COMPLETE;
- } catch (Error e) {
- file_transfer.state = FileTransfer.State.FAILED;
+ // Extract file name from URL
+ file_meta.file_name = Uri.unescape_string(message.body.substring(message.body.last_index_of("/") + 1));
+ if (file_meta.file_name.contains("#")) {
+ file_meta.file_name = file_meta.file_name.substring(0, file_meta.file_name.last_index_of("#"));
}
+
+ file_meta.message = message;
+
+ return file_meta;
}
+
+ public FileReceiveData? get_file_receive_data(FileTransfer file_transfer) {
+ Conversation? conversation = stream_interactor.get_module(ConversationManager.IDENTITY).get_conversation(file_transfer.counterpart.bare_jid, file_transfer.account);
+ if (conversation == null) return null;
+
+ Message? message = stream_interactor.get_module(MessageStorage.IDENTITY).get_message_by_id(int.parse(file_transfer.info), conversation);
+ if (message == null) return null;
+
+ var receive_data = new HttpFileReceiveData();
+ receive_data.url = message.body;
+
+ return receive_data;
+ }
+
+ public int get_id() { return 0; }
}
}
diff --git a/plugins/http-files/src/file_sender.vala b/plugins/http-files/src/file_sender.vala
new file mode 100644
index 00000000..ce40d2f8
--- /dev/null
+++ b/plugins/http-files/src/file_sender.vala
@@ -0,0 +1,126 @@
+using Dino.Entities;
+using Xmpp;
+using Gee;
+
+namespace Dino.Plugins.HttpFiles {
+
+public class HttpFileSender : FileSender, Object {
+ private StreamInteractor stream_interactor;
+ private Database db;
+ private HashMap<Account, long> max_file_sizes = new HashMap<Account, long>(Account.hash_func, Account.equals_func);
+
+ public HttpFileSender(StreamInteractor stream_interactor, Database db) {
+ this.stream_interactor = stream_interactor;
+ this.db = db;
+
+ stream_interactor.stream_negotiated.connect(on_stream_negotiated);
+ stream_interactor.get_module(MessageProcessor.IDENTITY).build_message_stanza.connect(check_add_oob);
+ }
+
+ public async FileSendData? prepare_send_file(Conversation conversation, FileTransfer file_transfer) throws FileSendError {
+ HttpFileSendData send_data = new HttpFileSendData();
+ if (send_data == null) return null;
+
+ Xmpp.XmppStream? stream = stream_interactor.get_stream(file_transfer.account);
+ if (stream == null) return null;
+
+ try {
+ var slot_result = yield stream_interactor.module_manager.get_module(file_transfer.account, Xmpp.Xep.HttpFileUpload.Module.IDENTITY).request_slot(stream, file_transfer.server_file_name, file_transfer.size, file_transfer.mime_type);
+ send_data.url_down = slot_result.url_get;
+ send_data.url_up = slot_result.url_put;
+ } catch (Xep.HttpFileUpload.HttpFileTransferError e) {
+ throw new FileSendError.UPLOAD_FAILED("Http file upload XMPP error: %s".printf(e.message));
+ }
+
+ return send_data;
+ }
+
+ public async void send_file(Conversation conversation, FileTransfer file_transfer, FileSendData file_send_data) throws FileSendError {
+ HttpFileSendData? send_data = file_send_data as HttpFileSendData;
+ if (send_data == null) return;
+
+ yield upload(file_transfer, send_data);
+
+ file_transfer.info = send_data.url_down; // store the message content temporarily so the message gets filtered out
+
+ Entities.Message message = stream_interactor.get_module(MessageProcessor.IDENTITY).create_out_message(send_data.url_down, conversation);
+
+ message.encryption = send_data.encrypt_message ? conversation.encryption : Encryption.NONE;
+ stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(message, conversation);
+
+ file_transfer.info = message.id.to_string();
+
+ ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item(conversation, 1, message.id);
+ if (content_item != null) {
+ stream_interactor.get_module(ContentItemStore.IDENTITY).set_item_hide(content_item, true);
+ }
+ }
+
+ public bool can_send(Conversation conversation, FileTransfer file_transfer) {
+ if (!max_file_sizes.has_key(conversation.account)) return false;
+
+ return file_transfer.size < max_file_sizes[conversation.account];
+ }
+
+ public bool is_upload_available(Conversation conversation) {
+ lock (max_file_sizes) {
+ return max_file_sizes.has_key(conversation.account);
+ }
+ }
+
+ public long get_max_file_size(Account account) {
+ lock (max_file_sizes) {
+ return max_file_sizes[account];
+ }
+ }
+
+ private async void upload(FileTransfer file_transfer, HttpFileSendData file_send_data) throws FileSendError {
+ Xmpp.XmppStream? stream = stream_interactor.get_stream(file_transfer.account);
+ if (stream == null) return;
+
+ uint8[] buf = new uint8[256];
+ Array<uint8> data = new Array<uint8>(false, true, 0);
+ size_t len = -1;
+ do {
+ try {
+ len = file_transfer.input_stream.read(buf);
+ } catch (IOError e) {
+ throw new FileSendError.UPLOAD_FAILED("HTTP upload: IOError reading stream: %s".printf(e.message));
+ }
+ data.append_vals(buf, (uint) len);
+ } while(len > 0);
+
+ Soup.Message message = new Soup.Message("PUT", file_send_data.url_up);
+ message.set_request(file_transfer.mime_type, Soup.MemoryUse.COPY, data.data);
+ Soup.Session session = new Soup.Session();
+ try {
+ yield session.send_async(message);
+ if (message.status_code < 200 && message.status_code >= 300) {
+ throw new FileSendError.UPLOAD_FAILED("HTTP status code %s".printf(message.status_code.to_string()));
+ }
+ } catch (Error e) {
+ throw new FileSendError.UPLOAD_FAILED("HTTP upload error: %s".printf(e.message));
+ }
+ }
+
+ private void on_stream_negotiated(Account account, XmppStream stream) {
+ stream_interactor.module_manager.get_module(account, Xmpp.Xep.HttpFileUpload.Module.IDENTITY).feature_available.connect((stream, max_file_size) => {
+ lock (max_file_sizes) {
+ max_file_sizes[account] = max_file_size;
+ }
+ upload_available(account);
+ });
+ }
+
+ private void check_add_oob(Entities.Message message, Xmpp.MessageStanza message_stanza, Conversation conversation) {
+ if (message.encryption == Encryption.NONE && message_is_file(db, message) && message.body.has_prefix("http")) {
+ Xep.OutOfBandData.add_url_to_message(message_stanza, message_stanza.body);
+ }
+ }
+
+ public int get_id() { return 0; }
+
+ public float get_priority() { return 100; }
+}
+
+}
diff --git a/plugins/http-files/src/manager.vala b/plugins/http-files/src/manager.vala
deleted file mode 100644
index 2f702be3..00000000
--- a/plugins/http-files/src/manager.vala
+++ /dev/null
@@ -1,147 +0,0 @@
-using Dino.Entities;
-using Xmpp;
-using Gee;
-
-namespace Dino.Plugins.HttpFiles {
-
-public class Manager : StreamInteractionModule, FileSender, Object {
- public static ModuleIdentity<Manager> IDENTITY = new ModuleIdentity<Manager>("http_files");
- public string id { get { return IDENTITY.id; } }
-
- public signal void uploading(FileTransfer file_transfer);
- public signal void uploaded(FileTransfer file_transfer, string url);
-
- private StreamInteractor stream_interactor;
- private Database db;
- private HashMap<Account, long> max_file_sizes = new HashMap<Account, long>(Account.hash_func, Account.equals_func);
-
- public static void start(StreamInteractor stream_interactor, Database db) {
- Manager m = new Manager(stream_interactor, db);
- stream_interactor.add_module(m);
- }
-
- private Manager(StreamInteractor stream_interactor, Database db) {
- this.stream_interactor = stream_interactor;
- this.db = db;
-
- stream_interactor.get_module(FileManager.IDENTITY).add_sender(this);
- stream_interactor.stream_negotiated.connect(on_stream_negotiated);
- stream_interactor.get_module(MessageProcessor.IDENTITY).build_message_stanza.connect(check_add_oob);
- }
-
- public delegate void OnUploadOk(XmppStream stream, string url_down);
- public delegate void OnError(XmppStream stream, string error);
- public void upload(XmppStream stream, FileTransfer file_transfer, owned OnUploadOk listener, owned OnError error_listener) {
- uint8[] buf = new uint8[256];
- Array<uint8> data = new Array<uint8>(false, true, 0);
- size_t len = -1;
- do {
- try {
- len = file_transfer.input_stream.read(buf);
- } catch (IOError error) {
- error_listener(stream, @"HTTP upload: IOError reading stream: $(error.message)");
- }
- data.append_vals(buf, (uint) len);
- } while(len > 0);
-
- stream_interactor.module_manager.get_module(file_transfer.account, Xmpp.Xep.HttpFileUpload.Module.IDENTITY).request_slot(stream, file_transfer.server_file_name, (int) data.length, file_transfer.mime_type,
- (stream, url_down, url_up) => {
- Soup.Message message = new Soup.Message("PUT", url_up);
- message.set_request(file_transfer.mime_type, Soup.MemoryUse.COPY, data.data);
- Soup.Session session = new Soup.Session();
- session.send_async.begin(message, null, (obj, res) => {
- try {
- session.send_async.end(res);
- if (message.status_code >= 200 && message.status_code < 300) {
- listener(stream, url_down);
- } else {
- error_listener(stream, "HTTP status code " + message.status_code.to_string());
- }
- } catch (Error e) {
- error_listener(stream, e.message);
- }
- });
- },
- (stream, error) => error_listener(stream, error));
- }
-
- public void send_file(Conversation conversation, FileTransfer file_transfer) {
- Xmpp.XmppStream? stream = stream_interactor.get_stream(file_transfer.account);
- if (stream != null) {
- upload(stream, file_transfer,
- (stream, url_down) => {
- uploaded(file_transfer, url_down);
- file_transfer.info = url_down; // store the message content temporarily so the message gets filtered out
- Entities.Message message = stream_interactor.get_module(MessageProcessor.IDENTITY).create_out_message(url_down, conversation);
- message.encryption = Encryption.NONE;
- stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(message, conversation);
- file_transfer.info = message.id.to_string();
-
- ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item(conversation, 1, message.id);
- if (content_item != null) {
- stream_interactor.get_module(ContentItemStore.IDENTITY).set_item_hide(content_item, true);
- }
- },
- (stream, error_str) => {
- warning("Failed getting upload url: %s", error_str);
- file_transfer.state = FileTransfer.State.FAILED;
- }
- );
- }
- }
-
- public bool can_send(Conversation conversation, FileTransfer file_transfer) {
- return file_transfer.encryption != Encryption.OMEMO;
- }
-
- public bool is_upload_available(Conversation conversation) {
- lock (max_file_sizes) {
- return max_file_sizes.has_key(conversation.account);
- }
- }
-
- public long get_max_file_size(Account account) {
- lock (max_file_sizes) {
- return max_file_sizes[account];
- }
- }
-
- private void on_stream_negotiated(Account account, XmppStream stream) {
- stream_interactor.module_manager.get_module(account, Xmpp.Xep.HttpFileUpload.Module.IDENTITY).feature_available.connect((stream, max_file_size) => {
- lock (max_file_sizes) {
- max_file_sizes[account] = max_file_size;
- }
- upload_available(account);
- });
- }
-
- private void check_add_oob(Entities.Message message, Xmpp.MessageStanza message_stanza, Conversation conversation) {
- if (message_is_file(db, message) && message.body.has_prefix("http")) {
- Xep.OutOfBandData.add_url_to_message(message_stanza, message_stanza.body);
- }
- }
-}
-
-public class FileMessageFilter : ContentFilter, Object {
- public Database db;
-
- public FileMessageFilter(Dino.Database db) {
- this.db = db;
- }
-
- public bool discard(ContentItem content_item) {
- if (content_item.type_ == MessageItem.TYPE) {
- MessageItem message_item = content_item as MessageItem;
- return message_is_file(db, message_item.message);
- }
- return false;
- }
-}
-
-private bool message_is_file(Database db, Entities.Message message) {
- Qlite.QueryBuilder builder = db.file_transfer.select({db.file_transfer.id}).with(db.file_transfer.info, "=", message.id.to_string());
- Qlite.QueryBuilder builder2 = db.file_transfer.select({db.file_transfer.id}).with(db.file_transfer.info, "=", message.body);
- return builder.count() > 0 || builder2.count() > 0;
-}
-
-}
diff --git a/plugins/http-files/src/message_filter.vala b/plugins/http-files/src/message_filter.vala
new file mode 100644
index 00000000..01035c4c
--- /dev/null
+++ b/plugins/http-files/src/message_filter.vala
@@ -0,0 +1,23 @@
+using Dino.Entities;
+using Xmpp;
+using Gee;
+
+namespace Dino.Plugins.HttpFiles {
+
+public class FileMessageFilter : ContentFilter, Object {
+ public Database db;
+
+ public FileMessageFilter(Dino.Database db) {
+ this.db = db;
+ }
+
+ public bool discard(ContentItem content_item) {
+ if (content_item.type_ == MessageItem.TYPE) {
+ MessageItem message_item = content_item as MessageItem;
+ return message_is_file(db, message_item.message);
+ }
+ return false;
+ }
+}
+
+}
diff --git a/plugins/http-files/src/plugin.vala b/plugins/http-files/src/plugin.vala
index e2ce5ad9..bdf48770 100644
--- a/plugins/http-files/src/plugin.vala
+++ b/plugins/http-files/src/plugin.vala
@@ -7,14 +7,17 @@ public class Plugin : RootInterface, Object {
public Dino.Application app;
public FileProvider file_provider;
+ public FileSender file_sender;
public void registered(Dino.Application app) {
this.app = app;
- Manager.start(this.app.stream_interactor, app.db);
file_provider = new FileProvider(app.stream_interactor, app.db);
+ file_sender = new HttpFileSender(app.stream_interactor, app.db);
app.stream_interactor.get_module(FileManager.IDENTITY).add_provider(file_provider);
+ app.stream_interactor.get_module(FileManager.IDENTITY).add_sender(file_sender);
+
app.stream_interactor.get_module(ContentItemStore.IDENTITY).add_filter(new FileMessageFilter(app.db));
}
@@ -23,4 +26,10 @@ public class Plugin : RootInterface, Object {
}
}
+private bool message_is_file(Database db, Entities.Message message) {
+ Qlite.QueryBuilder builder = db.file_transfer.select({db.file_transfer.id}).with(db.file_transfer.info, "=", message.id.to_string());
+ Qlite.QueryBuilder builder2 = db.file_transfer.select({db.file_transfer.id}).with(db.file_transfer.info, "=", message.body);
+ return builder.count() > 0 || builder2.count() > 0;
+}
+
}
diff --git a/plugins/omemo/CMakeLists.txt b/plugins/omemo/CMakeLists.txt
index 8639478c..482b49fa 100644
--- a/plugins/omemo/CMakeLists.txt
+++ b/plugins/omemo/CMakeLists.txt
@@ -34,8 +34,8 @@ SOURCES
src/register_plugin.vala
src/trust_level.vala
- src/file_transfer/file_provider.vala
- src/file_transfer/file_sender.vala
+ src/file_transfer/file_decryptor.vala
+ src/file_transfer/file_encryptor.vala
src/logic/database.vala
src/logic/encrypt_state.vala
diff --git a/plugins/omemo/src/file_transfer/file_decryptor.vala b/plugins/omemo/src/file_transfer/file_decryptor.vala
new file mode 100644
index 00000000..dcb65301
--- /dev/null
+++ b/plugins/omemo/src/file_transfer/file_decryptor.vala
@@ -0,0 +1,89 @@
+using Dino.Entities;
+
+using Signal;
+
+namespace Dino.Plugins.Omemo {
+
+public class OmemoHttpFileReceiveData : HttpFileReceiveData {
+ public string original_url;
+}
+
+public class OmemoFileDecryptor : FileDecryptor, Object {
+
+ private Regex url_regex = /^aesgcm:\/\/(.*)#(([A-Fa-f0-9]{2}){48}|([A-Fa-f0-9]{2}){44})$/;
+
+ public FileReceiveData prepare_get_meta_info(Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data) {
+ HttpFileReceiveData? http_receive_data = receive_data as HttpFileReceiveData;
+ if (http_receive_data == null) assert(false);
+// if ((receive_data as OmemoHttpFileReceiveData) != null) return receive_data;
+
+ var omemo_http_receive_data = new OmemoHttpFileReceiveData();
+ omemo_http_receive_data.url = aesgcm_to_https_link(http_receive_data.url);
+ omemo_http_receive_data.original_url = http_receive_data.url;
+
+ return omemo_http_receive_data;
+ }
+
+ public FileMeta prepare_download_file(Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta) {
+ if (file_meta.file_name != null) {
+ file_meta.file_name = file_meta.file_name.split("#")[0];
+ }
+ return file_meta;
+ }
+
+ public bool can_decrypt_file(Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data) {
+ HttpFileReceiveData? http_file_receive = receive_data as HttpFileReceiveData;
+ if (http_file_receive == null) return false;
+
+ return this.url_regex.match(http_file_receive.url) || (receive_data as OmemoHttpFileReceiveData) != null;
+ }
+
+ public async InputStream decrypt_file(InputStream encrypted_stream, Conversation conversation, FileTransfer file_transfer, FileReceiveData receive_data) {
+ OmemoHttpFileReceiveData? omemo_http_receive_data = receive_data as OmemoHttpFileReceiveData;
+ if (omemo_http_receive_data == null) assert(false);
+
+ // Decode IV and key
+ MatchInfo match_info;
+ this.url_regex.match(omemo_http_receive_data.original_url, 0, out match_info);
+ uint8[] iv_and_key = hex_to_bin(match_info.fetch(2).up());
+ uint8[] iv, key;
+ if (iv_and_key.length == 44) {
+ iv = iv_and_key[0:12];
+ key = iv_and_key[12:44];
+ } else {
+ iv = iv_and_key[0:16];
+ key = iv_and_key[16:48];
+ }
+
+ // Read data
+ uint8[] buf = new uint8[256];
+ Array<uint8> data = new Array<uint8>(false, true, 0);
+ size_t len = -1;
+ do {
+ len = yield encrypted_stream.read_async(buf);
+ data.append_vals(buf, (uint) len);
+ } while(len > 0);
+
+ // Decrypt
+ uint8[] cleartext = Signal.aes_decrypt(Cipher.AES_GCM_NOPADDING, key, iv, data.data);
+ file_transfer.encryption = Encryption.OMEMO;
+ return new MemoryInputStream.from_data(cleartext);
+ }
+
+ private uint8[] hex_to_bin(string hex) {
+ uint8[] bin = new uint8[hex.length / 2];
+ const string HEX = "0123456789ABCDEF";
+ for (int i = 0; i < hex.length / 2; i++) {
+ bin[i] = (uint8) (HEX.index_of_char(hex[i*2]) << 4) | HEX.index_of_char(hex[i*2+1]);
+ }
+ return bin;
+ }
+
+ private string aesgcm_to_https_link(string aesgcm_link) {
+ MatchInfo match_info;
+ this.url_regex.match(aesgcm_link, 0, out match_info);
+ return "https://" + match_info.fetch(1);
+ }
+}
+
+}
diff --git a/plugins/omemo/src/file_transfer/file_encryptor.vala b/plugins/omemo/src/file_transfer/file_encryptor.vala
new file mode 100644
index 00000000..a5445153
--- /dev/null
+++ b/plugins/omemo/src/file_transfer/file_encryptor.vala
@@ -0,0 +1,74 @@
+using Gee;
+using Gtk;
+
+using Dino.Entities;
+using Xmpp;
+using Signal;
+
+namespace Dino.Plugins.Omemo {
+
+public class OmemoHttpFileMeta : HttpFileMeta {
+ public uint8[] iv;
+ public uint8[] key;
+}
+
+public class OmemoFileEncryptor : Dino.FileEncryptor, Object {
+
+ public bool can_encrypt_file(Conversation conversation, FileTransfer file_transfer) {
+ return file_transfer.encryption == Encryption.OMEMO;
+ }
+
+ public FileMeta encrypt_file(Conversation conversation, FileTransfer file_transfer) throws FileSendError {
+ var omemo_http_file_meta = new OmemoHttpFileMeta();
+
+ try {
+ uint8[] buf = new uint8[256];
+ Array<uint8> data = new Array<uint8>(false, true, 0);
+ size_t len = -1;
+ do {
+ len = file_transfer.input_stream.read(buf);
+ data.append_vals(buf, (uint) len);
+ } while(len > 0);
+
+ //Create a key and use it to encrypt the file
+ uint8[] iv = new uint8[16];
+ Plugin.get_context().randomize(iv);
+ uint8[] key = new uint8[32];
+ Plugin.get_context().randomize(key);
+ uint8[] ciphertext = aes_encrypt(Cipher.AES_GCM_NOPADDING, key, iv, data.data);
+
+ omemo_http_file_meta.iv = iv;
+ omemo_http_file_meta.key = key;
+ omemo_http_file_meta.size = ciphertext.length;
+ omemo_http_file_meta.mime_type = "pgp";
+ file_transfer.input_stream = new MemoryInputStream.from_data(ciphertext, GLib.free);
+ } catch (Error error) {
+ throw new FileSendError.ENCRYPTION_FAILED("HTTP upload: Error encrypting stream: %s".printf(error.message));
+ }
+
+ return omemo_http_file_meta;
+ }
+
+ 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;
+
+ OmemoHttpFileMeta? omemo_http_file_meta = file_meta as OmemoHttpFileMeta;
+ if (omemo_http_file_meta == null) return null;
+
+ // Convert iv and key to hex
+ string iv_and_key = "";
+ foreach (uint8 byte in omemo_http_file_meta.iv) iv_and_key += byte.to_string("%02x");
+ foreach (uint8 byte in omemo_http_file_meta.key) iv_and_key += byte.to_string("%02x");
+
+ string aesgcm_link = send_data.url_down + "#" + iv_and_key;
+ aesgcm_link = "aesgcm://" + aesgcm_link.substring(8); // replace https:// by aesgcm://
+
+ send_data.url_down = aesgcm_link;
+ send_data.encrypt_message = true;
+
+ return file_send_data;
+ }
+}
+
+}
diff --git a/plugins/omemo/src/file_transfer/file_provider.vala b/plugins/omemo/src/file_transfer/file_provider.vala
deleted file mode 100644
index 938ec0cf..00000000
--- a/plugins/omemo/src/file_transfer/file_provider.vala
+++ /dev/null
@@ -1,168 +0,0 @@
-using Gee;
-using Gtk;
-
-using Dino.Entities;
-using Xmpp;
-using Signal;
-
-namespace Dino.Plugins.Omemo {
-
-public class FileProvider : Dino.FileProvider, Object {
- public string id { get { return "aesgcm"; } }
-
- private StreamInteractor stream_interactor;
- private Dino.Database dino_db;
- private Regex url_regex;
-
- public FileProvider(StreamInteractor stream_interactor, Dino.Database dino_db) {
- this.stream_interactor = stream_interactor;
- this.dino_db = dino_db;
- this.url_regex = /^aesgcm:\/\/(.*)#(([A-Fa-f0-9]{2}){48}|([A-Fa-f0-9]{2}){44})$/;
-
- stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(new ReceivedMessageListener(this));
- }
-
- private class ReceivedMessageListener : MessageListener {
-
- public string[] after_actions_const = new string[]{ "STORE" };
- public override string action_group { get { return ""; } }
- public override string[] after_actions { get { return after_actions_const; } }
-
- private FileProvider outer;
- private StreamInteractor stream_interactor;
-
- public ReceivedMessageListener(FileProvider outer) {
- this.outer = outer;
- this.stream_interactor = outer.stream_interactor;
- }
-
- public override async bool run(Entities.Message message, Xmpp.MessageStanza stanza, Conversation conversation) {
- if (message.body.has_prefix("aesgcm://") && outer.url_regex.match(message.body)) {
- yield outer.on_file_message(message, conversation);
- }
- return false;
- }
- }
-
- private async void on_file_message(Entities.Message message, Conversation conversation) {
- MatchInfo match_info;
- this.url_regex.match(message.body, 0, out match_info);
- string url_without_hash = match_info.fetch(1);
-
- FileTransfer file_transfer = new FileTransfer();
- file_transfer.account = conversation.account;
- file_transfer.counterpart = message.counterpart;
- file_transfer.ourpart = message.ourpart;
- file_transfer.encryption = Encryption.NONE;
- file_transfer.time = message.time;
- file_transfer.local_time = message.local_time;
- file_transfer.direction = message.direction;
- file_transfer.file_name = url_without_hash.substring(url_without_hash.last_index_of("/") + 1);
- file_transfer.size = -1;
- file_transfer.state = FileTransfer.State.NOT_STARTED;
- file_transfer.provider = 0;
- file_transfer.info = message.id.to_string();
-
- if (stream_interactor.get_module(FileManager.IDENTITY).is_sender_trustworthy(file_transfer, conversation)) {
- yield get_meta_info(file_transfer);
- if (file_transfer.size >= 0 && file_transfer.size < 5000000) {
- ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item(conversation, 1, message.id);
- if (content_item != null) {
- stream_interactor.get_module(ContentItemStore.IDENTITY).set_item_hide(content_item, true);
- }
- }
- file_incoming(file_transfer, conversation);
- }
- }
-
- public async void get_meta_info(FileTransfer file_transfer) {
- string url_body = dino_db.message.select({dino_db.message.body}).with(dino_db.message.id, "=", int.parse(file_transfer.info))[dino_db.message.body];
- string url = this.aesgcm_to_https_link(url_body);
- var session = new Soup.Session();
- var head_message = new Soup.Message("HEAD", url);
- if (head_message != null) {
- yield session.send_async(head_message, null);
-
- if (head_message.status_code >= 200 && head_message.status_code < 300) {
- string? content_type = null, content_length = null;
- head_message.response_headers.foreach((name, val) => {
- if (name == "Content-Type") content_type = val;
- if (name == "Content-Length") content_length = val;
- });
- file_transfer.mime_type = content_type;
- if (content_length != null) {
- file_transfer.size = int.parse(content_length);
- }
- } else {
- warning("HTTP HEAD download status code " + head_message.status_code.to_string());
- }
- }
- }
-
- public async void download(FileTransfer file_transfer, File file) {
- try {
- string url_body = dino_db.message.select({dino_db.message.body}).with(dino_db.message.id, "=", int.parse(file_transfer.info))[dino_db.message.body];
- string url = this.aesgcm_to_https_link(url_body);
- var session = new Soup.Session();
- Soup.Request request = session.request(url);
-
- file_transfer.input_stream = yield decrypt_file(yield request.send_async(null), url_body);
- file_transfer.encryption = Encryption.OMEMO;
-
- OutputStream os = file.create(FileCreateFlags.REPLACE_DESTINATION);
- yield os.splice_async(file_transfer.input_stream, 0);
- os.close();
- file_transfer.path = file.get_basename();
- file_transfer.input_stream = yield file.read_async();
-
- file_transfer.state = FileTransfer.State.COMPLETE;
- } catch (Error e) {
- file_transfer.state = FileTransfer.State.FAILED;
- }
- }
-
- public async InputStream? decrypt_file(InputStream input_stream, string url) {
- // Decode IV and key
- MatchInfo match_info;
- this.url_regex.match(url, 0, out match_info);
- uint8[] iv_and_key = hex_to_bin(match_info.fetch(2).up());
- uint8[] iv, key;
- if (iv_and_key.length == 44) {
- iv = iv_and_key[0:12];
- key = iv_and_key[12:44];
- } else {
- iv = iv_and_key[0:16];
- key = iv_and_key[16:48];
- }
-
- // Read data
- uint8[] buf = new uint8[256];
- Array<uint8> data = new Array<uint8>(false, true, 0);
- size_t len = -1;
- do {
- len = yield input_stream.read_async(buf);
- data.append_vals(buf, (uint) len);
- } while(len > 0);
-
- // Decrypt
- uint8[] cleartext = Signal.aes_decrypt(Cipher.AES_GCM_NOPADDING, key, iv, data.data);
- return new MemoryInputStream.from_data(cleartext);
- }
-
- private uint8[] hex_to_bin(string hex) {
- uint8[] bin = new uint8[hex.length / 2];
- const string HEX = "0123456789ABCDEF";
- for (int i = 0; i < hex.length / 2; i++) {
- bin[i] = (uint8) (HEX.index_of_char(hex[i*2]) << 4) | HEX.index_of_char(hex[i*2+1]);
- }
- return bin;
- }
-
- private string aesgcm_to_https_link(string aesgcm_link) {
- MatchInfo match_info;
- this.url_regex.match(aesgcm_link, 0, out match_info);
- return "https://" + match_info.fetch(1);
- }
-}
-
-}
diff --git a/plugins/omemo/src/file_transfer/file_sender.vala b/plugins/omemo/src/file_transfer/file_sender.vala
deleted file mode 100644
index b63d3dc5..00000000
--- a/plugins/omemo/src/file_transfer/file_sender.vala
+++ /dev/null
@@ -1,108 +0,0 @@
-using Dino.Entities;
-using Gee;
-using Signal;
-using Xmpp;
-
-namespace Dino.Plugins.Omemo {
-
-public class AesGcmFileSender : StreamInteractionModule, FileSender, Object {
- public static ModuleIdentity<Manager> IDENTITY = new ModuleIdentity<Manager>("http_files");
- public string id { get { return IDENTITY.id; } }
-
-
- private StreamInteractor stream_interactor;
- private HashMap<Account, long> max_file_sizes = new HashMap<Account, long>(Account.hash_func, Account.equals_func);
-
- public AesGcmFileSender(StreamInteractor stream_interactor) {
- this.stream_interactor = stream_interactor;
-
- stream_interactor.stream_negotiated.connect(on_stream_negotiated);
- }
-
- public void send_file(Conversation conversation, FileTransfer file_transfer) {
- Xmpp.XmppStream? stream = stream_interactor.get_stream(file_transfer.account);
- uint8[] buf = new uint8[256];
- Array<uint8> data = new Array<uint8>(false, true, 0);
- size_t len = -1;
- do {
- try {
- len = file_transfer.input_stream.read(buf);
- } catch (IOError error) {
- warning(@"HTTP upload: IOError reading stream: $(error.message)");
- file_transfer.state = FileTransfer.State.FAILED;
- }
- data.append_vals(buf, (uint) len);
- } while(len > 0);
-
- //Create a key and use it to encrypt the file
- uint8[] iv = new uint8[16];
- Plugin.get_context().randomize(iv);
- uint8[] key = new uint8[32];
- Plugin.get_context().randomize(key);
- uint8[] ciphertext = aes_encrypt(Cipher.AES_GCM_NOPADDING, key, iv, data.data);
-
- // Convert iv and key to hex
- string iv_and_key = "";
- foreach (uint8 byte in iv) iv_and_key += byte.to_string("%02x");
- foreach (uint8 byte in key) iv_and_key += byte.to_string("%02x");
-
- stream_interactor.module_manager.get_module(file_transfer.account, Xmpp.Xep.HttpFileUpload.Module.IDENTITY).request_slot(stream, file_transfer.server_file_name, (int) ciphertext.length, file_transfer.mime_type,
- (stream, url_down, url_up) => {
- Soup.Message message = new Soup.Message("PUT", url_up);
- message.set_request(file_transfer.mime_type, Soup.MemoryUse.COPY, ciphertext);
- Soup.Session session = new Soup.Session();
- session.send_async.begin(message, null, (obj, res) => {
- try {
- session.send_async.end(res);
- if (message.status_code >= 200 && message.status_code < 300) {
- string aesgcm_link = url_down + "#" + iv_and_key;
- aesgcm_link = "aesgcm://" + aesgcm_link.substring(8); // replace https:// by aesgcm://
-
- file_transfer.info = aesgcm_link; // store the message content temporarily so the message gets filtered out
- Entities.Message xmpp_message = stream_interactor.get_module(MessageProcessor.IDENTITY).create_out_message(aesgcm_link, conversation);
- xmpp_message.encryption = Encryption.OMEMO;
- stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(xmpp_message, conversation);
- file_transfer.info = xmpp_message.id.to_string();
-
- ContentItem? content_item = stream_interactor.get_module(ContentItemStore.IDENTITY).get_item(conversation, 1, xmpp_message.id);
- if (content_item != null) {
- stream_interactor.get_module(ContentItemStore.IDENTITY).set_item_hide(content_item, true);
- }
- } else {
- warning("HTTP upload status code " + message.status_code.to_string());
- file_transfer.state = FileTransfer.State.FAILED;
- }
- } catch (Error e) {
- warning("HTTP upload error: " + e.message);
- file_transfer.state = FileTransfer.State.FAILED;
- }
- });
- },
- (stream, error) => {
- warning("HTTP upload error: " + error);
- file_transfer.state = FileTransfer.State.FAILED;
- }
- );
- }
-
- public bool can_send(Conversation conversation, FileTransfer file_transfer) {
- return file_transfer.encryption == Encryption.OMEMO;
- }
-
- public bool is_upload_available(Conversation conversation) {
- lock (max_file_sizes) {
- return max_file_sizes.has_key(conversation.account);
- }
- }
-
- private void on_stream_negotiated(Account account, XmppStream stream) {
- stream_interactor.module_manager.get_module(account, Xmpp.Xep.HttpFileUpload.Module.IDENTITY).feature_available.connect((stream, max_file_size) => {
- lock (max_file_sizes) {
- max_file_sizes[account] = max_file_size;
- }
- upload_available(account);
- });
- }
-}
-
-}
diff --git a/plugins/omemo/src/plugin.vala b/plugins/omemo/src/plugin.vala
index 2cb96e10..9f1252c0 100644
--- a/plugins/omemo/src/plugin.vala
+++ b/plugins/omemo/src/plugin.vala
@@ -50,8 +50,9 @@ public class Plugin : RootInterface, Object {
this.own_notifications = new OwnNotifications(this, this.app.stream_interactor, account);
});
- app.stream_interactor.get_module(FileManager.IDENTITY).add_provider(new FileProvider(app.stream_interactor, app.db));
- this.app.stream_interactor.get_module(FileManager.IDENTITY).add_sender(new AesGcmFileSender(app.stream_interactor));
+ app.stream_interactor.get_module(FileManager.IDENTITY).add_file_decryptor(new OmemoFileDecryptor());
+ app.stream_interactor.get_module(FileManager.IDENTITY).add_file_encryptor(new OmemoFileEncryptor());
+
Manager.start(this.app.stream_interactor, db, trust_manager);
SimpleAction own_keys_action = new SimpleAction("own-keys", VariantType.INT32);
diff --git a/plugins/openpgp/CMakeLists.txt b/plugins/openpgp/CMakeLists.txt
index ab406123..eb50dc71 100644
--- a/plugins/openpgp/CMakeLists.txt
+++ b/plugins/openpgp/CMakeLists.txt
@@ -28,14 +28,15 @@ compile_gresources(
vala_precompile(OPENPGP_VALA_C
SOURCES
+ src/file_transfer/file_decryptor.vala
+ src/file_transfer/file_encryptor.vala
+
src/account_settings_entry.vala
src/account_settings_widget.vala
src/contact_details_provider.vala
src/database.vala
src/encryption_list_entry.vala
- src/in_file_processor.vala
src/manager.vala
- src/out_file_processor.vala
src/plugin.vala
src/register_plugin.vala
src/stream_flag.vala
diff --git a/plugins/openpgp/src/in_file_processor.vala b/plugins/openpgp/src/file_transfer/file_decryptor.vala
index 918f824a..7668023e 100644
--- a/plugins/openpgp/src/in_file_processor.vala
+++ b/plugins/openpgp/src/file_transfer/file_decryptor.vala
@@ -2,31 +2,40 @@ using Dino.Entities;
namespace Dino.Plugins.OpenPgp {
-public class InFileProcessor : IncomingFileProcessor, Object {
- public bool can_process(FileTransfer file_transfer) {
+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 void process(FileTransfer file_transfer) {
+ 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 = file_transfer.input_stream.read(buf);
+ 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.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);
}
+ return new MemoryInputStream.from_data(clear_data.data, GLib.free);
} catch (Error e) {
- file_transfer.state = FileTransfer.State.FAILED;
+ 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/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));
}