From 98836f8b7ee058c32d54562903676c4d16f83aa4 Mon Sep 17 00:00:00 2001 From: LemonBoy Date: Sat, 21 Oct 2017 10:21:03 +0200 Subject: Parse the K/V form in CAP LS This is a prerequisite for the IRC v3.2 compliance. --- src/irc/core/irc-cap.c | 43 +++++++++++++++++++++++++++++++++++++++---- src/irc/core/irc-servers.c | 2 +- src/irc/core/irc-servers.h | 2 +- 3 files changed, 41 insertions(+), 6 deletions(-) (limited to 'src/irc') diff --git a/src/irc/core/irc-cap.c b/src/irc/core/irc-cap.c index 5464e493..eac6f239 100644 --- a/src/irc/core/irc-cap.c +++ b/src/irc/core/irc-cap.c @@ -45,7 +45,7 @@ int cap_toggle (IRC_SERVER_REC *server, char *cap, int enable) if (enable && !gslist_find_string(server->cap_active, cap)) { /* Make sure the required cap is supported by the server */ - if (!gslist_find_string(server->cap_supported, cap)) + if (!g_hash_table_lookup_extended(server->cap_supported, cap, NULL, NULL)) return FALSE; irc_send_cmdv(server, "CAP REQ %s", cap); @@ -96,9 +96,44 @@ static void event_cap (IRC_SERVER_REC *server, char *args, char *nick, char *add caps_length = g_strv_length(caps); if (!g_strcmp0(evt, "LS")) { + if (server->cap_supported) { + g_hash_table_destroy(server->cap_supported); + } + /* Start with a fresh table */ + server->cap_supported = g_hash_table_new_full(g_str_hash, + g_str_equal, + g_free, g_free); + /* Create a list of the supported caps */ - for (i = 0; i < caps_length; i++) - server->cap_supported = g_slist_prepend(server->cap_supported, g_strdup(caps[i])); + for (i = 0; i < caps_length; i++) { + const char *name = caps[i]; + const char *eq = strchr(name, '='); + int fresh = TRUE; + + if (!eq) { + fresh = g_hash_table_insert(server->cap_supported, + g_strdup(name), + NULL); + } + /* Some values are in a KEY=VALUE form, parse them */ + else if (eq[1] != '\0') { + char *key = g_strndup(name, (int)(eq - name)); + char *val = g_strdup(eq + 1); + fresh = g_hash_table_insert(server->cap_supported, + key, val); + } + /* If the string ends after the '=' consider the value + * as invalid */ + else { + g_warning("Invalid CAP key/value pair"); + } + + /* The specification doesn't say anything about + * duplicated values, let's just warn the user */ + if (fresh == FALSE) { + g_warning("Duplicate value"); + } + } /* Request the required caps, if any */ if (server->cap_queue == NULL) { @@ -111,7 +146,7 @@ static void event_cap (IRC_SERVER_REC *server, char *args, char *nick, char *add /* Check whether the cap is supported by the server */ for (tmp = server->cap_queue; tmp != NULL; tmp = tmp->next) { - if (gslist_find_string(server->cap_supported, tmp->data)) { + if (g_hash_table_lookup_extended(server->cap_supported, tmp->data, NULL, NULL)) { if (avail_caps > 0) g_string_append_c(cmd, ' '); g_string_append(cmd, tmp->data); diff --git a/src/irc/core/irc-servers.c b/src/irc/core/irc-servers.c index 4eaab712..7fd83045 100644 --- a/src/irc/core/irc-servers.c +++ b/src/irc/core/irc-servers.c @@ -443,7 +443,7 @@ static void sig_disconnected(IRC_SERVER_REC *server) gslist_free_full(server->cap_active, (GDestroyNotify) g_free); server->cap_active = NULL; - gslist_free_full(server->cap_supported, (GDestroyNotify) g_free); + g_hash_table_destroy(server->cap_supported); server->cap_supported = NULL; gslist_free_full(server->cap_queue, (GDestroyNotify) g_free); diff --git a/src/irc/core/irc-servers.h b/src/irc/core/irc-servers.h index 09f3f81d..6c6002c8 100644 --- a/src/irc/core/irc-servers.h +++ b/src/irc/core/irc-servers.h @@ -75,7 +75,7 @@ struct _IRC_SERVER_REC { int max_whois_in_cmd; /* max. number of nicks in one /WHOIS command */ int max_msgs_in_cmd; /* max. number of targets in one /MSG */ - GSList *cap_supported; /* A list of caps supported by the server */ + GHashTable *cap_supported; /* A list of caps supported by the server */ GSList *cap_active; /* A list of caps active for this session */ GSList *cap_queue; /* A list of caps to request on connection */ -- cgit v1.2.3