diff options
author | dequis <dx@dxzone.com.ar> | 2015-08-18 02:53:17 -0300 |
---|---|---|
committer | dequis <dx@dxzone.com.ar> | 2015-09-30 22:01:48 -0300 |
commit | 355e2ade69fe6555320d8fe5b4b9d3a56c3e6edb (patch) | |
tree | e78724898e6312104a9acf6bd93195fb1db3e0eb /irc_commands.c | |
parent | d797fb45e38b005b25fa043f4d3c427ec7f7fe53 (diff) |
Move CAP commands to irc_cap.c; use enum for flags
Diffstat (limited to 'irc_commands.c')
-rw-r--r-- | irc_commands.c | 149 |
1 files changed, 0 insertions, 149 deletions
diff --git a/irc_commands.c b/irc_commands.c index ebcc300b..14a3fd9d 100644 --- a/irc_commands.c +++ b/irc_commands.c @@ -28,155 +28,6 @@ #include "help.h" #include "ipc.h" -typedef guint32 cap_flag; /* 32 bits ought to be enough for anybody */ - -typedef struct _cap_info { - char *name; - cap_flag flag; -} cap_info_t; - -#define CAP_FOO (1 << 0) -#define CAP_BAR (1 << 1) - -static const cap_info_t supported_caps[] = { - {"foo", CAP_FOO}, - {"bar", CAP_BAR}, - {NULL}, -}; - -static cap_flag cap_flag_from_string(char *cap_name) { - int i; - - if (!cap_name && !cap_name[0]) { - return 0; - } - - if (cap_name[0] == '-') { - cap_name++; - } - - for (i = 0; supported_caps[i].name; i++) { - if (strcmp(supported_caps[i].name, cap_name) == 0) { - return supported_caps[i].flag; - } - } - return 0; -} - -static gboolean irc_cmd_cap_req(irc_t *irc, char *caps) -{ - int i; - char *lower = NULL; - char **split = NULL; - cap_flag new_caps = irc->caps; - - if (!caps || !caps[0]) { - return FALSE; - } - - lower = g_ascii_strdown(caps, -1); - split = g_strsplit(lower, " ", -1); - g_free(lower); - - for (i = 0; split[i]; i++) { - gboolean remove; - cap_flag flag; - - if (!split[i][0]) { - continue; /* skip empty items (consecutive spaces) */ - } - - remove = (split[i][0] == '-'); - flag = cap_flag_from_string(split[i]); - - if (!flag || (remove && !(irc->caps & flag))) { - /* unsupported cap, or removing something that isn't there */ - g_strfreev(split); - return FALSE; - } - - if (remove) { - new_caps &= ~flag; - } else { - new_caps |= flag; - } - } - - /* if we got here, set the new caps and ack */ - irc->caps = new_caps; - - g_strfreev(split); - return TRUE; -} - -/* version can be "302" or NULL, but we don't need cap-3.2 for anything yet */ -static void irc_cmd_cap_ls(irc_t *irc, char *version) { - int i; - GString *str = g_string_sized_new(256); - - for (i = 0; supported_caps[i].name; i++) { - if (i != 0) { - g_string_append_c(str, ' '); - } - g_string_append(str, supported_caps[i].name); - } - - irc_send_cap(irc, "LS", str->str); - - g_string_free(str, TRUE); -} - -/* this one looks suspiciously similar to cap ls, - * but cap-3.2 will make them very different */ -static void irc_cmd_cap_list(irc_t *irc) { - int i; - gboolean first = TRUE; - GString *str = g_string_sized_new(256); - - for (i = 0; supported_caps[i].name; i++) { - if (irc->caps & supported_caps[i].flag) { - if (!first) { - g_string_append_c(str, ' '); - } - first = FALSE; - - g_string_append(str, supported_caps[i].name); - } - } - - irc_send_cap(irc, "LIST", str->str); - - g_string_free(str, TRUE); -} - -static void irc_cmd_cap(irc_t *irc, char **cmd) -{ - if (!(irc->status & USTATUS_LOGGED_IN)) { - /* Put registration on hold until CAP END */ - irc->status |= USTATUS_CAP_PENDING; - } - - if (g_strcasecmp(cmd[1], "LS") == 0) { - irc_cmd_cap_ls(irc, cmd[2]); - - } else if (g_strcasecmp(cmd[1], "LIST") == 0) { - irc_cmd_cap_list(irc); - - } else if (g_strcasecmp(cmd[1], "REQ") == 0) { - gboolean ack = irc_cmd_cap_req(irc, cmd[2]); - - irc_send_cap(irc, ack ? "ACK" : "NAK", cmd[2] ? : ""); - - } else if (g_strcasecmp(cmd[1], "END") == 0) { - irc->status &= ~USTATUS_CAP_PENDING; - irc_check_login(irc); - - } else { - irc_send_num(irc, 410, "%s :Invalid CAP command", cmd[1]); - } - -} - static void irc_cmd_pass(irc_t *irc, char **cmd) { if (irc->status & USTATUS_LOGGED_IN) { |