From 1b0ce852bba81c3bbed1f7cbf04d0b60f1f0961b Mon Sep 17 00:00:00 2001 From: Dominik Heidler Date: Mon, 11 Apr 2016 20:13:18 +0200 Subject: Implement XEP-0363: HTTP File Upload --- src/command/commands.c | 71 ++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 63 insertions(+), 8 deletions(-) (limited to 'src/command/commands.c') diff --git a/src/command/commands.c b/src/command/commands.c index 50877227..257b8b5c 100644 --- a/src/command/commands.c +++ b/src/command/commands.c @@ -32,10 +32,13 @@ * */ +#define _GNU_SOURCE 1 + #include "config.h" #include #include +#include #include #include #include @@ -80,6 +83,7 @@ #ifdef HAVE_GTK #include "tray.h" #endif +#include "tools/http_upload.h" static void _update_presence(const resource_presence_t presence, const char *const show, gchar **args); @@ -128,21 +132,21 @@ cmd_execute_default(ProfWin *window, const char *inp) { ProfChatWin *chatwin = (ProfChatWin*)window; assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK); - cl_ev_send_msg(chatwin, inp); + cl_ev_send_msg(chatwin, inp, NULL); break; } case WIN_PRIVATE: { ProfPrivateWin *privatewin = (ProfPrivateWin*)window; assert(privatewin->memcheck == PROFPRIVATEWIN_MEMCHECK); - cl_ev_send_priv_msg(privatewin, inp); + cl_ev_send_priv_msg(privatewin, inp, NULL); break; } case WIN_MUC: { ProfMucWin *mucwin = (ProfMucWin*)window; assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK); - cl_ev_send_muc_msg(mucwin, inp); + cl_ev_send_muc_msg(mucwin, inp, NULL); break; } case WIN_XML: @@ -1903,7 +1907,7 @@ cmd_msg(ProfWin *window, const char *const command, gchar **args) ui_focus_win((ProfWin*)privwin); if (msg) { - cl_ev_send_priv_msg(privwin, msg); + cl_ev_send_priv_msg(privwin, msg, NULL); } g_string_free(full_jid, TRUE); @@ -1928,7 +1932,7 @@ cmd_msg(ProfWin *window, const char *const command, gchar **args) ui_focus_win((ProfWin*)chatwin); if (msg) { - cl_ev_send_msg(chatwin, msg); + cl_ev_send_msg(chatwin, msg, NULL); } else { #ifdef HAVE_LIBOTR if (otr_is_secure(barejid)) { @@ -4224,6 +4228,57 @@ cmd_disco(ProfWin *window, const char *const command, gchar **args) return TRUE; } +gboolean +cmd_sendfile(ProfWin *window, const char *const command, gchar **args) +{ + jabber_conn_status_t conn_status = jabber_get_connection_status(); + char *filename = args[0]; + + // expand ~ to $HOME + if (filename[0] == '~' && filename[1] == '/') { + if (asprintf(&filename, "%s/%s", getenv("HOME"), filename+2) == -1) { + return TRUE; + } + } else { + filename = strdup(filename); + } + + if (conn_status != JABBER_CONNECTED) { + cons_show("You are not currently connected."); + free(filename); + return TRUE; + } + + if (window->type != WIN_CHAT && window->type != WIN_PRIVATE && window->type != WIN_MUC) { + cons_show_error("Unsupported window for file transmission."); + free(filename); + return TRUE; + } + + if (access(filename, R_OK) != 0) { + cons_show_error("Uploading '%s' failed: File not found!", filename); + free(filename); + return TRUE; + } + + if (!is_regular_file(filename)) { + cons_show_error("Uploading '%s' failed: Not a file!", filename); + free(filename); + return TRUE; + } + + HTTPUpload *upload = malloc(sizeof(HTTPUpload)); + upload->window = window; + + upload->filename = filename; + upload->filesize = file_size(filename); + upload->mime_type = file_mime_type(filename); + + iq_http_upload_request(upload); + + return TRUE; +} + gboolean cmd_lastactivity(ProfWin *window, const char *const command, gchar **args) { @@ -4375,21 +4430,21 @@ cmd_tiny(ProfWin *window, const char *const command, gchar **args) { ProfChatWin *chatwin = (ProfChatWin*)window; assert(chatwin->memcheck == PROFCHATWIN_MEMCHECK); - cl_ev_send_msg(chatwin, tiny); + cl_ev_send_msg(chatwin, tiny, NULL); break; } case WIN_PRIVATE: { ProfPrivateWin *privatewin = (ProfPrivateWin*)window; assert(privatewin->memcheck == PROFPRIVATEWIN_MEMCHECK); - cl_ev_send_priv_msg(privatewin, tiny); + cl_ev_send_priv_msg(privatewin, tiny, NULL); break; } case WIN_MUC: { ProfMucWin *mucwin = (ProfMucWin*)window; assert(mucwin->memcheck == PROFMUCWIN_MEMCHECK); - cl_ev_send_muc_msg(mucwin, tiny); + cl_ev_send_muc_msg(mucwin, tiny, NULL); break; } default: -- cgit 1.4.1-2-gfad0