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

Parse the K/V form in CAP LS

This is a prerequisite for the IRC v3.2 compliance.
This commit is contained in:
LemonBoy 2017-10-21 10:21:03 +02:00
parent d85f867261
commit 98836f8b7e
4 changed files with 52 additions and 10 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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 */

View File

@ -12,7 +12,10 @@ static void perl_irc_connect_fill_hash(HV *hv, IRC_SERVER_CONNECT_REC *conn)
static void perl_irc_server_fill_hash(HV *hv, IRC_SERVER_REC *server)
{
AV *av;
HV *hv_;
GSList *tmp;
GHashTableIter iter;
gpointer key_, val_;
perl_irc_connect_fill_hash(hv, server->connrec);
perl_server_fill_hash(hv, (SERVER_REC *) server);
@ -34,10 +37,14 @@ static void perl_irc_server_fill_hash(HV *hv, IRC_SERVER_REC *server)
(void) hv_store(hv, "cap_complete", 12, newSViv(server->cap_complete), 0);
(void) hv_store(hv, "sasl_success", 12, newSViv(server->sasl_success), 0);
av = newAV();
for (tmp = server->cap_supported; tmp != NULL; tmp = tmp->next)
av_push(av, new_pv(tmp->data));
(void) hv_store(hv, "cap_supported", 13, newRV_noinc((SV*)av), 0);
hv_ = newHV();
g_hash_table_iter_init(&iter, server->cap_supported);
while (g_hash_table_iter_next(&iter, &key_, &val_)) {
char *key = (char *)key_;
char *val = (char *)val_;
hv_store(hv_, key, strlen(key), new_pv(val), 0);
}
(void) hv_store(hv, "cap_supported", 13, newRV_noinc((SV*)hv_), 0);
av = newAV();
for (tmp = server->cap_active; tmp != NULL; tmp = tmp->next)