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 2007/07/31 18:56:14 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 < 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 /* wird der Channel neu angelegt? */
83 if( Channel_Search( channame )) {
86 if (Conf_PredefChannelsOnly) { /* this server does not allow creation of channels */
87 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
88 /* Try next name, if any */
89 channame = strchr(channame, ',');
95 /* Hat ein Server Channel-User-Modes uebergeben? */
96 if( Client_Type( Client ) == CLIENT_SERVER )
98 /* Channel-Flags extrahieren */
99 flags = strchr( channame, 0x7 );
108 if( Client_Type( Client ) == CLIENT_USER )
110 /* Test if the user has reached his maximum channel count */
111 if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
112 return IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG,
113 Client_ID( Client ), channame );
115 /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
118 /* Erster User im Channel: Operator-Flag setzen */
123 /* Existierenden Channel suchen */
124 chan = Channel_Search( channame );
125 assert( chan != NULL );
127 is_banned = Lists_Check(Channel_GetListBans(chan), target );
128 is_invited = Lists_Check(Channel_GetListInvites(chan), target );
130 /* Testen, ob Client gebanned ist */
131 if(( is_banned == true) && ( is_invited == false ))
133 /* Client ist gebanned (und nicht invited): */
134 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
136 /* Try next name, if any */
137 channame = strchr(channame, ',');
141 /* Ist der Channel "invite-only"? */
142 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
144 /* Channel ist "invite-only" und Client wurde nicht invited: */
145 IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
147 /* Try next name, if any */
148 channame = strchr(channame, ',');
152 /* Is the channel protected by a key? */
153 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
155 /* Bad channel key! */
156 IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
158 /* Try next name, if any */
159 channame = strchr(channame, ',');
163 /* Are there already too many members? */
164 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
166 /* Bad channel key! */
167 IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
169 /* Try next name, if any */
170 channame = strchr(channame, ',');
177 /* Remote server: we don't need to know whether the
178 * client is invited or not, but we have to make sure
179 * that the "one shot" entries (generated by INVITE
180 * commands) in this list become deleted when a user
181 * joins a channel this way. */
182 chan = Channel_Search( channame );
183 if( chan != NULL ) (void)Lists_Check(Channel_GetListInvites(chan), target);
186 /* Channel joinen (und ggf. anlegen) */
187 if( ! Channel_Join( target, channame ))
189 /* naechsten Namen ermitteln */
190 channame = strchr(channame, ',');
193 if( ! chan ) chan = Channel_Search( channame );
194 assert( chan != NULL );
196 /* Modes setzen (wenn vorhanden) */
197 while( flags && *flags )
199 Channel_UserModeAdd( chan, target, *flags );
203 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
204 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
206 /* Muessen Modes an andere Server gemeldet werden? */
207 strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
208 if( modes[1] ) modes[0] = 0x7;
209 else modes[0] = '\0';
211 /* An andere Server weiterleiten */
212 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
214 /* im Channel bekannt machen */
215 IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
218 /* Modes im Channel bekannt machen */
219 IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
222 if( Client_Type( Client ) == CLIENT_USER )
224 /* an Client bestaetigen */
225 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
227 /* Send topic to client, if any */
228 topic = Channel_Topic(chan);
230 IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
231 Client_ID(Client), channame, topic);
233 IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
234 Client_ID(Client), channame,
235 Channel_TopicWho(chan),
236 Channel_TopicTime(chan));
240 /* Mitglieder an Client Melden */
241 IRC_Send_NAMES( Client, chan );
242 IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
246 channame = channame_ptr;
249 channame_ptr = strchr(channame, ',');
250 if (channame_ptr) *channame_ptr = '\0';
254 key_ptr = strchr(key, ',');
255 if (key_ptr) *key_ptr = '\0';
264 IRC_PART( CLIENT *Client, REQUEST *Req )
269 assert( Client != NULL );
270 assert( Req != NULL );
272 /* Falsche Anzahl Parameter? */
273 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
275 /* Wer ist der Absender? */
276 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
277 else target = Client;
278 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
280 /* Channel-Namen durchgehen */
281 chan = strtok( Req->argv[0], "," );
284 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
286 /* naechsten Namen ermitteln */
287 chan = strtok( NULL, "," );
291 /* naechsten Namen ermitteln */
292 chan = strtok( NULL, "," );
299 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
306 assert( Client != NULL );
307 assert( Req != NULL );
309 /* Falsche Anzahl Parameter? */
310 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
312 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
314 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
316 /* Welcher Channel? */
317 chan = Channel_Search( Req->argv[0] );
318 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
320 /* Ist der User Mitglied in dem Channel? */
321 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
325 /* Request actual topic */
326 topic = Channel_Topic(chan);
328 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
329 Client_ID(Client), Channel_Name(chan), topic);
331 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
332 Client_ID(Client), Channel_Name(chan),
333 Channel_TopicWho(chan),
334 Channel_TopicTime(chan));
339 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
340 Client_ID(from), Channel_Name(chan));
343 if( strchr( Channel_Modes( chan ), 't' ))
345 /* Topic Lock. Ist der User ein Channel Operator? */
346 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
350 Channel_SetTopic(chan, from, Req->argv[1]);
351 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
352 Client_Mask(from), Channel_Name(chan),
353 Req->argv[1][0] ? Req->argv[1] : "<none>");
355 /* im Channel bekannt machen und an Server weiterleiten */
356 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
357 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
359 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
360 else return CONNECTED;
365 * Handler for the IRC "LIST" command.
366 * This implementation handles the local case as well as the forwarding of the
367 * LIST command to other servers in the IRC network.
370 IRC_LIST( CLIENT *Client, REQUEST *Req )
374 CLIENT *from, *target;
376 assert( Client != NULL );
377 assert( Req != NULL );
379 /* Bad number of prameters? */
381 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
382 Client_ID( Client ), Req->command );
385 pattern = strtok( Req->argv[0], "," );
389 /* Get sender from prefix, if any */
390 if( Client_Type( Client ) == CLIENT_SERVER )
391 from = Client_Search( Req->prefix );
396 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
397 Client_ID( Client ), Req->prefix );
401 /* Forward to other server? */
402 target = Client_Search( Req->argv[1] );
403 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
404 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
405 Client_ID( Client ), Req->argv[1] );
407 if( target != Client_ThisServer( ))
409 /* Target is indeed an other server, forward it! */
410 return IRC_WriteStrClientPrefix( target, from,
411 "LIST %s :%s", Client_ID( from ),
418 /* Loop through all the channels */
419 chan = Channel_First( );
422 /* Check search pattern */
423 if( Match( pattern, Channel_Name( chan )))
426 if( ! strchr( Channel_Modes( chan ), 's' ) ||
427 Channel_IsMemberOf( chan, from ))
429 if( ! IRC_WriteStrClient( from,
430 RPL_LIST_MSG, Client_ID( from ),
431 Channel_Name( chan ),
432 Channel_MemberCount( chan ),
433 Channel_Topic( chan )))
437 chan = Channel_Next( chan );
440 /* Get next name ... */
442 pattern = strtok( NULL, "," );
447 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
452 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
454 char modes_add[COMMAND_LEN], l[16], *ptr;
459 assert( Client != NULL );
460 assert( Req != NULL );
462 /* Bad number of parameters? */
463 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
465 /* Compatibility kludge */
466 if( Req->argc == 5 ) arg_topic = 4;
467 else if( Req->argc == 3 ) arg_topic = 2;
471 from = Client_Search( Req->prefix );
472 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
474 /* Search or create channel */
475 chan = Channel_Search( Req->argv[0] );
476 if( ! chan ) chan = Channel_Create( Req->argv[0] );
477 if( ! chan ) return CONNECTED;
479 if( Req->argv[1][0] == '+' )
481 ptr = Channel_Modes( chan );
484 /* OK, this channel doesn't have modes jet, set the received ones: */
485 Channel_SetModes( chan, &Req->argv[1][1] );
489 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
490 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
494 /* Delete modes which we never want to inherit */
495 Channel_ModeDel( chan, 'l' );
496 Channel_ModeDel( chan, 'k' );
499 strcpy( modes_add, "" );
500 ptr = Channel_Modes( chan );
505 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
506 strlcat( modes_add, l, sizeof( modes_add ));
510 strlcat( modes_add, " ", sizeof( modes_add ));
511 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
516 /* Inform members of this channel */
517 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
520 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
525 ptr = Channel_Topic( chan );
526 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
528 /* OK, there is no topic jet */
529 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
530 IRC_WriteStrChannelPrefix(Client, chan, from, false,
531 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
535 /* Forward CHANINFO to other serevrs */
536 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] );
537 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
538 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );