2 2ee05c9a 2002-03-03 alex * ngIRCd -- The Next Generation IRC Daemon
3 ec0b405d 2008-09-23 alex * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
5 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
11 490f28ff 2002-12-12 alex * IRC channel commands
15 ca33cbda 2002-03-12 alex #include "portab.h"
17 ca33cbda 2002-03-12 alex #include "imp.h"
18 2ee05c9a 2002-03-03 alex #include <assert.h>
19 43d9a624 2003-01-08 alex #include <stdlib.h>
20 43d9a624 2003-01-08 alex #include <stdio.h>
21 2ee05c9a 2002-03-03 alex #include <string.h>
23 ca33cbda 2002-03-12 alex #include "defines.h"
24 c2f60abe 2002-05-27 alex #include "conn.h"
25 c2f60abe 2002-05-27 alex #include "client.h"
26 c2f60abe 2002-05-27 alex #include "channel.h"
27 956bbe2c 2008-05-24 alex #include "conn-func.h"
28 c2f60abe 2002-05-27 alex #include "lists.h"
29 2ee05c9a 2002-03-03 alex #include "log.h"
30 39b9f65d 2002-06-26 alex #include "match.h"
31 2ee05c9a 2002-03-03 alex #include "messages.h"
32 c2f60abe 2002-05-27 alex #include "parse.h"
33 0c471b84 2002-11-30 alex #include "irc-info.h"
34 c2f60abe 2002-05-27 alex #include "irc-write.h"
35 5b8b3b83 2002-12-13 alex #include "resolve.h"
36 5b8b3b83 2002-12-13 alex #include "conf.h"
38 ca33cbda 2002-03-12 alex #include "exp.h"
39 2ee05c9a 2002-03-03 alex #include "irc-channel.h"
43 6bd35bf0 2008-02-26 fw * RFC 2812, (3.2.1 Join message Command):
44 6bd35bf0 2008-02-26 fw * Note that this message
45 6bd35bf0 2008-02-26 fw * accepts a special argument ("0"), which is a special request to leave all
46 6bd35bf0 2008-02-26 fw * channels the user is currently a member of. The server will process this
47 6bd35bf0 2008-02-26 fw * message as if the user had sent a PART command (See Section 3.2.2) for
48 6bd35bf0 2008-02-26 fw * each channel he is a member of.
51 6bd35bf0 2008-02-26 fw part_from_all_channels(CLIENT* client, CLIENT *target)
53 c6343037 2008-02-26 fw CL2CHAN *cl2chan;
54 6bd35bf0 2008-02-26 fw CHANNEL *chan;
56 c6343037 2008-02-26 fw while ((cl2chan = Channel_FirstChannelOf(target))) {
57 6bd35bf0 2008-02-26 fw chan = Channel_GetChannel(cl2chan);
58 6bd35bf0 2008-02-26 fw assert( chan != NULL );
59 6bd35bf0 2008-02-26 fw Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
61 6bd35bf0 2008-02-26 fw return CONNECTED;
66 634ef8c1 2008-02-26 fw join_allowed(CLIENT *Client, CLIENT *target, CHANNEL *chan,
67 634ef8c1 2008-02-26 fw const char *channame, const char *key)
69 634ef8c1 2008-02-26 fw bool is_invited, is_banned;
70 634ef8c1 2008-02-26 fw const char *channel_modes;
72 83bfdddf 2008-05-05 alex /* Allow IRC operators to overwrite channel limits */
73 83bfdddf 2008-05-05 alex if (strchr(Client_Modes(Client), 'o'))
74 83bfdddf 2008-05-05 alex return true;
76 634ef8c1 2008-02-26 fw is_banned = Lists_Check(Channel_GetListBans(chan), target);
77 634ef8c1 2008-02-26 fw is_invited = Lists_Check(Channel_GetListInvites(chan), target);
79 634ef8c1 2008-02-26 fw if (is_banned && !is_invited) {
80 634ef8c1 2008-02-26 fw IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
84 634ef8c1 2008-02-26 fw channel_modes = Channel_Modes(chan);
85 634ef8c1 2008-02-26 fw if ((strchr(channel_modes, 'i')) && !is_invited) {
86 634ef8c1 2008-02-26 fw /* Channel is "invite-only" (and Client wasn't invited) */
87 634ef8c1 2008-02-26 fw IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG, Client_ID(Client), channame);
91 634ef8c1 2008-02-26 fw /* Is the channel protected by a key? */
92 634ef8c1 2008-02-26 fw if (strchr(channel_modes, 'k') &&
93 634ef8c1 2008-02-26 fw strcmp(Channel_Key(chan), key ? key : ""))
95 634ef8c1 2008-02-26 fw IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG, Client_ID(Client), channame);
98 634ef8c1 2008-02-26 fw /* Are there already too many members? */
99 634ef8c1 2008-02-26 fw if ((strchr(channel_modes, 'l')) && (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
100 634ef8c1 2008-02-26 fw IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG, Client_ID(Client), channame);
101 634ef8c1 2008-02-26 fw return false;
108 634ef8c1 2008-02-26 fw join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
111 634ef8c1 2008-02-26 fw while (*flags) {
112 634ef8c1 2008-02-26 fw Channel_UserModeAdd(chan, target, *flags);
117 634ef8c1 2008-02-26 fw /* If channel persistent and client is ircop: make client chanop */
118 634ef8c1 2008-02-26 fw if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
119 634ef8c1 2008-02-26 fw Channel_UserModeAdd(chan, target, 'o');
124 ec0b405d 2008-09-23 alex cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
126 ec0b405d 2008-09-23 alex CONN_ID conn;
127 ec0b405d 2008-09-23 alex char str[COMMAND_LEN], *ptr = NULL;
129 ec0b405d 2008-09-23 alex strlcpy(str, (char *)Data, sizeof(str));
130 ec0b405d 2008-09-23 alex conn = Client_Conn(To);
132 ec0b405d 2008-09-23 alex if (Conn_Options(conn) & CONN_RFC1459) {
133 ec0b405d 2008-09-23 alex /* RFC 1459 compatibility mode, appended modes are NOT
134 ec0b405d 2008-09-23 alex * supported, so strip them off! */
135 ec0b405d 2008-09-23 alex ptr = strchr(str, 0x7);
137 ec0b405d 2008-09-23 alex *ptr++ = '\0';
140 ec0b405d 2008-09-23 alex IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
141 ec0b405d 2008-09-23 alex if (ptr && *ptr)
142 ec0b405d 2008-09-23 alex IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
143 ec0b405d 2008-09-23 alex Client_ID(Prefix));
144 ec0b405d 2008-09-23 alex } /* cb_join_forward */
147 ec0b405d 2008-09-23 alex static void
148 634ef8c1 2008-02-26 fw join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
149 634ef8c1 2008-02-26 fw const char *channame)
151 ec0b405d 2008-09-23 alex char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
153 634ef8c1 2008-02-26 fw strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
154 634ef8c1 2008-02-26 fw if (modes[1])
155 634ef8c1 2008-02-26 fw modes[0] = 0x7;
157 634ef8c1 2008-02-26 fw modes[0] = '\0';
159 634ef8c1 2008-02-26 fw /* forward to other servers */
160 ec0b405d 2008-09-23 alex snprintf(str, sizeof(str), "%s%s", channame, modes);
161 ec0b405d 2008-09-23 alex IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0', cb_join_forward, str);
163 634ef8c1 2008-02-26 fw /* tell users in this channel about the new client */
164 634ef8c1 2008-02-26 fw IRC_WriteStrChannelPrefix(Client, chan, target, false, "JOIN :%s", channame);
165 634ef8c1 2008-02-26 fw if (modes[1])
166 634ef8c1 2008-02-26 fw IRC_WriteStrChannelPrefix(Client, chan, target, false, "MODE %s +%s %s",
167 634ef8c1 2008-02-26 fw channame, &modes[1], Client_ID(target));
172 634ef8c1 2008-02-26 fw join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
173 634ef8c1 2008-02-26 fw const char *channame)
175 634ef8c1 2008-02-26 fw const char *topic;
177 634ef8c1 2008-02-26 fw if (Client_Type(Client) != CLIENT_USER)
179 634ef8c1 2008-02-26 fw /* acknowledge join */
180 634ef8c1 2008-02-26 fw if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
181 634ef8c1 2008-02-26 fw return false;
183 634ef8c1 2008-02-26 fw /* Send topic to client, if any */
184 634ef8c1 2008-02-26 fw topic = Channel_Topic(chan);
185 634ef8c1 2008-02-26 fw assert(topic != NULL);
186 634ef8c1 2008-02-26 fw if (*topic) {
187 634ef8c1 2008-02-26 fw if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
188 634ef8c1 2008-02-26 fw Client_ID(Client), channame, topic))
189 634ef8c1 2008-02-26 fw return false;
190 634ef8c1 2008-02-26 fw #ifndef STRICT_RFC
191 634ef8c1 2008-02-26 fw if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
192 634ef8c1 2008-02-26 fw Client_ID(Client), channame,
193 634ef8c1 2008-02-26 fw Channel_TopicWho(chan),
194 634ef8c1 2008-02-26 fw Channel_TopicTime(chan)))
195 634ef8c1 2008-02-26 fw return false;
198 634ef8c1 2008-02-26 fw /* send list of channel members to client */
199 634ef8c1 2008-02-26 fw if (!IRC_Send_NAMES(Client, chan))
200 634ef8c1 2008-02-26 fw return false;
201 634ef8c1 2008-02-26 fw return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
206 c2f60abe 2002-05-27 alex IRC_JOIN( CLIENT *Client, REQUEST *Req )
208 258e39e8 2008-06-16 alex char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
209 2ee05c9a 2002-03-03 alex CLIENT *target;
210 2ee05c9a 2002-03-03 alex CHANNEL *chan;
212 2ee05c9a 2002-03-03 alex assert( Client != NULL );
213 2ee05c9a 2002-03-03 alex assert( Req != NULL );
215 69ad0e38 2002-12-16 alex /* Bad number of arguments? */
216 69081851 2007-07-31 alex if (Req->argc < 1 || Req->argc > 2)
217 69081851 2007-07-31 alex return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
218 69081851 2007-07-31 alex Client_ID(Client), Req->command);
220 69ad0e38 2002-12-16 alex /* Who is the sender? */
221 634ef8c1 2008-02-26 fw if (Client_Type(Client) == CLIENT_SERVER)
222 634ef8c1 2008-02-26 fw target = Client_Search(Req->prefix);
224 634ef8c1 2008-02-26 fw target = Client;
227 634ef8c1 2008-02-26 fw return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
229 6bd35bf0 2008-02-26 fw /* Is argument "0"? */
230 6bd35bf0 2008-02-26 fw if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
231 6bd35bf0 2008-02-26 fw return part_from_all_channels(Client, target);
233 69ad0e38 2002-12-16 alex /* Are channel keys given? */
234 b90f71ca 2008-05-26 fw if (Req->argc > 1)
235 b90f71ca 2008-05-26 fw key = strtok_r(Req->argv[1], ",", &lastkey);
237 e7087905 2005-09-02 alex channame = Req->argv[0];
238 b90f71ca 2008-05-26 fw channame = strtok_r(channame, ",", &lastchan);
240 25814389 2008-07-22 alex /* Make sure that "channame" is not the empty string ("JOIN :") */
241 25814389 2008-07-22 alex if (! channame)
242 25814389 2008-07-22 alex return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
243 25814389 2008-07-22 alex Client_ID(Client), Req->command);
245 634ef8c1 2008-02-26 fw while (channame) {
246 634ef8c1 2008-02-26 fw flags = NULL;
248 634ef8c1 2008-02-26 fw /* Did the server include channel-user-modes? */
249 634ef8c1 2008-02-26 fw if (Client_Type(Client) == CLIENT_SERVER) {
250 634ef8c1 2008-02-26 fw flags = strchr(channame, 0x7);
252 e876e210 2002-06-10 alex *flags = '\0';
257 44cdf1b1 2008-02-26 fw chan = Channel_Search(channame);
258 44cdf1b1 2008-02-26 fw if (!chan && Conf_PredefChannelsOnly) {
259 44cdf1b1 2008-02-26 fw /* channel must be created, but server does not allow this */
260 44cdf1b1 2008-02-26 fw IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
264 233210b9 2004-04-09 alex /* Local client? */
265 634ef8c1 2008-02-26 fw if (Client_Type(Client) == CLIENT_USER) {
266 00e75ccd 2002-12-14 alex /* Test if the user has reached his maximum channel count */
267 634ef8c1 2008-02-26 fw if ((Conf_MaxJoins > 0) && (Channel_CountForUser(Client) >= Conf_MaxJoins))
268 634ef8c1 2008-02-26 fw return IRC_WriteStrClient(Client, ERR_TOOMANYCHANNELS_MSG,
269 634ef8c1 2008-02-26 fw Client_ID(Client), channame);
270 530112b1 2008-05-01 alex if (!chan) {
272 530112b1 2008-05-01 alex * New Channel: first user will be channel operator
273 f7c2e822 2008-05-01 alex * unless this is a modeless channel.
275 530112b1 2008-05-01 alex if (*channame != '+')
276 530112b1 2008-05-01 alex flags = "o";
278 634ef8c1 2008-02-26 fw if (!join_allowed(Client, target, chan, channame, key))
281 956bbe2c 2008-05-24 alex /* Local client: update idle time */
282 956bbe2c 2008-05-24 alex Conn_UpdateIdle(Client_Conn(Client));
284 233210b9 2004-04-09 alex /* Remote server: we don't need to know whether the
285 233210b9 2004-04-09 alex * client is invited or not, but we have to make sure
286 233210b9 2004-04-09 alex * that the "one shot" entries (generated by INVITE
287 233210b9 2004-04-09 alex * commands) in this list become deleted when a user
288 233210b9 2004-04-09 alex * joins a channel this way. */
289 634ef8c1 2008-02-26 fw if (chan) (void)Lists_Check(Channel_GetListInvites(chan), target);
292 634ef8c1 2008-02-26 fw /* Join channel (and create channel if it doesn't exist) */
293 634ef8c1 2008-02-26 fw if (!Channel_Join(target, channame))
296 530112b1 2008-05-01 alex if (!chan) { /* channel is new; it has been created above */
297 634ef8c1 2008-02-26 fw chan = Channel_Search(channame);
298 530112b1 2008-05-01 alex assert(chan != NULL);
299 530112b1 2008-05-01 alex if (*channame == '+') { /* modeless channel... */
300 530112b1 2008-05-01 alex Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
301 530112b1 2008-05-01 alex Channel_ModeAdd(chan, 'n'); /* no external msgs */
304 634ef8c1 2008-02-26 fw assert(chan != NULL);
306 634ef8c1 2008-02-26 fw join_set_channelmodes(chan, target, flags);
308 634ef8c1 2008-02-26 fw join_forward(Client, target, chan, channame);
310 634ef8c1 2008-02-26 fw if (!join_send_topic(Client, target, chan, channame))
311 634ef8c1 2008-02-26 fw break; /* write error */
313 e7087905 2005-09-02 alex /* next channel? */
314 b90f71ca 2008-05-26 fw channame = strtok_r(NULL, ",", &lastchan);
315 b90f71ca 2008-05-26 fw if (channame && key)
316 b90f71ca 2008-05-26 fw key = strtok_r(NULL, ",", &lastkey);
318 2ee05c9a 2002-03-03 alex return CONNECTED;
319 2ee05c9a 2002-03-03 alex } /* IRC_JOIN */
323 25f48a2a 2008-04-24 alex * Handler for the IRC "PART" command.
326 25f48a2a 2008-04-24 alex IRC_PART(CLIENT * Client, REQUEST * Req)
328 2ee05c9a 2002-03-03 alex CLIENT *target;
331 25f48a2a 2008-04-24 alex assert(Client != NULL);
332 25f48a2a 2008-04-24 alex assert(Req != NULL);
334 463c5cb7 2008-01-07 fw if (Req->argc < 1 || Req->argc > 2)
335 463c5cb7 2008-01-07 fw return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
336 25f48a2a 2008-04-24 alex Client_ID(Client), Req->command);
338 25f48a2a 2008-04-24 alex /* Get the sender */
339 25f48a2a 2008-04-24 alex if (Client_Type(Client) == CLIENT_SERVER)
340 25f48a2a 2008-04-24 alex target = Client_Search(Req->prefix);
342 25f48a2a 2008-04-24 alex target = Client;
343 25f48a2a 2008-04-24 alex if (!target)
344 25f48a2a 2008-04-24 alex return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
345 25f48a2a 2008-04-24 alex Client_ID(Client), Req->prefix);
347 25f48a2a 2008-04-24 alex /* Loop over all the given channel names */
348 463c5cb7 2008-01-07 fw chan = strtok(Req->argv[0], ",");
350 25814389 2008-07-22 alex /* Make sure that "chan" is not the empty string ("PART :") */
351 25814389 2008-07-22 alex if (! chan)
352 25814389 2008-07-22 alex return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
353 25814389 2008-07-22 alex Client_ID(Client), Req->command);
355 463c5cb7 2008-01-07 fw while (chan) {
356 25f48a2a 2008-04-24 alex Channel_Part(target, Client, chan,
357 25f48a2a 2008-04-24 alex Req->argc > 1 ? Req->argv[1] : Client_ID(target));
358 463c5cb7 2008-01-07 fw chan = strtok(NULL, ",");
361 956bbe2c 2008-05-24 alex /* Update idle time, if local client */
362 956bbe2c 2008-05-24 alex if (Client_Conn(Client) > NONE)
363 956bbe2c 2008-05-24 alex Conn_UpdateIdle(Client_Conn(Client));
365 2ee05c9a 2002-03-03 alex return CONNECTED;
366 2ee05c9a 2002-03-03 alex } /* IRC_PART */
370 c2f60abe 2002-05-27 alex IRC_TOPIC( CLIENT *Client, REQUEST *Req )
372 2ee05c9a 2002-03-03 alex CHANNEL *chan;
373 2ee05c9a 2002-03-03 alex CLIENT *from;
377 2ee05c9a 2002-03-03 alex assert( Client != NULL );
378 2ee05c9a 2002-03-03 alex assert( Req != NULL );
380 530112b1 2008-05-01 alex if ((Req->argc < 1) || (Req->argc > 2))
381 530112b1 2008-05-01 alex return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
383 bc4ed226 2002-03-25 alex if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
384 2ee05c9a 2002-03-03 alex else from = Client;
385 2ee05c9a 2002-03-03 alex if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
387 2ee05c9a 2002-03-03 alex /* Welcher Channel? */
388 2ee05c9a 2002-03-03 alex chan = Channel_Search( Req->argv[0] );
389 a2119a66 2002-06-01 alex if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
391 2ee05c9a 2002-03-03 alex /* Ist der User Mitglied in dem Channel? */
392 2ee05c9a 2002-03-03 alex if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
394 2ee05c9a 2002-03-03 alex if( Req->argc == 1 )
396 ca32c1b3 2005-09-02 alex /* Request actual topic */
397 ca32c1b3 2005-09-02 alex topic = Channel_Topic(chan);
398 ca32c1b3 2005-09-02 alex if (*topic) {
399 ca32c1b3 2005-09-02 alex r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
400 ca32c1b3 2005-09-02 alex Client_ID(Client), Channel_Name(chan), topic);
401 ca32c1b3 2005-09-02 alex #ifndef STRICT_RFC
402 ca32c1b3 2005-09-02 alex r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
403 ca32c1b3 2005-09-02 alex Client_ID(Client), Channel_Name(chan),
404 ca32c1b3 2005-09-02 alex Channel_TopicWho(chan),
405 ca32c1b3 2005-09-02 alex Channel_TopicTime(chan));
410 ca32c1b3 2005-09-02 alex return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
411 ca32c1b3 2005-09-02 alex Client_ID(from), Channel_Name(chan));
414 2ee05c9a 2002-03-03 alex if( strchr( Channel_Modes( chan ), 't' ))
416 2ee05c9a 2002-03-03 alex /* Topic Lock. Ist der User ein Channel Operator? */
417 2ee05c9a 2002-03-03 alex if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
420 ca32c1b3 2005-09-02 alex /* Set new topic */
421 ca32c1b3 2005-09-02 alex Channel_SetTopic(chan, from, Req->argv[1]);
422 d93030ad 2008-09-23 alex LogDebug("%s \"%s\" set topic on \"%s\": %s",
423 d93030ad 2008-09-23 alex Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
424 d93030ad 2008-09-23 alex Req->argv[1][0] ? Req->argv[1] : "<none>");
426 2ee05c9a 2002-03-03 alex /* im Channel bekannt machen und an Server weiterleiten */
427 2ee05c9a 2002-03-03 alex IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
428 8adff592 2005-03-19 fw IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
430 2ee05c9a 2002-03-03 alex if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
431 2ee05c9a 2002-03-03 alex else return CONNECTED;
432 2ee05c9a 2002-03-03 alex } /* IRC_TOPIC */
436 94dd7fa7 2005-06-12 alex * Handler for the IRC "LIST" command.
437 94dd7fa7 2005-06-12 alex * This implementation handles the local case as well as the forwarding of the
438 94dd7fa7 2005-06-12 alex * LIST command to other servers in the IRC network.
441 c2f60abe 2002-05-27 alex IRC_LIST( CLIENT *Client, REQUEST *Req )
443 8adff592 2005-03-19 fw char *pattern;
444 3c0c3c3c 2002-04-23 alex CHANNEL *chan;
445 005391ca 2002-09-16 alex CLIENT *from, *target;
447 3c0c3c3c 2002-04-23 alex assert( Client != NULL );
448 3c0c3c3c 2002-04-23 alex assert( Req != NULL );
450 94dd7fa7 2005-06-12 alex /* Bad number of prameters? */
451 94dd7fa7 2005-06-12 alex if( Req->argc > 2 )
452 94dd7fa7 2005-06-12 alex return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
453 94dd7fa7 2005-06-12 alex Client_ID( Client ), Req->command );
455 94dd7fa7 2005-06-12 alex if( Req->argc > 0 )
456 94dd7fa7 2005-06-12 alex pattern = strtok( Req->argv[0], "," );
458 94dd7fa7 2005-06-12 alex pattern = "*";
460 94dd7fa7 2005-06-12 alex /* Get sender from prefix, if any */
461 94dd7fa7 2005-06-12 alex if( Client_Type( Client ) == CLIENT_SERVER )
462 94dd7fa7 2005-06-12 alex from = Client_Search( Req->prefix );
464 94dd7fa7 2005-06-12 alex from = Client;
466 94dd7fa7 2005-06-12 alex if( ! from )
467 94dd7fa7 2005-06-12 alex return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
468 94dd7fa7 2005-06-12 alex Client_ID( Client ), Req->prefix );
470 005391ca 2002-09-16 alex if( Req->argc == 2 )
472 94dd7fa7 2005-06-12 alex /* Forward to other server? */
473 005391ca 2002-09-16 alex target = Client_Search( Req->argv[1] );
474 94dd7fa7 2005-06-12 alex if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
475 94dd7fa7 2005-06-12 alex return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
476 94dd7fa7 2005-06-12 alex Client_ID( Client ), Req->argv[1] );
478 005391ca 2002-09-16 alex if( target != Client_ThisServer( ))
480 94dd7fa7 2005-06-12 alex /* Target is indeed an other server, forward it! */
481 94dd7fa7 2005-06-12 alex return IRC_WriteStrClientPrefix( target, from,
482 94dd7fa7 2005-06-12 alex "LIST %s :%s", Client_ID( from ),
483 94dd7fa7 2005-06-12 alex Req->argv[1] );
487 3c0c3c3c 2002-04-23 alex while( pattern )
489 4ef172d6 2005-03-02 alex /* Loop through all the channels */
490 3c0c3c3c 2002-04-23 alex chan = Channel_First( );
491 3c0c3c3c 2002-04-23 alex while( chan )
493 4ef172d6 2005-03-02 alex /* Check search pattern */
494 39b9f65d 2002-06-26 alex if( Match( pattern, Channel_Name( chan )))
496 4ef172d6 2005-03-02 alex /* Gotcha! */
497 4ef172d6 2005-03-02 alex if( ! strchr( Channel_Modes( chan ), 's' ) ||
498 4ef172d6 2005-03-02 alex Channel_IsMemberOf( chan, from ))
500 94dd7fa7 2005-06-12 alex if( ! IRC_WriteStrClient( from,
501 94dd7fa7 2005-06-12 alex RPL_LIST_MSG, Client_ID( from ),
502 94dd7fa7 2005-06-12 alex Channel_Name( chan ),
503 94dd7fa7 2005-06-12 alex Channel_MemberCount( chan ),
504 94dd7fa7 2005-06-12 alex Channel_Topic( chan )))
505 94dd7fa7 2005-06-12 alex return DISCONNECTED;
508 3c0c3c3c 2002-04-23 alex chan = Channel_Next( chan );
511 94dd7fa7 2005-06-12 alex /* Get next name ... */
512 94dd7fa7 2005-06-12 alex if( Req->argc > 0 )
513 94dd7fa7 2005-06-12 alex pattern = strtok( NULL, "," );
515 94dd7fa7 2005-06-12 alex pattern = NULL;
518 94dd7fa7 2005-06-12 alex return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
519 3c0c3c3c 2002-04-23 alex } /* IRC_LIST */
523 1f9ba7b3 2002-09-03 alex IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
525 8adff592 2005-03-19 fw char modes_add[COMMAND_LEN], l[16], *ptr;
526 1f9ba7b3 2002-09-03 alex CLIENT *from;
527 1f9ba7b3 2002-09-03 alex CHANNEL *chan;
528 8adff592 2005-03-19 fw int arg_topic;
530 1f9ba7b3 2002-09-03 alex assert( Client != NULL );
531 1f9ba7b3 2002-09-03 alex assert( Req != NULL );
533 43d9a624 2003-01-08 alex /* Bad number of parameters? */
534 43d9a624 2003-01-08 alex if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
536 43d9a624 2003-01-08 alex /* Compatibility kludge */
537 43d9a624 2003-01-08 alex if( Req->argc == 5 ) arg_topic = 4;
538 43d9a624 2003-01-08 alex else if( Req->argc == 3 ) arg_topic = 2;
539 43d9a624 2003-01-08 alex else arg_topic = -1;
541 43d9a624 2003-01-08 alex /* Search origin */
542 1f9ba7b3 2002-09-03 alex from = Client_Search( Req->prefix );
543 1f9ba7b3 2002-09-03 alex if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
545 43d9a624 2003-01-08 alex /* Search or create channel */
546 1f9ba7b3 2002-09-03 alex chan = Channel_Search( Req->argv[0] );
547 1f9ba7b3 2002-09-03 alex if( ! chan ) chan = Channel_Create( Req->argv[0] );
548 1f9ba7b3 2002-09-03 alex if( ! chan ) return CONNECTED;
550 1f9ba7b3 2002-09-03 alex if( Req->argv[1][0] == '+' )
552 1f9ba7b3 2002-09-03 alex ptr = Channel_Modes( chan );
553 1f9ba7b3 2002-09-03 alex if( ! *ptr )
555 bb94d181 2003-01-08 alex /* OK, this channel doesn't have modes jet, set the received ones: */
556 1f9ba7b3 2002-09-03 alex Channel_SetModes( chan, &Req->argv[1][1] );
558 43d9a624 2003-01-08 alex if( Req->argc == 5 )
560 43d9a624 2003-01-08 alex if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
561 43d9a624 2003-01-08 alex if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
565 43d9a624 2003-01-08 alex /* Delete modes which we never want to inherit */
566 43d9a624 2003-01-08 alex Channel_ModeDel( chan, 'l' );
567 43d9a624 2003-01-08 alex Channel_ModeDel( chan, 'k' );
570 43d9a624 2003-01-08 alex strcpy( modes_add, "" );
571 43d9a624 2003-01-08 alex ptr = Channel_Modes( chan );
572 43d9a624 2003-01-08 alex while( *ptr )
574 43d9a624 2003-01-08 alex if( *ptr == 'l' )
576 6e105bf8 2006-10-06 fw snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
577 43d9a624 2003-01-08 alex strlcat( modes_add, l, sizeof( modes_add ));
579 43d9a624 2003-01-08 alex if( *ptr == 'k' )
581 43d9a624 2003-01-08 alex strlcat( modes_add, " ", sizeof( modes_add ));
582 43d9a624 2003-01-08 alex strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
587 43d9a624 2003-01-08 alex /* Inform members of this channel */
588 8adff592 2005-03-19 fw IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
591 43d9a624 2003-01-08 alex else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
593 43d9a624 2003-01-08 alex if( arg_topic > 0 )
595 43d9a624 2003-01-08 alex /* We got a topic */
596 1f9ba7b3 2002-09-03 alex ptr = Channel_Topic( chan );
597 43d9a624 2003-01-08 alex if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
599 43d9a624 2003-01-08 alex /* OK, there is no topic jet */
600 ca32c1b3 2005-09-02 alex Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
601 ca32c1b3 2005-09-02 alex IRC_WriteStrChannelPrefix(Client, chan, from, false,
602 ca32c1b3 2005-09-02 alex "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
606 43d9a624 2003-01-08 alex /* Forward CHANINFO to other serevrs */
607 43d9a624 2003-01-08 alex if( Req->argc == 5 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2], Req->argv[3], Req->argv[4] );
608 43d9a624 2003-01-08 alex else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
609 43d9a624 2003-01-08 alex else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );
611 1f9ba7b3 2002-09-03 alex return CONNECTED;
612 1f9ba7b3 2002-09-03 alex } /* IRC_CHANINFO */
615 2ee05c9a 2002-03-03 alex /* -eof- */