From 2208ff9799b8b551b4da5227a32a09f9e00ffb6a Mon Sep 17 00:00:00 2001 From: fiaxh Date: Wed, 14 Nov 2018 18:17:10 +0100 Subject: Restructure (http) file provider flow: Separate download&get_info, download in provider, move XmppStreamModule into xmpp-vala --- libdino/src/entity/file_transfer.vala | 8 +- libdino/src/service/file_manager.vala | 24 ++- libdino/src/service/module_manager.vala | 1 + plugins/http-files/CMakeLists.txt | 1 - plugins/http-files/src/file_provider.vala | 140 ++++++++------- plugins/http-files/src/manager.vala | 40 ++++- plugins/http-files/src/plugin.vala | 4 - plugins/http-files/src/upload_stream_module.vala | 195 --------------------- plugins/openpgp/src/out_file_processor.vala | 1 + xmpp-vala/CMakeLists.txt | 1 + .../src/module/xep/0363_http_file_upload.vala | 160 +++++++++++++++++ 11 files changed, 301 insertions(+), 274 deletions(-) delete mode 100644 plugins/http-files/src/upload_stream_module.vala create mode 100644 xmpp-vala/src/module/xep/0363_http_file_upload.vala diff --git a/libdino/src/entity/file_transfer.vala b/libdino/src/entity/file_transfer.vala index be472796..2c45aa08 100644 --- a/libdino/src/entity/file_transfer.vala +++ b/libdino/src/entity/file_transfer.vala @@ -18,6 +18,12 @@ public class FileTransfer : Object { public Account account { get; set; } public Jid counterpart { get; set; } public Jid ourpart { get; set; } + public Jid? from { + get { return direction == DIRECTION_SENT ? ourpart : counterpart; } + } + public Jid? to { + get { return direction == DIRECTION_SENT ? counterpart : ourpart; } + } public bool direction { get; set; } public DateTime time { get; set; } public DateTime? local_time { get; set; } @@ -47,7 +53,7 @@ public class FileTransfer : Object { set { server_file_name_ = value; } } public string path { get; set; } - public string mime_type { get; set; } + public string? mime_type { get; set; } public int size { get; set; } public State state { get; set; } diff --git a/libdino/src/service/file_manager.vala b/libdino/src/service/file_manager.vala index 340205af..b35a4e09 100644 --- a/libdino/src/service/file_manager.vala +++ b/libdino/src/service/file_manager.vala @@ -16,7 +16,7 @@ public class FileManager : StreamInteractionModule, Object { private StreamInteractor stream_interactor; private Database db; private Gee.List file_senders = new ArrayList(); - private Gee.List incomming_processors = new ArrayList(); + public Gee.List incomming_processors = new ArrayList(); private Gee.List outgoing_processors = new ArrayList(); public static void start(StreamInteractor stream_interactor, Database db) { @@ -116,7 +116,7 @@ public class FileManager : StreamInteractionModule, Object { } public void add_provider(FileProvider file_provider) { - file_provider.file_incoming.connect(handle_incomming_file); + file_provider.file_incoming.connect((file_transfer, conversation) => { handle_incomming_file.begin(file_provider, file_transfer, conversation); }); } public void add_sender(FileSender file_sender) { @@ -134,13 +134,18 @@ public class FileManager : StreamInteractionModule, Object { outgoing_processors.add(processor); } - private void handle_incomming_file(FileTransfer file_transfer, Conversation conversation) { - foreach (IncommingFileProcessor processor in incomming_processors) { - if (processor.can_process(file_transfer)) { - processor.process(file_transfer); - } - } - save_file(file_transfer); + public bool is_sender_trustworthy(FileTransfer file_transfer, Conversation conversation) { + Jid relevant_jid = stream_interactor.get_module(MucManager.IDENTITY).get_real_jid(file_transfer.from, conversation.account) ?? conversation.counterpart; + bool in_roster = stream_interactor.get_module(RosterManager.IDENTITY).get_roster_item(conversation.account, relevant_jid) != null; + return file_transfer.direction == FileTransfer.DIRECTION_SENT || in_roster; + } + + private async void handle_incomming_file(FileProvider file_provider, FileTransfer file_transfer, Conversation conversation) { + if (!is_sender_trustworthy(file_transfer, conversation)) return; + + string filename = Random.next_int().to_string("%x") + "_" + file_transfer.file_name; + File file = File.new_for_path(Path.build_filename(get_storage_dir(), filename)); + yield file_provider.download(file_transfer, file); try { FileInfo file_info = file_transfer.get_file().query_info("*", FileQueryInfoFlags.NONE); @@ -170,6 +175,7 @@ public class FileManager : StreamInteractionModule, Object { public interface FileProvider : Object { public signal void file_incoming(FileTransfer file_transfer, Conversation conversation); + public abstract async void download(FileTransfer file_transfer, File file); } public interface FileSender : Object { diff --git a/libdino/src/service/module_manager.vala b/libdino/src/service/module_manager.vala index b9197f90..41a2c6a0 100644 --- a/libdino/src/service/module_manager.vala +++ b/libdino/src/service/module_manager.vala @@ -77,6 +77,7 @@ public class ModuleManager { module_map[account].add(new Xep.DelayedDelivery.Module()); module_map[account].add(new StreamError.Module()); module_map[account].add(new Xep.InBandRegistration.Module()); + module_map[account].add(new Xep.HttpFileUpload.Module()); initialize_account_modules(account, module_map[account]); } } diff --git a/plugins/http-files/CMakeLists.txt b/plugins/http-files/CMakeLists.txt index 340ff5b2..93ccbf67 100644 --- a/plugins/http-files/CMakeLists.txt +++ b/plugins/http-files/CMakeLists.txt @@ -13,7 +13,6 @@ SOURCES src/manager.vala src/plugin.vala src/register_plugin.vala - src/upload_stream_module.vala CUSTOM_VAPIS ${CMAKE_BINARY_DIR}/exports/xmpp-vala.vapi ${CMAKE_BINARY_DIR}/exports/dino.vapi diff --git a/plugins/http-files/src/file_provider.vala b/plugins/http-files/src/file_provider.vala index 6f4b19f4..7b52a7fb 100644 --- a/plugins/http-files/src/file_provider.vala +++ b/plugins/http-files/src/file_provider.vala @@ -10,12 +10,14 @@ 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 Gee.List ignore_once = new ArrayList(); public FileProvider(StreamInteractor stream_interactor, Dino.Database dino_db) { this.stream_interactor = stream_interactor; + this.dino_db = dino_db; 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`!()\[\]{};:'".,<>?«»“”‘’]))$"""); stream_interactor.get_module(MessageProcessor.IDENTITY).received_pipeline.connect(new ReceivedMessageListener(this)); @@ -39,77 +41,91 @@ 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)) 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) { - yield outer.download_url(message, conversation); + if (outer.url_regex.match(message.body)) { + string? oob_url = Xmpp.Xep.OutOfBandData.get_url_from_message(message.stanza); + if (oob_url != null && oob_url == message.body) { + outer.on_file_message(message, conversation); + } } return false; } } - private async bool download_url(Message message, Conversation conversation) { - bool success = false; + 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.mime_type = null; + 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)) { + 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); + } + yield get_meta_info(file_transfer); + file_incoming(file_transfer, conversation); + } + } + + private 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]; var session = new Soup.Session(); - var head_message = new Soup.Message("HEAD", message.body); + var head_message = new Soup.Message("HEAD", url_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"); - 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; - }); - if (content_length != null && int.parse(content_length) < 5000000) { - FileTransfer file_transfer = new FileTransfer(); - try { - Soup.Request request = session.request(message.body); - request.send_async.begin(null, (obj, res) => { - try { - file_transfer.input_stream = request.send_async.end(res); - } catch (Error e) { - Idle.add((owned)callback); - return; - } - 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.mime_type = content_type; - file_transfer.size = int.parse(content_length); - file_transfer.state = FileTransfer.State.NOT_STARTED; - file_transfer.provider = 0; - file_transfer.info = message.id.to_string(); - file_incoming(file_transfer, conversation); - - 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); - } - - success = true; - Idle.add((owned)callback); - }); - } catch (Error e) { - Idle.add((owned)callback); - } - } else { - Idle.add((owned)callback); - } + SourceFunc callback = get_meta_info.callback; + 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; }); - yield; + file_transfer.mime_type = content_type; + file_transfer.size = int.parse(content_length); + } + } + + 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); + + foreach (IncommingFileProcessor processor in stream_interactor.get_module(FileManager.IDENTITY).incomming_processors) { + 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); + os.splice(file_transfer.input_stream, 0); + os.close(); + file_transfer.path = file.get_basename(); + file_transfer.input_stream = file.read(); + + file_transfer.state = FileTransfer.State.COMPLETE; + } catch (Error e) { + file_transfer.state = FileTransfer.State.FAILED; } - return success; } } diff --git a/plugins/http-files/src/manager.vala b/plugins/http-files/src/manager.vala index 78697f9c..b5aa0d26 100644 --- a/plugins/http-files/src/manager.vala +++ b/plugins/http-files/src/manager.vala @@ -29,10 +29,46 @@ public class Manager : StreamInteractionModule, FileSender, Object { 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 data = new Array(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) { - stream_interactor.module_manager.get_module(file_transfer.account, UploadStreamModule.IDENTITY).upload(stream, file_transfer.input_stream, file_transfer.server_file_name, file_transfer.mime_type, + upload(stream, file_transfer, (stream, url_down) => { uploaded(file_transfer, url_down); file_transfer.info = url_down; @@ -71,7 +107,7 @@ public class Manager : StreamInteractionModule, FileSender, Object { } private void on_stream_negotiated(Account account, XmppStream stream) { - stream_interactor.module_manager.get_module(account, UploadStreamModule.IDENTITY).feature_available.connect((stream, max_file_size) => { + 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; } diff --git a/plugins/http-files/src/plugin.vala b/plugins/http-files/src/plugin.vala index bd136f31..e2ce5ad9 100644 --- a/plugins/http-files/src/plugin.vala +++ b/plugins/http-files/src/plugin.vala @@ -14,10 +14,6 @@ public class Plugin : RootInterface, Object { file_provider = new FileProvider(app.stream_interactor, app.db); - app.stream_interactor.module_manager.initialize_account_modules.connect((account, list) => { - list.add(new UploadStreamModule()); - }); - app.stream_interactor.get_module(FileManager.IDENTITY).add_provider(file_provider); app.stream_interactor.get_module(ContentItemStore.IDENTITY).add_filter(new FileMessageFilter(app.db)); } diff --git a/plugins/http-files/src/upload_stream_module.vala b/plugins/http-files/src/upload_stream_module.vala deleted file mode 100644 index 934d4df5..00000000 --- a/plugins/http-files/src/upload_stream_module.vala +++ /dev/null @@ -1,195 +0,0 @@ -using Xmpp; -using Xmpp; -using Xmpp.Xep; - -namespace Dino.Plugins.HttpFiles { - -private const string NS_URI = "urn:xmpp:http:upload"; -private const string NS_URI_0 = "urn:xmpp:http:upload:0"; - -public class UploadStreamModule : XmppStreamModule { - public static Xmpp.ModuleIdentity IDENTITY = new Xmpp.ModuleIdentity(NS_URI, "0363_http_file_upload"); - - public signal void feature_available(XmppStream stream, long max_file_size); - public signal void received_url(XmppStream stream, MessageStanza message); - - public delegate void OnUploadOk(XmppStream stream, string url_down); - public delegate void OnError(XmppStream stream, string error); - public void upload(XmppStream stream, InputStream input_stream, string file_name, string file_content_type, owned OnUploadOk listener, owned OnError error_listener) { - uint8[] buf = new uint8[256]; - Array data = new Array(false, true, 0); - size_t len = -1; - do { - try { - len = 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); - - request_slot(stream, file_name, (int) data.length, file_content_type, - (stream, url_down, url_up) => { - Soup.Message message = new Soup.Message("PUT", url_up); - message.set_request(file_content_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)); - } - - private delegate void OnSlotOk(XmppStream stream, string url_get, string url_put); - private void request_slot(XmppStream stream, string filename, int file_size, string? content_type, owned OnSlotOk listener, owned OnError error_listener) { - Flag? flag = stream.get_flag(Flag.IDENTITY); - if (flag == null) return; - - StanzaNode? request_node = null; - switch (flag.ns_ver) { - case NS_URI_0: - request_node = new StanzaNode.build("request", NS_URI_0).add_self_xmlns(); - request_node.put_attribute("filename", filename).put_attribute("size", file_size.to_string()); - if (content_type != null) request_node.put_attribute("content-type", content_type); - break; - case NS_URI: - request_node = new StanzaNode.build("request", NS_URI).add_self_xmlns() - .put_node(new StanzaNode.build("filename", NS_URI).put_node(new StanzaNode.text(filename))) - .put_node(new StanzaNode.build("size", NS_URI).put_node(new StanzaNode.text(file_size.to_string()))); - if (content_type != null) { - request_node.put_node(new StanzaNode.build("content-type", NS_URI).put_node(new StanzaNode.text(content_type))); - } - break; - } - Iq.Stanza iq = new Iq.Stanza.get(request_node) { to=flag.file_store_jid }; - stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, iq) => { - if (iq.is_error()) { - error_listener(stream, "Error getting upload/download url"); - return; - } - string? url_get = null, url_put = null; - // FIXME change back to switch on version in a while (prosody bug) - url_get = iq.stanza.get_deep_attribute(flag.ns_ver + ":slot", flag.ns_ver + ":get", flag.ns_ver + ":url"); - url_put = iq.stanza.get_deep_attribute(flag.ns_ver + ":slot", flag.ns_ver + ":put", flag.ns_ver + ":url"); - if (url_get == null && url_put == null) { - url_get = iq.stanza.get_deep_string_content(flag.ns_ver + ":slot", flag.ns_ver + ":get"); - url_put = iq.stanza.get_deep_string_content(flag.ns_ver + ":slot", flag.ns_ver + ":put"); - } - if (url_get == null || url_put == null) { - error_listener(stream, "Error getting upload/download url"); - } - listener(stream, url_get, url_put); - }); - } - - public override void attach(XmppStream stream) { - stream.stream_negotiated.connect(query_availability); - } - - public override void detach(XmppStream stream) { - stream.get_module(Bind.Module.IDENTITY).bound_to_resource.disconnect(query_availability); - } - - public override string get_ns() { return NS_URI; } - public override string get_id() { return IDENTITY.id; } - - private void query_availability(XmppStream stream) { - stream.get_module(ServiceDiscovery.Module.IDENTITY).request_info(stream, stream.remote_name, (stream, info_result) => { - bool available = check_ns_in_info(stream, stream.remote_name, info_result); - if (!available) { - stream.get_module(ServiceDiscovery.Module.IDENTITY).request_items(stream, stream.remote_name, (stream, items_result) => { - foreach (Xep.ServiceDiscovery.Item item in items_result.items) { - stream.get_module(ServiceDiscovery.Module.IDENTITY).request_info(stream, item.jid, (stream, info_result) => { - check_ns_in_info(stream, item.jid, info_result); - }); - } - }); - } - }); - } - - private bool check_ns_in_info(XmppStream stream, Jid jid, Xep.ServiceDiscovery.InfoResult info_result) { - bool ver_available = false; - bool ver_0_available = false; - foreach (string feature in info_result.features) { - if (feature == NS_URI_0) { - ver_0_available = true; - break; - } else if (feature == NS_URI) { - ver_available = true; - } - } - - if (ver_available || ver_0_available) { - long max_file_size = extract_max_file_size(info_result); - if (ver_0_available) { - stream.add_flag(new Flag(jid, NS_URI_0)); - } else if (ver_available) { - stream.add_flag(new Flag(jid, NS_URI)); - } - - feature_available(stream, max_file_size); - return true; - } - return false; - } - - private long extract_max_file_size(Xep.ServiceDiscovery.InfoResult info_result) { - string? max_file_size_str = null; - StanzaNode x_node = info_result.iq.stanza.get_deep_subnode("http://jabber.org/protocol/disco#info:query", "jabber:x:data:x"); - Gee.List field_nodes = x_node.get_subnodes("field", "jabber:x:data"); - foreach (StanzaNode node in field_nodes) { - string? var_attr = node.get_attribute("var"); - if (var_attr == "max-file-size") { - StanzaNode value_node = node.get_subnode("value", "jabber:x:data"); - max_file_size_str = value_node.get_string_content(); - break; - } - } - if (max_file_size_str != null) return long.parse(max_file_size_str); - return -1; - } -} - -public class ReceivedPipelineListener : StanzaListener { - - private const string[] after_actions_const = {"EXTRACT_MESSAGE_2"}; - - public override string action_group { get { return "EXTRACT_MESSAGE_2"; } } - public override string[] after_actions { get { return after_actions_const; } } - - public override async bool run(XmppStream stream, MessageStanza message) { - string? oob_url = OutOfBandData.get_url_from_message(message); - if (oob_url != null && oob_url == message.body) { - stream.get_module(UploadStreamModule.IDENTITY).received_url(stream, message); - } - return true; - } -} - -public class Flag : XmppStreamFlag { - public static FlagIdentity IDENTITY = new FlagIdentity(NS_URI, "service_discovery"); - - public Jid file_store_jid; - public string ns_ver; - public int? max_file_size; - - public Flag(Jid file_store_jid, string ns_ver) { - this.file_store_jid = file_store_jid; - this.ns_ver = ns_ver; - } - - public override string get_ns() { return NS_URI; } - public override string get_id() { return IDENTITY.id; } -} - -} diff --git a/plugins/openpgp/src/out_file_processor.vala b/plugins/openpgp/src/out_file_processor.vala index 522e518a..40eef7f5 100644 --- a/plugins/openpgp/src/out_file_processor.vala +++ b/plugins/openpgp/src/out_file_processor.vala @@ -23,6 +23,7 @@ public class OutFileProcessor : OutgoingFileProcessor, Object { 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/xmpp-vala/CMakeLists.txt b/xmpp-vala/CMakeLists.txt index 1649411e..fba9966d 100644 --- a/xmpp-vala/CMakeLists.txt +++ b/xmpp-vala/CMakeLists.txt @@ -68,6 +68,7 @@ SOURCES "src/module/xep/0280_message_carbons.vala" "src/module/xep/0313_message_archive_management.vala" "src/module/xep/0333_chat_markers.vala" + "src/module/xep/0363_http_file_upload.vala" "src/module/xep/0368_srv_records_tls.vala" "src/module/xep/0380_explicit_encryption.vala" "src/module/xep/pixbuf_storage.vala" diff --git a/xmpp-vala/src/module/xep/0363_http_file_upload.vala b/xmpp-vala/src/module/xep/0363_http_file_upload.vala new file mode 100644 index 00000000..d2e2af2c --- /dev/null +++ b/xmpp-vala/src/module/xep/0363_http_file_upload.vala @@ -0,0 +1,160 @@ +using Xmpp; +using Xmpp; +using Xmpp.Xep; + +namespace Xmpp.Xep.HttpFileUpload { + +private const string NS_URI = "urn:xmpp:http:upload"; +private const string NS_URI_0 = "urn:xmpp:http:upload:0"; + +public class Module : XmppStreamModule { + public static Xmpp.ModuleIdentity IDENTITY = new Xmpp.ModuleIdentity(NS_URI, "0363_http_file_upload"); + + public signal void feature_available(XmppStream stream, long max_file_size); + public signal void received_url(XmppStream stream, MessageStanza message); + + public delegate void OnSlotOk(XmppStream stream, string url_get, string url_put); + public delegate void OnError(XmppStream stream, string error); + public void request_slot(XmppStream stream, string filename, int file_size, string? content_type, owned OnSlotOk listener, owned OnError error_listener) { + Flag? flag = stream.get_flag(Flag.IDENTITY); + if (flag == null) return; + + StanzaNode? request_node = null; + switch (flag.ns_ver) { + case NS_URI_0: + request_node = new StanzaNode.build("request", NS_URI_0).add_self_xmlns(); + request_node.put_attribute("filename", filename).put_attribute("size", file_size.to_string()); + if (content_type != null) request_node.put_attribute("content-type", content_type); + break; + case NS_URI: + request_node = new StanzaNode.build("request", NS_URI).add_self_xmlns() + .put_node(new StanzaNode.build("filename", NS_URI).put_node(new StanzaNode.text(filename))) + .put_node(new StanzaNode.build("size", NS_URI).put_node(new StanzaNode.text(file_size.to_string()))); + if (content_type != null) { + request_node.put_node(new StanzaNode.build("content-type", NS_URI).put_node(new StanzaNode.text(content_type))); + } + break; + } + Iq.Stanza iq = new Iq.Stanza.get(request_node) { to=flag.file_store_jid }; + stream.get_module(Iq.Module.IDENTITY).send_iq(stream, iq, (stream, iq) => { + if (iq.is_error()) { + error_listener(stream, "Error getting upload/download url (Error Iq)"); + return; + } + string? url_get = null, url_put = null; + // FIXME change back to switch on version in a while (prosody bug) + url_get = iq.stanza.get_deep_attribute(flag.ns_ver + ":slot", flag.ns_ver + ":get", flag.ns_ver + ":url"); + url_put = iq.stanza.get_deep_attribute(flag.ns_ver + ":slot", flag.ns_ver + ":put", flag.ns_ver + ":url"); + if (url_get == null && url_put == null) { + url_get = iq.stanza.get_deep_string_content(flag.ns_ver + ":slot", flag.ns_ver + ":get"); + url_put = iq.stanza.get_deep_string_content(flag.ns_ver + ":slot", flag.ns_ver + ":put"); + } + if (url_get == null || url_put == null) { + error_listener(stream, "Error getting upload/download url"); + } + listener(stream, url_get, url_put); + }); + } + + public override void attach(XmppStream stream) { + stream.stream_negotiated.connect(query_availability); + } + + public override void detach(XmppStream stream) { + stream.get_module(Bind.Module.IDENTITY).bound_to_resource.disconnect(query_availability); + } + + public override string get_ns() { return NS_URI; } + public override string get_id() { return IDENTITY.id; } + + private void query_availability(XmppStream stream) { + stream.get_module(ServiceDiscovery.Module.IDENTITY).request_info(stream, stream.remote_name, (stream, info_result) => { + bool available = check_ns_in_info(stream, stream.remote_name, info_result); + if (!available) { + stream.get_module(ServiceDiscovery.Module.IDENTITY).request_items(stream, stream.remote_name, (stream, items_result) => { + foreach (Xep.ServiceDiscovery.Item item in items_result.items) { + stream.get_module(ServiceDiscovery.Module.IDENTITY).request_info(stream, item.jid, (stream, info_result) => { + check_ns_in_info(stream, item.jid, info_result); + }); + } + }); + } + }); + } + + private bool check_ns_in_info(XmppStream stream, Jid jid, Xep.ServiceDiscovery.InfoResult info_result) { + bool ver_available = false; + bool ver_0_available = false; + foreach (string feature in info_result.features) { + if (feature == NS_URI_0) { + ver_0_available = true; + break; + } else if (feature == NS_URI) { + ver_available = true; + } + } + + if (ver_available || ver_0_available) { + long max_file_size = extract_max_file_size(info_result); + if (ver_0_available) { + stream.add_flag(new Flag(jid, NS_URI_0)); + } else if (ver_available) { + stream.add_flag(new Flag(jid, NS_URI)); + } + + feature_available(stream, max_file_size); + return true; + } + return false; + } + + private long extract_max_file_size(Xep.ServiceDiscovery.InfoResult info_result) { + string? max_file_size_str = null; + StanzaNode x_node = info_result.iq.stanza.get_deep_subnode("http://jabber.org/protocol/disco#info:query", "jabber:x:data:x"); + Gee.List field_nodes = x_node.get_subnodes("field", "jabber:x:data"); + foreach (StanzaNode node in field_nodes) { + string? var_attr = node.get_attribute("var"); + if (var_attr == "max-file-size") { + StanzaNode value_node = node.get_subnode("value", "jabber:x:data"); + max_file_size_str = value_node.get_string_content(); + break; + } + } + if (max_file_size_str != null) return long.parse(max_file_size_str); + return -1; + } +} + +public class ReceivedPipelineListener : StanzaListener { + + private const string[] after_actions_const = {"EXTRACT_MESSAGE_2"}; + + public override string action_group { get { return "EXTRACT_MESSAGE_2"; } } + public override string[] after_actions { get { return after_actions_const; } } + + public override async bool run(XmppStream stream, MessageStanza message) { + string? oob_url = OutOfBandData.get_url_from_message(message); + if (oob_url != null && oob_url == message.body) { + stream.get_module(Module.IDENTITY).received_url(stream, message); + } + return true; + } +} + +public class Flag : XmppStreamFlag { + public static FlagIdentity IDENTITY = new FlagIdentity(NS_URI, "http_file_upload"); + + public Jid file_store_jid; + public string ns_ver; + public int? max_file_size; + + public Flag(Jid file_store_jid, string ns_ver) { + this.file_store_jid = file_store_jid; + this.ns_ver = ns_ver; + } + + public override string get_ns() { return NS_URI; } + public override string get_id() { return IDENTITY.id; } +} + +} -- cgit v1.2.3-70-g09d2