From 2e414797a472a4f55206453fb7d5e139f7a30f3b Mon Sep 17 00:00:00 2001 From: James Booth Date: Sat, 27 Jan 2018 22:42:31 +0000 Subject: Allow filtering rooms list by glob --- src/command/cmd_ac.c | 43 ++++++++++++++++++++++++++ src/command/cmd_defs.c | 17 +++++++---- src/command/cmd_funcs.c | 80 +++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 126 insertions(+), 14 deletions(-) (limited to 'src/command') diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c index 8e53bb10..5ecdfbe7 100644 --- a/src/command/cmd_ac.c +++ b/src/command/cmd_ac.c @@ -98,6 +98,7 @@ static char* _sendfile_autocomplete(ProfWin *window, const char *const input, gb static char* _blocked_autocomplete(ProfWin *window, const char *const input, gboolean previous); static char* _tray_autocomplete(ProfWin *window, const char *const input, gboolean previous); static char* _presence_autocomplete(ProfWin *window, const char *const input, gboolean previous); +static char* _rooms_autocomplete(ProfWin *window, const char *const input, gboolean previous); static char* _script_autocomplete_func(const char *const prefix, gboolean previous); @@ -161,6 +162,7 @@ static Autocomplete alias_ac; static Autocomplete aliases_ac; static Autocomplete join_property_ac; static Autocomplete room_ac; +static Autocomplete rooms_ac; static Autocomplete affiliation_ac; static Autocomplete role_ac; static Autocomplete privilege_cmd_ac; @@ -591,6 +593,10 @@ cmd_ac_init(void) autocomplete_add(room_ac, "destroy"); autocomplete_add(room_ac, "config"); + rooms_ac = autocomplete_new(); + autocomplete_add(rooms_ac, "service"); + autocomplete_add(rooms_ac, "match"); + affiliation_ac = autocomplete_new(); autocomplete_add(affiliation_ac, "owner"); autocomplete_add(affiliation_ac, "admin"); @@ -1005,6 +1011,7 @@ cmd_ac_reset(ProfWin *window) autocomplete_reset(aliases_ac); autocomplete_reset(join_property_ac); autocomplete_reset(room_ac); + autocomplete_reset(rooms_ac); autocomplete_reset(affiliation_ac); autocomplete_reset(role_ac); autocomplete_reset(privilege_cmd_ac); @@ -1124,6 +1131,7 @@ cmd_ac_uninit(void) autocomplete_free(aliases_ac); autocomplete_free(join_property_ac); autocomplete_free(room_ac); + autocomplete_free(rooms_ac); autocomplete_free(affiliation_ac); autocomplete_free(role_ac); autocomplete_free(privilege_cmd_ac); @@ -1395,6 +1403,7 @@ _cmd_ac_complete_params(ProfWin *window, const char *const input, gboolean previ g_hash_table_insert(ac_funcs, "/blocked", _blocked_autocomplete); g_hash_table_insert(ac_funcs, "/tray", _tray_autocomplete); g_hash_table_insert(ac_funcs, "/presence", _presence_autocomplete); + g_hash_table_insert(ac_funcs, "/rooms", _rooms_autocomplete); int len = strlen(input); char parsed[len+1]; @@ -3090,3 +3099,37 @@ _presence_autocomplete(ProfWin *window, const char *const input, gboolean previo return NULL; } +static char* +_rooms_autocomplete(ProfWin *window, const char *const input, gboolean previous) +{ + char *found = NULL; + gboolean result = FALSE; + + gchar **args = parse_args(input, 0, 4, &result); + + if (result) { + gboolean space_at_end = g_str_has_suffix(input, " "); + int num_args = g_strv_length(args); + if (num_args <= 1) { + found = autocomplete_param_with_ac(input, "/rooms", rooms_ac, TRUE, previous); + if (found) { + g_strfreev(args); + return found; + } + } + if ((num_args == 2 && space_at_end) || (num_args == 3 && !space_at_end)) { + GString *beginning = g_string_new("/rooms"); + g_string_append_printf(beginning, " %s %s", args[0], args[1]); + found = autocomplete_param_with_ac(input, beginning->str, rooms_ac, TRUE, previous); + g_string_free(beginning, TRUE); + if (found) { + g_strfreev(args); + return found; + } + } + } + + g_strfreev(args); + + return NULL; +} \ No newline at end of file diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index fa7e50d0..8d483fe0 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -788,20 +788,27 @@ static struct cmd_t command_defs[] = }, { "/rooms", - parse_args, 0, 1, NULL, + parse_args, 0, 4, NULL, CMD_NOSUBFUNCS CMD_MAINFUNC(cmd_rooms) CMD_TAGS( CMD_TAG_GROUPCHAT) CMD_SYN( - "/rooms []") + "/rooms", + "/rooms match ", + "/rooms service ", + "/rooms service match ") CMD_DESC( "List the chat rooms available at the specified conference service. " - "If no argument is supplied, the account preference 'muc.service' is used, 'conference.' by default.") + "If no argument is supplied, the account preference 'muc.service' is used, 'conference.' by default. " + "The match argument accepts a glob and returns only room names that match.") CMD_ARGS( - { "", "The conference service to query." }) + { "service ", "The conference service to query." }, + { "match ", "The string to match before displaying results."}) CMD_EXAMPLES( - "/rooms conference.jabber.org") + "/rooms", + "/rooms match *development*", + "/rooms service conference.jabber.org") }, { "/bookmark", diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index 509e19ee..90c4b567 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -4393,18 +4393,80 @@ cmd_rooms(ProfWin *window, const char *const command, gchar **args) return TRUE; } - if (args[0]) { - iq_room_list_request(args[0]); - return TRUE; + char *service = NULL; + char *match = NULL; + if (args[0] != NULL) { + if (g_strcmp0(args[0], "service") == 0) { + if (args[1] == NULL) { + cons_bad_cmd_usage(command); + cons_show(""); + return TRUE; + } + service = g_strdup(args[1]); + } else if (g_strcmp0(args[0], "match") == 0) { + if (args[1] == NULL) { + cons_bad_cmd_usage(command); + cons_show(""); + return TRUE; + } + match = g_strdup(args[1]); + } else { + cons_bad_cmd_usage(command); + cons_show(""); + return TRUE; + } + } + if (g_strv_length(args) >=3 ) { + if (g_strcmp0(args[2], "service") == 0) { + if (args[3] == NULL) { + cons_bad_cmd_usage(command); + cons_show(""); + g_free(service); + g_free(match); + return TRUE; + } + g_free(service); + service = g_strdup(args[3]); + } else if (g_strcmp0(args[2], "match") == 0) { + if (args[3] == NULL) { + cons_bad_cmd_usage(command); + cons_show(""); + g_free(service); + g_free(match); + return TRUE; + } + g_free(match); + match = g_strdup(args[3]); + } else { + cons_bad_cmd_usage(command); + cons_show(""); + return TRUE; + } } - ProfAccount *account = accounts_get_account(session_get_account_name()); - if (account->muc_service) { - iq_room_list_request(account->muc_service); - } else { - cons_show("Account MUC service property not found."); + GPatternSpec *glob = NULL; + if (match != NULL) { + glob = g_pattern_spec_new(match); + g_free(match); } - account_free(account); + + if (service == NULL) { + ProfAccount *account = accounts_get_account(session_get_account_name()); + if (account->muc_service) { + service = g_strdup(account->muc_service); + account_free(account); + } else { + cons_show("Account MUC service property not found."); + account_free(account); + g_free(service); + g_free(match); + return TRUE; + } + } + + iq_room_list_request(service, glob); + + g_free(service); return TRUE; } -- cgit 1.4.1-2-gfad0