about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2018-01-27 22:42:31 +0000
committerJames Booth <boothj5@gmail.com>2018-01-27 22:42:31 +0000
commit2e414797a472a4f55206453fb7d5e139f7a30f3b (patch)
treecfdd3fcfa3c7cd08f8132eefbf4717b3d81b42d3
parentd4f06ac3008931ea439f3256853f3bf5b9f809b1 (diff)
downloadprofani-tty-2e414797a472a4f55206453fb7d5e139f7a30f3b.tar.gz
Allow filtering rooms list by glob
-rw-r--r--src/command/cmd_ac.c43
-rw-r--r--src/command/cmd_defs.c17
-rw-r--r--src/command/cmd_funcs.c80
-rw-r--r--src/xmpp/iq.c76
-rw-r--r--src/xmpp/xmpp.h2
5 files changed, 196 insertions, 22 deletions
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 [<service>]")
+            "/rooms",
+            "/rooms match <glob>",
+            "/rooms service <service>",
+            "/rooms service <service> match <glob>")
         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.<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(
-            { "<service>", "The conference service to query." })
+            { "service <service>", "The conference service to query." },
+            { "match <glob>", "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;
 }
diff --git a/src/xmpp/iq.c b/src/xmpp/iq.c
index 74c40c28..ce163858 100644
--- a/src/xmpp/iq.c
+++ b/src/xmpp/iq.c
@@ -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_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 _room_list_id_handler(xmpp_stanza_t *const stanza, void *const userdata);
 
 static void _iq_free_room_data(ProfRoomInfoData *roominfo);
 static void _iq_free_affiliation_set(ProfPrivilegeSet *affiliation_set);
@@ -126,6 +127,9 @@ static void _iq_free_affiliation_set(ProfPrivilegeSet *affiliation_set);
 // scheduled
 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 GTimer *autoping_time = NULL;
 static GHashTable *id_handlers;
@@ -293,10 +297,14 @@ iq_set_autoping(const int seconds)
 }
 
 void
-iq_room_list_request(gchar *conferencejid)
+iq_room_list_request(gchar *conferencejid, GPatternSpec *glob)
 {
     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);
     xmpp_stanza_release(iq);
 }
@@ -891,6 +899,63 @@ _caps_response_legacy_id_handler(xmpp_stanza_t *const stanza, void *const userda
 }
 
 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
 _enable_carbons_id_handler(xmpp_stanza_t *const stanza, void *const userdata)
 {
     const char *type = xmpp_stanza_get_type(stanza);
@@ -1992,8 +2057,7 @@ _disco_items_result_handler(xmpp_stanza_t *const stanza)
     const char *from = xmpp_stanza_get_from(stanza);
     GSList *items = NULL;
 
-    if ((g_strcmp0(id, "confreq") != 0) &&
-            (g_strcmp0(id, "discoitemsreq") != 0) &&
+    if ((g_strcmp0(id, "discoitemsreq") != 0) &&
             (g_strcmp0(id, "discoitemsreq_onconnect") != 0)) {
         return;
     }
@@ -2030,9 +2094,7 @@ _disco_items_result_handler(xmpp_stanza_t *const stanza)
         child = xmpp_stanza_get_next(child);
     }
 
-    if (g_strcmp0(id, "confreq") == 0) {
-        cons_show_room_list(items, from);
-    } else if (g_strcmp0(id, "discoitemsreq") == 0) {
+    if (g_strcmp0(id, "discoitemsreq") == 0) {
         cons_show_disco_items(items, from);
     } else if (g_strcmp0(id, "discoitemsreq_onconnect") == 0) {
         connection_set_disco_items(items);
diff --git a/src/xmpp/xmpp.h b/src/xmpp/xmpp.h
index 29401c52..38364624 100644
--- a/src/xmpp/xmpp.h
+++ b/src/xmpp/xmpp.h
@@ -161,7 +161,7 @@ gboolean presence_sub_request_exists(const char *const bare_jid);
 void iq_enable_carbons(void);
 void iq_disable_carbons(void);
 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_items_request(gchar *jid);
 void iq_last_activity_request(gchar *jid);