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.30.2.1 2006/03/16 20:03:14 fw Exp $";
35 #include "irc-write.h"
40 #include "irc-channel.h"
44 IRC_JOIN( CLIENT *Client, REQUEST *Req )
46 char *channame, *key, *flags, *topic, modes[8];
47 bool is_new_chan, is_invited, is_banned;
51 assert( Client != NULL );
52 assert( Req != NULL );
54 /* Bad number of arguments? */
55 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
57 /* Who is the sender? */
58 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
60 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
62 /* Are channel keys given? */
63 if( Req->argc > 1 ) key = Req->argv[1];
66 /* Channel-Namen durchgehen */
68 channame = strtok( Req->argv[0], "," );
71 chan = NULL; flags = NULL;
73 /* wird der Channel neu angelegt? */
74 if( Channel_Search( channame )) is_new_chan = false;
75 else is_new_chan = true;
77 /* Hat ein Server Channel-User-Modes uebergeben? */
78 if( Client_Type( Client ) == CLIENT_SERVER )
80 /* Channel-Flags extrahieren */
81 flags = strchr( channame, 0x7 );
90 if( Client_Type( Client ) == CLIENT_USER )
92 /* Test if the user has reached his maximum channel count */
93 if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins )) {
94 IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG,
95 Client_ID( Client ), channame );
99 /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
102 /* Erster User im Channel: Operator-Flag setzen */
107 /* Existierenden Channel suchen */
108 chan = Channel_Search( channame );
109 assert( chan != NULL );
111 is_banned = Lists_CheckBanned( target, chan );
112 is_invited = Lists_CheckInvited( target, chan );
114 /* Testen, ob Client gebanned ist */
115 if(( is_banned == true) && ( is_invited == false ))
117 /* Client ist gebanned (und nicht invited): */
118 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
120 /* Try next name, if any */
121 channame = strtok( NULL, "," );
125 /* Ist der Channel "invite-only"? */
126 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
128 /* Channel ist "invite-only" und Client wurde nicht invited: */
129 IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
131 /* Try next name, if any */
132 channame = strtok( NULL, "," );
136 /* Is the channel protected by a key? */
137 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
139 /* Bad channel key! */
140 IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
142 /* Try next name, if any */
143 channame = strtok( NULL, "," );
147 /* Are there already too many members? */
148 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
150 /* Bad channel key! */
151 IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
153 /* Try next name, if any */
154 channame = strtok( NULL, "," );
161 /* Remote server: we don't need to know whether the
162 * client is invited or not, but we have to make sure
163 * that the "one shot" entries (generated by INVITE
164 * commands) in this list become deleted when a user
165 * joins a channel this way. */
166 chan = Channel_Search( channame );
167 if( chan != NULL ) (void)Lists_CheckInvited( target, chan );
170 /* Channel joinen (und ggf. anlegen) */
171 if( ! Channel_Join( target, channame ))
173 /* naechsten Namen ermitteln */
174 channame = strtok( NULL, "," );
177 if( ! chan ) chan = Channel_Search( channame );
178 assert( chan != NULL );
180 /* Modes setzen (wenn vorhanden) */
181 while( flags && *flags )
183 Channel_UserModeAdd( chan, target, *flags );
187 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
188 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
190 /* Muessen Modes an andere Server gemeldet werden? */
191 strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
192 if( modes[1] ) modes[0] = 0x7;
193 else modes[0] = '\0';
195 /* An andere Server weiterleiten */
196 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
198 /* im Channel bekannt machen */
199 IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
202 /* Modes im Channel bekannt machen */
203 IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
206 if( Client_Type( Client ) == CLIENT_USER )
208 /* an Client bestaetigen */
209 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
211 /* Topic an Client schicken */
212 topic = Channel_Topic( chan );
213 if( *topic ) IRC_WriteStrClient( Client, RPL_TOPIC_MSG, Client_ID( Client ), channame, topic );
215 /* Mitglieder an Client Melden */
216 IRC_Send_NAMES( Client, chan );
217 IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
220 /* naechsten Namen ermitteln */
221 channame = strtok( NULL, "," );
228 IRC_PART( CLIENT *Client, REQUEST *Req )
233 assert( Client != NULL );
234 assert( Req != NULL );
236 /* Falsche Anzahl Parameter? */
237 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
239 /* Wer ist der Absender? */
240 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
241 else target = Client;
242 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
244 /* Channel-Namen durchgehen */
245 chan = strtok( Req->argv[0], "," );
248 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
250 /* naechsten Namen ermitteln */
251 chan = strtok( NULL, "," );
255 /* naechsten Namen ermitteln */
256 chan = strtok( NULL, "," );
263 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
269 assert( Client != NULL );
270 assert( Req != NULL );
272 /* Falsche Anzahl Parameter? */
273 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
275 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
277 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
279 /* Welcher Channel? */
280 chan = Channel_Search( Req->argv[0] );
281 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
283 /* Ist der User Mitglied in dem Channel? */
284 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
289 topic = Channel_Topic( chan );
290 if( *topic ) return IRC_WriteStrClient( from, RPL_TOPIC_MSG, Client_ID( from ), Channel_Name( chan ), topic );
291 else return IRC_WriteStrClient( from, RPL_NOTOPIC_MSG, Client_ID( from ), Channel_Name( chan ));
294 if( strchr( Channel_Modes( chan ), 't' ))
296 /* Topic Lock. Ist der User ein Channel Operator? */
297 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
301 Channel_SetTopic( chan, Req->argv[1] );
302 Log( LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s", Client_Mask( from ), Channel_Name( chan ), Req->argv[1][0] ? Req->argv[1] : "<none>" );
304 /* im Channel bekannt machen und an Server weiterleiten */
305 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
306 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
308 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
309 else return CONNECTED;
314 * Handler for the IRC "LIST" command.
315 * This implementation handles the local case as well as the forwarding of the
316 * LIST command to other servers in the IRC network.
319 IRC_LIST( CLIENT *Client, REQUEST *Req )
323 CLIENT *from, *target;
325 assert( Client != NULL );
326 assert( Req != NULL );
328 /* Bad number of prameters? */
330 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
331 Client_ID( Client ), Req->command );
334 pattern = strtok( Req->argv[0], "," );
338 /* Get sender from prefix, if any */
339 if( Client_Type( Client ) == CLIENT_SERVER )
340 from = Client_Search( Req->prefix );
345 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
346 Client_ID( Client ), Req->prefix );
350 /* Forward to other server? */
351 target = Client_Search( Req->argv[1] );
352 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
353 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
354 Client_ID( Client ), Req->argv[1] );
356 if( target != Client_ThisServer( ))
358 /* Target is indeed an other server, forward it! */
359 return IRC_WriteStrClientPrefix( target, from,
360 "LIST %s :%s", Client_ID( from ),
367 /* Loop through all the channels */
368 chan = Channel_First( );
371 /* Check search pattern */
372 if( Match( pattern, Channel_Name( chan )))
375 if( ! strchr( Channel_Modes( chan ), 's' ) ||
376 Channel_IsMemberOf( chan, from ))
378 if( ! IRC_WriteStrClient( from,
379 RPL_LIST_MSG, Client_ID( from ),
380 Channel_Name( chan ),
381 Channel_MemberCount( chan ),
382 Channel_Topic( chan )))
386 chan = Channel_Next( chan );
389 /* Get next name ... */
391 pattern = strtok( NULL, "," );
396 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
401 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
403 char modes_add[COMMAND_LEN], l[16], *ptr;
408 assert( Client != NULL );
409 assert( Req != NULL );
411 /* Bad number of parameters? */
412 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
414 /* Compatibility kludge */
415 if( Req->argc == 5 ) arg_topic = 4;
416 else if( Req->argc == 3 ) arg_topic = 2;
420 from = Client_Search( Req->prefix );
421 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
423 /* Search or create channel */
424 chan = Channel_Search( Req->argv[0] );
425 if( ! chan ) chan = Channel_Create( Req->argv[0] );
426 if( ! chan ) return CONNECTED;
428 if( Req->argv[1][0] == '+' )
430 ptr = Channel_Modes( chan );
433 /* OK, this channel doesn't have modes jet, set the received ones: */
434 Channel_SetModes( chan, &Req->argv[1][1] );
438 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
439 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
443 /* Delete modes which we never want to inherit */
444 Channel_ModeDel( chan, 'l' );
445 Channel_ModeDel( chan, 'k' );
448 strcpy( modes_add, "" );
449 ptr = Channel_Modes( chan );
454 snprintf( l, sizeof( l ), " %ld", Channel_MaxUsers( chan ));
455 strlcat( modes_add, l, sizeof( modes_add ));
459 strlcat( modes_add, " ", sizeof( modes_add ));
460 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
465 /* Inform members of this channel */
466 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
469 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
474 ptr = Channel_Topic( chan );
475 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
477 /* OK, there is no topic jet */
478 Channel_SetTopic( chan, Req->argv[arg_topic] );
479 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Channel_Topic( chan ));
483 /* Forward CHANINFO to other serevrs */
484 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] );
485 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
486 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );