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.40.2.2 2008/02/26 12:07:41 fw 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 < 1 || Req->argc > 2)
56 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
57 Client_ID(Client), Req->command);
59 /* Who is the sender? */
60 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
62 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
64 /* Are channel keys given? */
67 key_ptr = strchr(key, ',');
68 if (key_ptr) *key_ptr = '\0';
73 channame = Req->argv[0];
74 channame_ptr = strchr(channame, ',');
75 if (channame_ptr) *channame_ptr = '\0';
77 /* Channel-Namen durchgehen */
80 chan = NULL; flags = NULL;
82 if (Client_Type(Client) == CLIENT_SERVER) {
83 flags = strchr( channame, 0x7 );
90 /* wird der Channel neu angelegt? */
91 if( Channel_Search( channame )) {
94 if (Conf_PredefChannelsOnly) { /* this server does not allow creation of channels */
95 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
96 /* Try next name, if any */
97 channame = strchr(channame, ',');
104 if( Client_Type( Client ) == CLIENT_USER )
106 /* Test if the user has reached his maximum channel count */
107 if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
108 return IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG,
109 Client_ID( Client ), channame );
111 /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
114 /* Erster User im Channel: Operator-Flag setzen */
119 /* Existierenden Channel suchen */
120 chan = Channel_Search( channame );
121 assert( chan != NULL );
123 is_banned = Lists_Check(Channel_GetListBans(chan), target );
124 is_invited = Lists_Check(Channel_GetListInvites(chan), target );
126 /* Testen, ob Client gebanned ist */
127 if(( is_banned == true) && ( is_invited == false ))
129 /* Client ist gebanned (und nicht invited): */
130 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
132 /* Try next name, if any */
133 channame = strchr(channame, ',');
137 /* Ist der Channel "invite-only"? */
138 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
140 /* Channel ist "invite-only" und Client wurde nicht invited: */
141 IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
143 /* Try next name, if any */
144 channame = strchr(channame, ',');
148 /* Is the channel protected by a key? */
149 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
151 /* Bad channel key! */
152 IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
154 /* Try next name, if any */
155 channame = strchr(channame, ',');
159 /* Are there already too many members? */
160 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
162 /* Bad channel key! */
163 IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
165 /* Try next name, if any */
166 channame = strchr(channame, ',');
173 /* Remote server: we don't need to know whether the
174 * client is invited or not, but we have to make sure
175 * that the "one shot" entries (generated by INVITE
176 * commands) in this list become deleted when a user
177 * joins a channel this way. */
178 chan = Channel_Search( channame );
179 if( chan != NULL ) (void)Lists_Check(Channel_GetListInvites(chan), target);
182 /* Channel joinen (und ggf. anlegen) */
183 if( ! Channel_Join( target, channame ))
185 /* naechsten Namen ermitteln */
186 channame = strchr(channame, ',');
189 if( ! chan ) chan = Channel_Search( channame );
190 assert( chan != NULL );
192 /* Modes setzen (wenn vorhanden) */
193 while( flags && *flags )
195 Channel_UserModeAdd( chan, target, *flags );
199 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
200 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
202 /* Muessen Modes an andere Server gemeldet werden? */
203 strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
204 if( modes[1] ) modes[0] = 0x7;
205 else modes[0] = '\0';
207 /* An andere Server weiterleiten */
208 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
210 /* im Channel bekannt machen */
211 IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
214 /* Modes im Channel bekannt machen */
215 IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
218 if( Client_Type( Client ) == CLIENT_USER )
220 /* an Client bestaetigen */
221 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
223 /* Send topic to client, if any */
224 topic = Channel_Topic(chan);
226 IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
227 Client_ID(Client), channame, topic);
229 IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
230 Client_ID(Client), channame,
231 Channel_TopicWho(chan),
232 Channel_TopicTime(chan));
236 /* Mitglieder an Client Melden */
237 IRC_Send_NAMES( Client, chan );
238 IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
242 channame = channame_ptr;
245 channame_ptr = strchr(channame, ',');
246 if (channame_ptr) *channame_ptr = '\0';
250 key_ptr = strchr(key, ',');
251 if (key_ptr) *key_ptr = '\0';
260 IRC_PART( CLIENT *Client, REQUEST *Req )
265 assert( Client != NULL );
266 assert( Req != NULL );
268 if (Req->argc < 1 || Req->argc > 2)
269 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
270 Client_ID(Client), Req->command);
272 /* Wer ist der Absender? */
273 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
274 else target = Client;
275 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
277 /* Channel-Namen durchgehen */
278 chan = strtok(Req->argv[0], ",");
280 Channel_Part(target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID(target));
282 chan = strtok(NULL, ",");
289 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
296 assert( Client != NULL );
297 assert( Req != NULL );
299 /* Falsche Anzahl Parameter? */
300 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
302 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
304 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
306 /* Welcher Channel? */
307 chan = Channel_Search( Req->argv[0] );
308 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
310 /* Ist der User Mitglied in dem Channel? */
311 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
315 /* Request actual topic */
316 topic = Channel_Topic(chan);
318 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
319 Client_ID(Client), Channel_Name(chan), topic);
321 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
322 Client_ID(Client), Channel_Name(chan),
323 Channel_TopicWho(chan),
324 Channel_TopicTime(chan));
329 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
330 Client_ID(from), Channel_Name(chan));
333 if( strchr( Channel_Modes( chan ), 't' ))
335 /* Topic Lock. Ist der User ein Channel Operator? */
336 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
340 Channel_SetTopic(chan, from, Req->argv[1]);
341 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
342 Client_Mask(from), Channel_Name(chan),
343 Req->argv[1][0] ? Req->argv[1] : "<none>");
345 /* im Channel bekannt machen und an Server weiterleiten */
346 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
347 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
349 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
350 else return CONNECTED;
355 * Handler for the IRC "LIST" command.
356 * This implementation handles the local case as well as the forwarding of the
357 * LIST command to other servers in the IRC network.
360 IRC_LIST( CLIENT *Client, REQUEST *Req )
364 CLIENT *from, *target;
366 assert( Client != NULL );
367 assert( Req != NULL );
369 /* Bad number of prameters? */
371 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
372 Client_ID( Client ), Req->command );
375 pattern = strtok( Req->argv[0], "," );
379 /* Get sender from prefix, if any */
380 if( Client_Type( Client ) == CLIENT_SERVER )
381 from = Client_Search( Req->prefix );
386 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
387 Client_ID( Client ), Req->prefix );
391 /* Forward to other server? */
392 target = Client_Search( Req->argv[1] );
393 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
394 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
395 Client_ID( Client ), Req->argv[1] );
397 if( target != Client_ThisServer( ))
399 /* Target is indeed an other server, forward it! */
400 return IRC_WriteStrClientPrefix( target, from,
401 "LIST %s :%s", Client_ID( from ),
408 /* Loop through all the channels */
409 chan = Channel_First( );
412 /* Check search pattern */
413 if( Match( pattern, Channel_Name( chan )))
416 if( ! strchr( Channel_Modes( chan ), 's' ) ||
417 Channel_IsMemberOf( chan, from ))
419 if( ! IRC_WriteStrClient( from,
420 RPL_LIST_MSG, Client_ID( from ),
421 Channel_Name( chan ),
422 Channel_MemberCount( chan ),
423 Channel_Topic( chan )))
427 chan = Channel_Next( chan );
430 /* Get next name ... */
432 pattern = strtok( NULL, "," );
437 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
442 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
444 char modes_add[COMMAND_LEN], l[16], *ptr;
449 assert( Client != NULL );
450 assert( Req != NULL );
452 /* Bad number of parameters? */
453 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
455 /* Compatibility kludge */
456 if( Req->argc == 5 ) arg_topic = 4;
457 else if( Req->argc == 3 ) arg_topic = 2;
461 from = Client_Search( Req->prefix );
462 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
464 /* Search or create channel */
465 chan = Channel_Search( Req->argv[0] );
466 if( ! chan ) chan = Channel_Create( Req->argv[0] );
467 if( ! chan ) return CONNECTED;
469 if( Req->argv[1][0] == '+' )
471 ptr = Channel_Modes( chan );
474 /* OK, this channel doesn't have modes jet, set the received ones: */
475 Channel_SetModes( chan, &Req->argv[1][1] );
479 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
480 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
484 /* Delete modes which we never want to inherit */
485 Channel_ModeDel( chan, 'l' );
486 Channel_ModeDel( chan, 'k' );
489 strcpy( modes_add, "" );
490 ptr = Channel_Modes( chan );
495 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
496 strlcat( modes_add, l, sizeof( modes_add ));
500 strlcat( modes_add, " ", sizeof( modes_add ));
501 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
506 /* Inform members of this channel */
507 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
510 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
515 ptr = Channel_Topic( chan );
516 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
518 /* OK, there is no topic jet */
519 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
520 IRC_WriteStrChannelPrefix(Client, chan, from, false,
521 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
525 /* Forward CHANINFO to other serevrs */
526 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] );
527 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
528 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );