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
|
|
|
|
|
|
|
private StreamInteractor stream_interactor;
|
2018-11-14 17:17:10 +00:00
|
|
|
private Dino.Database dino_db;
|
2019-12-26 14:39:12 +00:00
|
|
|
private static Regex http_url_regex = /^https?:\/\/([^\s#]*)$/; // Spaces are invalid in URLs and we can't use fragments for downloads
|
|
|
|
private static Regex omemo_url_regex = /^aesgcm:\/\/(.*)#(([A-Fa-f0-9]{2}){48}|([A-Fa-f0-9]{2}){44})$/;
|
2017-08-29 22:03:37 +00:00
|
|
|
|
|
|
|
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;
|
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" };
|
2021-12-09 14:46:16 +00:00
|
|
|
public override string action_group { get { return "MESSAGE_REINTERPRETING"; } }
|
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) {
|
2019-12-26 14:39:12 +00:00
|
|
|
string? oob_url = Xmpp.Xep.OutOfBandData.get_url_from_message(stanza);
|
|
|
|
bool normal_file = oob_url != null && oob_url == message.body && FileProvider.http_url_regex.match(message.body);
|
|
|
|
bool omemo_file = FileProvider.omemo_url_regex.match(message.body);
|
|
|
|
if (normal_file || omemo_file) {
|
2021-12-09 14:46:16 +00:00
|
|
|
outer.on_file_message(message, conversation);
|
|
|
|
return true;
|
2018-01-19 21:37:02 +00:00
|
|
|
}
|
|
|
|
return false;
|
2017-12-11 17:39:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-30 16:36:52 +00:00
|
|
|
private class LimitInputStream : InputStream {
|
|
|
|
InputStream inner;
|
|
|
|
int64 remaining_size;
|
|
|
|
|
|
|
|
public LimitInputStream(InputStream inner, int64 max_size) {
|
|
|
|
this.inner = inner;
|
|
|
|
this.remaining_size = max_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
private ssize_t check_limit(ssize_t read) throws IOError {
|
|
|
|
this.remaining_size -= read;
|
|
|
|
if (remaining_size < 0) throw new IOError.FAILED("Stream length exceeded limit");
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override ssize_t read(uint8[] buffer, Cancellable? cancellable = null) throws IOError {
|
|
|
|
return check_limit(inner.read(buffer, cancellable));
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async ssize_t read_async(uint8[]? buffer, int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError {
|
|
|
|
return check_limit(yield inner.read_async(buffer, io_priority, cancellable));
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool close(Cancellable? cancellable = null) throws IOError {
|
|
|
|
return inner.close(cancellable);
|
|
|
|
}
|
|
|
|
|
|
|
|
public override async bool close_async(int io_priority = GLib.Priority.DEFAULT, Cancellable? cancellable = null) throws IOError {
|
|
|
|
return yield inner.close_async(io_priority, cancellable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 14:46:16 +00:00
|
|
|
private void on_file_message(Entities.Message message, Conversation conversation) {
|
2019-07-18 00:03:42 +00:00
|
|
|
var additional_info = message.id.to_string();
|
|
|
|
|
|
|
|
var receive_data = new HttpFileReceiveData();
|
|
|
|
receive_data.url = message.body;
|
|
|
|
|
|
|
|
var file_meta = new HttpFileMeta();
|
2019-08-21 17:31:46 +00:00
|
|
|
file_meta.file_name = extract_file_name_from_url(message.body);
|
2019-07-18 00:03:42 +00:00
|
|
|
file_meta.message = message;
|
|
|
|
|
|
|
|
file_incoming(additional_info, message.from, message.time, message.local_time, conversation, receive_data, file_meta);
|
2018-11-14 17:17:10 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 00:03:42 +00:00
|
|
|
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;
|
|
|
|
|
2017-08-29 22:03:37 +00:00
|
|
|
var session = new Soup.Session();
|
2022-03-30 16:36:52 +00:00
|
|
|
session.user_agent = @"Dino/$(Dino.get_short_version()) ";
|
2019-07-18 00:03:42 +00:00
|
|
|
var head_message = new Soup.Message("HEAD", http_receive_data.url);
|
2022-04-08 22:28:42 +00:00
|
|
|
head_message.request_headers.append("Accept-Encoding", "identity");
|
2019-07-18 00:03:42 +00:00
|
|
|
|
2022-04-08 22:28:42 +00:00
|
|
|
try {
|
|
|
|
#if SOUP_3
|
|
|
|
yield session.send_async(head_message, GLib.Priority.LOW, null);
|
|
|
|
#else
|
|
|
|
yield session.send_async(head_message, null);
|
|
|
|
#endif
|
|
|
|
} catch (Error e) {
|
|
|
|
throw new FileReceiveError.GET_METADATA_FAILED("HEAD request failed");
|
|
|
|
}
|
2018-11-14 17:17:10 +00:00
|
|
|
|
2022-04-08 22:28:42 +00:00
|
|
|
string? content_type = null, content_length = null;
|
|
|
|
head_message.response_headers.foreach((name, val) => {
|
|
|
|
if (name.down() == "content-type") content_type = val;
|
|
|
|
if (name.down() == "content-length") content_length = val;
|
|
|
|
});
|
|
|
|
file_meta.mime_type = content_type;
|
|
|
|
if (content_length != null) {
|
|
|
|
file_meta.size = int64.parse(content_length);
|
2018-11-14 17:17:10 +00:00
|
|
|
}
|
2019-07-18 00:03:42 +00:00
|
|
|
|
|
|
|
return file_meta;
|
2018-11-14 17:17:10 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 14:47:41 +00:00
|
|
|
public Encryption get_encryption(FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta) {
|
|
|
|
return Encryption.NONE;
|
|
|
|
}
|
|
|
|
|
2019-07-18 00:03:42 +00:00
|
|
|
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);
|
|
|
|
|
2022-04-08 22:28:42 +00:00
|
|
|
var session = new Soup.Session();
|
|
|
|
session.user_agent = @"Dino/$(Dino.get_short_version()) ";
|
|
|
|
var get_message = new Soup.Message("GET", http_receive_data.url);
|
|
|
|
|
2018-11-14 17:17:10 +00:00
|
|
|
try {
|
2022-04-08 22:28:42 +00:00
|
|
|
#if SOUP_3
|
|
|
|
InputStream stream = yield session.send_async(get_message, GLib.Priority.LOW, file_transfer.cancellable);
|
|
|
|
#else
|
|
|
|
InputStream stream = yield session.send_async(get_message, file_transfer.cancellable);
|
|
|
|
#endif
|
2022-03-30 16:36:52 +00:00
|
|
|
if (file_meta.size != -1) {
|
|
|
|
return new LimitInputStream(stream, file_meta.size);
|
|
|
|
} else {
|
|
|
|
return stream;
|
|
|
|
}
|
2019-07-18 00:03:42 +00:00
|
|
|
} catch (Error e) {
|
|
|
|
throw new FileReceiveError.DOWNLOAD_FAILED("Downloading file error: %s".printf(e.message));
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 17:17:10 +00:00
|
|
|
|
2019-07-18 00:03:42 +00:00
|
|
|
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");
|
2018-11-14 17:17:10 +00:00
|
|
|
|
2020-11-14 15:59:21 +00:00
|
|
|
Message? message = stream_interactor.get_module(MessageStorage.IDENTITY).get_message_by_id(int.parse(file_transfer.info), conversation);
|
2019-07-18 00:03:42 +00:00
|
|
|
if (message == null) throw new FileReceiveError.GET_METADATA_FAILED("No message");
|
2018-11-14 17:17:10 +00:00
|
|
|
|
2019-07-18 00:03:42 +00:00
|
|
|
var file_meta = new HttpFileMeta();
|
|
|
|
file_meta.size = file_transfer.size;
|
|
|
|
file_meta.mime_type = file_transfer.mime_type;
|
2018-11-14 17:17:10 +00:00
|
|
|
|
2019-08-21 17:31:46 +00:00
|
|
|
file_meta.file_name = extract_file_name_from_url(message.body);
|
2019-07-18 00:03:42 +00:00
|
|
|
|
|
|
|
file_meta.message = message;
|
|
|
|
|
|
|
|
return file_meta;
|
2017-08-29 22:03:37 +00:00
|
|
|
}
|
2019-07-18 00:03:42 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2020-11-14 15:59:21 +00:00
|
|
|
Message? message = stream_interactor.get_module(MessageStorage.IDENTITY).get_message_by_id(int.parse(file_transfer.info), conversation);
|
2019-07-18 00:03:42 +00:00
|
|
|
if (message == null) return null;
|
|
|
|
|
|
|
|
var receive_data = new HttpFileReceiveData();
|
|
|
|
receive_data.url = message.body;
|
|
|
|
|
|
|
|
return receive_data;
|
|
|
|
}
|
|
|
|
|
2019-08-21 17:31:46 +00:00
|
|
|
private string extract_file_name_from_url(string url) {
|
2021-06-07 16:00:47 +00:00
|
|
|
string ret = url;
|
2019-08-21 17:31:46 +00:00
|
|
|
if (ret.contains("#")) {
|
|
|
|
ret = ret.substring(0, ret.last_index_of("#"));
|
|
|
|
}
|
2021-06-07 16:00:47 +00:00
|
|
|
ret = Uri.unescape_string(ret.substring(ret.last_index_of("/") + 1));
|
2019-08-21 17:31:46 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-18 00:03:42 +00:00
|
|
|
public int get_id() { return 0; }
|
2017-08-29 22:03:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|