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.36 2006/10/05 18:26:54 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 > 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(( Conf_MaxJoins > 0 ) && ( Channel_CountForUser( Client ) >= Conf_MaxJoins ))
101 return IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG,
102 Client_ID( Client ), channame );
104 /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
107 /* Erster User im Channel: Operator-Flag setzen */
112 /* Existierenden Channel suchen */
113 chan = Channel_Search( channame );
114 assert( chan != NULL );
116 is_banned = Lists_CheckBanned( target, chan );
117 is_invited = Lists_CheckInvited( target, chan );
119 /* Testen, ob Client gebanned ist */
120 if(( is_banned == true) && ( is_invited == false ))
122 /* Client ist gebanned (und nicht invited): */
123 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
125 /* Try next name, if any */
126 channame = strchr(channame, ',');
130 /* Ist der Channel "invite-only"? */
131 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
133 /* Channel ist "invite-only" und Client wurde nicht invited: */
134 IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
136 /* Try next name, if any */
137 channame = strchr(channame, ',');
141 /* Is the channel protected by a key? */
142 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
144 /* Bad channel key! */
145 IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
147 /* Try next name, if any */
148 channame = strchr(channame, ',');
152 /* Are there already too many members? */
153 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
155 /* Bad channel key! */
156 IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
158 /* Try next name, if any */
159 channame = strchr(channame, ',');
166 /* Remote server: we don't need to know whether the
167 * client is invited or not, but we have to make sure
168 * that the "one shot" entries (generated by INVITE
169 * commands) in this list become deleted when a user
170 * joins a channel this way. */
171 chan = Channel_Search( channame );
172 if( chan != NULL ) (void)Lists_CheckInvited( target, chan );
175 /* Channel joinen (und ggf. anlegen) */
176 if( ! Channel_Join( target, channame ))
178 /* naechsten Namen ermitteln */
179 channame = strchr(channame, ',');
182 if( ! chan ) chan = Channel_Search( channame );
183 assert( chan != NULL );
185 /* Modes setzen (wenn vorhanden) */
186 while( flags && *flags )
188 Channel_UserModeAdd( chan, target, *flags );
192 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
193 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
195 /* Muessen Modes an andere Server gemeldet werden? */
196 strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
197 if( modes[1] ) modes[0] = 0x7;
198 else modes[0] = '\0';
200 /* An andere Server weiterleiten */
201 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
203 /* im Channel bekannt machen */
204 IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
207 /* Modes im Channel bekannt machen */
208 IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
211 if( Client_Type( Client ) == CLIENT_USER )
213 /* an Client bestaetigen */
214 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
216 /* Send topic to client, if any */
217 topic = Channel_Topic(chan);
219 IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
220 Client_ID(Client), channame, topic);
222 IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
223 Client_ID(Client), channame,
224 Channel_TopicWho(chan),
225 Channel_TopicTime(chan));
229 /* Mitglieder an Client Melden */
230 IRC_Send_NAMES( Client, chan );
231 IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
235 channame = channame_ptr;
238 channame_ptr = strchr(channame, ',');
239 if (channame_ptr) *channame_ptr = '\0';
243 key_ptr = strchr(key, ',');
244 if (key_ptr) *key_ptr = '\0';
253 IRC_PART( CLIENT *Client, REQUEST *Req )
258 assert( Client != NULL );
259 assert( Req != NULL );
261 /* Falsche Anzahl Parameter? */
262 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
264 /* Wer ist der Absender? */
265 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
266 else target = Client;
267 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
269 /* Channel-Namen durchgehen */
270 chan = strtok( Req->argv[0], "," );
273 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
275 /* naechsten Namen ermitteln */
276 chan = strtok( NULL, "," );
280 /* naechsten Namen ermitteln */
281 chan = strtok( NULL, "," );
288 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
295 assert( Client != NULL );
296 assert( Req != NULL );
298 /* Falsche Anzahl Parameter? */
299 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
301 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
303 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
305 /* Welcher Channel? */
306 chan = Channel_Search( Req->argv[0] );
307 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
309 /* Ist der User Mitglied in dem Channel? */
310 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
314 /* Request actual topic */
315 topic = Channel_Topic(chan);
317 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
318 Client_ID(Client), Channel_Name(chan), topic);
320 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
321 Client_ID(Client), Channel_Name(chan),
322 Channel_TopicWho(chan),
323 Channel_TopicTime(chan));
328 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
329 Client_ID(from), Channel_Name(chan));
332 if( strchr( Channel_Modes( chan ), 't' ))
334 /* Topic Lock. Ist der User ein Channel Operator? */
335 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
339 Channel_SetTopic(chan, from, Req->argv[1]);
340 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
341 Client_Mask(from), Channel_Name(chan),
342 Req->argv[1][0] ? Req->argv[1] : "<none>");
344 /* im Channel bekannt machen und an Server weiterleiten */
345 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
346 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
348 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
349 else return CONNECTED;
354 * Handler for the IRC "LIST" command.
355 * This implementation handles the local case as well as the forwarding of the
356 * LIST command to other servers in the IRC network.
359 IRC_LIST( CLIENT *Client, REQUEST *Req )
363 CLIENT *from, *target;
365 assert( Client != NULL );
366 assert( Req != NULL );
368 /* Bad number of prameters? */
370 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
371 Client_ID( Client ), Req->command );
374 pattern = strtok( Req->argv[0], "," );
378 /* Get sender from prefix, if any */
379 if( Client_Type( Client ) == CLIENT_SERVER )
380 from = Client_Search( Req->prefix );
385 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
386 Client_ID( Client ), Req->prefix );
390 /* Forward to other server? */
391 target = Client_Search( Req->argv[1] );
392 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
393 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
394 Client_ID( Client ), Req->argv[1] );
396 if( target != Client_ThisServer( ))
398 /* Target is indeed an other server, forward it! */
399 return IRC_WriteStrClientPrefix( target, from,
400 "LIST %s :%s", Client_ID( from ),
407 /* Loop through all the channels */
408 chan = Channel_First( );
411 /* Check search pattern */
412 if( Match( pattern, Channel_Name( chan )))
415 if( ! strchr( Channel_Modes( chan ), 's' ) ||
416 Channel_IsMemberOf( chan, from ))
418 if( ! IRC_WriteStrClient( from,
419 RPL_LIST_MSG, Client_ID( from ),
420 Channel_Name( chan ),
421 Channel_MemberCount( chan ),
422 Channel_Topic( chan )))
426 chan = Channel_Next( chan );
429 /* Get next name ... */
431 pattern = strtok( NULL, "," );
436 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
441 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
443 char modes_add[COMMAND_LEN], l[16], *ptr;
448 assert( Client != NULL );
449 assert( Req != NULL );
451 /* Bad number of parameters? */
452 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
454 /* Compatibility kludge */
455 if( Req->argc == 5 ) arg_topic = 4;
456 else if( Req->argc == 3 ) arg_topic = 2;
460 from = Client_Search( Req->prefix );
461 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
463 /* Search or create channel */
464 chan = Channel_Search( Req->argv[0] );
465 if( ! chan ) chan = Channel_Create( Req->argv[0] );
466 if( ! chan ) return CONNECTED;
468 if( Req->argv[1][0] == '+' )
470 ptr = Channel_Modes( chan );
473 /* OK, this channel doesn't have modes jet, set the received ones: */
474 Channel_SetModes( chan, &Req->argv[1][1] );
478 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
479 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
483 /* Delete modes which we never want to inherit */
484 Channel_ModeDel( chan, 'l' );
485 Channel_ModeDel( chan, 'k' );
488 strcpy( modes_add, "" );
489 ptr = Channel_Modes( chan );
494 snprintf( l, sizeof( l ), " %ld", Channel_MaxUsers( chan ));
495 strlcat( modes_add, l, sizeof( modes_add ));
499 strlcat( modes_add, " ", sizeof( modes_add ));
500 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
505 /* Inform members of this channel */
506 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
509 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
514 ptr = Channel_Topic( chan );
515 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
517 /* OK, there is no topic jet */
518 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
519 IRC_WriteStrChannelPrefix(Client, chan, from, false,
520 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
524 /* Forward CHANINFO to other serevrs */
525 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] );
526 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
527 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );