diff options
Diffstat (limited to 'plugins/http-files/src/file_provider.vala')
-rw-r--r-- | plugins/http-files/src/file_provider.vala | 50 |
1 files changed, 38 insertions, 12 deletions
diff --git a/plugins/http-files/src/file_provider.vala b/plugins/http-files/src/file_provider.vala index 493aaa61..bfeca922 100644 --- a/plugins/http-files/src/file_provider.vala +++ b/plugins/http-files/src/file_provider.vala @@ -20,23 +20,39 @@ public class FileProvider : Dino.FileProvider, Object { this.url_regex = new 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`!()\[\]{};:'".,<>?«»“”‘’]))$"""); this.file_ext_regex = new Regex("""\.(png|jpg|jpeg|svg|gif|pgp)$"""); - stream_interactor.get_module(MessageProcessor.IDENTITY).message_received.connect(check_in_message); stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect(check_out_message); + stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(new ReceivedMessageListener(this)); stream_interactor.get_module(Manager.IDENTITY).uploaded.connect((file_transfer, url) => { file_transfer.info = url; ignore_once.add(url); }); } - private void check_in_message(Message message, Conversation conversation) { - if (!url_regex.match(message.body)) return; - Jid relevant_jid = stream_interactor.get_module(MucManager.IDENTITY).get_real_jid(message.from, conversation.account) ?? conversation.counterpart; - bool in_roster = stream_interactor.get_module(RosterManager.IDENTITY).get_roster_item(conversation.account, relevant_jid) != null; - if (message.direction == Message.DIRECTION_RECEIVED && !in_roster) return; + private class ReceivedMessageListener : MessageListener { + + public string[] after_actions_const = new string[]{ "" }; + public override string action_group { get { return "DECRYPT"; } } + public override string[] after_actions { get { return after_actions_const; } } - string? oob_url = Xmpp.Xep.OutOfBandData.get_url_from_message(message.stanza); - if ((oob_url != null && oob_url == message.body) || file_ext_regex.match(message.body)) { - download_url(message, conversation); + 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 (!outer.url_regex.match(message.body)) return false; + Jid relevant_jid = stream_interactor.get_module(MucManager.IDENTITY).get_real_jid(message.from, conversation.account) ?? conversation.counterpart; + bool in_roster = stream_interactor.get_module(RosterManager.IDENTITY).get_roster_item(conversation.account, relevant_jid) != null; + if (message.direction == Message.DIRECTION_RECEIVED && !in_roster) return false; + + string? oob_url = Xmpp.Xep.OutOfBandData.get_url_from_message(message.stanza); + if ((oob_url != null && oob_url == message.body) || outer.file_ext_regex.match(message.body)) { + yield outer.download_url(message, conversation); + } + return false; } } @@ -45,14 +61,15 @@ public class FileProvider : Dino.FileProvider, Object { if (message.body.length < 5) return; if (!url_regex.match(message.body)) return; if (!file_ext_regex.match(message.body)) return; - download_url(message, conversation); } - private void download_url(Message message, Conversation conversation) { + private async bool download_url(Message message, Conversation conversation) { + bool success = false; var session = new Soup.Session(); var head_message = new Soup.Message("HEAD", message.body); if (head_message != null) { + SourceFunc callback = download_url.callback; session.send_async.begin(head_message, null, (obj, res) => { string? content_type = null, content_length = null; print(message.body + ":\n"); @@ -69,6 +86,7 @@ public class FileProvider : Dino.FileProvider, Object { try { file_transfer.input_stream = request.send_async.end(res); } catch (Error e) { + Idle.add((owned)callback); return; } file_transfer.account = conversation.account; @@ -85,11 +103,19 @@ public class FileProvider : Dino.FileProvider, Object { file_transfer.provider = 0; file_transfer.info = message.body; file_incoming(file_transfer); + success = true; + Idle.add((owned)callback); }); - } catch (Error e) { } + } catch (Error e) { + Idle.add((owned)callback); + } + } else { + Idle.add((owned)callback); } }); + yield; } + return success; } } |