about summary refs log tree commit diff stats
path: root/src/xmpp
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/xmpp
parenta952776b899dd29056ad6779b14dab75a4125924 (diff)
downloadprofani-tty-b11d3a79df49970b4563bbd40c4db3f53c99dfeb.tar.gz
Add conf win callback
Diffstat (limited to 'src/xmpp')
-rw-r--r--src/xmpp/iq.c68
-rw-r--r--src/xmpp/xmpp.h4
2 files changed, 41 insertions, 31 deletions
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);