source: irc_cap.c @ ad9ac5d

Last change on this file since ad9ac5d was 8fdeaa5, checked in by dequis <dx@…>, at 2015-11-20T16:13:35Z

IRCv3 cap-3.2/sasl-3.2 capabilities (just send sasl mechanism list)

Nothing else needed, i think. No need for multiline replies, cap-notify,
sasl reauthentication just works, and the authentication layer is always
available.

  • Property mode set to 100644
File size: 4.5 KB
Line 
1/********************************************************************\
2  * BitlBee -- An IRC to other IM-networks gateway                     *
3  *                                                                    *
4  * Copyright 2002-2013 Wilmer van der Gaast and others                *
5  \********************************************************************/
6
7/* IRCv3 CAP command
8 *
9 * Specs:
10 *  - v3.1: http://ircv3.net/specs/core/capability-negotiation-3.1.html
11 *  - v3.2: http://ircv3.net/specs/core/capability-negotiation-3.2.html
12 *
13 * */
14
15/*
16  This program is free software; you can redistribute it and/or modify
17  it under the terms of the GNU General Public License as published by
18  the Free Software Foundation; either version 2 of the License, or
19  (at your option) any later version.
20
21  This program is distributed in the hope that it will be useful,
22  but WITHOUT ANY WARRANTY; without even the implied warranty of
23  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  GNU General Public License for more details.
25
26  You should have received a copy of the GNU General Public License with
27  the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL;
28  if not, write to the Free Software Foundation, Inc., 51 Franklin St.,
29  Fifth Floor, Boston, MA  02110-1301  USA
30*/
31
32#include "bitlbee.h"
33
34typedef struct {
35        char *name;
36        irc_cap_flag_t flag;
37} cap_info_t;
38
39static const cap_info_t supported_caps[] = {
40        {"sasl", CAP_SASL},
41        {"multi-prefix", CAP_MULTI_PREFIX},
42        {"extended-join", CAP_EXTENDED_JOIN},
43        {"away-notify", CAP_AWAY_NOTIFY},
44        {NULL},
45};
46
47static irc_cap_flag_t cap_flag_from_string(char *cap_name)
48{
49        int i;
50
51        if (!cap_name || !cap_name[0]) {
52                return 0;
53        }
54
55        if (cap_name[0] == '-') {
56                cap_name++;
57        }
58
59        for (i = 0; supported_caps[i].name; i++) {
60                if (strcmp(supported_caps[i].name, cap_name) == 0) {
61                        return supported_caps[i].flag;
62                }
63        }
64        return 0;
65}
66
67static gboolean irc_cmd_cap_req(irc_t *irc, char *caps)
68{
69        int i;
70        char *lower = NULL;
71        char **split = NULL;
72        irc_cap_flag_t new_caps = irc->caps;
73
74        if (!caps || !caps[0]) {
75                return FALSE;
76        }
77
78        lower = g_ascii_strdown(caps, -1);
79        split = g_strsplit(lower, " ", -1);
80        g_free(lower);
81
82        for (i = 0; split[i]; i++) {
83                gboolean remove;
84                irc_cap_flag_t flag;
85
86                if (!split[i][0]) {
87                        continue;   /* skip empty items (consecutive spaces) */
88                }
89
90                remove = (split[i][0] == '-');
91                flag = cap_flag_from_string(split[i]);
92               
93                if (!flag || (remove && !(irc->caps & flag))) {
94                        /* unsupported cap, or removing something that isn't there */
95                        g_strfreev(split);
96                        return FALSE;
97                }
98
99                if (remove) {
100                        new_caps &= ~flag;
101                } else {
102                        new_caps |= flag;
103                }
104        }
105
106        /* if we got here, set the new caps and ack */
107        irc->caps = new_caps;
108
109        g_strfreev(split);
110        return TRUE;
111}
112
113/* version can be 0, 302, 303, or garbage from user input. thanks user input */
114static void irc_cmd_cap_ls(irc_t *irc, long version)
115{
116        int i;
117        GString *str = g_string_sized_new(256);
118
119        for (i = 0; supported_caps[i].name; i++) {
120                if (i != 0) {
121                        g_string_append_c(str, ' ');
122                }
123                g_string_append(str, supported_caps[i].name);
124
125                if (version >= 302 && supported_caps[i].flag == CAP_SASL) {
126                        g_string_append(str, "=PLAIN");
127                }
128        }
129
130        irc_send_cap(irc, "LS", str->str);
131
132        g_string_free(str, TRUE);
133}
134
135/* this one looks suspiciously similar to cap ls,
136 * but cap-3.2 will make them very different */
137static void irc_cmd_cap_list(irc_t *irc)
138{
139        int i;
140        gboolean first = TRUE;
141        GString *str = g_string_sized_new(256);
142
143        for (i = 0; supported_caps[i].name; i++) {
144                if (irc->caps & supported_caps[i].flag) {
145                        if (!first) {
146                                g_string_append_c(str, ' ');
147                        }
148                        first = FALSE;
149
150                        g_string_append(str, supported_caps[i].name);
151                }
152        }
153
154        irc_send_cap(irc, "LIST", str->str);
155
156        g_string_free(str, TRUE);
157}
158
159void irc_cmd_cap(irc_t *irc, char **cmd)
160{
161        if (!(irc->status & USTATUS_LOGGED_IN)) {
162                /* Put registration on hold until CAP END */
163                irc->status |= USTATUS_CAP_PENDING;
164        }
165
166        if (g_strcasecmp(cmd[1], "LS") == 0) {
167                irc_cmd_cap_ls(irc, cmd[2] ? strtol(cmd[2], NULL, 10) : 0);
168
169        } else if (g_strcasecmp(cmd[1], "LIST") == 0) {
170                irc_cmd_cap_list(irc);
171
172        } else if (g_strcasecmp(cmd[1], "REQ") == 0) {
173                gboolean ack = irc_cmd_cap_req(irc, cmd[2]);
174
175                irc_send_cap(irc, ack ? "ACK" : "NAK", cmd[2] ? : "");
176
177        } else if (g_strcasecmp(cmd[1], "END") == 0) {
178                irc->status &= ~USTATUS_CAP_PENDING;
179
180                if (irc->status & USTATUS_SASL_PLAIN_PENDING) {
181                        irc_send_num(irc, 906, ":SASL authentication aborted");
182                        irc->status &= ~USTATUS_SASL_PLAIN_PENDING;
183                }
184
185                irc_check_login(irc);
186
187        } else {
188                irc_send_num(irc, 410, "%s :Invalid CAP command", cmd[1]);
189        }
190
191}
192
Note: See TracBrowser for help on using the repository browser.