aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorfiaxh <git@mx.ax.lt>2017-08-30 00:03:37 +0200
committerfiaxh <git@mx.ax.lt>2017-08-31 18:54:38 +0200
commita257b163376174e4f5efcbc82c9fdd56463c3191 (patch)
tree5aedafe95426e732d4b4790edc4e23de25844e5d /plugins
parent9b5bd0ccf0cdeb49f900450df8eea41c6a0dea75 (diff)
downloaddino-a257b163376174e4f5efcbc82c9fdd56463c3191.tar.gz
dino-a257b163376174e4f5efcbc82c9fdd56463c3191.zip
Download & inline display images
Diffstat (limited to 'plugins')
-rw-r--r--plugins/http-files/CMakeLists.txt1
-rw-r--r--plugins/http-files/src/file_provider.vala106
-rw-r--r--plugins/http-files/src/manager.vala24
-rw-r--r--plugins/http-files/src/plugin.vala13
-rw-r--r--plugins/http-files/src/upload_stream_module.vala15
5 files changed, 151 insertions, 8 deletions
diff --git a/plugins/http-files/CMakeLists.txt b/plugins/http-files/CMakeLists.txt
index 565cfef0..bbb2bf64 100644
--- a/plugins/http-files/CMakeLists.txt
+++ b/plugins/http-files/CMakeLists.txt
@@ -10,6 +10,7 @@ find_packages(HTTP_FILES_PACKAGES REQUIRED
vala_precompile(HTTP_FILES_VALA_C
SOURCES
src/contact_titlebar_entry.vala
+ src/file_provider.vala
src/manager.vala
src/plugin.vala
src/register_plugin.vala
diff --git a/plugins/http-files/src/file_provider.vala b/plugins/http-files/src/file_provider.vala
new file mode 100644
index 00000000..b4a69ddb
--- /dev/null
+++ b/plugins/http-files/src/file_provider.vala
@@ -0,0 +1,106 @@
+using Gee;
+using Gtk;
+
+using Dino.Entities;
+
+namespace Dino.Plugins.HttpFiles {
+
+public class FileProvider : Plugins.FileProvider, Object {
+ public string id { get { return "http"; } }
+
+ private StreamInteractor stream_interactor;
+ private Regex url_regex;
+ private Regex file_ext_regex;
+
+ private Gee.List<string> ignore_once = new ArrayList<string>();
+
+ public FileProvider(StreamInteractor stream_interactor, Dino.Database dino_db) {
+ this.stream_interactor = stream_interactor;
+ 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)""");
+
+ Application app = GLib.Application.get_default() as Application;
+ app.plugin_registry.register_message_display(new FileMessageFilterDisplay(dino_db));
+
+ stream_interactor.get_module(MessageProcessor.IDENTITY).message_received.connect(check_message);
+ stream_interactor.get_module(MessageProcessor.IDENTITY).message_sent.connect(check_message);
+ stream_interactor.get_module(Manager.IDENTITY).uploading.connect((file_transfer) => {
+ file_transfer.provider = 0;
+ file_incoming(file_transfer);
+ });
+ stream_interactor.get_module(Manager.IDENTITY).uploaded.connect((file_transfer, url) => {
+ file_transfer.info = url;
+ ignore_once.add(url);
+ });
+ }
+
+ public void check_message(Message message, Conversation conversation) {
+ if (ignore_once.remove(message.body)) return;
+ bool in_roster = stream_interactor.get_module(RosterManager.IDENTITY).get_roster_item(conversation.account, conversation.counterpart) != null;
+ if (message.direction == Message.DIRECTION_RECEIVED && !in_roster) return;
+ if (message.body.length < 5) return;
+ if (!url_regex.match(message.body)) return;
+ if (!file_ext_regex.match(message.body)) return;
+
+ var session = new Soup.Session();
+ var head_message = new Soup.Message("HEAD", message.body);
+ if (head_message != null) {
+ session.send_message(head_message);
+ 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_type != null && content_type.has_prefix("image") && content_length != null && int.parse(content_length) < 5000000) {
+ Soup.Request request = session.request (message.body);
+ FileTransfer file_transfer = new FileTransfer();
+ file_transfer.account = conversation.account;
+ file_transfer.counterpart = conversation.counterpart;
+ file_transfer.ourpart = message.ourpart;
+ file_transfer.encryption = Encryption.NONE;
+ file_transfer.time = new DateTime.now_utc();
+ file_transfer.local_time = new DateTime.now_utc();
+ file_transfer.direction = message.direction;
+ file_transfer.input_stream = request.send();
+ 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.body;
+ file_incoming(file_transfer);
+ }
+ }
+ }
+}
+
+public class FileMessageFilterDisplay : Plugins.MessageDisplayProvider, Object {
+ public string id { get; set; default="file_message_filter"; }
+ public double priority { get; set; default=10; }
+
+ public Database db;
+
+ public FileMessageFilterDisplay(Dino.Database db) {
+ this.db = db;
+ }
+
+ public bool can_display(Entities.Message? message) {
+ return message_is_file(message);
+ }
+
+ public Plugins.MetaConversationItem? get_item(Entities.Message message, Conversation conversation) {
+ return null;
+ }
+
+ private bool message_is_file(Entities.Message message) {
+ Qlite.QueryBuilder builder = db.file_transfer.select()
+ .with(db.file_transfer.info, "=", message.body)
+ .with(db.file_transfer.account_id, "=", message.account.id)
+ .with(db.file_transfer.counterpart_id, "=", db.get_jid_id(message.counterpart));
+ return builder.count() > 0;
+ }
+}
+
+}
diff --git a/plugins/http-files/src/manager.vala b/plugins/http-files/src/manager.vala
index f398b700..14c190af 100644
--- a/plugins/http-files/src/manager.vala
+++ b/plugins/http-files/src/manager.vala
@@ -9,6 +9,8 @@ public class Manager : StreamInteractionModule, Object {
public string id { get { return IDENTITY.id; } }
public signal void upload_available(Account account);
+ public signal void uploading(FileTransfer file_transfer);
+ public signal void uploaded(FileTransfer file_transfer, string url);
private StreamInteractor stream_interactor;
private HashMap<Account, int?> max_file_sizes = new HashMap<Account, int?>(Account.hash_func, Account.equals_func);
@@ -22,11 +24,31 @@ public class Manager : StreamInteractionModule, Object {
public void send(Conversation conversation, string file_uri) {
Xmpp.Core.XmppStream? stream = stream_interactor.get_stream(conversation.account);
if (stream != null) {
+ File file = File.new_for_path(file_uri);
+ FileInfo file_info = file.query_info("*", FileQueryInfoFlags.NONE);
+
+ FileTransfer file_transfer = new FileTransfer();
+ file_transfer.account = conversation.account;
+ file_transfer.counterpart = conversation.counterpart;
+ file_transfer.ourpart = conversation.account.bare_jid;
+ file_transfer.direction = FileTransfer.DIRECTION_SENT;
+ file_transfer.time = new DateTime.now_utc();
+ file_transfer.local_time = new DateTime.now_utc();
+ file_transfer.encryption = Encryption.NONE;
+ file_transfer.file_name = file_info.get_display_name();
+ file_transfer.input_stream = file.read();
+ file_transfer.mime_type = file_info.get_content_type();
+ file_transfer.size = (int)file_info.get_size();
+ uploading(file_transfer);
+
stream_interactor.module_manager.get_module(conversation.account, UploadStreamModule.IDENTITY).upload(stream, file_uri,
(stream, url_down) => {
+ uploaded(file_transfer, url_down);
stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(url_down, conversation);
},
- () => {}
+ () => {
+ file_transfer.state = FileTransfer.State.FAILED;
+ }
);
}
diff --git a/plugins/http-files/src/plugin.vala b/plugins/http-files/src/plugin.vala
index ac6ca87a..d91b0c97 100644
--- a/plugins/http-files/src/plugin.vala
+++ b/plugins/http-files/src/plugin.vala
@@ -7,17 +7,22 @@ public class Plugin : RootInterface, Object {
public Dino.Application app;
public ConversationsTitlebarEntry conversations_titlebar_entry;
+ public FileProvider file_provider;
public void registered(Dino.Application app) {
try {
this.app = app;
- this.conversations_titlebar_entry = new ConversationsTitlebarEntry(app.stream_interactor);
+ Manager.start(this.app.stream_interactor);
+
+ conversations_titlebar_entry = new ConversationsTitlebarEntry(app.stream_interactor);
+ file_provider = new FileProvider(app.stream_interactor, app.db);
- this.app.plugin_registry.register_contact_titlebar_entry(conversations_titlebar_entry);
- this.app.stream_interactor.module_manager.initialize_account_modules.connect((account, list) => {
+ app.plugin_registry.register_contact_titlebar_entry(conversations_titlebar_entry);
+ app.stream_interactor.module_manager.initialize_account_modules.connect((account, list) => {
list.add(new UploadStreamModule());
});
- Manager.start(this.app.stream_interactor);
+
+ app.stream_interactor.get_module(FileManager.IDENTITY).add_provider(file_provider);
} catch (Error e) {
print(@"Error initializing http-files: $(e.message)\n");
}
diff --git a/plugins/http-files/src/upload_stream_module.vala b/plugins/http-files/src/upload_stream_module.vala
index 2e697afa..2e794593 100644
--- a/plugins/http-files/src/upload_stream_module.vala
+++ b/plugins/http-files/src/upload_stream_module.vala
@@ -25,9 +25,18 @@ public class UploadStreamModule : XmppStreamModule {
Soup.Message message = new Soup.Message("PUT", url_up);
message.set_request(file_info.get_content_type(), Soup.MemoryUse.COPY, data);
Soup.Session session = new Soup.Session();
- session.send_async(message);
-
- listener(stream, url_down);
+ session.send_async.begin(message, null, (obj, res) => {
+ try {
+ session.send_async.end(res);
+ if (message.status_code == 200) {
+ listener(stream, url_down);
+ } else {
+ error_listener(stream, "HTTP status code " + message.status_code.to_string());
+ }
+ } catch (Error e) {
+ error_listener(stream, e.message);
+ }
+ });
},
error_listener);
}