Changeset dc96e6e for irc_commands.c


Ignore:
Timestamp:
2015-09-11T02:31:10Z (9 years ago)
Author:
dequis <dx@…>
Branches:
master
Children:
b57fed0
Parents:
0ef1c92
git-author:
dequis <dx@…> (28-07-15 04:48:49)
git-committer:
dequis <dx@…> (11-09-15 02:31:10)
Message:

CAP REQ

File:
1 edited

Legend:

Unmodified
Added
Removed
  • irc_commands.c

    r0ef1c92 rdc96e6e  
    2929#include "ipc.h"
    3030
     31typedef guint32 cap_flag;     /* 32 bits ought to be enough for anybody */
     32
     33typedef struct _cap_info {
     34        char *name;
     35        cap_flag flag;
     36} cap_info_t;
     37
     38#define CAP_FOO (1 << 0)
     39#define CAP_BAR (1 << 1)
     40
     41static const cap_info_t supported_caps[] = {
     42        {"foo", CAP_FOO},
     43        {"bar", CAP_BAR},
     44        {NULL},
     45};
     46
     47static cap_flag cap_flag_from_string(char *cap_name) {
     48        int i;
     49
     50        if (!cap_name && !cap_name[0]) {
     51                return 0;
     52        }
     53
     54        if (cap_name[0] == '-') {
     55                cap_name++;
     56        }
     57
     58        for (i = 0; supported_caps[i].name; i++) {
     59                if (strcmp(supported_caps[i].name, cap_name) == 0) {
     60                        return supported_caps[i].flag;
     61                }
     62        }
     63        return 0;
     64}
     65
     66static gboolean irc_cmd_cap_req(irc_t *irc, char *caps)
     67{
     68        int i;
     69        char *lower = NULL;
     70        char **split = NULL;
     71        cap_flag new_caps = irc->caps;
     72
     73        if (!caps || !caps[0]) {
     74                return FALSE;
     75        }
     76
     77        lower = g_ascii_strdown(caps, -1);
     78        split = g_strsplit(lower, " ", -1);
     79        g_free(lower);
     80
     81        for (i = 0; split[i]; i++) {
     82                gboolean remove;
     83                cap_flag flag;
     84
     85                if (!split[i][0]) {
     86                        continue;   /* skip empty items (consecutive spaces) */
     87                }
     88
     89                remove = (split[i][0] == '-');
     90                flag = cap_flag_from_string(split[i]);
     91               
     92                if (!flag || (remove && !(irc->caps & flag))) {
     93                        /* unsupported cap, or removing something that isn't there */
     94                        g_strfreev(split);
     95                        return FALSE;
     96                }
     97
     98                if (remove) {
     99                        new_caps &= ~flag;
     100                } else {
     101                        new_caps |= flag;
     102                }
     103        }
     104
     105        /* if we got here, set the new caps and ack */
     106        irc->caps = new_caps;
     107
     108        g_strfreev(split);
     109        return TRUE;
     110}
     111
    31112static void irc_cmd_cap(irc_t *irc, char **cmd)
    32113{
     
    38119        if (g_strcasecmp(cmd[1], "LS") == 0) {
    39120                /* gboolean irc302 = (g_strcmp0(cmd[2], "302") == 0); */
    40                 irc_send_cap(irc, "LS", "");
     121                //char *ls = g_strjoinv(" ", (char **) supported_caps);
     122
     123                irc_send_cap(irc, "LS", "foo bar");
     124
     125                //g_free(ls);
    41126
    42127        } else if (g_strcasecmp(cmd[1], "LIST") == 0) {
     
    44129
    45130        } else if (g_strcasecmp(cmd[1], "REQ") == 0) {
    46                 irc_send_cap(irc, "NAK", cmd[2] ? : "");
     131                gboolean ack = irc_cmd_cap_req(irc, cmd[2]);
     132
     133                irc_send_cap(irc, ack ? "ACK" : "NAK", cmd[2] ? : "");
    47134
    48135        } else if (g_strcasecmp(cmd[1], "END") == 0) {
Note: See TracChangeset for help on using the changeset viewer.