2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
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.
11 * IRC channel commands
27 #include "conn-func.h"
34 #include "irc-write.h"
39 #include "irc-channel.h"
43 * RFC 2812, (3.2.1 Join message Command):
44 * Note that this message
45 * accepts a special argument ("0"), which is a special request to leave all
46 * channels the user is currently a member of. The server will process this
47 * message as if the user had sent a PART command (See Section 3.2.2) for
48 * each channel he is a member of.
51 part_from_all_channels(CLIENT* client, CLIENT *target)
56 while ((cl2chan = Channel_FirstChannelOf(target))) {
57 chan = Channel_GetChannel(cl2chan);
58 assert( chan != NULL );
59 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
66 join_allowed(CLIENT *Client, CLIENT *target, CHANNEL *chan,
67 const char *channame, const char *key)
69 bool is_invited, is_banned;
70 const char *channel_modes;
72 /* Allow IRC operators to overwrite channel limits */
73 if (strchr(Client_Modes(Client), 'o'))
76 is_banned = Lists_Check(Channel_GetListBans(chan), target);
77 is_invited = Lists_Check(Channel_GetListInvites(chan), target);
79 if (is_banned && !is_invited) {
80 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
84 channel_modes = Channel_Modes(chan);
85 if ((strchr(channel_modes, 'i')) && !is_invited) {
86 /* Channel is "invite-only" (and Client wasn't invited) */
87 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG, Client_ID(Client), channame);
91 /* Is the channel protected by a key? */
92 if (strchr(channel_modes, 'k') &&
93 strcmp(Channel_Key(chan), key ? key : ""))
95 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG, Client_ID(Client), channame);
98 /* Are there already too many members? */
99 if ((strchr(channel_modes, 'l')) && (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
100 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG, Client_ID(Client), channame);
108 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
112 Channel_UserModeAdd(chan, target, *flags);
117 /* If channel persistent and client is ircop: make client chanop */
118 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
119 Channel_UserModeAdd(chan, target, 'o');
124 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
127 char str[COMMAND_LEN], *ptr = NULL;
129 strlcpy(str, (char *)Data, sizeof(str));
130 conn = Client_Conn(To);
132 if (Conn_Options(conn) & CONN_RFC1459) {
133 /* RFC 1459 compatibility mode, appended modes are NOT
134 * supported, so strip them off! */
135 ptr = strchr(str, 0x7);
140 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
142 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
144 } /* cb_join_forward */
148 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
149 const char *channame)
151 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
153 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
159 /* forward to other servers (if it is not a local channel) */
160 if (!Channel_IsLocal(chan)) {
161 snprintf(str, sizeof(str), "%s%s", channame, modes);
162 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
163 cb_join_forward, str);
166 /* tell users in this channel about the new client */
167 IRC_WriteStrChannelPrefix(Client, chan, target, false,
168 "JOIN :%s", channame);
170 /* syncronize channel modes */
172 IRC_WriteStrChannelPrefix(Client, chan, target, false,
173 "MODE %s +%s %s", channame,
174 &modes[1], Client_ID(target));
180 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
181 const char *channame)
185 if (Client_Type(Client) != CLIENT_USER)
187 /* acknowledge join */
188 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
191 /* Send topic to client, if any */
192 topic = Channel_Topic(chan);
193 assert(topic != NULL);
195 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
196 Client_ID(Client), channame, topic))
199 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
200 Client_ID(Client), channame,
201 Channel_TopicWho(chan),
202 Channel_TopicTime(chan)))
206 /* send list of channel members to client */
207 if (!IRC_Send_NAMES(Client, chan))
209 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
214 IRC_JOIN( CLIENT *Client, REQUEST *Req )
216 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
220 assert( Client != NULL );
221 assert( Req != NULL );
223 /* Bad number of arguments? */
224 if (Req->argc < 1 || Req->argc > 2)
225 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
226 Client_ID(Client), Req->command);
228 /* Who is the sender? */
229 if (Client_Type(Client) == CLIENT_SERVER)
230 target = Client_Search(Req->prefix);
235 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
237 /* Is argument "0"? */
238 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
239 return part_from_all_channels(Client, target);
241 /* Are channel keys given? */
243 key = strtok_r(Req->argv[1], ",", &lastkey);
245 channame = Req->argv[0];
246 channame = strtok_r(channame, ",", &lastchan);
248 /* Make sure that "channame" is not the empty string ("JOIN :") */
250 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
251 Client_ID(Client), Req->command);
256 /* Did the server include channel-user-modes? */
257 if (Client_Type(Client) == CLIENT_SERVER) {
258 flags = strchr(channame, 0x7);
265 chan = Channel_Search(channame);
266 if (!chan && Conf_PredefChannelsOnly) {
267 /* channel must be created, but server does not allow this */
268 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
273 if (Client_Type(Client) == CLIENT_USER) {
274 /* Test if the user has reached his maximum channel count */
275 if ((Conf_MaxJoins > 0) && (Channel_CountForUser(Client) >= Conf_MaxJoins))
276 return IRC_WriteStrClient(Client, ERR_TOOMANYCHANNELS_MSG,
277 Client_ID(Client), channame);
280 * New Channel: first user will be channel operator
281 * unless this is a modeless channel.
283 if (*channame != '+')
286 if (!join_allowed(Client, target, chan, channame, key))
289 /* Local client: update idle time */
290 Conn_UpdateIdle(Client_Conn(Client));
292 /* Remote server: we don't need to know whether the
293 * client is invited or not, but we have to make sure
294 * that the "one shot" entries (generated by INVITE
295 * commands) in this list become deleted when a user
296 * joins a channel this way. */
297 if (chan) (void)Lists_Check(Channel_GetListInvites(chan), target);
300 /* Join channel (and create channel if it doesn't exist) */
301 if (!Channel_Join(target, channame))
304 if (!chan) { /* channel is new; it has been created above */
305 chan = Channel_Search(channame);
306 assert(chan != NULL);
307 if (*channame == '+') { /* modeless channel... */
308 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
309 Channel_ModeAdd(chan, 'n'); /* no external msgs */
312 assert(chan != NULL);
314 join_set_channelmodes(chan, target, flags);
316 join_forward(Client, target, chan, channame);
318 if (!join_send_topic(Client, target, chan, channame))
319 break; /* write error */
322 channame = strtok_r(NULL, ",", &lastchan);
324 key = strtok_r(NULL, ",", &lastkey);
331 * Handler for the IRC "PART" command.
334 IRC_PART(CLIENT * Client, REQUEST * Req)
339 assert(Client != NULL);
342 if (Req->argc < 1 || Req->argc > 2)
343 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
344 Client_ID(Client), Req->command);
347 if (Client_Type(Client) == CLIENT_SERVER)
348 target = Client_Search(Req->prefix);
352 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
353 Client_ID(Client), Req->prefix);
355 /* Loop over all the given channel names */
356 chan = strtok(Req->argv[0], ",");
358 /* Make sure that "chan" is not the empty string ("PART :") */
360 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
361 Client_ID(Client), Req->command);
364 Channel_Part(target, Client, chan,
365 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
366 chan = strtok(NULL, ",");
369 /* Update idle time, if local client */
370 if (Client_Conn(Client) > NONE)
371 Conn_UpdateIdle(Client_Conn(Client));
378 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
385 assert( Client != NULL );
386 assert( Req != NULL );
388 if ((Req->argc < 1) || (Req->argc > 2))
389 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
391 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
393 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
395 /* Welcher Channel? */
396 chan = Channel_Search( Req->argv[0] );
397 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
399 /* Ist der User Mitglied in dem Channel? */
400 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
404 /* Request actual topic */
405 topic = Channel_Topic(chan);
407 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
408 Client_ID(Client), Channel_Name(chan), topic);
410 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
411 Client_ID(Client), Channel_Name(chan),
412 Channel_TopicWho(chan),
413 Channel_TopicTime(chan));
418 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
419 Client_ID(from), Channel_Name(chan));
422 if( strchr( Channel_Modes( chan ), 't' ))
424 /* Topic Lock. Ist der User ein Channel Operator? */
425 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
429 Channel_SetTopic(chan, from, Req->argv[1]);
430 LogDebug("%s \"%s\" set topic on \"%s\": %s",
431 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
432 Req->argv[1][0] ? Req->argv[1] : "<none>");
434 /* Update channel and forward new topic to other servers */
435 if (!Channel_IsLocal(chan))
436 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
437 Req->argv[0], Req->argv[1]);
438 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
439 Req->argv[0], Req->argv[1]);
441 if (Client_Type(Client) == CLIENT_USER)
442 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
443 Req->argv[0], Req->argv[1]);
450 * Handler for the IRC "LIST" command.
451 * This implementation handles the local case as well as the forwarding of the
452 * LIST command to other servers in the IRC network.
455 IRC_LIST( CLIENT *Client, REQUEST *Req )
459 CLIENT *from, *target;
461 assert( Client != NULL );
462 assert( Req != NULL );
464 /* Bad number of prameters? */
466 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
467 Client_ID( Client ), Req->command );
470 pattern = strtok( Req->argv[0], "," );
474 /* Get sender from prefix, if any */
475 if( Client_Type( Client ) == CLIENT_SERVER )
476 from = Client_Search( Req->prefix );
481 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
482 Client_ID( Client ), Req->prefix );
486 /* Forward to other server? */
487 target = Client_Search( Req->argv[1] );
488 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
489 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
490 Client_ID( Client ), Req->argv[1] );
492 if( target != Client_ThisServer( ))
494 /* Target is indeed an other server, forward it! */
495 return IRC_WriteStrClientPrefix( target, from,
496 "LIST %s :%s", Client_ID( from ),
503 /* Loop through all the channels */
504 chan = Channel_First( );
507 /* Check search pattern */
508 if( Match( pattern, Channel_Name( chan )))
511 if( ! strchr( Channel_Modes( chan ), 's' ) ||
512 Channel_IsMemberOf( chan, from ))
514 if( ! IRC_WriteStrClient( from,
515 RPL_LIST_MSG, Client_ID( from ),
516 Channel_Name( chan ),
517 Channel_MemberCount( chan ),
518 Channel_Topic( chan )))
522 chan = Channel_Next( chan );
525 /* Get next name ... */
527 pattern = strtok( NULL, "," );
532 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
537 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
539 char modes_add[COMMAND_LEN], l[16], *ptr;
544 assert( Client != NULL );
545 assert( Req != NULL );
547 /* Bad number of parameters? */
548 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
550 /* Compatibility kludge */
551 if( Req->argc == 5 ) arg_topic = 4;
552 else if( Req->argc == 3 ) arg_topic = 2;
556 from = Client_Search( Req->prefix );
557 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
559 /* Search or create channel */
560 chan = Channel_Search( Req->argv[0] );
561 if( ! chan ) chan = Channel_Create( Req->argv[0] );
562 if( ! chan ) return CONNECTED;
564 if( Req->argv[1][0] == '+' )
566 ptr = Channel_Modes( chan );
569 /* OK, this channel doesn't have modes jet, set the received ones: */
570 Channel_SetModes( chan, &Req->argv[1][1] );
574 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
575 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
579 /* Delete modes which we never want to inherit */
580 Channel_ModeDel( chan, 'l' );
581 Channel_ModeDel( chan, 'k' );
584 strcpy( modes_add, "" );
585 ptr = Channel_Modes( chan );
590 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
591 strlcat( modes_add, l, sizeof( modes_add ));
595 strlcat( modes_add, " ", sizeof( modes_add ));
596 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
601 /* Inform members of this channel */
602 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
605 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
610 ptr = Channel_Topic( chan );
611 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
613 /* OK, there is no topic jet */
614 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
615 IRC_WriteStrChannelPrefix(Client, chan, from, false,
616 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
620 /* Forward CHANINFO to other serevrs */
621 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] );
622 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
623 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );