2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2014 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-write.h"
37 * Set CAP negotiation status and mark client as "supports capabilities".
39 * @param Client The client to handle.
42 Set_CAP_Negotiation(CLIENT *Client)
44 assert(Client != NULL);
46 if (Client_Type(Client) != CLIENT_USER)
47 Client_CapAdd(Client, CLIENT_CAP_PENDING);
48 Client_CapAdd(Client, CLIENT_CAP_SUPPORTED);
52 * Parse capability string and return numeric flag value.
54 * @param Args The string containing space-separated capability names.
55 * @return Changed capability flags or 0 on error.
58 Parse_CAP(int Capabilities, char *Args)
60 static char tmp[COMMAND_LEN];
65 strlcpy(tmp, Args, sizeof(tmp));
67 ptr = strtok(tmp, " ");
70 /* drop capabilities */
72 if (strcmp(ptr, "multi-prefix") == 0)
73 Capabilities &= ~CLIENT_CAP_MULTI_PREFIX;
77 /* request capabilities */
78 if (strcmp(ptr, "multi-prefix") == 0)
79 Capabilities |= CLIENT_CAP_MULTI_PREFIX;
83 ptr = strtok(NULL, " ");
90 * Return textual representation of capability flags.
92 * Please note: this function returns a pointer to a global buffer and
93 * therefore isn't thread safe!
95 * @param Capabilities Capability flags (bitmask).
96 * @return Pointer to textual representation.
99 Get_CAP_String(int Capabilities)
101 static char txt[COMMAND_LEN];
105 if (Capabilities & CLIENT_CAP_MULTI_PREFIX)
106 strlcat(txt, "multi-prefix ", sizeof(txt));
112 * Handler for the IRCv3 sub-command "CAP LS".
114 * @param Client The client from which this command has been received.
115 * @param Arg Command argument or NULL.
116 * @return CONNECTED or DISCONNECTED.
119 Handle_CAP_LS(CLIENT *Client, UNUSED char *Arg)
121 assert(Client != NULL);
123 Set_CAP_Negotiation(Client);
125 return IRC_WriteStrClient(Client,
126 "CAP %s LS :multi-prefix",
131 * Handler for the IRCv3 sub-command "CAP LIST".
133 * @param Client The client from which this command has been received.
134 * @param Arg Command argument or NULL.
135 * @return CONNECTED or DISCONNECTED.
138 Handle_CAP_LIST(CLIENT *Client, UNUSED char *Arg)
140 assert(Client != NULL);
142 return IRC_WriteStrClient(Client, "CAP %s LIST :%s", Client_ID(Client),
143 Get_CAP_String(Client_Cap(Client)));
147 * Handler for the IRCv3 sub-command "CAP REQ".
149 * @param Client The client from which this command has been received.
150 * @param Arg Command argument.
151 * @return CONNECTED or DISCONNECTED.
154 Handle_CAP_REQ(CLIENT *Client, char *Arg)
158 assert(Client != NULL);
161 Set_CAP_Negotiation(Client);
163 new_cap = Parse_CAP(Client_Cap(Client), Arg);
166 return IRC_WriteStrClient(Client, "CAP %s NAK :%s",
167 Client_ID(Client), Arg);
169 Client_CapSet(Client, new_cap);
170 return IRC_WriteStrClient(Client, "CAP %s ACK :%s",
171 Client_ID(Client), Arg);
175 * Handler for the IRCv3 sub-command "CAP ACK".
177 * @param Client The client from which this command has been received.
178 * @param Arg Command argument.
179 * @return CONNECTED or DISCONNECTED.
182 Handle_CAP_ACK(UNUSED CLIENT *Client, UNUSED char *Arg)
184 assert(Client != NULL);
191 * Handler for the IRCv3 sub-command "CAP CLEAR".
193 * @param Client The client from which this command has been received.
194 * @return CONNECTED or DISCONNECTED.
197 Handle_CAP_CLEAR(CLIENT *Client)
201 assert(Client != NULL);
203 cap_old = Client_Cap(Client);
204 if (cap_old & CLIENT_CAP_MULTI_PREFIX)
205 Client_CapDel(Client, CLIENT_CAP_MULTI_PREFIX);
207 return IRC_WriteStrClient(Client, "CAP %s ACK :%s", Client_ID(Client),
208 Get_CAP_String(cap_old));
212 * Handler for the IRCv3 sub-command "CAP END".
214 * @param Client The client from which this command has been received.
215 * @return CONNECTED or DISCONNECTED.
218 Handle_CAP_END(CLIENT *Client)
220 assert(Client != NULL);
222 if (Client_Type(Client) != CLIENT_USER) {
223 /* User is still logging in ... */
224 Client_CapDel(Client, CLIENT_CAP_PENDING);
226 if (Client_Type(Client) == CLIENT_WAITCAPEND) {
227 /* Only "CAP END" was missing: log in! */
228 return Login_User(Client);
235 /* Global functions */
238 * Handler for the IRCv3 command "CAP".
240 * @param Client The client from which this command has been received.
241 * @param Req Request structure with prefix and all parameters.
242 * @return CONNECTED or DISCONNECTED.
245 IRC_CAP(CLIENT *Client, REQUEST *Req)
247 assert(Client != NULL);
250 LogDebug("Got \"%s %s\" command from \"%s\" ...",
251 Req->command, Req->argv[0], Client_ID(Client));
253 if (Req->argc == 1) {
254 if (strcasecmp(Req->argv[0], "CLEAR") == 0)
255 return Handle_CAP_CLEAR(Client);
256 if (strcasecmp(Req->argv[0], "END") == 0)
257 return Handle_CAP_END(Client);
259 if (Req->argc >= 1 && Req->argc <= 2) {
260 if (strcasecmp(Req->argv[0], "LS") == 0)
261 return Handle_CAP_LS(Client, Req->argv[1]);
262 if (strcasecmp(Req->argv[0], "LIST") == 0)
263 return Handle_CAP_LIST(Client, Req->argv[1]);
265 if (Req->argc == 2) {
266 if (strcasecmp(Req->argv[0], "REQ") == 0)
267 return Handle_CAP_REQ(Client, Req->argv[1]);
268 if (strcasecmp(Req->argv[0], "ACK") == 0)
269 return Handle_CAP_ACK(Client, Req->argv[1]);
272 return IRC_WriteErrClient(Client, ERR_INVALIDCAP_MSG,
273 Client_ID(Client), Req->argv[0]);