aboutsummaryrefslogtreecommitdiff
path: root/plugins/http-files/src
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/http-files/src')
-rw-r--r--plugins/http-files/src/file_provider.vala194
-rw-r--r--plugins/http-files/src/file_sender.vala198
-rw-r--r--plugins/http-files/src/plugin.vala32
-rw-r--r--plugins/http-files/src/register_plugin.vala3
4 files changed, 0 insertions, 427 deletions
diff --git a/plugins/http-files/src/file_provider.vala b/plugins/http-files/src/file_provider.vala
deleted file mode 100644
index 702dd3b3..00000000
--- a/plugins/http-files/src/file_provider.vala
+++ /dev/null
@@ -1,194 +0,0 @@
-using Gee;
-
-using Dino.Entities;
-using Xmpp;
-
-namespace Dino.Plugins.HttpFiles {
-
-public class FileProvider : Dino.FileProvider, Object {
-
- private StreamInteractor stream_interactor;
- private Dino.Database dino_db;
- private Soup.Session session;
- 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})$/;
-
- public FileProvider(StreamInteractor stream_interactor, Dino.Database dino_db) {
- this.stream_interactor = stream_interactor;
- this.dino_db = dino_db;
- this.session = new Soup.Session();
-
- session.user_agent = @"Dino/$(Dino.get_short_version()) ";
- 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 "MESSAGE_REINTERPRETING"; } }
- 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 (Xep.StatelessFileSharing.get_file_shares(stanza) != null || Xep.StatelessFileSharing.get_source_attachments(stanza) != null) {
- return false;
- }
-
- 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) {
- outer.on_file_message(message, conversation);
- return true;
- }
- return false;
- }
- }
-
- private void on_file_message(Entities.Message message, Conversation conversation) {
- 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 = extract_file_name_from_url(message.body);
- file_meta.message = message;
-
- file_incoming(additional_info, message.from, message.time, message.local_time, conversation, receive_data, file_meta);
- }
-
- 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 head_message = new Soup.Message("HEAD", http_receive_data.url);
- head_message.request_headers.append("Accept-Encoding", "identity");
-
-#if SOUP_3_0
- string transfer_host = Uri.parse(http_receive_data.url, UriFlags.NONE).get_host();
- head_message.accept_certificate.connect((peer_cert, errors) => { return ConnectionManager.on_invalid_certificate(transfer_host, peer_cert, errors); });
-#endif
-
- try {
-#if SOUP_3_0
- 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");
- }
-
- 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.content_type = new FileContentType.from_mime_type(content_type);
- if (content_length != null) {
- file_meta.size = int64.parse(content_length);
- }
-
- return file_meta;
- }
-
- public Encryption get_encryption(FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta) {
- return Encryption.NONE;
- }
-
- public async InputStream download(FileTransfer file_transfer, FileReceiveData receive_data, FileMeta file_meta) throws IOError {
- HttpFileReceiveData? http_receive_data = receive_data as HttpFileReceiveData;
- if (http_receive_data == null) assert(false);
-
- var get_message = new Soup.Message("GET", http_receive_data.url);
-
-#if SOUP_3_0
- string transfer_host = Uri.parse(http_receive_data.url, UriFlags.NONE).get_host();
- get_message.accept_certificate.connect((peer_cert, errors) => { return ConnectionManager.on_invalid_certificate(transfer_host, peer_cert, errors); });
-#endif
-
-#if SOUP_3_0
- 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
- if (file_meta.size != -1) {
- return new LimitInputStream(stream, file_meta.size);
- } else {
- return stream;
- }
- }
-
- public FileMeta get_file_meta(FileTransfer file_transfer) throws FileReceiveError {
- if (file_transfer.provider == FileManager.SFS_PROVIDER_ID) {
- var file_meta = new HttpFileMeta();
- file_meta.size = file_transfer.size;
- file_meta.content_type = file_transfer.content_type;
- file_meta.file_name = file_transfer.file_name;
- file_meta.message = null;
- return file_meta;
- }
-
- 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");
-
- 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");
-
- var file_meta = new HttpFileMeta();
- file_meta.size = file_transfer.size;
- file_meta.content_type = file_transfer.content_type;
-
- file_meta.file_name = extract_file_name_from_url(message.body);
-
- file_meta.message = message;
-
- return file_meta;
- }
-
- public FileReceiveData? get_file_receive_data(FileTransfer file_transfer) {
- if (file_transfer.provider == FileManager.SFS_PROVIDER_ID) {
- if (!file_transfer.sfs_sources.is_empty) {
- var http_source = file_transfer.sfs_sources.get(0) as Xep.StatelessFileSharing.HttpSource;
- if (http_source != null) {
- var receive_data = new HttpFileReceiveData();
- receive_data.url = http_source.url;
- return receive_data;
- }
- }
- return null;
- }
-
- 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;
- }
-
- private string extract_file_name_from_url(string url) {
- string ret = url;
- if (ret.contains("#")) {
- ret = ret.substring(0, ret.last_index_of("#"));
- }
- ret = Uri.unescape_string(ret.substring(ret.last_index_of("/") + 1));
- return ret;
- }
-
- public int get_id() { return 0; }
-}
-
-}
diff --git a/plugins/http-files/src/file_sender.vala b/plugins/http-files/src/file_sender.vala
deleted file mode 100644
index 7e48d211..00000000
--- a/plugins/http-files/src/file_sender.vala
+++ /dev/null
@@ -1,198 +0,0 @@
-using Dino.Entities;
-using Xmpp;
-using Gee;
-
-namespace Dino.Plugins.HttpFiles {
-
-public class HttpFileSender : FileSender, Object {
- private StreamInteractor stream_interactor;
- private Database db;
- private Soup.Session session;
- 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;
- this.session = new Soup.Session();
-
- session.user_agent = @"Dino/$(Dino.get_short_version()) ";
- stream_interactor.stream_negotiated.connect(on_stream_negotiated);
- stream_interactor.get_module(MessageProcessor.IDENTITY).build_message_stanza.connect(check_add_sfs_element);
- }
-
- public async FileSendData? prepare_send_file(Conversation conversation, FileTransfer file_transfer, FileMeta file_meta) 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_meta.size, file_meta.content_type);
- send_data.url_down = slot_result.url_get;
- send_data.url_up = slot_result.url_put;
- send_data.headers = slot_result.headers;
- } 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, FileMeta file_meta) throws FileSendError {
- HttpFileSendData? send_data = file_send_data as HttpFileSendData;
- if (send_data == null) return;
-
- bool can_reference_element = conversation.type_ == Conversation.Type.CHAT || (
- // The stable stanza ID XEP is not clear about an announcing MUC having to attach stanza-ids, thus we also check for MAM, which requires this.
- stream_interactor.get_module(EntityInfo.IDENTITY).has_feature_cached(conversation.account, conversation.counterpart, Xep.UniqueStableStanzaIDs.NS_URI) &&
- stream_interactor.get_module(EntityInfo.IDENTITY).has_feature_cached(conversation.account, conversation.counterpart, Xmpp.MessageArchiveManagement.NS_URI)
- );
-
- // Share unencrypted files via SFS (only if we'll be able to reference messages)
- if (conversation.encryption == Encryption.NONE && can_reference_element) {
- // Announce the file share
- Entities.Message file_share_message = stream_interactor.get_module(MessageProcessor.IDENTITY).create_out_message(null, conversation);
- file_transfer.info = file_share_message.id.to_string();
- file_transfer.file_sharing_id = Xmpp.random_uuid();
- stream_interactor.get_module(MessageProcessor.IDENTITY).send_xmpp_message(file_share_message, conversation);
-
- // Upload file
- yield upload(file_transfer, send_data, file_meta);
-
- // Wait until we know the server id of the file share message (in MUCs; we get that from the reflected message)
- if (conversation.type_.is_muc_semantic()) {
- if (file_share_message.server_id == null) {
- ulong server_id_notify_id = file_share_message.notify["server-id"].connect(() => {
- Idle.add(send_file.callback);
- });
- yield;
- file_share_message.disconnect(server_id_notify_id);
- }
- }
-
- file_transfer.sfs_sources.add(new Xep.StatelessFileSharing.HttpSource() { url=send_data.url_down } );
-
- // Send source attachment
- MessageStanza stanza = new MessageStanza() { to = conversation.counterpart, type_ = conversation.type_ == GROUPCHAT ? MessageStanza.TYPE_GROUPCHAT : MessageStanza.TYPE_CHAT };
- stanza.body = send_data.url_down;
- Xep.OutOfBandData.add_url_to_message(stanza, send_data.url_down);
- var sources = new ArrayList<Xep.StatelessFileSharing.Source>();
- sources.add(new Xep.StatelessFileSharing.HttpSource() { url = send_data.url_down });
- string attach_to_id = stream_interactor.get_module(MessageStorage.IDENTITY).get_reference_id(file_share_message, conversation);
- Xep.StatelessFileSharing.set_sfs_attachment(stanza, attach_to_id, file_transfer.file_sharing_id, sources);
-
- var stream = stream_interactor.get_stream(conversation.account);
- if (stream == null) throw new FileSendError.UPLOAD_FAILED("No stream");
-
- stream.get_module(MessageModule.IDENTITY).send_message.begin(stream, stanza);
- }
- // Share encrypted files without SFS
- else {
- yield upload(file_transfer, send_data, file_meta);
-
- Entities.Message message = stream_interactor.get_module(MessageProcessor.IDENTITY).create_out_message(send_data.url_down, conversation);
- file_transfer.info = message.id.to_string();
-
- message.encryption = send_data.encrypt_message ? conversation.encryption : Encryption.NONE;
- stream_interactor.get_module(MessageProcessor.IDENTITY).send_xmpp_message(message, conversation);
- }
- }
-
- public async 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 async long get_file_size_limit(Conversation conversation) {
- long? max_size = max_file_sizes[conversation.account];
- if (max_size != null) {
- return max_size;
- }
- return -1;
- }
-
- public async bool can_encrypt(Conversation conversation, FileTransfer file_transfer) {
- return false;
- }
-
- public async bool is_upload_available(Conversation conversation) {
- lock (max_file_sizes) {
- return max_file_sizes.has_key(conversation.account);
- }
- }
-
-#if !SOUP_3_0
- private static void transfer_more_bytes(InputStream stream, Soup.MessageBody body) {
- uint8[] bytes = new uint8[4096];
- ssize_t read = stream.read(bytes);
- if (read == 0) {
- body.complete();
- return;
- }
- bytes.length = (int)read;
- body.append_buffer(new Soup.Buffer.take(bytes));
- }
-#endif
-
- private async void upload(FileTransfer file_transfer, HttpFileSendData file_send_data, FileMeta file_meta) throws FileSendError {
- Xmpp.XmppStream? stream = stream_interactor.get_stream(file_transfer.account);
- if (stream == null) return;
-
- var put_message = new Soup.Message("PUT", file_send_data.url_up);
-
-#if SOUP_3_0
- string transfer_host = Uri.parse(file_send_data.url_up, UriFlags.NONE).get_host();
- put_message.accept_certificate.connect((peer_cert, errors) => { return ConnectionManager.on_invalid_certificate(transfer_host, peer_cert, errors); });
- put_message.set_request_body(file_meta.content_type.get_mime_type(), file_transfer.input_stream, (ssize_t) file_meta.size);
-#else
- put_message.request_headers.set_content_type(file_meta.content_type.get_mime_type(), null);
- put_message.request_headers.set_content_length(file_meta.size);
- put_message.request_body.set_accumulate(false);
- put_message.wrote_headers.connect(() => transfer_more_bytes(file_transfer.input_stream, put_message.request_body));
- put_message.wrote_chunk.connect(() => transfer_more_bytes(file_transfer.input_stream, put_message.request_body));
-#endif
- foreach (var entry in file_send_data.headers.entries) {
- put_message.request_headers.append(entry.key, entry.value);
- }
- try {
-#if SOUP_3_0
- yield session.send_async(put_message, GLib.Priority.LOW, file_transfer.cancellable);
-#else
- yield session.send_async(put_message, file_transfer.cancellable);
-#endif
- if (put_message.status_code < 200 || put_message.status_code >= 300) {
- throw new FileSendError.UPLOAD_FAILED("HTTP status code %s".printf(put_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_sfs_element(Entities.Message message, Xmpp.MessageStanza message_stanza, Conversation conversation) {
- if (message.encryption != Encryption.NONE) return;
-
- FileTransfer? file_transfer = stream_interactor.get_module(FileTransferStorage.IDENTITY).get_file_by_message_id(message.id, conversation);
- if (file_transfer == null) return;
-
- Xep.StatelessFileSharing.set_sfs_element(message_stanza, file_transfer.file_sharing_id, file_transfer.file_metadata, file_transfer.sfs_sources);
-
- Xep.MessageProcessingHints.set_message_hint(message_stanza, Xep.MessageProcessingHints.HINT_STORE);
- }
-
- public int get_id() { return 0; }
-
- public float get_priority() { return 100; }
-}
-
-}
diff --git a/plugins/http-files/src/plugin.vala b/plugins/http-files/src/plugin.vala
deleted file mode 100644
index 649d1ceb..00000000
--- a/plugins/http-files/src/plugin.vala
+++ /dev/null
@@ -1,32 +0,0 @@
-extern const string GETTEXT_PACKAGE;
-extern const string LOCALE_INSTALL_DIR;
-
-namespace Dino.Plugins.HttpFiles {
-
-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;
-//
-// 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);
- }
-
- public void shutdown() {
- // Nothing to do
- }
-}
-
-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());
- return builder.count() > 0;
-}
-
-}
diff --git a/plugins/http-files/src/register_plugin.vala b/plugins/http-files/src/register_plugin.vala
deleted file mode 100644
index 21fac531..00000000
--- a/plugins/http-files/src/register_plugin.vala
+++ /dev/null
@@ -1,3 +0,0 @@
-public Type register_plugin(Module module) {
- return typeof (Dino.Plugins.HttpFiles.Plugin);
-}