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.31 2005/09/02 12:50:25 alex 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( Client_Type( Client ) == CLIENT_USER )
95 if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
97 IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG, Client_ID( Client ), channame );
102 /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
105 /* Erster User im Channel: Operator-Flag setzen */
110 /* Existierenden Channel suchen */
111 chan = Channel_Search( channame );
112 assert( chan != NULL );
114 is_banned = Lists_CheckBanned( target, chan );
115 is_invited = Lists_CheckInvited( target, chan );
117 /* Testen, ob Client gebanned ist */
118 if(( is_banned == true) && ( is_invited == false ))
120 /* Client ist gebanned (und nicht invited): */
121 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
123 /* Try next name, if any */
124 channame = strtok( NULL, "," );
128 /* Ist der Channel "invite-only"? */
129 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
131 /* Channel ist "invite-only" und Client wurde nicht invited: */
132 IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
134 /* Try next name, if any */
135 channame = strtok( NULL, "," );
139 /* Is the channel protected by a key? */
140 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
142 /* Bad channel key! */
143 IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
145 /* Try next name, if any */
146 channame = strtok( NULL, "," );
150 /* Are there already too many members? */
151 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
153 /* Bad channel key! */
154 IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
156 /* Try next name, if any */
157 channame = strtok( NULL, "," );
164 /* Remote server: we don't need to know whether the
165 * client is invited or not, but we have to make sure
166 * that the "one shot" entries (generated by INVITE
167 * commands) in this list become deleted when a user
168 * joins a channel this way. */
169 chan = Channel_Search( channame );
170 if( chan != NULL ) (void)Lists_CheckInvited( target, chan );
173 /* Channel joinen (und ggf. anlegen) */
174 if( ! Channel_Join( target, channame ))
176 /* naechsten Namen ermitteln */
177 channame = strtok( NULL, "," );
180 if( ! chan ) chan = Channel_Search( channame );
181 assert( chan != NULL );
183 /* Modes setzen (wenn vorhanden) */
184 while( flags && *flags )
186 Channel_UserModeAdd( chan, target, *flags );
190 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
191 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
193 /* Muessen Modes an andere Server gemeldet werden? */
194 strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
195 if( modes[1] ) modes[0] = 0x7;
196 else modes[0] = '\0';
198 /* An andere Server weiterleiten */
199 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
201 /* im Channel bekannt machen */
202 IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
205 /* Modes im Channel bekannt machen */
206 IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
209 if( Client_Type( Client ) == CLIENT_USER )
211 /* an Client bestaetigen */
212 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
214 /* Send topic to client, if any */
215 topic = Channel_Topic(chan);
217 IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
218 Client_ID(Client), channame, topic);
220 IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
221 Client_ID(Client), channame,
222 Channel_TopicWho(chan),
223 Channel_TopicTime(chan));
227 /* Mitglieder an Client Melden */
228 IRC_Send_NAMES( Client, chan );
229 IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
232 /* naechsten Namen ermitteln */
233 channame = strtok( NULL, "," );
240 IRC_PART( CLIENT *Client, REQUEST *Req )
245 assert( Client != NULL );
246 assert( Req != NULL );
248 /* Falsche Anzahl Parameter? */
249 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
251 /* Wer ist der Absender? */
252 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
253 else target = Client;
254 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
256 /* Channel-Namen durchgehen */
257 chan = strtok( Req->argv[0], "," );
260 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
262 /* naechsten Namen ermitteln */
263 chan = strtok( NULL, "," );
267 /* naechsten Namen ermitteln */
268 chan = strtok( NULL, "," );
275 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
282 assert( Client != NULL );
283 assert( Req != NULL );
285 /* Falsche Anzahl Parameter? */
286 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
288 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
290 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
292 /* Welcher Channel? */
293 chan = Channel_Search( Req->argv[0] );
294 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
296 /* Ist der User Mitglied in dem Channel? */
297 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
301 /* Request actual topic */
302 topic = Channel_Topic(chan);
304 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
305 Client_ID(Client), Channel_Name(chan), topic);
307 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
308 Client_ID(Client), Channel_Name(chan),
309 Channel_TopicWho(chan),
310 Channel_TopicTime(chan));
315 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
316 Client_ID(from), Channel_Name(chan));
319 if( strchr( Channel_Modes( chan ), 't' ))
321 /* Topic Lock. Ist der User ein Channel Operator? */
322 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
326 Channel_SetTopic(chan, from, Req->argv[1]);
327 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
328 Client_Mask(from), Channel_Name(chan),
329 Req->argv[1][0] ? Req->argv[1] : "<none>");
331 /* im Channel bekannt machen und an Server weiterleiten */
332 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
333 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
335 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
336 else return CONNECTED;
341 * Handler for the IRC "LIST" command.
342 * This implementation handles the local case as well as the forwarding of the
343 * LIST command to other servers in the IRC network.
346 IRC_LIST( CLIENT *Client, REQUEST *Req )
350 CLIENT *from, *target;
352 assert( Client != NULL );
353 assert( Req != NULL );
355 /* Bad number of prameters? */
357 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
358 Client_ID( Client ), Req->command );
361 pattern = strtok( Req->argv[0], "," );
365 /* Get sender from prefix, if any */
366 if( Client_Type( Client ) == CLIENT_SERVER )
367 from = Client_Search( Req->prefix );
372 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
373 Client_ID( Client ), Req->prefix );
377 /* Forward to other server? */
378 target = Client_Search( Req->argv[1] );
379 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
380 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
381 Client_ID( Client ), Req->argv[1] );
383 if( target != Client_ThisServer( ))
385 /* Target is indeed an other server, forward it! */
386 return IRC_WriteStrClientPrefix( target, from,
387 "LIST %s :%s", Client_ID( from ),
394 /* Loop through all the channels */
395 chan = Channel_First( );
398 /* Check search pattern */
399 if( Match( pattern, Channel_Name( chan )))
402 if( ! strchr( Channel_Modes( chan ), 's' ) ||
403 Channel_IsMemberOf( chan, from ))
405 if( ! IRC_WriteStrClient( from,
406 RPL_LIST_MSG, Client_ID( from ),
407 Channel_Name( chan ),
408 Channel_MemberCount( chan ),
409 Channel_Topic( chan )))
413 chan = Channel_Next( chan );
416 /* Get next name ... */
418 pattern = strtok( NULL, "," );
423 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
428 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
430 char modes_add[COMMAND_LEN], l[16], *ptr;
435 assert( Client != NULL );
436 assert( Req != NULL );
438 /* Bad number of parameters? */
439 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
441 /* Compatibility kludge */
442 if( Req->argc == 5 ) arg_topic = 4;
443 else if( Req->argc == 3 ) arg_topic = 2;
447 from = Client_Search( Req->prefix );
448 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
450 /* Search or create channel */
451 chan = Channel_Search( Req->argv[0] );
452 if( ! chan ) chan = Channel_Create( Req->argv[0] );
453 if( ! chan ) return CONNECTED;
455 if( Req->argv[1][0] == '+' )
457 ptr = Channel_Modes( chan );
460 /* OK, this channel doesn't have modes jet, set the received ones: */
461 Channel_SetModes( chan, &Req->argv[1][1] );
465 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
466 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
470 /* Delete modes which we never want to inherit */
471 Channel_ModeDel( chan, 'l' );
472 Channel_ModeDel( chan, 'k' );
475 strcpy( modes_add, "" );
476 ptr = Channel_Modes( chan );
481 snprintf( l, sizeof( l ), " %ld", Channel_MaxUsers( chan ));
482 strlcat( modes_add, l, sizeof( modes_add ));
486 strlcat( modes_add, " ", sizeof( modes_add ));
487 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
492 /* Inform members of this channel */
493 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
496 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
501 ptr = Channel_Topic( chan );
502 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
504 /* OK, there is no topic jet */
505 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
506 IRC_WriteStrChannelPrefix(Client, chan, from, false,
507 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
511 /* Forward CHANINFO to other serevrs */
512 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] );
513 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
514 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );