about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorPaul Fariello <paul@fariello.eu>2018-05-30 20:45:06 +0320
committerPaul Fariello <paul@fariello.eu>2018-09-05 13:51:00 +0200
commitb11d3a79df49970b4563bbd40c4db3f53c99dfeb (patch)
treefd728212d3ef74b13a59b6e8fd3fe9a3727611ee /src
parenta952776b899dd29056ad6779b14dab75a4125924 (diff)
downloadprofani-tty-b11d3a79df49970b4563bbd40c4db3f53c99dfeb.tar.gz
Add conf win callback
Diffstat (limited to 'src')
-rw-r--r--src/command/cmd_funcs.c4
-rw-r--r--src/ui/ui.h3
-rw-r--r--src/ui/win_types.h9
-rw-r--r--src/ui/window.c11
-rw-r--r--src/ui/window_list.c4
-rw-r--r--src/ui/window_list.h2
-rw-r--r--src/xmpp/iq.c68
-rw-r--r--src/xmpp/xmpp.h4
8 files changed, 65 insertions, 40 deletions
diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c
index 6f1d7d51..475a81b4 100644
--- a/src/command/cmd_funcs.c
+++ b/src/command/cmd_funcs.c
@@ -3899,11 +3899,11 @@ cmd_form(ProfWin *window, const char *const command, gchar **args)
     }
 
     if (g_strcmp0(args[0], "submit") == 0) {
-        iq_submit_room_config(confwin->roomjid, confwin->form);
+        confwin->submit(confwin);
     }
 
     if (g_strcmp0(args[0], "cancel") == 0) {
-        iq_room_config_cancel(confwin->roomjid);
+        confwin->cancel(confwin);
     }
 
     if ((g_strcmp0(args[0], "submit") == 0) || (g_strcmp0(args[0], "cancel") == 0)) {
diff --git a/src/ui/ui.h b/src/ui/ui.h
index 573fdfcc..1bcc20fc 100644
--- a/src/ui/ui.h
+++ b/src/ui/ui.h
@@ -346,7 +346,7 @@ ProfWin* win_create_console(void);
 ProfWin* win_create_xmlconsole(void);
 ProfWin* win_create_chat(const char *const barejid);
 ProfWin* win_create_muc(const char *const roomjid);
-ProfWin* win_create_config(const char *const title, DataForm *form);
+ProfWin* win_create_config(const char *const title, DataForm *form, ProfConfWinCallback submit, ProfConfWinCallback cancel);
 ProfWin* win_create_private(const char *const fulljid);
 ProfWin* win_create_plugin(const char *const plugin_name, const char *const tag);
 void win_update_virtual(ProfWin *window);
@@ -380,6 +380,7 @@ char* win_to_string(ProfWin *window);
 void win_command_list_error(ProfWin *window, const char *const error);
 void win_command_exec_error(ProfWin *window, const char *const command, const char *const error, ...);
 void win_handle_command_list(ProfWin *window, GSList *cmds);
+void win_handle_command_exec_status(ProfWin *window, const char *const type, const char *const value);
 void win_handle_command_exec_result_note(ProfWin *window, const char *const type, const char *const value);
 
 // desktop notifications
diff --git a/src/ui/win_types.h b/src/ui/win_types.h
index 28a785bf..c9dc623b 100644
--- a/src/ui/win_types.h
+++ b/src/ui/win_types.h
@@ -172,12 +172,17 @@ typedef struct prof_muc_win_t {
     char *message_char;
 } ProfMucWin;
 
-typedef struct prof_conf_win_t {
+typedef struct prof_conf_win_t ProfConfWin;
+typedef void (*ProfConfWinCallback)(ProfConfWin *);
+
+struct prof_conf_win_t {
     ProfWin window;
     char *roomjid;
     DataForm *form;
     unsigned long memcheck;
-} ProfConfWin;
+    ProfConfWinCallback submit;
+    ProfConfWinCallback cancel;
+};
 
 typedef struct prof_private_win_t {
     ProfWin window;
diff --git a/src/ui/window.c b/src/ui/window.c
index 16cd9490..f34e354e 100644
--- a/src/ui/window.c
+++ b/src/ui/window.c
@@ -203,13 +203,15 @@ win_create_muc(const char *const roomjid)
 }
 
 ProfWin*
-win_create_config(const char *const roomjid, DataForm *form)
+win_create_config(const char *const roomjid, DataForm *form, ProfConfWinCallback submit, ProfConfWinCallback cancel)
 {
     ProfConfWin *new_win = malloc(sizeof(ProfConfWin));
     new_win->window.type = WIN_CONFIG;
     new_win->window.layout = _win_create_simple_layout();
     new_win->roomjid = strdup(roomjid);
     new_win->form = form;
+    new_win->submit = submit;
+    new_win->cancel = cancel;
 
     new_win->memcheck = PROFCONFWIN_MEMCHECK;
 
@@ -1769,6 +1771,13 @@ win_handle_command_list(ProfWin *window, GSList *cmds)
 }
 
 void
+win_handle_command_exec_status(ProfWin *window, const char *const command, const char *const value)
+{
+    assert(window != NULL);
+    win_println(window, THEME_DEFAULT, '!', "%s %s", command, value);
+}
+
+void
 win_handle_command_exec_result_note(ProfWin *window, const char *const type, const char *const value)
 {
     assert(window != NULL);
diff --git a/src/ui/window_list.c b/src/ui/window_list.c
index d386f1dd..8f886b54 100644
--- a/src/ui/window_list.c
+++ b/src/ui/window_list.c
@@ -657,12 +657,12 @@ wins_new_muc(const char *const roomjid)
 }
 
 ProfWin*
-wins_new_config(const char *const roomjid, DataForm *form)
+wins_new_config(const char *const roomjid, DataForm *form, ProfConfWinCallback submit, ProfConfWinCallback cancel)
 {
     GList *keys = g_hash_table_get_keys(windows);
     int result = _wins_get_next_available_num(keys);
     g_list_free(keys);
-    ProfWin *newwin = win_create_config(roomjid, form);
+    ProfWin *newwin = win_create_config(roomjid, form, submit, cancel);
     g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin);
     return newwin;
 }
diff --git a/src/ui/window_list.h b/src/ui/window_list.h
index 0309fe00..f427a6d7 100644
--- a/src/ui/window_list.h
+++ b/src/ui/window_list.h
@@ -42,7 +42,7 @@ void wins_init(void);
 ProfWin* wins_new_xmlconsole(void);
 ProfWin* wins_new_chat(const char *const barejid);
 ProfWin* wins_new_muc(const char *const roomjid);
-ProfWin* wins_new_config(const char *const roomjid, DataForm *form);
+ProfWin* wins_new_config(const char *const roomjid, DataForm *form, ProfConfWinCallback submit, ProfConfWinCallback cancel);
 ProfWin* wins_new_private(const char *const fulljid);
 ProfWin* wins_new_plugin(const char *const plugin_name, const char *const tag);
 
diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c
index 60c19b88..8e7ecc0b 100644
--- a/src/xmpp/iq.c
+++ b/src/xmpp/iq.c
@@ -586,10 +586,10 @@ iq_request_room_config_form(const char *const room_jid)
 }
 
 void
-iq_submit_room_config(const char *const room, DataForm *form)
+iq_submit_room_config(ProfConfWin *confwin)
 {
     xmpp_ctx_t * const ctx = connection_get_ctx();
-    xmpp_stanza_t *iq = stanza_create_room_config_submit_iq(ctx, room, form);
+    xmpp_stanza_t *iq = stanza_create_room_config_submit_iq(ctx, confwin->roomjid, confwin->form);
 
     const char *id = xmpp_stanza_get_id(iq);
     iq_id_handler_add(id, _room_config_submit_id_handler, NULL, NULL);
@@ -599,10 +599,10 @@ iq_submit_room_config(const char *const room, DataForm *form)
 }
 
 void
-iq_room_config_cancel(const char *const room_jid)
+iq_room_config_cancel(ProfConfWin *confwin)
 {
     xmpp_ctx_t * const ctx = connection_get_ctx();
-    xmpp_stanza_t *iq = stanza_create_room_config_cancel_iq(ctx, room_jid);
+    xmpp_stanza_t *iq = stanza_create_room_config_cancel_iq(ctx, confwin->roomjid);
     iq_send_stanza(iq);
     xmpp_stanza_release(iq);
 }
@@ -1106,61 +1106,71 @@ _command_exec_response_handler(xmpp_stanza_t *const stanza, void *const userdata
         log_debug("IQ command exec response handler fired.");
     }
 
+    ProfWin *win = wins_get_by_string(from);
+    if (win == NULL) {
+        /* No more window associated with this command.
+         * Fallback to console. */
+        win = wins_get_console();
+    }
+
     // handle error responses
     if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) {
         char *error_message = stanza_get_error_message(stanza);
         log_debug("Error executing command %s for %s: %s", command, from, error_message);
-        ProfWin *win = wins_get_by_string(from);
-        if (win) {
-            win_command_exec_error(win, command, error_message);
-        }
+        win_command_exec_error(win, command, error_message);
         free(error_message);
         return 0;
     }
 
     xmpp_stanza_t *cmd = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_COMMAND);
     if (!cmd) {
-        /* TODO */
+        log_error("No command element for command response");
+        win_command_exec_error(win, command, "Malformed command response");
+        return 0;
     }
 
-    ProfWin *win = wins_get_by_string(from);
 
     const char *status = xmpp_stanza_get_attribute(cmd, STANZA_ATTR_STATUS);
     if (g_strcmp0(status, "completed") == 0) {
-        if (win) {
-            xmpp_stanza_t *note = xmpp_stanza_get_child_by_name(cmd, "note");
-            if (note) {
-                const char *type = xmpp_stanza_get_attribute(note, "type");
-                const char *value = xmpp_stanza_get_text(note);
-                win_handle_command_exec_result_note(win, type, value);
-            }
+        win_handle_command_exec_status(win, command, "completed");
+        xmpp_stanza_t *note = xmpp_stanza_get_child_by_name(cmd, "note");
+        if (note) {
+            const char *type = xmpp_stanza_get_attribute(note, "type");
+            const char *value = xmpp_stanza_get_text(note);
+            win_handle_command_exec_result_note(win, type, value);
         }
-        return 0;
-    }
+    } else if (g_strcmp0(status, "executing") == 0) {
+        win_handle_command_exec_status(win, command, "executing");
 
-    if (g_strcmp0(status, "executing") == 0) {
+        /* Looking for a jabber:x:data type form */
         xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(cmd, STANZA_NS_DATA);
         if (x == NULL) {
-            /* TODO */
             return 0;
         }
 
         const char *form_type = xmpp_stanza_get_type(x);
         if (g_strcmp0(form_type, "form") != 0) {
-            /* TODO */
+            log_error("Unsupported payload in command response");
+            win_command_exec_error(win, command, "Unsupported command response");
             return 0;
         }
 
         DataForm *form = form_create(x);
-        ProfConfWin *confwin = (ProfConfWin*)wins_new_config(from, form);
+        ProfConfWin *confwin = (ProfConfWin*)wins_new_config(from, form, NULL, NULL);
         confwin_handle_configuration(confwin, form);
+    } else if (g_strcmp0(status, "canceled") == 0) {
+        win_handle_command_exec_status(win, command, "canceled");
+        xmpp_stanza_t *note = xmpp_stanza_get_child_by_name(cmd, "note");
+        if (note) {
+            const char *type = xmpp_stanza_get_attribute(note, "type");
+            const char *value = xmpp_stanza_get_text(note);
+            win_handle_command_exec_result_note(win, type, value);
+        }
+    } else {
+        log_error("Unsupported command status %s", status);
+        win_command_exec_error(win, command, "Malformed command response");
     }
 
-    if (g_strcmp0(status, "canceled") == 0) {
-    }
-
-    /* TODO */
-
     return 0;
 }
 
@@ -1693,7 +1703,7 @@ _room_config_id_handler(xmpp_stanza_t *const stanza, void *const userdata)
     }
 
     DataForm *form = form_create(x);
-    ProfConfWin *confwin = (ProfConfWin*)wins_new_config(from, form);
+    ProfConfWin *confwin = (ProfConfWin*)wins_new_config(from, form, iq_submit_room_config, iq_room_config_cancel);
     confwin_handle_configuration(confwin, form);
 
     return 0;
diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h
index c0e1477d..44acd97e 100644
--- a/src/xmpp/xmpp.h
+++ b/src/xmpp/xmpp.h
@@ -171,8 +171,8 @@ void iq_set_autoping(int seconds);
 void iq_confirm_instant_room(const char *const room_jid);
 void iq_destroy_room(const char *const room_jid);
 void iq_request_room_config_form(const char *const room_jid);
-void iq_submit_room_config(const char *const room, DataForm *form);
-void iq_room_config_cancel(const char *const room_jid);
+void iq_submit_room_config(ProfConfWin *confwin);
+void iq_room_config_cancel(ProfConfWin *confwin);
 void iq_send_ping(const char *const target);
 void iq_room_info_request(const char *const room, gboolean display_result);
 void iq_room_affiliation_list(const char *const room, char *affiliation);