source: irc_cap.c @ d63f37c

Last change on this file since d63f37c was d63f37c, checked in by dequis <dx@…>, at 2015-11-08T08:16:15Z

IRCv3 extended-join capability

Not very useful for the account features (and i won't implement
account-notify), but it has a real name field, and it's *really* easy to
implement.

  • Property mode set to 100644
File size: 4.4 KB
RevLine 
[355e2ad]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[] = {
[58b63de6]40        {"sasl", CAP_SASL},
[687ec88]41        {"multi-prefix", CAP_MULTI_PREFIX},
[d63f37c]42        {"extended-join", CAP_EXTENDED_JOIN},
[355e2ad]43        {NULL},
44};
45
46static irc_cap_flag_t cap_flag_from_string(char *cap_name)
47{
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        irc_cap_flag_t 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                irc_cap_flag_t 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
112/* version can be "302" or NULL, but we don't need cap-3.2 for anything yet */
113static void irc_cmd_cap_ls(irc_t *irc, char *version)
114{
115        int i;
116        GString *str = g_string_sized_new(256);
117
118        for (i = 0; supported_caps[i].name; i++) {
119                if (i != 0) {
120                        g_string_append_c(str, ' ');
121                }
122                g_string_append(str, supported_caps[i].name);
123        }
124
125        irc_send_cap(irc, "LS", str->str);
126
127        g_string_free(str, TRUE);
128}
129
130/* this one looks suspiciously similar to cap ls,
131 * but cap-3.2 will make them very different */
132static void irc_cmd_cap_list(irc_t *irc)
133{
134        int i;
135        gboolean first = TRUE;
136        GString *str = g_string_sized_new(256);
137
138        for (i = 0; supported_caps[i].name; i++) {
139                if (irc->caps & supported_caps[i].flag) {
140                        if (!first) {
141                                g_string_append_c(str, ' ');
142                        }
143                        first = FALSE;
144
145                        g_string_append(str, supported_caps[i].name);
146                }
147        }
148
149        irc_send_cap(irc, "LIST", str->str);
150
151        g_string_free(str, TRUE);
152}
153
154void irc_cmd_cap(irc_t *irc, char **cmd)
155{
156        if (!(irc->status & USTATUS_LOGGED_IN)) {
157                /* Put registration on hold until CAP END */
158                irc->status |= USTATUS_CAP_PENDING;
159        }
160
161        if (g_strcasecmp(cmd[1], "LS") == 0) {
162                irc_cmd_cap_ls(irc, cmd[2]);
163
164        } else if (g_strcasecmp(cmd[1], "LIST") == 0) {
165                irc_cmd_cap_list(irc);
166
167        } else if (g_strcasecmp(cmd[1], "REQ") == 0) {
168                gboolean ack = irc_cmd_cap_req(irc, cmd[2]);
169
170                irc_send_cap(irc, ack ? "ACK" : "NAK", cmd[2] ? : "");
171
172        } else if (g_strcasecmp(cmd[1], "END") == 0) {
173                irc->status &= ~USTATUS_CAP_PENDING;
[58b63de6]174
175                if (irc->status & USTATUS_SASL_PLAIN_PENDING) {
176                        irc_send_num(irc, 906, ":SASL authentication aborted");
177                        irc->status &= ~USTATUS_SASL_PLAIN_PENDING;
178                }
179
[355e2ad]180                irc_check_login(irc);
181
182        } else {
183                irc_send_num(irc, 410, "%s :Invalid CAP command", cmd[1]);
184        }
185
186}
187
Note: See TracBrowser for help on using the repository browser.