From dd91b521df5adc08e6b45be1927d0af6e5c2dcd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Sat, 13 Jun 2015 12:27:58 +0200 Subject: [PATCH] kike: some refactoring --- kike.c | 37 +++++++++++++++++++++---------------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/kike.c b/kike.c index 3a80b5d..783c6e2 100644 --- a/kike.c +++ b/kike.c @@ -1130,6 +1130,8 @@ irc_try_finish_registration (struct client *c) // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +// IRCv3 capability negotiation. See http://ircv3.org for details. + struct irc_cap_args { const char *subcommand; ///< The subcommand being processed @@ -1138,6 +1140,19 @@ struct irc_cap_args const char *target; ///< Target parameter for replies }; +static struct +{ + unsigned flag; ///< Flag + const char *name; ///< Name of the capability +} +irc_cap_table[] = +{ + { IRC_CAP_MULTI_PREFIX, "multi-prefix" }, + { IRC_CAP_INVITE_NOTIFY, "invite-notify" }, + { IRC_CAP_ECHO_MESSAGE, "echo-message" }, + { IRC_CAP_USERHOST_IN_NAMES, "userhost-in-names" }, +}; + static void irc_handle_cap_ls (struct client *c, struct irc_cap_args *a) { @@ -1157,14 +1172,9 @@ irc_handle_cap_list (struct client *c, struct irc_cap_args *a) struct str_vector caps; str_vector_init (&caps); - if (c->caps_enabled & IRC_CAP_MULTI_PREFIX) - str_vector_add (&caps, "multi-prefix"); - if (c->caps_enabled & IRC_CAP_INVITE_NOTIFY) - str_vector_add (&caps, "invite-notify"); - if (c->caps_enabled & IRC_CAP_ECHO_MESSAGE) - str_vector_add (&caps, "echo-message"); - if (c->caps_enabled & IRC_CAP_USERHOST_IN_NAMES) - str_vector_add (&caps, "userhost-in-names"); + for (size_t i = 0; i < N_ELEMENTS (irc_cap_table); i++) + if (c->caps_enabled & irc_cap_table[i].flag) + str_vector_add (&caps, irc_cap_table[i].name); char *caps_str = join_str_vector (&caps, ' '); str_vector_free (&caps); @@ -1175,14 +1185,9 @@ irc_handle_cap_list (struct client *c, struct irc_cap_args *a) static unsigned irc_decode_capability (const char *name) { - if (!strcmp (name, "multi-prefix")) - return IRC_CAP_MULTI_PREFIX; - if (!strcmp (name, "invite-notify")) - return IRC_CAP_INVITE_NOTIFY; - if (!strcmp (name, "echo-message")) - return IRC_CAP_ECHO_MESSAGE; - if (!strcmp (name, "userhost-in-names")) - return IRC_CAP_USERHOST_IN_NAMES; + for (size_t i = 0; i < N_ELEMENTS (irc_cap_table); i++) + if (!strcmp (irc_cap_table[i].name, name)) + return irc_cap_table[i].flag; return 0; }