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.33 2005/09/05 08:11:11 alex Exp $";
35 #include "irc-write.h"
40 #include "irc-channel.h"
44 IRC_JOIN( CLIENT *Client, REQUEST *Req )
46 char *channame, *channame_ptr, *key, *key_ptr, *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? */
65 key_ptr = strchr(key, ',');
66 if (key_ptr) *key_ptr = '\0';
71 channame = Req->argv[0];
72 channame_ptr = strchr(channame, ',');
73 if (channame_ptr) *channame_ptr = '\0';
75 /* Channel-Namen durchgehen */
78 chan = NULL; flags = NULL;
80 /* wird der Channel neu angelegt? */
81 if( Channel_Search( channame )) is_new_chan = false;
82 else is_new_chan = true;
84 /* Hat ein Server Channel-User-Modes uebergeben? */
85 if( Client_Type( Client ) == CLIENT_SERVER )
87 /* Channel-Flags extrahieren */
88 flags = strchr( channame, 0x7 );
97 if( Client_Type( Client ) == CLIENT_USER )
99 /* Test if the user has reached his maximum channel count */
100 if( Client_Type( Client ) == CLIENT_USER )
102 if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
104 IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG, Client_ID( Client ), channame );
109 /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
112 /* Erster User im Channel: Operator-Flag setzen */
117 /* Existierenden Channel suchen */
118 chan = Channel_Search( channame );
119 assert( chan != NULL );
121 is_banned = Lists_CheckBanned( target, chan );
122 is_invited = Lists_CheckInvited( target, chan );
124 /* Testen, ob Client gebanned ist */
125 if(( is_banned == true) && ( is_invited == false ))
127 /* Client ist gebanned (und nicht invited): */
128 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
130 /* Try next name, if any */
131 channame = strtok( NULL, "," );
135 /* Ist der Channel "invite-only"? */
136 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
138 /* Channel ist "invite-only" und Client wurde nicht invited: */
139 IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
141 /* Try next name, if any */
142 channame = strtok( NULL, "," );
146 /* Is the channel protected by a key? */
147 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
149 /* Bad channel key! */
150 IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
152 /* Try next name, if any */
153 channame = strtok( NULL, "," );
157 /* Are there already too many members? */
158 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
160 /* Bad channel key! */
161 IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
163 /* Try next name, if any */
164 channame = strtok( NULL, "," );
171 /* Remote server: we don't need to know whether the
172 * client is invited or not, but we have to make sure
173 * that the "one shot" entries (generated by INVITE
174 * commands) in this list become deleted when a user
175 * joins a channel this way. */
176 chan = Channel_Search( channame );
177 if( chan != NULL ) (void)Lists_CheckInvited( target, chan );
180 /* Channel joinen (und ggf. anlegen) */
181 if( ! Channel_Join( target, channame ))
183 /* naechsten Namen ermitteln */
184 channame = strtok( NULL, "," );
187 if( ! chan ) chan = Channel_Search( channame );
188 assert( chan != NULL );
190 /* Modes setzen (wenn vorhanden) */
191 while( flags && *flags )
193 Channel_UserModeAdd( chan, target, *flags );
197 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
198 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
200 /* Muessen Modes an andere Server gemeldet werden? */
201 strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
202 if( modes[1] ) modes[0] = 0x7;
203 else modes[0] = '\0';
205 /* An andere Server weiterleiten */
206 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
208 /* im Channel bekannt machen */
209 IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
212 /* Modes im Channel bekannt machen */
213 IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
216 if( Client_Type( Client ) == CLIENT_USER )
218 /* an Client bestaetigen */
219 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
221 /* Send topic to client, if any */
222 topic = Channel_Topic(chan);
224 IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
225 Client_ID(Client), channame, topic);
227 IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
228 Client_ID(Client), channame,
229 Channel_TopicWho(chan),
230 Channel_TopicTime(chan));
234 /* Mitglieder an Client Melden */
235 IRC_Send_NAMES( Client, chan );
236 IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
240 channame = channame_ptr;
243 channame_ptr = strchr(channame, ',');
244 if (channame_ptr) *channame_ptr = '\0';
248 key_ptr = strchr(key, ',');
249 if (key_ptr) *key_ptr = '\0';
258 IRC_PART( CLIENT *Client, REQUEST *Req )
263 assert( Client != NULL );
264 assert( Req != NULL );
266 /* Falsche Anzahl Parameter? */
267 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
269 /* Wer ist der Absender? */
270 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
271 else target = Client;
272 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
274 /* Channel-Namen durchgehen */
275 chan = strtok( Req->argv[0], "," );
278 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
280 /* naechsten Namen ermitteln */
281 chan = strtok( NULL, "," );
285 /* naechsten Namen ermitteln */
286 chan = strtok( NULL, "," );
293 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
300 assert( Client != NULL );
301 assert( Req != NULL );
303 /* Falsche Anzahl Parameter? */
304 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
306 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
308 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
310 /* Welcher Channel? */
311 chan = Channel_Search( Req->argv[0] );
312 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
314 /* Ist der User Mitglied in dem Channel? */
315 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
319 /* Request actual topic */
320 topic = Channel_Topic(chan);
322 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
323 Client_ID(Client), Channel_Name(chan), topic);
325 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
326 Client_ID(Client), Channel_Name(chan),
327 Channel_TopicWho(chan),
328 Channel_TopicTime(chan));
333 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
334 Client_ID(from), Channel_Name(chan));
337 if( strchr( Channel_Modes( chan ), 't' ))
339 /* Topic Lock. Ist der User ein Channel Operator? */
340 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
344 Channel_SetTopic(chan, from, Req->argv[1]);
345 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
346 Client_Mask(from), Channel_Name(chan),
347 Req->argv[1][0] ? Req->argv[1] : "<none>");
349 /* im Channel bekannt machen und an Server weiterleiten */
350 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
351 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
353 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
354 else return CONNECTED;
359 * Handler for the IRC "LIST" command.
360 * This implementation handles the local case as well as the forwarding of the
361 * LIST command to other servers in the IRC network.
364 IRC_LIST( CLIENT *Client, REQUEST *Req )
368 CLIENT *from, *target;
370 assert( Client != NULL );
371 assert( Req != NULL );
373 /* Bad number of prameters? */
375 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
376 Client_ID( Client ), Req->command );
379 pattern = strtok( Req->argv[0], "," );
383 /* Get sender from prefix, if any */
384 if( Client_Type( Client ) == CLIENT_SERVER )
385 from = Client_Search( Req->prefix );
390 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
391 Client_ID( Client ), Req->prefix );
395 /* Forward to other server? */
396 target = Client_Search( Req->argv[1] );
397 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
398 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
399 Client_ID( Client ), Req->argv[1] );
401 if( target != Client_ThisServer( ))
403 /* Target is indeed an other server, forward it! */
404 return IRC_WriteStrClientPrefix( target, from,
405 "LIST %s :%s", Client_ID( from ),
412 /* Loop through all the channels */
413 chan = Channel_First( );
416 /* Check search pattern */
417 if( Match( pattern, Channel_Name( chan )))
420 if( ! strchr( Channel_Modes( chan ), 's' ) ||
421 Channel_IsMemberOf( chan, from ))
423 if( ! IRC_WriteStrClient( from,
424 RPL_LIST_MSG, Client_ID( from ),
425 Channel_Name( chan ),
426 Channel_MemberCount( chan ),
427 Channel_Topic( chan )))
431 chan = Channel_Next( chan );
434 /* Get next name ... */
436 pattern = strtok( NULL, "," );
441 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
446 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
448 char modes_add[COMMAND_LEN], l[16], *ptr;
453 assert( Client != NULL );
454 assert( Req != NULL );
456 /* Bad number of parameters? */
457 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
459 /* Compatibility kludge */
460 if( Req->argc == 5 ) arg_topic = 4;
461 else if( Req->argc == 3 ) arg_topic = 2;
465 from = Client_Search( Req->prefix );
466 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
468 /* Search or create channel */
469 chan = Channel_Search( Req->argv[0] );
470 if( ! chan ) chan = Channel_Create( Req->argv[0] );
471 if( ! chan ) return CONNECTED;
473 if( Req->argv[1][0] == '+' )
475 ptr = Channel_Modes( chan );
478 /* OK, this channel doesn't have modes jet, set the received ones: */
479 Channel_SetModes( chan, &Req->argv[1][1] );
483 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
484 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
488 /* Delete modes which we never want to inherit */
489 Channel_ModeDel( chan, 'l' );
490 Channel_ModeDel( chan, 'k' );
493 strcpy( modes_add, "" );
494 ptr = Channel_Modes( chan );
499 snprintf( l, sizeof( l ), " %ld", Channel_MaxUsers( chan ));
500 strlcat( modes_add, l, sizeof( modes_add ));
504 strlcat( modes_add, " ", sizeof( modes_add ));
505 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
510 /* Inform members of this channel */
511 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
514 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
519 ptr = Channel_Topic( chan );
520 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
522 /* OK, there is no topic jet */
523 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
524 IRC_WriteStrChannelPrefix(Client, chan, from, false,
525 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
529 /* Forward CHANINFO to other serevrs */
530 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] );
531 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
532 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );