1
0
Fork 0

Add support for command config execution

This commit is contained in:
Paul Fariello 2018-06-20 19:30:44 +03:20
parent b11d3a79df
commit 233e076be9
9 changed files with 75 additions and 9 deletions

View File

@ -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, ProfConfWinCallback submit, ProfConfWinCallback cancel);
ProfWin* win_create_config(const char *const title, DataForm *form, ProfConfWinCallback submit, ProfConfWinCallback cancel, const void *userdata);
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);

View File

@ -182,6 +182,7 @@ struct prof_conf_win_t {
unsigned long memcheck;
ProfConfWinCallback submit;
ProfConfWinCallback cancel;
const void *userdata;
};
typedef struct prof_private_win_t {

View File

@ -203,7 +203,7 @@ win_create_muc(const char *const roomjid)
}
ProfWin*
win_create_config(const char *const roomjid, DataForm *form, ProfConfWinCallback submit, ProfConfWinCallback cancel)
win_create_config(const char *const roomjid, DataForm *form, ProfConfWinCallback submit, ProfConfWinCallback cancel, const void *userdata)
{
ProfConfWin *new_win = malloc(sizeof(ProfConfWin));
new_win->window.type = WIN_CONFIG;
@ -212,6 +212,7 @@ win_create_config(const char *const roomjid, DataForm *form, ProfConfWinCallback
new_win->form = form;
new_win->submit = submit;
new_win->cancel = cancel;
new_win->userdata = userdata;
new_win->memcheck = PROFCONFWIN_MEMCHECK;

View File

@ -657,12 +657,12 @@ wins_new_muc(const char *const roomjid)
}
ProfWin*
wins_new_config(const char *const roomjid, DataForm *form, ProfConfWinCallback submit, ProfConfWinCallback cancel)
wins_new_config(const char *const roomjid, DataForm *form, ProfConfWinCallback submit, ProfConfWinCallback cancel, const void *userdata)
{
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, submit, cancel);
ProfWin *newwin = win_create_config(roomjid, form, submit, cancel, userdata);
g_hash_table_insert(windows, GINT_TO_POINTER(result), newwin);
return newwin;
}

View File

@ -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, ProfConfWinCallback submit, ProfConfWinCallback cancel);
ProfWin* wins_new_config(const char *const roomjid, DataForm *form, ProfConfWinCallback submit, ProfConfWinCallback cancel, const void *userdata);
ProfWin* wins_new_private(const char *const fulljid);
ProfWin* wins_new_plugin(const char *const plugin_name, const char *const tag);

View File

@ -88,6 +88,11 @@ typedef struct privilege_set_t {
char *privilege;
} ProfPrivilegeSet;
typedef struct command_config_data_t {
char *sessionid;
char *command;
} CommandConfigData;
static int _iq_handler(xmpp_conn_t *const conn, xmpp_stanza_t *const stanza, void *const userdata);
static void _error_handler(xmpp_stanza_t *const stanza);
@ -724,6 +729,36 @@ iq_command_exec(const char *const target, const char *const command)
xmpp_stanza_release(iq);
}
void
iq_submit_command_config(ProfConfWin *confwin)
{
xmpp_ctx_t * const ctx = connection_get_ctx();
CommandConfigData *data = (CommandConfigData *)confwin->userdata;
xmpp_stanza_t *iq = stanza_create_command_config_submit_iq(ctx, confwin->roomjid, data->command, data->sessionid, confwin->form);
const char *id = xmpp_stanza_get_id(iq);
iq_id_handler_add(id, _command_exec_response_handler, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
free(data->sessionid);
free(data->command);
free(data);
}
void
iq_cancel_command_config(ProfConfWin *confwin)
{
xmpp_ctx_t * const ctx = connection_get_ctx();
CommandConfigData *data = (CommandConfigData *)confwin->userdata;
xmpp_stanza_t *iq = stanza_create_room_config_cancel_iq(ctx, confwin->roomjid);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
free(data->sessionid);
free(data->command);
free(data);
}
static void
_error_handler(xmpp_stanza_t *const stanza)
{
@ -1098,7 +1133,7 @@ _command_exec_response_handler(xmpp_stanza_t *const stanza, void *const userdata
const char *id = xmpp_stanza_get_id(stanza);
const char *type = xmpp_stanza_get_type(stanza);
const char *from = xmpp_stanza_get_from(stanza);
const char *const command = userdata;
char *command = userdata;
if (id) {
log_debug("IQ command exec response handler fired, id: %s.", id);
@ -1156,7 +1191,10 @@ _command_exec_response_handler(xmpp_stanza_t *const stanza, void *const userdata
}
DataForm *form = form_create(x);
ProfConfWin *confwin = (ProfConfWin*)wins_new_config(from, form, NULL, NULL);
CommandConfigData *data = malloc(sizeof(CommandConfigData));
data->sessionid = strdup(xmpp_stanza_get_attribute(cmd, "sessionid"));
data->command = command;
ProfConfWin *confwin = (ProfConfWin*)wins_new_config(from, form, iq_submit_command_config, iq_cancel_command_config, data);
confwin_handle_configuration(confwin, form);
} else if (g_strcmp0(status, "canceled") == 0) {
win_handle_command_exec_status(win, command, "canceled");
@ -1703,7 +1741,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, iq_submit_room_config, iq_room_config_cancel);
ProfConfWin *confwin = (ProfConfWin*)wins_new_config(from, form, iq_submit_room_config, iq_room_config_cancel, NULL);
confwin_handle_configuration(confwin, form);
return 0;

View File

@ -2063,6 +2063,31 @@ stanza_create_command_exec_iq(xmpp_ctx_t *ctx, const char *const target,
return iq;
}
xmpp_stanza_t*
stanza_create_command_config_submit_iq(xmpp_ctx_t *ctx, const char *const room,
const char *const node, const char *const sessionid, DataForm *form)
{
char *id = connection_create_stanza_id("commandconf_submit");
xmpp_stanza_t *iq = xmpp_iq_new(ctx, STANZA_TYPE_SET, id);
free(id);
xmpp_stanza_set_to(iq, room);
xmpp_stanza_t *command = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(command, STANZA_NAME_COMMAND);
xmpp_stanza_set_ns(command, STANZA_NS_COMMAND);
xmpp_stanza_set_attribute(command, "node", node);
xmpp_stanza_set_attribute(command, "sessionid", sessionid);
xmpp_stanza_t *x = form_create_submission(form);
xmpp_stanza_add_child(command, x);
xmpp_stanza_release(x);
xmpp_stanza_add_child(iq, command);
xmpp_stanza_release(command);
return iq;
}
static void
_stanza_add_unique_id(xmpp_stanza_t *stanza, char *prefix)
{

View File

@ -282,6 +282,7 @@ xmpp_stanza_t* stanza_create_room_kick_iq(xmpp_ctx_t *const ctx, const char *con
const char *const reason);
xmpp_stanza_t* stanza_create_command_exec_iq(xmpp_ctx_t *ctx, const char *const target, const char *const node);
xmpp_stanza_t* stanza_create_command_config_submit_iq(xmpp_ctx_t *ctx, const char *const room, const char *const node, const char *const sessionid, DataForm *form);
int stanza_get_idle_time(xmpp_stanza_t *const stanza);

View File

@ -494,7 +494,7 @@ ProfWin* win_create_muc(const char * const roomjid)
{
return NULL;
}
ProfWin* win_create_config(const char * const title, DataForm *form)
ProfWin* win_create_config(const char *const title, DataForm *form, ProfConfWinCallback submit, ProfConfWinCallback cancel, const void *userdata)
{
return NULL;
}