2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by 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
17 static char UNUSED id[] = "$Id: irc-channel.c,v 1.42 2008/02/05 13:31:50 fw Exp $";
35 #include "irc-write.h"
40 #include "irc-channel.h"
44 * RFC 2812, (3.2.1 Join message Command):
45 * Note that this message
46 * accepts a special argument ("0"), which is a special request to leave all
47 * channels the user is currently a member of. The server will process this
48 * message as if the user had sent a PART command (See Section 3.2.2) for
49 * each channel he is a member of.
52 part_from_all_channels(CLIENT* client, CLIENT *target)
54 CL2CHAN *cl2chan = Channel_FirstChannelOf(target);
58 chan = Channel_GetChannel(cl2chan);
59 assert( chan != NULL );
60 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
63 cl2chan = Channel_FirstChannelOf(target);
70 IRC_JOIN( CLIENT *Client, REQUEST *Req )
72 char *channame, *channame_ptr, *key, *key_ptr, *flags, *topic, modes[8];
73 bool is_new_chan, is_invited, is_banned;
77 assert( Client != NULL );
78 assert( Req != NULL );
80 /* Bad number of arguments? */
81 if (Req->argc < 1 || Req->argc > 2)
82 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
83 Client_ID(Client), Req->command);
85 /* Who is the sender? */
86 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
88 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
90 /* Is argument "0"? */
91 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
92 return part_from_all_channels(Client, target);
94 /* Are channel keys given? */
97 key_ptr = strchr(key, ',');
98 if (key_ptr) *key_ptr = '\0';
101 key = key_ptr = NULL;
103 channame = Req->argv[0];
104 channame_ptr = strchr(channame, ',');
105 if (channame_ptr) *channame_ptr = '\0';
107 /* Channel-Namen durchgehen */
110 chan = NULL; flags = NULL;
112 /* wird der Channel neu angelegt? */
113 if( Channel_Search( channame )) {
116 if (Conf_PredefChannelsOnly) { /* this server does not allow creation of channels */
117 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
118 /* Try next name, if any */
119 channame = strchr(channame, ',');
125 /* Hat ein Server Channel-User-Modes uebergeben? */
126 if( Client_Type( Client ) == CLIENT_SERVER )
128 /* Channel-Flags extrahieren */
129 flags = strchr( channame, 0x7 );
138 if( Client_Type( Client ) == CLIENT_USER )
140 /* Test if the user has reached his maximum channel count */
141 if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
142 return IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG,
143 Client_ID( Client ), channame );
145 /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
148 /* Erster User im Channel: Operator-Flag setzen */
153 /* Existierenden Channel suchen */
154 chan = Channel_Search( channame );
155 assert( chan != NULL );
157 is_banned = Lists_Check(Channel_GetListBans(chan), target );
158 is_invited = Lists_Check(Channel_GetListInvites(chan), target );
160 /* Testen, ob Client gebanned ist */
161 if(( is_banned == true) && ( is_invited == false ))
163 /* Client ist gebanned (und nicht invited): */
164 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
166 /* Try next name, if any */
167 channame = strchr(channame, ',');
171 /* Ist der Channel "invite-only"? */
172 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
174 /* Channel ist "invite-only" und Client wurde nicht invited: */
175 IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
177 /* Try next name, if any */
178 channame = strchr(channame, ',');
182 /* Is the channel protected by a key? */
183 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
185 /* Bad channel key! */
186 IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
188 /* Try next name, if any */
189 channame = strchr(channame, ',');
193 /* Are there already too many members? */
194 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
196 /* Bad channel key! */
197 IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
199 /* Try next name, if any */
200 channame = strchr(channame, ',');
207 /* Remote server: we don't need to know whether the
208 * client is invited or not, but we have to make sure
209 * that the "one shot" entries (generated by INVITE
210 * commands) in this list become deleted when a user
211 * joins a channel this way. */
212 chan = Channel_Search( channame );
213 if( chan != NULL ) (void)Lists_Check(Channel_GetListInvites(chan), target);
216 /* Channel joinen (und ggf. anlegen) */
217 if( ! Channel_Join( target, channame ))
219 /* naechsten Namen ermitteln */
220 channame = strchr(channame, ',');
223 if( ! chan ) chan = Channel_Search( channame );
224 assert( chan != NULL );
226 /* Modes setzen (wenn vorhanden) */
227 while( flags && *flags )
229 Channel_UserModeAdd( chan, target, *flags );
233 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
234 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
236 /* Muessen Modes an andere Server gemeldet werden? */
237 strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
238 if( modes[1] ) modes[0] = 0x7;
239 else modes[0] = '\0';
241 /* An andere Server weiterleiten */
242 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
244 /* im Channel bekannt machen */
245 IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
248 /* Modes im Channel bekannt machen */
249 IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
252 if( Client_Type( Client ) == CLIENT_USER )
254 /* an Client bestaetigen */
255 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
257 /* Send topic to client, if any */
258 topic = Channel_Topic(chan);
260 IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
261 Client_ID(Client), channame, topic);
263 IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
264 Client_ID(Client), channame,
265 Channel_TopicWho(chan),
266 Channel_TopicTime(chan));
270 /* Mitglieder an Client Melden */
271 IRC_Send_NAMES( Client, chan );
272 IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
276 channame = channame_ptr;
279 channame_ptr = strchr(channame, ',');
280 if (channame_ptr) *channame_ptr = '\0';
284 key_ptr = strchr(key, ',');
285 if (key_ptr) *key_ptr = '\0';
294 IRC_PART( CLIENT *Client, REQUEST *Req )
299 assert( Client != NULL );
300 assert( Req != NULL );
302 if (Req->argc < 1 || Req->argc > 2)
303 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
304 Client_ID(Client), Req->command);
306 /* Wer ist der Absender? */
307 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
308 else target = Client;
309 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
311 /* Channel-Namen durchgehen */
312 chan = strtok(Req->argv[0], ",");
314 Channel_Part(target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID(target));
316 chan = strtok(NULL, ",");
323 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
330 assert( Client != NULL );
331 assert( Req != NULL );
333 /* Falsche Anzahl Parameter? */
334 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
336 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
338 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
340 /* Welcher Channel? */
341 chan = Channel_Search( Req->argv[0] );
342 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
344 /* Ist der User Mitglied in dem Channel? */
345 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
349 /* Request actual topic */
350 topic = Channel_Topic(chan);
352 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
353 Client_ID(Client), Channel_Name(chan), topic);
355 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
356 Client_ID(Client), Channel_Name(chan),
357 Channel_TopicWho(chan),
358 Channel_TopicTime(chan));
363 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
364 Client_ID(from), Channel_Name(chan));
367 if( strchr( Channel_Modes( chan ), 't' ))
369 /* Topic Lock. Ist der User ein Channel Operator? */
370 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
374 Channel_SetTopic(chan, from, Req->argv[1]);
375 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
376 Client_Mask(from), Channel_Name(chan),
377 Req->argv[1][0] ? Req->argv[1] : "<none>");
379 /* im Channel bekannt machen und an Server weiterleiten */
380 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
381 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
383 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
384 else return CONNECTED;
389 * Handler for the IRC "LIST" command.
390 * This implementation handles the local case as well as the forwarding of the
391 * LIST command to other servers in the IRC network.
394 IRC_LIST( CLIENT *Client, REQUEST *Req )
398 CLIENT *from, *target;
400 assert( Client != NULL );
401 assert( Req != NULL );
403 /* Bad number of prameters? */
405 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
406 Client_ID( Client ), Req->command );
409 pattern = strtok( Req->argv[0], "," );
413 /* Get sender from prefix, if any */
414 if( Client_Type( Client ) == CLIENT_SERVER )
415 from = Client_Search( Req->prefix );
420 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
421 Client_ID( Client ), Req->prefix );
425 /* Forward to other server? */
426 target = Client_Search( Req->argv[1] );
427 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
428 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
429 Client_ID( Client ), Req->argv[1] );
431 if( target != Client_ThisServer( ))
433 /* Target is indeed an other server, forward it! */
434 return IRC_WriteStrClientPrefix( target, from,
435 "LIST %s :%s", Client_ID( from ),
442 /* Loop through all the channels */
443 chan = Channel_First( );
446 /* Check search pattern */
447 if( Match( pattern, Channel_Name( chan )))
450 if( ! strchr( Channel_Modes( chan ), 's' ) ||
451 Channel_IsMemberOf( chan, from ))
453 if( ! IRC_WriteStrClient( from,
454 RPL_LIST_MSG, Client_ID( from ),
455 Channel_Name( chan ),
456 Channel_MemberCount( chan ),
457 Channel_Topic( chan )))
461 chan = Channel_Next( chan );
464 /* Get next name ... */
466 pattern = strtok( NULL, "," );
471 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
476 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
478 char modes_add[COMMAND_LEN], l[16], *ptr;
483 assert( Client != NULL );
484 assert( Req != NULL );
486 /* Bad number of parameters? */
487 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
489 /* Compatibility kludge */
490 if( Req->argc == 5 ) arg_topic = 4;
491 else if( Req->argc == 3 ) arg_topic = 2;
495 from = Client_Search( Req->prefix );
496 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
498 /* Search or create channel */
499 chan = Channel_Search( Req->argv[0] );
500 if( ! chan ) chan = Channel_Create( Req->argv[0] );
501 if( ! chan ) return CONNECTED;
503 if( Req->argv[1][0] == '+' )
505 ptr = Channel_Modes( chan );
508 /* OK, this channel doesn't have modes jet, set the received ones: */
509 Channel_SetModes( chan, &Req->argv[1][1] );
513 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
514 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
518 /* Delete modes which we never want to inherit */
519 Channel_ModeDel( chan, 'l' );
520 Channel_ModeDel( chan, 'k' );
523 strcpy( modes_add, "" );
524 ptr = Channel_Modes( chan );
529 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
530 strlcat( modes_add, l, sizeof( modes_add ));
534 strlcat( modes_add, " ", sizeof( modes_add ));
535 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
540 /* Inform members of this channel */
541 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
544 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
549 ptr = Channel_Topic( chan );
550 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
552 /* OK, there is no topic jet */
553 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
554 IRC_WriteStrChannelPrefix(Client, chan, from, false,
555 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
559 /* Forward CHANINFO to other serevrs */
560 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] );
561 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
562 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );