1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

Allow filtering rooms list by glob

This commit is contained in:
James Booth 2018-01-27 22:42:31 +00:00
parent d4f06ac300
commit 2e414797a4
5 changed files with 196 additions and 22 deletions

View File

@ -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* _blocked_autocomplete(ProfWin *window, const char *const input, gboolean previous);
static char* _tray_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* _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); 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 aliases_ac;
static Autocomplete join_property_ac; static Autocomplete join_property_ac;
static Autocomplete room_ac; static Autocomplete room_ac;
static Autocomplete rooms_ac;
static Autocomplete affiliation_ac; static Autocomplete affiliation_ac;
static Autocomplete role_ac; static Autocomplete role_ac;
static Autocomplete privilege_cmd_ac; static Autocomplete privilege_cmd_ac;
@ -591,6 +593,10 @@ cmd_ac_init(void)
autocomplete_add(room_ac, "destroy"); autocomplete_add(room_ac, "destroy");
autocomplete_add(room_ac, "config"); autocomplete_add(room_ac, "config");
rooms_ac = autocomplete_new();
autocomplete_add(rooms_ac, "service");
autocomplete_add(rooms_ac, "match");
affiliation_ac = autocomplete_new(); affiliation_ac = autocomplete_new();
autocomplete_add(affiliation_ac, "owner"); autocomplete_add(affiliation_ac, "owner");
autocomplete_add(affiliation_ac, "admin"); autocomplete_add(affiliation_ac, "admin");
@ -1005,6 +1011,7 @@ cmd_ac_reset(ProfWin *window)
autocomplete_reset(aliases_ac); autocomplete_reset(aliases_ac);
autocomplete_reset(join_property_ac); autocomplete_reset(join_property_ac);
autocomplete_reset(room_ac); autocomplete_reset(room_ac);
autocomplete_reset(rooms_ac);
autocomplete_reset(affiliation_ac); autocomplete_reset(affiliation_ac);
autocomplete_reset(role_ac); autocomplete_reset(role_ac);
autocomplete_reset(privilege_cmd_ac); autocomplete_reset(privilege_cmd_ac);
@ -1124,6 +1131,7 @@ cmd_ac_uninit(void)
autocomplete_free(aliases_ac); autocomplete_free(aliases_ac);
autocomplete_free(join_property_ac); autocomplete_free(join_property_ac);
autocomplete_free(room_ac); autocomplete_free(room_ac);
autocomplete_free(rooms_ac);
autocomplete_free(affiliation_ac); autocomplete_free(affiliation_ac);
autocomplete_free(role_ac); autocomplete_free(role_ac);
autocomplete_free(privilege_cmd_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, "/blocked", _blocked_autocomplete);
g_hash_table_insert(ac_funcs, "/tray", _tray_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, "/presence", _presence_autocomplete);
g_hash_table_insert(ac_funcs, "/rooms", _rooms_autocomplete);
int len = strlen(input); int len = strlen(input);
char parsed[len+1]; char parsed[len+1];
@ -3090,3 +3099,37 @@ _presence_autocomplete(ProfWin *window, const char *const input, gboolean previo
return NULL; 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;
}

View File

@ -788,20 +788,27 @@ static struct cmd_t command_defs[] =
}, },
{ "/rooms", { "/rooms",
parse_args, 0, 1, NULL, parse_args, 0, 4, NULL,
CMD_NOSUBFUNCS CMD_NOSUBFUNCS
CMD_MAINFUNC(cmd_rooms) CMD_MAINFUNC(cmd_rooms)
CMD_TAGS( CMD_TAGS(
CMD_TAG_GROUPCHAT) CMD_TAG_GROUPCHAT)
CMD_SYN( CMD_SYN(
"/rooms [<service>]") "/rooms",
"/rooms match <glob>",
"/rooms service <service>",
"/rooms service <service> match <glob>")
CMD_DESC( CMD_DESC(
"List the chat rooms available at the specified conference service. " "List the chat rooms available at the specified conference service. "
"If no argument is supplied, the account preference 'muc.service' is used, 'conference.<domain-part>' by default.") "If no argument is supplied, the account preference 'muc.service' is used, 'conference.<domain-part>' by default. "
"The match argument accepts a glob and returns only room names that match.")
CMD_ARGS( CMD_ARGS(
{ "<service>", "The conference service to query." }) { "service <service>", "The conference service to query." },
{ "match <glob>", "The string to match before displaying results."})
CMD_EXAMPLES( CMD_EXAMPLES(
"/rooms conference.jabber.org") "/rooms",
"/rooms match *development*",
"/rooms service conference.jabber.org")
}, },
{ "/bookmark", { "/bookmark",

View File

@ -4393,18 +4393,80 @@ cmd_rooms(ProfWin *window, const char *const command, gchar **args)
return TRUE; return TRUE;
} }
if (args[0]) { char *service = NULL;
iq_room_list_request(args[0]); 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; 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;
}
}
GPatternSpec *glob = NULL;
if (match != NULL) {
glob = g_pattern_spec_new(match);
g_free(match);
}
if (service == NULL) {
ProfAccount *account = accounts_get_account(session_get_account_name()); ProfAccount *account = accounts_get_account(session_get_account_name());
if (account->muc_service) { if (account->muc_service) {
iq_room_list_request(account->muc_service); service = g_strdup(account->muc_service);
account_free(account);
} else { } else {
cons_show("Account MUC service property not found."); cons_show("Account MUC service property not found.");
}
account_free(account); account_free(account);
g_free(service);
g_free(match);
return TRUE;
}
}
iq_room_list_request(service, glob);
g_free(service);
return TRUE; return TRUE;
} }

View File

@ -119,6 +119,7 @@ static int _caps_response_id_handler(xmpp_stanza_t *const stanza, void *const us
static int _caps_response_for_jid_id_handler(xmpp_stanza_t *const stanza, void *const userdata); static int _caps_response_for_jid_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 _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 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);
@ -126,6 +127,9 @@ static void _iq_free_affiliation_set(ProfPrivilegeSet *affiliation_set);
// scheduled // scheduled
static int _autoping_timed_send(xmpp_conn_t *const conn, void *const userdata); static int _autoping_timed_send(xmpp_conn_t *const conn, void *const userdata);
static void _identity_destroy(DiscoIdentity *identity);
static void _item_destroy(DiscoItem *item);
static gboolean autoping_wait = FALSE; static gboolean autoping_wait = FALSE;
static GTimer *autoping_time = NULL; static GTimer *autoping_time = NULL;
static GHashTable *id_handlers; static GHashTable *id_handlers;
@ -293,10 +297,14 @@ iq_set_autoping(const int seconds)
} }
void void
iq_room_list_request(gchar *conferencejid) iq_room_list_request(gchar *conferencejid, GPatternSpec *glob)
{ {
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, "confreq", conferencejid); char *id = create_unique_id("confreq");
xmpp_stanza_t *iq = stanza_create_disco_items_iq(ctx, id, conferencejid);
iq_id_handler_add(id, _room_list_id_handler, (ProfIdFreeCallback)g_pattern_spec_free, glob);
iq_send_stanza(iq); iq_send_stanza(iq);
xmpp_stanza_release(iq); xmpp_stanza_release(iq);
} }
@ -890,6 +898,63 @@ _caps_response_legacy_id_handler(xmpp_stanza_t *const stanza, void *const userda
return 0; return 0;
} }
static int
_room_list_id_handler(xmpp_stanza_t *const stanza, void *const userdata)
{
const char *id = xmpp_stanza_get_id(stanza);
const char *from = xmpp_stanza_get_from(stanza);
log_debug("Response to query: %s", id);
xmpp_stanza_t *query = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_QUERY);
if (query == NULL) {
return 0;
}
xmpp_stanza_t *child = xmpp_stanza_get_children(query);
if (child == NULL) {
return 0;
}
GPatternSpec *glob = (GPatternSpec*)userdata;
if (child == NULL) {
cons_show("No rooms found for service: %s", from);
return 0;
}
gboolean matched = FALSE;
cons_show("Chat rooms at: %s", from);
while (child) {
const char *stanza_name = xmpp_stanza_get_name(child);
if (stanza_name && (g_strcmp0(stanza_name, STANZA_NAME_ITEM) == 0)) {
const char *item_jid = xmpp_stanza_get_attribute(child, STANZA_ATTR_JID);
const char *item_name = xmpp_stanza_get_attribute(child, STANZA_ATTR_NAME);
if ((item_jid) && ((glob == NULL) ||
((g_pattern_match(glob, strlen(item_jid), item_jid, NULL)) ||
(item_name && g_pattern_match(glob, strlen(item_name), item_name, NULL))))) {
if (glob) {
matched = TRUE;
}
GString *item = g_string_new(item_jid);
if (item_name) {
g_string_append(item, " (");
g_string_append(item, item_name);
g_string_append(item, ")");
}
cons_show(" %s", item->str);
g_string_free(item, TRUE);
}
}
child = xmpp_stanza_get_next(child);
}
if (glob && matched == FALSE) {
cons_show(" No rooms found matching pattern.");
}
return 0;
}
static int static int
_enable_carbons_id_handler(xmpp_stanza_t *const stanza, void *const userdata) _enable_carbons_id_handler(xmpp_stanza_t *const stanza, void *const userdata)
{ {
@ -1992,8 +2057,7 @@ _disco_items_result_handler(xmpp_stanza_t *const stanza)
const char *from = xmpp_stanza_get_from(stanza); const char *from = xmpp_stanza_get_from(stanza);
GSList *items = NULL; GSList *items = NULL;
if ((g_strcmp0(id, "confreq") != 0) && if ((g_strcmp0(id, "discoitemsreq") != 0) &&
(g_strcmp0(id, "discoitemsreq") != 0) &&
(g_strcmp0(id, "discoitemsreq_onconnect") != 0)) { (g_strcmp0(id, "discoitemsreq_onconnect") != 0)) {
return; return;
} }
@ -2030,9 +2094,7 @@ _disco_items_result_handler(xmpp_stanza_t *const stanza)
child = xmpp_stanza_get_next(child); child = xmpp_stanza_get_next(child);
} }
if (g_strcmp0(id, "confreq") == 0) { if (g_strcmp0(id, "discoitemsreq") == 0) {
cons_show_room_list(items, from);
} else if (g_strcmp0(id, "discoitemsreq") == 0) {
cons_show_disco_items(items, from); cons_show_disco_items(items, from);
} else if (g_strcmp0(id, "discoitemsreq_onconnect") == 0) { } else if (g_strcmp0(id, "discoitemsreq_onconnect") == 0) {
connection_set_disco_items(items); connection_set_disco_items(items);

View File

@ -161,7 +161,7 @@ gboolean presence_sub_request_exists(const char *const bare_jid);
void iq_enable_carbons(void); void iq_enable_carbons(void);
void iq_disable_carbons(void); void iq_disable_carbons(void);
void iq_send_software_version(const char *const fulljid); void iq_send_software_version(const char *const fulljid);
void iq_room_list_request(gchar *conferencejid); void iq_room_list_request(gchar *conferencejid, GPatternSpec *glob);
void iq_disco_info_request(gchar *jid); void iq_disco_info_request(gchar *jid);
void iq_disco_items_request(gchar *jid); void iq_disco_items_request(gchar *jid);
void iq_last_activity_request(gchar *jid); void iq_last_activity_request(gchar *jid);