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.32 2005/09/02 15:46:49 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 Log(LOG_INFO, "channame=%s, key=%s", channame, key ? key : "-");
79 chan = NULL; flags = NULL;
81 /* wird der Channel neu angelegt? */
82 if( Channel_Search( channame )) is_new_chan = false;
83 else is_new_chan = true;
85 /* Hat ein Server Channel-User-Modes uebergeben? */
86 if( Client_Type( Client ) == CLIENT_SERVER )
88 /* Channel-Flags extrahieren */
89 flags = strchr( channame, 0x7 );
98 if( Client_Type( Client ) == CLIENT_USER )
100 /* Test if the user has reached his maximum channel count */
101 if( Client_Type( Client ) == CLIENT_USER )
103 if(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
105 IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG, Client_ID( Client ), channame );
110 /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
113 /* Erster User im Channel: Operator-Flag setzen */
118 /* Existierenden Channel suchen */
119 chan = Channel_Search( channame );
120 assert( chan != NULL );
122 is_banned = Lists_CheckBanned( target, chan );
123 is_invited = Lists_CheckInvited( target, chan );
125 /* Testen, ob Client gebanned ist */
126 if(( is_banned == true) && ( is_invited == false ))
128 /* Client ist gebanned (und nicht invited): */
129 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
131 /* Try next name, if any */
132 channame = strtok( NULL, "," );
136 /* Ist der Channel "invite-only"? */
137 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
139 /* Channel ist "invite-only" und Client wurde nicht invited: */
140 IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
142 /* Try next name, if any */
143 channame = strtok( NULL, "," );
147 /* Is the channel protected by a key? */
148 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
150 /* Bad channel key! */
151 IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
153 /* Try next name, if any */
154 channame = strtok( NULL, "," );
158 /* Are there already too many members? */
159 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
161 /* Bad channel key! */
162 IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
164 /* Try next name, if any */
165 channame = strtok( NULL, "," );
172 /* Remote server: we don't need to know whether the
173 * client is invited or not, but we have to make sure
174 * that the "one shot" entries (generated by INVITE
175 * commands) in this list become deleted when a user
176 * joins a channel this way. */
177 chan = Channel_Search( channame );
178 if( chan != NULL ) (void)Lists_CheckInvited( target, chan );
181 /* Channel joinen (und ggf. anlegen) */
182 if( ! Channel_Join( target, channame ))
184 /* naechsten Namen ermitteln */
185 channame = strtok( NULL, "," );
188 if( ! chan ) chan = Channel_Search( channame );
189 assert( chan != NULL );
191 /* Modes setzen (wenn vorhanden) */
192 while( flags && *flags )
194 Channel_UserModeAdd( chan, target, *flags );
198 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
199 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
201 /* Muessen Modes an andere Server gemeldet werden? */
202 strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
203 if( modes[1] ) modes[0] = 0x7;
204 else modes[0] = '\0';
206 /* An andere Server weiterleiten */
207 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
209 /* im Channel bekannt machen */
210 IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
213 /* Modes im Channel bekannt machen */
214 IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
217 if( Client_Type( Client ) == CLIENT_USER )
219 /* an Client bestaetigen */
220 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
222 /* Send topic to client, if any */
223 topic = Channel_Topic(chan);
225 IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
226 Client_ID(Client), channame, topic);
228 IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
229 Client_ID(Client), channame,
230 Channel_TopicWho(chan),
231 Channel_TopicTime(chan));
235 /* Mitglieder an Client Melden */
236 IRC_Send_NAMES( Client, chan );
237 IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
241 channame = channame_ptr;
244 channame_ptr = strchr(channame, ',');
245 if (channame_ptr) *channame_ptr = '\0';
249 key_ptr = strchr(key, ',');
250 if (key_ptr) *key_ptr = '\0';
259 IRC_PART( CLIENT *Client, REQUEST *Req )
264 assert( Client != NULL );
265 assert( Req != NULL );
267 /* Falsche Anzahl Parameter? */
268 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
270 /* Wer ist der Absender? */
271 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
272 else target = Client;
273 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
275 /* Channel-Namen durchgehen */
276 chan = strtok( Req->argv[0], "," );
279 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
281 /* naechsten Namen ermitteln */
282 chan = strtok( NULL, "," );
286 /* naechsten Namen ermitteln */
287 chan = strtok( NULL, "," );
294 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
301 assert( Client != NULL );
302 assert( Req != NULL );
304 /* Falsche Anzahl Parameter? */
305 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
307 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
309 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
311 /* Welcher Channel? */
312 chan = Channel_Search( Req->argv[0] );
313 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
315 /* Ist der User Mitglied in dem Channel? */
316 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
320 /* Request actual topic */
321 topic = Channel_Topic(chan);
323 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
324 Client_ID(Client), Channel_Name(chan), topic);
326 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
327 Client_ID(Client), Channel_Name(chan),
328 Channel_TopicWho(chan),
329 Channel_TopicTime(chan));
334 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
335 Client_ID(from), Channel_Name(chan));
338 if( strchr( Channel_Modes( chan ), 't' ))
340 /* Topic Lock. Ist der User ein Channel Operator? */
341 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
345 Channel_SetTopic(chan, from, Req->argv[1]);
346 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
347 Client_Mask(from), Channel_Name(chan),
348 Req->argv[1][0] ? Req->argv[1] : "<none>");
350 /* im Channel bekannt machen und an Server weiterleiten */
351 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
352 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
354 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
355 else return CONNECTED;
360 * Handler for the IRC "LIST" command.
361 * This implementation handles the local case as well as the forwarding of the
362 * LIST command to other servers in the IRC network.
365 IRC_LIST( CLIENT *Client, REQUEST *Req )
369 CLIENT *from, *target;
371 assert( Client != NULL );
372 assert( Req != NULL );
374 /* Bad number of prameters? */
376 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
377 Client_ID( Client ), Req->command );
380 pattern = strtok( Req->argv[0], "," );
384 /* Get sender from prefix, if any */
385 if( Client_Type( Client ) == CLIENT_SERVER )
386 from = Client_Search( Req->prefix );
391 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
392 Client_ID( Client ), Req->prefix );
396 /* Forward to other server? */
397 target = Client_Search( Req->argv[1] );
398 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
399 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
400 Client_ID( Client ), Req->argv[1] );
402 if( target != Client_ThisServer( ))
404 /* Target is indeed an other server, forward it! */
405 return IRC_WriteStrClientPrefix( target, from,
406 "LIST %s :%s", Client_ID( from ),
413 /* Loop through all the channels */
414 chan = Channel_First( );
417 /* Check search pattern */
418 if( Match( pattern, Channel_Name( chan )))
421 if( ! strchr( Channel_Modes( chan ), 's' ) ||
422 Channel_IsMemberOf( chan, from ))
424 if( ! IRC_WriteStrClient( from,
425 RPL_LIST_MSG, Client_ID( from ),
426 Channel_Name( chan ),
427 Channel_MemberCount( chan ),
428 Channel_Topic( chan )))
432 chan = Channel_Next( chan );
435 /* Get next name ... */
437 pattern = strtok( NULL, "," );
442 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
447 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
449 char modes_add[COMMAND_LEN], l[16], *ptr;
454 assert( Client != NULL );
455 assert( Req != NULL );
457 /* Bad number of parameters? */
458 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
460 /* Compatibility kludge */
461 if( Req->argc == 5 ) arg_topic = 4;
462 else if( Req->argc == 3 ) arg_topic = 2;
466 from = Client_Search( Req->prefix );
467 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
469 /* Search or create channel */
470 chan = Channel_Search( Req->argv[0] );
471 if( ! chan ) chan = Channel_Create( Req->argv[0] );
472 if( ! chan ) return CONNECTED;
474 if( Req->argv[1][0] == '+' )
476 ptr = Channel_Modes( chan );
479 /* OK, this channel doesn't have modes jet, set the received ones: */
480 Channel_SetModes( chan, &Req->argv[1][1] );
484 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
485 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
489 /* Delete modes which we never want to inherit */
490 Channel_ModeDel( chan, 'l' );
491 Channel_ModeDel( chan, 'k' );
494 strcpy( modes_add, "" );
495 ptr = Channel_Modes( chan );
500 snprintf( l, sizeof( l ), " %ld", Channel_MaxUsers( chan ));
501 strlcat( modes_add, l, sizeof( modes_add ));
505 strlcat( modes_add, " ", sizeof( modes_add ));
506 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
511 /* Inform members of this channel */
512 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
515 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
520 ptr = Channel_Topic( chan );
521 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
523 /* OK, there is no topic jet */
524 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
525 IRC_WriteStrChannelPrefix(Client, chan, from, false,
526 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
530 /* Forward CHANINFO to other serevrs */
531 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] );
532 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
533 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );