2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
16 * Handler for IRC capability ("CAP") commands
25 #include "client-cap.h"
26 #include "irc-macros.h"
27 #include "irc-write.h"
38 * Set CAP negotiation status and mark client as "supports capabilities".
40 * @param Client The client to handle.
43 Set_CAP_Negotiation(CLIENT *Client)
45 assert(Client != NULL);
47 if (Client_Type(Client) != CLIENT_USER)
48 Client_CapAdd(Client, CLIENT_CAP_PENDING);
49 Client_CapAdd(Client, CLIENT_CAP_SUPPORTED);
53 * Parse capability string and return numeric flag value.
55 * @param Args The string containing space-separated capability names.
56 * @return Changed capability flags or 0 on error.
59 Parse_CAP(int Capabilities, char *Args)
61 static char tmp[COMMAND_LEN];
66 strlcpy(tmp, Args, sizeof(tmp));
68 ptr = strtok(tmp, " ");
71 /* drop capabilities */
73 if (strcmp(ptr, "multi-prefix") == 0)
74 Capabilities &= ~CLIENT_CAP_MULTI_PREFIX;
78 /* request capabilities */
79 if (strcmp(ptr, "multi-prefix") == 0)
80 Capabilities |= CLIENT_CAP_MULTI_PREFIX;
84 ptr = strtok(NULL, " ");
91 * Return textual representation of capability flags.
93 * Please note: this function returns a pointer to a global buffer and
94 * therefore isn't thread safe!
96 * @param Capabilities Capability flags (bitmask).
97 * @return Pointer to textual representation.
100 Get_CAP_String(int Capabilities)
102 static char txt[COMMAND_LEN];
106 if (Capabilities & CLIENT_CAP_MULTI_PREFIX)
107 strlcat(txt, "multi-prefix ", sizeof(txt));
113 * Handler for the IRCv3 sub-command "CAP LS".
115 * @param Client The client from which this command has been received.
116 * @param Arg Command argument or NULL.
117 * @return CONNECTED or DISCONNECTED.
120 Handle_CAP_LS(CLIENT *Client, UNUSED char *Arg)
122 assert(Client != NULL);
124 Set_CAP_Negotiation(Client);
126 return IRC_WriteStrClient(Client,
127 "CAP %s LS :multi-prefix",
132 * Handler for the IRCv3 sub-command "CAP LIST".
134 * @param Client The client from which this command has been received.
135 * @param Arg Command argument or NULL.
136 * @return CONNECTED or DISCONNECTED.
139 Handle_CAP_LIST(CLIENT *Client, UNUSED char *Arg)
141 assert(Client != NULL);
143 return IRC_WriteStrClient(Client, "CAP %s LIST :%s", Client_ID(Client),
144 Get_CAP_String(Client_Cap(Client)));
148 * Handler for the IRCv3 sub-command "CAP REQ".
150 * @param Client The client from which this command has been received.
151 * @param Arg Command argument.
152 * @return CONNECTED or DISCONNECTED.
155 Handle_CAP_REQ(CLIENT *Client, char *Arg)
159 assert(Client != NULL);
162 Set_CAP_Negotiation(Client);
164 new_cap = Parse_CAP(Client_Cap(Client), Arg);
167 return IRC_WriteStrClient(Client, "CAP %s NAK :%s",
168 Client_ID(Client), Arg);
170 Client_CapSet(Client, new_cap);
171 return IRC_WriteStrClient(Client, "CAP %s ACK :%s",
172 Client_ID(Client), Arg);
176 * Handler for the IRCv3 sub-command "CAP ACK".
178 * @param Client The client from which this command has been received.
179 * @param Arg Command argument.
180 * @return CONNECTED or DISCONNECTED.
183 Handle_CAP_ACK(UNUSED CLIENT *Client, UNUSED char *Arg)
185 assert(Client != NULL);
192 * Handler for the IRCv3 sub-command "CAP CLEAR".
194 * @param Client The client from which this command has been received.
195 * @return CONNECTED or DISCONNECTED.
198 Handle_CAP_CLEAR(CLIENT *Client)
202 assert(Client != NULL);
204 cap_old = Client_Cap(Client);
205 if (cap_old & CLIENT_CAP_MULTI_PREFIX)
206 Client_CapDel(Client, CLIENT_CAP_MULTI_PREFIX);
208 return IRC_WriteStrClient(Client, "CAP %s ACK :%s", Client_ID(Client),
209 Get_CAP_String(cap_old));
213 * Handler for the IRCv3 sub-command "CAP END".
215 * @param Client The client from which this command has been received.
216 * @return CONNECTED or DISCONNECTED.
219 Handle_CAP_END(CLIENT *Client)
221 assert(Client != NULL);
223 if (Client_Type(Client) != CLIENT_USER) {
224 /* User is still logging in ... */
225 Client_CapDel(Client, CLIENT_CAP_PENDING);
227 if (Client_Type(Client) == CLIENT_WAITCAPEND) {
228 /* Only "CAP END" was missing: log in! */
229 return Login_User(Client);
236 /* Global functions */
239 * Handler for the IRCv3 command "CAP".
241 * @param Client The client from which this command has been received.
242 * @param Req Request structure with prefix and all parameters.
243 * @return CONNECTED or DISCONNECTED.
246 IRC_CAP(CLIENT *Client, REQUEST *Req)
248 assert(Client != NULL);
251 LogDebug("Got \"%s %s\" command from \"%s\" ...",
252 Req->command, Req->argv[0], Client_ID(Client));
254 if (Req->argc == 1) {
255 if (strcasecmp(Req->argv[0], "CLEAR") == 0)
256 return Handle_CAP_CLEAR(Client);
257 if (strcasecmp(Req->argv[0], "END") == 0)
258 return Handle_CAP_END(Client);
260 if (Req->argc >= 1 && Req->argc <= 2) {
261 if (strcasecmp(Req->argv[0], "LS") == 0)
262 return Handle_CAP_LS(Client, Req->argv[1]);
263 if (strcasecmp(Req->argv[0], "LIST") == 0)
264 return Handle_CAP_LIST(Client, Req->argv[1]);
266 if (Req->argc == 2) {
267 if (strcasecmp(Req->argv[0], "REQ") == 0)
268 return Handle_CAP_REQ(Client, Req->argv[1]);
269 if (strcasecmp(Req->argv[0], "ACK") == 0)
270 return Handle_CAP_ACK(Client, Req->argv[1]);
273 return IRC_WriteErrClient(Client, ERR_INVALIDCAP_MSG,
274 Client_ID(Client), Req->argv[0]);