2017-08-29 22:03:37 +00:00
|
|
|
using Gee;
|
|
|
|
using Gtk;
|
|
|
|
|
|
|
|
using Dino.Entities;
|
2018-01-12 20:03:09 +00:00
|
|
|
using Xmpp;
|
2017-08-29 22:03:37 +00:00
|
|
|
|
|
|
|
namespace Dino.Plugins.HttpFiles {
|
|
|
|
|
2017-10-14 17:34:30 +00:00
|
|
|
public class FileProvider : Dino.FileProvider, Object {
|
2017-08-29 22:03:37 +00:00
|
|
|
public string id { get { return "http"; } }
|
|
|
|
|
|
|
|
private StreamInteractor stream_interactor;
|
2018-11-14 17:17:10 +00:00
|
|
|
private Dino.Database dino_db;
|
2017-08-29 22:03:37 +00:00
|
|
|
private Regex url_regex;
|
|
|
|
|
|
|
|
public FileProvider(StreamInteractor stream_interactor, Dino.Database dino_db) {
|
|
|
|
this.stream_interactor = stream_interactor;
|
2018-11-14 17:17:10 +00:00
|
|
|
this.dino_db = dino_db;
|
2019-03-14 16:38:48 +00:00
|
|
|
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`!()\[\]{};:'".,<>?«»“”‘’]))$/;
|
2017-08-29 22:03:37 +00:00
|
|
|
|
2018-01-19 21:37:02 +00:00
|
|
|
stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(new ReceivedMessageListener(this));
|
2017-08-29 22:03:37 +00:00
|
|
|
}
|
|
|
|
|
2018-01-19 21:37:02 +00:00
|
|
|
private class ReceivedMessageListener : MessageListener {
|
|
|
|
|
2018-08-27 12:57:02 +00:00
|
|
|
public string[] after_actions_const = new string[]{ "STORE" };
|
|
|
|
public override string action_group { get { return ""; } }
|
2018-01-19 21:37:02 +00:00
|
|
|
public override string[] after_actions { get { return after_actions_const; } }
|
2017-12-11 17:39:55 +00:00
|
|
|
|
2018-01-19 21:37:02 +00:00
|
|
|
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) {
|
2018-11-14 17:17:10 +00:00
|
|
|
if (outer.url_regex.match(message.body)) {
|
2019-05-21 19:42:39 +00:00
|
|
|
string? oob_url = Xmpp.Xep.OutOfBandData.get_url_from_message(stanza);
|
2018-11-14 17:17:10 +00:00
|
|
|
if (oob_url != null && oob_url == message.body) {
|
2018-11-23 19:11:32 +00:00
|
|
|
yield outer.on_file_message(message, conversation);
|
2018-11-14 17:17:10 +00:00
|
|
|
}
|
2018-01-19 21:37:02 +00:00
|
|
|
}
|
|
|
|
return false;
|
2017-12-11 17:39:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 17:17:10 +00:00
|
|
|
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);
|
2018-11-27 17:28:28 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 17:17:10 +00:00
|
|
|
file_incoming(file_transfer, conversation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-27 17:28:28 +00:00
|
|
|
public async void get_meta_info(FileTransfer file_transfer) {
|
2018-11-14 17:17:10 +00:00
|
|
|
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];
|
2017-08-29 22:03:37 +00:00
|
|
|
var session = new Soup.Session();
|
2018-11-14 17:17:10 +00:00
|
|
|
var head_message = new Soup.Message("HEAD", url_body);
|
2017-08-29 22:03:37 +00:00
|
|
|
if (head_message != null) {
|
2018-11-14 17:17:10 +00:00
|
|
|
yield session.send_async(head_message, null);
|
|
|
|
|
|
|
|
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;
|
2017-11-23 23:38:09 +00:00
|
|
|
});
|
2018-11-14 17:17:10 +00:00
|
|
|
file_transfer.mime_type = content_type;
|
2018-11-29 15:30:20 +00:00
|
|
|
if (content_length != null) {
|
|
|
|
file_transfer.size = int.parse(content_length);
|
|
|
|
}
|
2018-11-14 17:17:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async void download(FileTransfer file_transfer, File file_) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
file_transfer.input_stream = yield request.send_async(null);
|
|
|
|
|
2019-06-01 15:09:26 +00:00
|
|
|
foreach (IncomingFileProcessor processor in stream_interactor.get_module(FileManager.IDENTITY).incoming_processors) {
|
2018-11-14 17:17:10 +00:00
|
|
|
if (processor.can_process(file_transfer)) {
|
|
|
|
processor.process(file_transfer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-02-13 20:50:15 +00:00
|
|
|
yield os.splice_async(file_transfer.input_stream, 0);
|
2018-11-14 17:17:10 +00:00
|
|
|
os.close();
|
|
|
|
file_transfer.path = file.get_basename();
|
2019-02-13 20:50:15 +00:00
|
|
|
file_transfer.input_stream = yield file.read_async();
|
2018-11-14 17:17:10 +00:00
|
|
|
|
|
|
|
file_transfer.state = FileTransfer.State.COMPLETE;
|
|
|
|
} catch (Error e) {
|
|
|
|
file_transfer.state = FileTransfer.State.FAILED;
|
2017-08-29 22:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|