summaryrefslogtreecommitdiff
path: root/src/irc
diff options
context:
space:
mode:
authorLemonBoy <thatlemon@gmail.com>2017-10-21 10:35:49 +0200
committerLemonBoy <thatlemon@gmail.com>2018-01-07 12:36:18 +0100
commitd21706e1cc78284d5e7b2d69ebe4873e459d0e9b (patch)
treee43ba46f5cfd343d9704cc770164611240239e8e /src/irc
parent98836f8b7ee058c32d54562903676c4d16f83aa4 (diff)
downloadirssi-d21706e1cc78284d5e7b2d69ebe4873e459d0e9b.zip
Factor out the parsing function
This is also needed for CAP NEW and CAP DEL.
Diffstat (limited to 'src/irc')
-rw-r--r--src/irc/core/irc-cap.c58
1 files changed, 36 insertions, 22 deletions
diff --git a/src/irc/core/irc-cap.c b/src/irc/core/irc-cap.c
index eac6f239..9ac232b5 100644
--- a/src/irc/core/irc-cap.c
+++ b/src/irc/core/irc-cap.c
@@ -79,6 +79,33 @@ static void cap_emit_signal (IRC_SERVER_REC *server, char *cmd, char *args)
g_free(signal_name);
}
+static gboolean parse_cap_name(char *name, char **key, char **val)
+{
+ g_return_val_if_fail(name != NULL, FALSE);
+ g_return_val_if_fail(name[0] != '\0', FALSE);
+
+ const char *eq = strchr(name, '=');
+ /* KEY only value */
+ if (!eq) {
+ *key = g_strdup(name);
+ *val = NULL;
+ return TRUE;
+ }
+ /* Some values are in a KEY=VALUE form, parse them */
+ else if (eq[1] != '\0') {
+ *key = g_strndup(name, (int)(eq - name));
+ *val = g_strdup(eq + 1);
+ return TRUE;
+ }
+ /* If the string ends after the '=' consider the value
+ * as invalid */
+ else {
+ *key = NULL;
+ *val = NULL;
+ return FALSE;
+ }
+}
+
static void event_cap (IRC_SERVER_REC *server, char *args, char *nick, char *address)
{
GSList *tmp;
@@ -106,33 +133,20 @@ static void event_cap (IRC_SERVER_REC *server, char *args, char *nick, char *add
/* Create a list of the supported caps */
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);
+ char *key, *val;
+
+ if (parse_cap_name(caps[i], &key, &val)) {
+ if (!g_hash_table_insert(server->cap_supported,
+ key, val)) {
+ /* The specification doesn't say anything about
+ * duplicated values, let's just warn the user */
+ g_warning("Duplicate value");
+ }
}
- /* 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 */