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.34 2006/03/16 19:56:34 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 IRC_WriteStrClient( Client, ERR_TOOMANYCHANNELS_MSG,
102 Client_ID( Client ), channame );
106 /* Existiert der Channel bereits, oder wird er im Moment neu erzeugt? */
109 /* Erster User im Channel: Operator-Flag setzen */
114 /* Existierenden Channel suchen */
115 chan = Channel_Search( channame );
116 assert( chan != NULL );
118 is_banned = Lists_CheckBanned( target, chan );
119 is_invited = Lists_CheckInvited( target, chan );
121 /* Testen, ob Client gebanned ist */
122 if(( is_banned == true) && ( is_invited == false ))
124 /* Client ist gebanned (und nicht invited): */
125 IRC_WriteStrClient( Client, ERR_BANNEDFROMCHAN_MSG, Client_ID( Client ), channame );
127 /* Try next name, if any */
128 channame = strtok( NULL, "," );
132 /* Ist der Channel "invite-only"? */
133 if(( strchr( Channel_Modes( chan ), 'i' )) && ( is_invited == false ))
135 /* Channel ist "invite-only" und Client wurde nicht invited: */
136 IRC_WriteStrClient( Client, ERR_INVITEONLYCHAN_MSG, Client_ID( Client ), channame );
138 /* Try next name, if any */
139 channame = strtok( NULL, "," );
143 /* Is the channel protected by a key? */
144 if(( strchr( Channel_Modes( chan ), 'k' )) && ( strcmp( Channel_Key( chan ), key ? key : "" ) != 0 ))
146 /* Bad channel key! */
147 IRC_WriteStrClient( Client, ERR_BADCHANNELKEY_MSG, Client_ID( Client ), channame );
149 /* Try next name, if any */
150 channame = strtok( NULL, "," );
154 /* Are there already too many members? */
155 if(( strchr( Channel_Modes( chan ), 'l' )) && ( Channel_MaxUsers( chan ) <= Channel_MemberCount( chan )))
157 /* Bad channel key! */
158 IRC_WriteStrClient( Client, ERR_CHANNELISFULL_MSG, Client_ID( Client ), channame );
160 /* Try next name, if any */
161 channame = strtok( NULL, "," );
168 /* Remote server: we don't need to know whether the
169 * client is invited or not, but we have to make sure
170 * that the "one shot" entries (generated by INVITE
171 * commands) in this list become deleted when a user
172 * joins a channel this way. */
173 chan = Channel_Search( channame );
174 if( chan != NULL ) (void)Lists_CheckInvited( target, chan );
177 /* Channel joinen (und ggf. anlegen) */
178 if( ! Channel_Join( target, channame ))
180 /* naechsten Namen ermitteln */
181 channame = strtok( NULL, "," );
184 if( ! chan ) chan = Channel_Search( channame );
185 assert( chan != NULL );
187 /* Modes setzen (wenn vorhanden) */
188 while( flags && *flags )
190 Channel_UserModeAdd( chan, target, *flags );
194 /* Wenn persistenter Channel und IRC-Operator: zum Channel-OP machen */
195 if(( strchr( Channel_Modes( chan ), 'P' )) && ( strchr( Client_Modes( target ), 'o' ))) Channel_UserModeAdd( chan, target, 'o' );
197 /* Muessen Modes an andere Server gemeldet werden? */
198 strlcpy( &modes[1], Channel_UserModes( chan, target ), sizeof( modes ) - 1 );
199 if( modes[1] ) modes[0] = 0x7;
200 else modes[0] = '\0';
202 /* An andere Server weiterleiten */
203 IRC_WriteStrServersPrefix( Client, target, "JOIN :%s%s", channame, modes );
205 /* im Channel bekannt machen */
206 IRC_WriteStrChannelPrefix( Client, chan, target, false, "JOIN :%s", channame );
209 /* Modes im Channel bekannt machen */
210 IRC_WriteStrChannelPrefix( Client, chan, target, false, "MODE %s +%s %s", channame, &modes[1], Client_ID( target ));
213 if( Client_Type( Client ) == CLIENT_USER )
215 /* an Client bestaetigen */
216 IRC_WriteStrClientPrefix( Client, target, "JOIN :%s", channame );
218 /* Send topic to client, if any */
219 topic = Channel_Topic(chan);
221 IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
222 Client_ID(Client), channame, topic);
224 IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
225 Client_ID(Client), channame,
226 Channel_TopicWho(chan),
227 Channel_TopicTime(chan));
231 /* Mitglieder an Client Melden */
232 IRC_Send_NAMES( Client, chan );
233 IRC_WriteStrClient( Client, RPL_ENDOFNAMES_MSG, Client_ID( Client ), Channel_Name( chan ));
237 channame = channame_ptr;
240 channame_ptr = strchr(channame, ',');
241 if (channame_ptr) *channame_ptr = '\0';
245 key_ptr = strchr(key, ',');
246 if (key_ptr) *key_ptr = '\0';
255 IRC_PART( CLIENT *Client, REQUEST *Req )
260 assert( Client != NULL );
261 assert( Req != NULL );
263 /* Falsche Anzahl Parameter? */
264 if(( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
266 /* Wer ist der Absender? */
267 if( Client_Type( Client ) == CLIENT_SERVER ) target = Client_Search( Req->prefix );
268 else target = Client;
269 if( ! target ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
271 /* Channel-Namen durchgehen */
272 chan = strtok( Req->argv[0], "," );
275 if( ! Channel_Part( target, Client, chan, Req->argc > 1 ? Req->argv[1] : Client_ID( target )))
277 /* naechsten Namen ermitteln */
278 chan = strtok( NULL, "," );
282 /* naechsten Namen ermitteln */
283 chan = strtok( NULL, "," );
290 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
297 assert( Client != NULL );
298 assert( Req != NULL );
300 /* Falsche Anzahl Parameter? */
301 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
303 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
305 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
307 /* Welcher Channel? */
308 chan = Channel_Search( Req->argv[0] );
309 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
311 /* Ist der User Mitglied in dem Channel? */
312 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
316 /* Request actual topic */
317 topic = Channel_Topic(chan);
319 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
320 Client_ID(Client), Channel_Name(chan), topic);
322 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
323 Client_ID(Client), Channel_Name(chan),
324 Channel_TopicWho(chan),
325 Channel_TopicTime(chan));
330 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
331 Client_ID(from), Channel_Name(chan));
334 if( strchr( Channel_Modes( chan ), 't' ))
336 /* Topic Lock. Ist der User ein Channel Operator? */
337 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
341 Channel_SetTopic(chan, from, Req->argv[1]);
342 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
343 Client_Mask(from), Channel_Name(chan),
344 Req->argv[1][0] ? Req->argv[1] : "<none>");
346 /* im Channel bekannt machen und an Server weiterleiten */
347 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
348 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
350 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
351 else return CONNECTED;
356 * Handler for the IRC "LIST" command.
357 * This implementation handles the local case as well as the forwarding of the
358 * LIST command to other servers in the IRC network.
361 IRC_LIST( CLIENT *Client, REQUEST *Req )
365 CLIENT *from, *target;
367 assert( Client != NULL );
368 assert( Req != NULL );
370 /* Bad number of prameters? */
372 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
373 Client_ID( Client ), Req->command );
376 pattern = strtok( Req->argv[0], "," );
380 /* Get sender from prefix, if any */
381 if( Client_Type( Client ) == CLIENT_SERVER )
382 from = Client_Search( Req->prefix );
387 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
388 Client_ID( Client ), Req->prefix );
392 /* Forward to other server? */
393 target = Client_Search( Req->argv[1] );
394 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
395 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
396 Client_ID( Client ), Req->argv[1] );
398 if( target != Client_ThisServer( ))
400 /* Target is indeed an other server, forward it! */
401 return IRC_WriteStrClientPrefix( target, from,
402 "LIST %s :%s", Client_ID( from ),
409 /* Loop through all the channels */
410 chan = Channel_First( );
413 /* Check search pattern */
414 if( Match( pattern, Channel_Name( chan )))
417 if( ! strchr( Channel_Modes( chan ), 's' ) ||
418 Channel_IsMemberOf( chan, from ))
420 if( ! IRC_WriteStrClient( from,
421 RPL_LIST_MSG, Client_ID( from ),
422 Channel_Name( chan ),
423 Channel_MemberCount( chan ),
424 Channel_Topic( chan )))
428 chan = Channel_Next( chan );
431 /* Get next name ... */
433 pattern = strtok( NULL, "," );
438 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
443 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
445 char modes_add[COMMAND_LEN], l[16], *ptr;
450 assert( Client != NULL );
451 assert( Req != NULL );
453 /* Bad number of parameters? */
454 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
456 /* Compatibility kludge */
457 if( Req->argc == 5 ) arg_topic = 4;
458 else if( Req->argc == 3 ) arg_topic = 2;
462 from = Client_Search( Req->prefix );
463 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
465 /* Search or create channel */
466 chan = Channel_Search( Req->argv[0] );
467 if( ! chan ) chan = Channel_Create( Req->argv[0] );
468 if( ! chan ) return CONNECTED;
470 if( Req->argv[1][0] == '+' )
472 ptr = Channel_Modes( chan );
475 /* OK, this channel doesn't have modes jet, set the received ones: */
476 Channel_SetModes( chan, &Req->argv[1][1] );
480 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
481 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
485 /* Delete modes which we never want to inherit */
486 Channel_ModeDel( chan, 'l' );
487 Channel_ModeDel( chan, 'k' );
490 strcpy( modes_add, "" );
491 ptr = Channel_Modes( chan );
496 snprintf( l, sizeof( l ), " %ld", Channel_MaxUsers( chan ));
497 strlcat( modes_add, l, sizeof( modes_add ));
501 strlcat( modes_add, " ", sizeof( modes_add ));
502 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
507 /* Inform members of this channel */
508 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
511 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
516 ptr = Channel_Topic( chan );
517 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
519 /* OK, there is no topic jet */
520 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
521 IRC_WriteStrChannelPrefix(Client, chan, from, false,
522 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
526 /* Forward CHANINFO to other serevrs */
527 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] );
528 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
529 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );