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.43 2008/02/05 19:00:52 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)
57 while ((cl2chan = Channel_FirstChannelOf(target))) {
58 chan = Channel_GetChannel(cl2chan);
59 assert( chan != NULL );
60 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
67 IRC_JOIN( CLIENT *Client, REQUEST *Req )
69 char *channame, *channame_ptr, *key, *key_ptr, *flags, *topic, modes[8];
70 bool is_new_chan, is_invited, is_banned;
74 assert( Client != NULL );
75 assert( Req != NULL );
77 /* Bad number of arguments? */
78 if (Req->argc < 1 || Req->argc > 2)
79 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
80 Client_ID(Client), Req->command);
82 /* Who is the sender? */
83 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
85 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
87 /* Is argument "0"? */
88 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
89 return part_from_all_channels(Client, target);
91 /* Are channel keys given? */
94 key_ptr = strchr(key, ',');
95 if (key_ptr) *key_ptr = '\0';
100 channame = Req->argv[0];
101 channame_ptr = strchr(channame, ',');
102 if (channame_ptr) *channame_ptr = '\0';
104 /* Channel-Namen durchgehen */
107 chan = NULL; flags = NULL;
109 /* wird der Channel neu angelegt? */
110 if( Channel_Search( channame )) {
113 if (Conf_PredefChannelsOnly) { /* this server does not allow creation of channels */
114 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
115 /* Try next name, if any */
116 channame = strchr(channame, ',');
122 /* Hat ein Server Channel-User-Modes uebergeben? */
123 if( Client_Type( Client ) == CLIENT_SERVER )
125 /* Channel-Flags extrahieren */
126 flags = strchr( channame, 0x7 );
135 if( Client_Type( Client ) == CLIENT_USER )
137 /* Test if the user has reached his maximum channel count */
138 if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
139 return IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG,
140 Client_ID( Client ), channame );
142 /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
145 /* Erster User im Channel: Operator-Flag setzen */
150 /* Existierenden Channel suchen */
151 chan = Channel_Search( channame );
152 assert( chan != NULL );
154 is_banned = Lists_Check(Channel_GetListBans(chan), target );
155 is_invited = Lists_Check(Channel_GetListInvites(chan), target );
157 /* Testen, ob Client gebanned ist */
158 if(( is_banned == true) && ( is_invited == false ))
160 /* Client ist gebanned (und nicht invited): */
161 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
163 /* Try next name, if any */
164 channame = strchr(channame, ',');
168 /* Ist der Channel "invite-only"? */
169 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
171 /* Channel ist "invite-only" und Client wurde nicht invited: */
172 IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
174 /* Try next name, if any */
175 channame = strchr(channame, ',');
179 /* Is the channel protected by a key? */
180 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
182 /* Bad channel key! */
183 IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
185 /* Try next name, if any */
186 channame = strchr(channame, ',');
190 /* Are there already too many members? */
191 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
193 /* Bad channel key! */
194 IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
196 /* Try next name, if any */
197 channame = strchr(channame, ',');
204 /* Remote server: we don't need to know whether the
205 * client is invited or not, but we have to make sure
206 * that the "one shot" entries (generated by INVITE
207 * commands) in this list become deleted when a user
208 * joins a channel this way. */
209 chan = Channel_Search( channame );
210 if( chan != NULL ) (void)Lists_Check(Channel_GetListInvites(chan), target);
213 /* Channel joinen (und ggf. anlegen) */
214 if( ! Channel_Join( target, channame ))
216 /* naechsten Namen ermitteln */
217 channame = strchr(channame, ',');
220 if( ! chan ) chan = Channel_Search( channame );
221 assert( chan != NULL );
223 /* Modes setzen (wenn vorhanden) */
224 while( flags && *flags )
226 Channel_UserModeAdd( chan, target, *flags );
230 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
231 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
233 /* Muessen Modes an andere Server gemeldet werden? */
234 strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
235 if( modes[1] ) modes[0] = 0x7;
236 else modes[0] = '\0';
238 /* An andere Server weiterleiten */
239 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
241 /* im Channel bekannt machen */
242 IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
245 /* Modes im Channel bekannt machen */
246 IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
249 if( Client_Type( Client ) == CLIENT_USER )
251 /* an Client bestaetigen */
252 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
254 /* Send topic to client, if any */
255 topic = Channel_Topic(chan);
257 IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
258 Client_ID(Client), channame, topic);
260 IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
261 Client_ID(Client), channame,
262 Channel_TopicWho(chan),
263 Channel_TopicTime(chan));
267 /* Mitglieder an Client Melden */
268 IRC_Send_NAMES( Client, chan );
269 IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
273 channame = channame_ptr;
276 channame_ptr = strchr(channame, ',');
277 if (channame_ptr) *channame_ptr = '\0';
281 key_ptr = strchr(key, ',');
282 if (key_ptr) *key_ptr = '\0';
291 IRC_PART( CLIENT *Client, REQUEST *Req )
296 assert( Client != NULL );
297 assert( Req != NULL );
299 if (Req->argc < 1 || Req->argc > 2)
300 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
301 Client_ID(Client), Req->command);
303 /* Wer ist der Absender? */
304 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
305 else target = Client;
306 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
308 /* Channel-Namen durchgehen */
309 chan = strtok(Req->argv[0], ",");
311 Channel_Part(target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID(target));
313 chan = strtok(NULL, ",");
320 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
327 assert( Client != NULL );
328 assert( Req != NULL );
330 /* Falsche Anzahl Parameter? */
331 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
333 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
335 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
337 /* Welcher Channel? */
338 chan = Channel_Search( Req->argv[0] );
339 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
341 /* Ist der User Mitglied in dem Channel? */
342 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
346 /* Request actual topic */
347 topic = Channel_Topic(chan);
349 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
350 Client_ID(Client), Channel_Name(chan), topic);
352 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
353 Client_ID(Client), Channel_Name(chan),
354 Channel_TopicWho(chan),
355 Channel_TopicTime(chan));
360 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
361 Client_ID(from), Channel_Name(chan));
364 if( strchr( Channel_Modes( chan ), 't' ))
366 /* Topic Lock. Ist der User ein Channel Operator? */
367 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
371 Channel_SetTopic(chan, from, Req->argv[1]);
372 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
373 Client_Mask(from), Channel_Name(chan),
374 Req->argv[1][0] ? Req->argv[1] : "<none>");
376 /* im Channel bekannt machen und an Server weiterleiten */
377 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
378 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
380 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
381 else return CONNECTED;
386 * Handler for the IRC "LIST" command.
387 * This implementation handles the local case as well as the forwarding of the
388 * LIST command to other servers in the IRC network.
391 IRC_LIST( CLIENT *Client, REQUEST *Req )
395 CLIENT *from, *target;
397 assert( Client != NULL );
398 assert( Req != NULL );
400 /* Bad number of prameters? */
402 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
403 Client_ID( Client ), Req->command );
406 pattern = strtok( Req->argv[0], "," );
410 /* Get sender from prefix, if any */
411 if( Client_Type( Client ) == CLIENT_SERVER )
412 from = Client_Search( Req->prefix );
417 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
418 Client_ID( Client ), Req->prefix );
422 /* Forward to other server? */
423 target = Client_Search( Req->argv[1] );
424 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
425 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
426 Client_ID( Client ), Req->argv[1] );
428 if( target != Client_ThisServer( ))
430 /* Target is indeed an other server, forward it! */
431 return IRC_WriteStrClientPrefix( target, from,
432 "LIST %s :%s", Client_ID( from ),
439 /* Loop through all the channels */
440 chan = Channel_First( );
443 /* Check search pattern */
444 if( Match( pattern, Channel_Name( chan )))
447 if( ! strchr( Channel_Modes( chan ), 's' ) ||
448 Channel_IsMemberOf( chan, from ))
450 if( ! IRC_WriteStrClient( from,
451 RPL_LIST_MSG, Client_ID( from ),
452 Channel_Name( chan ),
453 Channel_MemberCount( chan ),
454 Channel_Topic( chan )))
458 chan = Channel_Next( chan );
461 /* Get next name ... */
463 pattern = strtok( NULL, "," );
468 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
473 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
475 char modes_add[COMMAND_LEN], l[16], *ptr;
480 assert( Client != NULL );
481 assert( Req != NULL );
483 /* Bad number of parameters? */
484 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
486 /* Compatibility kludge */
487 if( Req->argc == 5 ) arg_topic = 4;
488 else if( Req->argc == 3 ) arg_topic = 2;
492 from = Client_Search( Req->prefix );
493 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
495 /* Search or create channel */
496 chan = Channel_Search( Req->argv[0] );
497 if( ! chan ) chan = Channel_Create( Req->argv[0] );
498 if( ! chan ) return CONNECTED;
500 if( Req->argv[1][0] == '+' )
502 ptr = Channel_Modes( chan );
505 /* OK, this channel doesn't have modes jet, set the received ones: */
506 Channel_SetModes( chan, &Req->argv[1][1] );
510 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
511 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
515 /* Delete modes which we never want to inherit */
516 Channel_ModeDel( chan, 'l' );
517 Channel_ModeDel( chan, 'k' );
520 strcpy( modes_add, "" );
521 ptr = Channel_Modes( chan );
526 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
527 strlcat( modes_add, l, sizeof( modes_add ));
531 strlcat( modes_add, " ", sizeof( modes_add ));
532 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
537 /* Inform members of this channel */
538 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
541 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
546 ptr = Channel_Topic( chan );
547 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
549 /* OK, there is no topic jet */
550 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
551 IRC_WriteStrChannelPrefix(Client, chan, from, false,
552 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
556 /* Forward CHANINFO to other serevrs */
557 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] );
558 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
559 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );