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/contact_titlebar_entry.vala68
-rw-r--r--plugins/http-files/src/manager.vala56
-rw-r--r--plugins/http-files/src/plugin.vala31
-rw-r--r--plugins/http-files/src/register_plugin.vala3
-rw-r--r--plugins/http-files/src/upload_stream_module.vala159
5 files changed, 317 insertions, 0 deletions
diff --git a/plugins/http-files/src/contact_titlebar_entry.vala b/plugins/http-files/src/contact_titlebar_entry.vala
new file mode 100644
index 00000000..a87c7ddf
--- /dev/null
+++ b/plugins/http-files/src/contact_titlebar_entry.vala
@@ -0,0 +1,68 @@
+using Gtk;
+
+using Dino.Entities;
+
+namespace Dino.Plugins.HttpFiles {
+
+public class ConversationsTitlebarEntry : Plugins.ConversationTitlebarEntry {
+ public override string id { get { return "send_files"; } }
+
+ StreamInteractor stream_interactor;
+
+ public ConversationsTitlebarEntry(StreamInteractor stream_interactor) {
+ this.stream_interactor = stream_interactor;
+ }
+
+ public override double order { get { return 4; } }
+ public override Plugins.ConversationTitlebarWidget get_widget() {
+ return new ConversationTitlebarWidget(stream_interactor) { visible=true };
+ }
+}
+
+public class ConversationTitlebarWidget : Button, Plugins.ConversationTitlebarWidget {
+
+ private Conversation? conversation;
+ private StreamInteractor stream_interactor;
+
+ public ConversationTitlebarWidget(StreamInteractor stream_interactor) {
+ this.stream_interactor = stream_interactor;
+ image = new Image.from_icon_name("mail-attachment-symbolic", IconSize.MENU);
+ clicked.connect(on_clicked);
+ stream_interactor.get_module(Manager.IDENTITY).upload_available.connect(on_upload_available);
+ }
+
+ public void on_clicked() {
+ FileChooserDialog chooser = new FileChooserDialog (
+ "Select file", null, FileChooserAction.OPEN,
+ "Cancel", ResponseType.CANCEL,
+ "Select", ResponseType.ACCEPT);
+ int? max_file_size = stream_interactor.get_module(Manager.IDENTITY).get_max_file_size(conversation.account);
+ if (max_file_size != null) {
+ FileFilter filter = new FileFilter();
+ filter.add_custom(FileFilterFlags.URI, (filter_info) => {
+ File file = File.new_for_uri(filter_info.uri);
+ FileInfo file_info = file.query_info("*", FileQueryInfoFlags.NONE);
+ return file_info.get_size() <= max_file_size;
+ });
+ chooser.set_filter(filter);
+ }
+ if (chooser.run() == Gtk.ResponseType.ACCEPT) {
+ string uri = chooser.get_filename();
+ stream_interactor.get_module(Manager.IDENTITY).send(conversation, uri);
+ }
+ chooser.close();
+ }
+
+ public void on_upload_available(Account account) {
+ if (conversation.account.equals(account)) {
+ visible = true;
+ }
+ }
+
+ public new void set_conversation(Conversation conversation) {
+ this.conversation = conversation;
+ visible = stream_interactor.get_module(Manager.IDENTITY).is_upload_available(conversation.account);
+ }
+}
+
+}
diff --git a/plugins/http-files/src/manager.vala b/plugins/http-files/src/manager.vala
new file mode 100644
index 00000000..5b804a00
--- /dev/null
+++ b/plugins/http-files/src/manager.vala
@@ -0,0 +1,56 @@
+using Dino.Entities;
+using Xmpp;
+using Gee;
+
+namespace Dino.Plugins.HttpFiles {
+
+public class Manager : StreamInteractionModule, Object {
+ public static ModuleIdentity<Manager> IDENTITY = new ModuleIdentity<Manager>("http_files");
+ public string id { get { return IDENTITY.id; } }
+
+ public signal void upload_available(Account account);
+
+ private StreamInteractor stream_interactor;
+ private HashMap<Account, int?> max_file_sizes = new HashMap<Account, int?>(Account.hash_func, Account.equals_func);
+
+ private Manager(StreamInteractor stream_interactor) {
+ this.stream_interactor = stream_interactor;
+
+ stream_interactor.stream_negotiated.connect(on_stream_negotiated);
+ }
+
+ public void send(Conversation conversation, string file_uri) {
+ Xmpp.Core.XmppStream? stream = stream_interactor.get_stream(conversation.account);
+ if (stream != null) {
+ stream_interactor.module_manager.get_module(conversation.account, UploadStreamModule.IDENTITY).upload(stream, file_uri,
+ (stream, url_down) => {
+ stream_interactor.get_module(MessageProcessor.IDENTITY).send_message(url_down, conversation);
+ },
+ () => {}
+ );
+
+ }
+ }
+
+ public bool is_upload_available(Account account) {
+ return max_file_sizes.has_key(account);
+ }
+
+ public int? get_max_file_size(Account account) {
+ return max_file_sizes[account];
+ }
+
+ private void on_stream_negotiated(Account account, Core.XmppStream stream) {
+ stream_interactor.module_manager.get_module(account, UploadStreamModule.IDENTITY).feature_available.connect((stream, max_file_size) => {
+ max_file_sizes[account] = max_file_size;
+ upload_available(account);
+ });
+ }
+
+ public static void start(StreamInteractor stream_interactor) {
+ Manager m = new Manager(stream_interactor);
+ stream_interactor.add_module(m);
+ }
+}
+
+}
diff --git a/plugins/http-files/src/plugin.vala b/plugins/http-files/src/plugin.vala
new file mode 100644
index 00000000..572efd93
--- /dev/null
+++ b/plugins/http-files/src/plugin.vala
@@ -0,0 +1,31 @@
+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 ConversationsTitlebarEntry conversations_titlebar_entry;
+
+ public void registered(Dino.Application app) {
+ try {
+ this.app = app;
+ this.conversations_titlebar_entry = new ConversationsTitlebarEntry(app.stream_interaction);
+
+ this.app.plugin_registry.register_contact_titlebar_entry(conversations_titlebar_entry);
+ this.app.stream_interaction.module_manager.initialize_account_modules.connect((account, list) => {
+ list.add(new UploadStreamModule());
+ });
+ Manager.start(this.app.stream_interaction);
+ } catch (Error e) {
+ print(@"Error initializing http-files: $(e.message)\n");
+ }
+ }
+
+ public void shutdown() {
+ // Nothing to do
+ }
+}
+
+}
diff --git a/plugins/http-files/src/register_plugin.vala b/plugins/http-files/src/register_plugin.vala
new file mode 100644
index 00000000..21fac531
--- /dev/null
+++ b/plugins/http-files/src/register_plugin.vala
@@ -0,0 +1,3 @@
+public Type register_plugin(Module module) {
+ return typeof (Dino.Plugins.HttpFiles.Plugin);
+}
diff --git a/plugins/http-files/src/upload_stream_module.vala b/plugins/http-files/src/upload_stream_module.vala
new file mode 100644
index 00000000..765d212e
--- /dev/null
+++ b/plugins/http-files/src/upload_stream_module.vala
@@ -0,0 +1,159 @@
+using Xmpp;
+using Xmpp.Core;
+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 Core.ModuleIdentity<UploadStreamModule> IDENTITY = new Core.ModuleIdentity<UploadStreamModule>(NS_URI, "0363_http_file_upload");
+
+ public signal void feature_available(XmppStream stream, int? max_file_size);
+
+ public delegate void OnUploadOk(XmppStream stream, string url_down);
+ public delegate void OnError(XmppStream stream, string error);
+ public void upload(XmppStream stream, string file_uri, owned OnUploadOk listener, owned OnError error_listener) {
+ File file = File.new_for_path(file_uri);
+ FileInfo file_info = file.query_info("*", FileQueryInfoFlags.NONE);
+ request_slot(stream, file.get_basename(), (int)file_info.get_size(), file_info.get_content_type(),
+ (stream, url_down, url_up) => {
+ uint8[] data;
+ FileUtils.get_data(file_uri, out data);
+
+ 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_up);
+ },
+ error_listener);
+ }
+
+ 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;
+ if (flag.ns_ver == 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);
+ } else{
+ 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)));
+ }
+ }
+ 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, "");
+ } else {
+ string? url_get = iq.stanza.get_deep_string_content(flag.ns_ver + ":slot", flag.ns_ver + ":get");
+ string? url_put = iq.stanza.get_deep_string_content(flag.ns_ver + ":slot", flag.ns_ver + ":put");
+ listener(stream, url_get, url_put);
+ }
+ });
+ }
+
+ public override void attach(XmppStream stream) {
+ Iq.Module.require(stream);
+ ServiceDiscovery.Module.require(stream);
+
+ query_availability(stream);
+ }
+
+ public override void detach(XmppStream stream) {
+ stream.get_module(Bind.Module.IDENTITY).bound_to_resource.disconnect(query_availability);
+ }
+
+ public static void require(XmppStream stream) {
+ if (stream.get_module(IDENTITY) == null) stream.add_module(new ChatMarkers.Module());
+ }
+
+ 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) {
+ if (item.name == "HTTP File Upload") {
+ stream.get_module(ServiceDiscovery.Module.IDENTITY).request_info(stream, item.jid, (stream, info_result) => {
+ check_ns_in_info(stream, item.jid, info_result);
+ });
+ break;
+ }
+ }
+ });
+ }
+ });
+ }
+
+ private bool check_ns_in_info(XmppStream stream, string 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) {
+ int? 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 int? 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<StanzaNode> 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;
+ }
+ }
+ return max_file_size_str != null ? int.parse(max_file_size_str) : (int?) null;
+ }
+}
+
+public class Flag : XmppStreamFlag {
+ public static FlagIdentity<Flag> IDENTITY = new FlagIdentity<Flag>(NS_URI, "service_discovery");
+
+ public string file_store_jid;
+ public string ns_ver;
+ public int? max_file_size;
+
+ public Flag(string 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; }
+}
+
+}