1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-10-27 20:30:13 -04:00

Add command subcommands: list and exec

Also handle list result
This commit is contained in:
Paul Fariello 2018-03-22 04:47:52 +02:20
parent ca022ec75e
commit c9f6a78f57
10 changed files with 158 additions and 26 deletions

View File

@ -2300,19 +2300,24 @@ static struct cmd_t command_defs[] =
"/export ~/contacts.csv") "/export ~/contacts.csv")
}, },
{ "/command", { "/cmd",
parse_args, 1, 1, NULL, parse_args, 1, 3, NULL,
CMD_NOSUBFUNCS CMD_SUBFUNCS(
CMD_MAINFUNC(cmd_command) { "list", cmd_command_list },
{ "exec", cmd_command_exec })
CMD_NOMAINFUNC
CMD_NOTAGS CMD_NOTAGS
CMD_SYN( CMD_SYN(
"/command <cmd>") "/otr list",
"/otr exec <command>")
CMD_DESC( CMD_DESC(
"Execute an ad hoc command") "Execute ad hoc commands.")
CMD_ARGS( CMD_ARGS(
{ "<cmd>", "Command to be executed" }) { "list", "List supported ad hoc commands." },
{ "exec <command>", "Execute a command." })
CMD_EXAMPLES( CMD_EXAMPLES(
"/command ping") "/cmd list",
"/cmd exec ping")
} }
}; };

View File

@ -7470,7 +7470,7 @@ cmd_encwarn(ProfWin *window, const char *const command, gchar **args)
} }
gboolean gboolean
cmd_command(ProfWin *window, const char *const command, gchar **args) cmd_command_list(ProfWin *window, const char *const command, gchar **args)
{ {
jabber_conn_status_t conn_status = connection_get_status(); jabber_conn_status_t conn_status = connection_get_status();
@ -7479,14 +7479,37 @@ cmd_command(ProfWin *window, const char *const command, gchar **args)
return TRUE; return TRUE;
} }
if (args[0] == NULL && connection_supports(XMPP_FEATURE_COMMANDS) == FALSE) { if (connection_supports(XMPP_FEATURE_COMMANDS) == FALSE) {
cons_show("Server does not support ad hoc commands."); cons_show("Server does not support ad hoc commands.");
return TRUE; return TRUE;
} }
ProfMucWin *mucwin = (ProfMucWin*)window; ProfMucWin *mucwin = (ProfMucWin*)window;
iq_send_command(mucwin->roomjid, args[0]); iq_command_list(mucwin->roomjid);
cons_show("List available ad hoc commands");
return TRUE;
}
gboolean
cmd_command_exec(ProfWin *window, const char *const command, gchar **args)
{
jabber_conn_status_t conn_status = connection_get_status();
if (conn_status != JABBER_CONNECTED) {
cons_show("You are not currently connected.");
return TRUE;
}
if (connection_supports(XMPP_FEATURE_COMMANDS) == FALSE) {
cons_show("Server does not support ad hoc commands.");
return TRUE;
}
ProfMucWin *mucwin = (ProfMucWin*)window;
iq_command_exec(mucwin->roomjid, args[0]);
cons_show("Execute %s...", args[0]); cons_show("Execute %s...", args[0]);
return TRUE; return TRUE;

View File

@ -158,7 +158,8 @@ gboolean cmd_script(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_export(ProfWin *window, const char *const command, gchar **args); gboolean cmd_export(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_charset(ProfWin *window, const char *const command, gchar **args); gboolean cmd_charset(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_console(ProfWin *window, const char *const command, gchar **args); gboolean cmd_console(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_command(ProfWin *window, const char *const command, gchar **args); gboolean cmd_command_list(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_command_exec(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_plugins(ProfWin *window, const char *const command, gchar **args); gboolean cmd_plugins(ProfWin *window, const char *const command, gchar **args);
gboolean cmd_plugins_sourcepath(ProfWin *window, const char *const command, gchar **args); gboolean cmd_plugins_sourcepath(ProfWin *window, const char *const command, gchar **args);

View File

@ -377,6 +377,8 @@ void win_show_info(ProfWin *window, PContact contact);
void win_clear(ProfWin *window); void win_clear(ProfWin *window);
char* win_get_tab_identifier(ProfWin *window); char* win_get_tab_identifier(ProfWin *window);
char* win_to_string(ProfWin *window); char* win_to_string(ProfWin *window);
void win_command_list_error(ProfWin *window, const char *const error);
void win_handle_command_list(ProfWin *window, GSList *cmds);
// desktop notifications // desktop notifications
void notifier_initialise(void); void notifier_initialise(void);

View File

@ -1724,3 +1724,31 @@ win_sub_newline_lazy(WINDOW *win)
wmove(win, cury+1, 0); wmove(win, cury+1, 0);
} }
} }
void
win_command_list_error(ProfWin *window, const char *const error)
{
assert(window != NULL);
win_println(window, THEME_ERROR, '!', "Error retrieving command list: %s", error);
}
void
win_handle_command_list(ProfWin *window, GSList *cmds)
{
assert(window != NULL);
if (cmds) {
win_println(window, THEME_DEFAULT, '!', "Ad hoc commands:");
GSList *curr_cmd = cmds;
while (curr_cmd) {
const char *cmd = curr_cmd->data;
win_println(window, THEME_DEFAULT, '!', " %s", cmd);
curr_cmd = g_slist_next(curr_cmd);
}
win_println(window, THEME_DEFAULT, '!', "");
} else {
win_println(window, THEME_DEFAULT, '!', "No commands found");
win_println(window, THEME_DEFAULT, '!', "");
}
}

View File

@ -120,7 +120,8 @@ static int _caps_response_for_jid_id_handler(xmpp_stanza_t *const stanza, void *
static int _caps_response_legacy_id_handler(xmpp_stanza_t *const stanza, void *const userdata); static int _caps_response_legacy_id_handler(xmpp_stanza_t *const stanza, void *const userdata);
static int _auto_pong_id_handler(xmpp_stanza_t *const stanza, void *const userdata); static int _auto_pong_id_handler(xmpp_stanza_t *const stanza, void *const userdata);
static int _room_list_id_handler(xmpp_stanza_t *const stanza, void *const userdata); static int _room_list_id_handler(xmpp_stanza_t *const stanza, void *const userdata);
static int _command_response_handler(xmpp_stanza_t *const stanza, void *const userdata); static int _command_list_result_handler(xmpp_stanza_t *const stanza, void *const userdata);
static int _command_exec_response_handler(xmpp_stanza_t *const stanza, void *const userdata);
static void _iq_free_room_data(ProfRoomInfoData *roominfo); static void _iq_free_room_data(ProfRoomInfoData *roominfo);
static void _iq_free_affiliation_set(ProfPrivilegeSet *affiliation_set); static void _iq_free_affiliation_set(ProfPrivilegeSet *affiliation_set);
@ -320,7 +321,7 @@ iq_room_list_request(gchar *conferencejid, gchar *filter)
xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_ctx_t * const ctx = connection_get_ctx();
char *id = connection_create_stanza_id("confreq"); char *id = connection_create_stanza_id("confreq");
xmpp_stanza_t *iq = stanza_create_disco_items_iq(ctx, id, conferencejid); xmpp_stanza_t *iq = stanza_create_disco_items_iq(ctx, id, conferencejid, NULL);
iq_id_handler_add(id, _room_list_id_handler, NULL, filter); iq_id_handler_add(id, _room_list_id_handler, NULL, filter);
@ -522,7 +523,7 @@ void
iq_disco_items_request(gchar *jid) iq_disco_items_request(gchar *jid)
{ {
xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_ctx_t * const ctx = connection_get_ctx();
xmpp_stanza_t *iq = stanza_create_disco_items_iq(ctx, "discoitemsreq", jid); xmpp_stanza_t *iq = stanza_create_disco_items_iq(ctx, "discoitemsreq", jid, NULL);
iq_send_stanza(iq); iq_send_stanza(iq);
xmpp_stanza_release(iq); xmpp_stanza_release(iq);
} }
@ -531,7 +532,7 @@ void
iq_disco_items_request_onconnect(gchar *jid) iq_disco_items_request_onconnect(gchar *jid)
{ {
xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_ctx_t * const ctx = connection_get_ctx();
xmpp_stanza_t *iq = stanza_create_disco_items_iq(ctx, "discoitemsreq_onconnect", jid); xmpp_stanza_t *iq = stanza_create_disco_items_iq(ctx, "discoitemsreq_onconnect", jid, NULL);
iq_send_stanza(iq); iq_send_stanza(iq);
xmpp_stanza_release(iq); xmpp_stanza_release(iq);
} }
@ -698,13 +699,26 @@ iq_send_ping(const char *const target)
} }
void void
iq_send_command(const char *const target, const char *const command) iq_command_list(const char *const target)
{ {
xmpp_ctx_t * const ctx = connection_get_ctx(); xmpp_ctx_t * const ctx = connection_get_ctx();
xmpp_stanza_t *iq = stanza_create_command_iq(ctx, target, command); const char *id = connection_create_stanza_id("cmdlist");
xmpp_stanza_t *iq = stanza_create_disco_items_iq(ctx, id, target, STANZA_NS_COMMAND);
iq_id_handler_add(id, _command_list_result_handler, NULL, NULL);
iq_send_stanza(iq);
xmpp_stanza_release(iq);
}
void
iq_command_exec(const char *const target, const char *const command)
{
xmpp_ctx_t * const ctx = connection_get_ctx();
xmpp_stanza_t *iq = stanza_create_command_exec_iq(ctx, target, command);
const char *id = xmpp_stanza_get_id(iq); const char *id = xmpp_stanza_get_id(iq);
iq_id_handler_add(id, _command_response_handler, free, strdup(command)); iq_id_handler_add(id, _command_exec_response_handler, free, strdup(command));
iq_send_stanza(iq); iq_send_stanza(iq);
xmpp_stanza_release(iq); xmpp_stanza_release(iq);
@ -1026,9 +1040,62 @@ _room_list_id_handler(xmpp_stanza_t *const stanza, void *const userdata)
} }
static int static int
_command_response_handler(xmpp_stanza_t *const stanza, void *const userdata) _command_list_result_handler(xmpp_stanza_t *const stanza, void *const userdata)
{ {
cons_show("Plop", NULL); const char *id = xmpp_stanza_get_id(stanza);
const char *type = xmpp_stanza_get_type(stanza);
char *from = strdup(xmpp_stanza_get_from(stanza));
if (id) {
log_debug("IQ command list result handler fired, id: %s.", id);
} else {
log_debug("IQ command list result handler fired.");
}
// handle error responses
if (g_strcmp0(type, STANZA_TYPE_ERROR) == 0) {
char *error_message = stanza_get_error_message(stanza);
log_debug("Error retrieving command list for %s: %s", from, error_message);
ProfWin *win = wins_get_by_string(from);
if (win) {
win_command_list_error(win, error_message);
}
free(error_message);
free(from);
return 0;
}
GSList *cmds = NULL;
xmpp_stanza_t *query = xmpp_stanza_get_child_by_ns(stanza, XMPP_NS_DISCO_ITEMS);
if (query) {
xmpp_stanza_t *child = xmpp_stanza_get_children(query);
while (child) {
const char *name = xmpp_stanza_get_name(child);
if (g_strcmp0(name, "item") == 0) {
const char *node = xmpp_stanza_get_attribute(child, STANZA_ATTR_NODE);
if (node) {
cmds = g_slist_insert_sorted(cmds, (gpointer)node, (GCompareFunc)g_strcmp0);
}
}
child = xmpp_stanza_get_next(child);
}
}
ProfWin *win = wins_get_by_string(from);
if (win) {
win_handle_command_list(win, cmds);
}
g_slist_free(cmds);
free(from);
return 0;
}
static int
_command_exec_response_handler(xmpp_stanza_t *const stanza, void *const userdata)
{
cons_show("%s", NULL, __func__);
return 0; return 0;
} }

View File

@ -924,7 +924,7 @@ stanza_create_disco_info_iq(xmpp_ctx_t *ctx, const char *const id, const char *c
xmpp_stanza_t* xmpp_stanza_t*
stanza_create_disco_items_iq(xmpp_ctx_t *ctx, const char *const id, stanza_create_disco_items_iq(xmpp_ctx_t *ctx, const char *const id,
const char *const jid) const char *const jid, const char *const node)
{ {
xmpp_stanza_t *iq = xmpp_iq_new(ctx, STANZA_TYPE_GET, id); xmpp_stanza_t *iq = xmpp_iq_new(ctx, STANZA_TYPE_GET, id);
xmpp_stanza_set_to(iq, jid); xmpp_stanza_set_to(iq, jid);
@ -932,6 +932,9 @@ stanza_create_disco_items_iq(xmpp_ctx_t *ctx, const char *const id,
xmpp_stanza_t *query = xmpp_stanza_new(ctx); xmpp_stanza_t *query = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(query, STANZA_NAME_QUERY); xmpp_stanza_set_name(query, STANZA_NAME_QUERY);
xmpp_stanza_set_ns(query, XMPP_NS_DISCO_ITEMS); xmpp_stanza_set_ns(query, XMPP_NS_DISCO_ITEMS);
if (node) {
xmpp_stanza_set_attribute(query, STANZA_ATTR_NODE, node);
}
xmpp_stanza_add_child(iq, query); xmpp_stanza_add_child(iq, query);
xmpp_stanza_release(query); xmpp_stanza_release(query);
@ -2039,7 +2042,7 @@ stanza_parse_presence(xmpp_stanza_t *stanza, int *err)
} }
xmpp_stanza_t* xmpp_stanza_t*
stanza_create_command_iq(xmpp_ctx_t *ctx, const char *const target, stanza_create_command_exec_iq(xmpp_ctx_t *ctx, const char *const target,
const char *const node) const char *const node)
{ {
char *id = create_unique_id("command"); char *id = create_unique_id("command");

View File

@ -280,7 +280,7 @@ xmpp_stanza_t* stanza_create_room_subject_message(xmpp_ctx_t *ctx, const char *c
xmpp_stanza_t* stanza_create_room_kick_iq(xmpp_ctx_t *const ctx, const char *const room, const char *const nick, xmpp_stanza_t* stanza_create_room_kick_iq(xmpp_ctx_t *const ctx, const char *const room, const char *const nick,
const char *const reason); const char *const reason);
xmpp_stanza_t* stanza_create_command_iq(xmpp_ctx_t *ctx, const char *const target, const char *const node); xmpp_stanza_t* stanza_create_command_exec_iq(xmpp_ctx_t *ctx, const char *const target, const char *const node);
int stanza_get_idle_time(xmpp_stanza_t *const stanza); int stanza_get_idle_time(xmpp_stanza_t *const stanza);
@ -296,7 +296,7 @@ EntityCapabilities* stanza_create_caps_from_query_element(xmpp_stanza_t *query);
const char* stanza_get_presence_string_from_type(resource_presence_t presence_type); const char* stanza_get_presence_string_from_type(resource_presence_t presence_type);
xmpp_stanza_t* stanza_create_software_version_iq(xmpp_ctx_t *ctx, const char *const fulljid); xmpp_stanza_t* stanza_create_software_version_iq(xmpp_ctx_t *ctx, const char *const fulljid);
xmpp_stanza_t* stanza_create_disco_items_iq(xmpp_ctx_t *ctx, const char *const id, const char *const jid); xmpp_stanza_t* stanza_create_disco_items_iq(xmpp_ctx_t *ctx, const char *const id, const char *const jid, const char *const node);
char* stanza_get_status(xmpp_stanza_t *stanza, char *def); char* stanza_get_status(xmpp_stanza_t *stanza, char *def);
char* stanza_get_show(xmpp_stanza_t *stanza, char *def); char* stanza_get_show(xmpp_stanza_t *stanza, char *def);

View File

@ -183,7 +183,8 @@ void iq_room_role_set(const char *const room, const char *const nick, char *role
void iq_room_role_list(const char * const room, char *role); void iq_room_role_list(const char * const room, char *role);
void iq_autoping_check(void); void iq_autoping_check(void);
void iq_http_upload_request(HTTPUpload *upload); void iq_http_upload_request(HTTPUpload *upload);
void iq_send_command(const char *const target, const char *const command); void iq_command_list(const char *const target);
void iq_command_exec(const char *const target, const char *const command);
EntityCapabilities* caps_lookup(const char *const jid); EntityCapabilities* caps_lookup(const char *const jid);
void caps_close(void); void caps_close(void);

View File

@ -205,6 +205,8 @@ void iq_room_role_list(const char * const room, char *role) {}
void iq_last_activity_request(gchar *jid) {} void iq_last_activity_request(gchar *jid) {}
void iq_autoping_check(void) {} void iq_autoping_check(void) {}
void iq_rooms_cache_clear(void) {} void iq_rooms_cache_clear(void) {}
void iq_command_list(const char *const target) {}
void iq_command_exec(const char *const target, const char *const command) {}
// caps functions // caps functions
void caps_add_feature(char *feature) {} void caps_add_feature(char *feature) {}