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.45 2008/02/24 18:57:38 fw Exp $";
35 #include "irc-write.h"
40 #include "irc-channel.h"
44 * RFC 2812, (3.2.1 Join message Command):
45 * Note that this message
46 * accepts a special argument ("0"), which is a special request to leave all
47 * channels the user is currently a member of. The server will process this
48 * message as if the user had sent a PART command (See Section 3.2.2) for
49 * each channel he is a member of.
52 part_from_all_channels(CLIENT* client, CLIENT *target)
57 while ((cl2chan = Channel_FirstChannelOf(target))) {
58 chan = Channel_GetChannel(cl2chan);
59 assert( chan != NULL );
60 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
67 join_allowed(CLIENT *Client, CLIENT *target, CHANNEL *chan,
68 const char *channame, const char *key)
70 bool is_invited, is_banned;
71 const char *channel_modes;
73 is_banned = Lists_Check(Channel_GetListBans(chan), target);
74 is_invited = Lists_Check(Channel_GetListInvites(chan), target);
76 if (is_banned && !is_invited) {
77 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
81 channel_modes = Channel_Modes(chan);
82 if ((strchr(channel_modes, 'i')) && !is_invited) {
83 /* Channel is "invite-only" (and Client wasn't invited) */
84 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG, Client_ID(Client), channame);
88 /* Is the channel protected by a key? */
89 if (strchr(channel_modes, 'k') &&
90 strcmp(Channel_Key(chan), key ? key : ""))
92 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG, Client_ID(Client), channame);
95 /* Are there already too many members? */
96 if ((strchr(channel_modes, 'l')) && (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
97 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG, Client_ID(Client), channame);
105 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
109 Channel_UserModeAdd(chan, target, *flags);
114 /* If channel persistent and client is ircop: make client chanop */
115 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
116 Channel_UserModeAdd(chan, target, 'o');
121 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
122 const char *channame)
126 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
132 /* forward to other servers */
133 IRC_WriteStrServersPrefix(Client, target, "JOIN :%s%s", channame, modes);
135 /* tell users in this channel about the new client */
136 IRC_WriteStrChannelPrefix(Client, chan, target, false, "JOIN :%s", channame);
138 IRC_WriteStrChannelPrefix(Client, chan, target, false, "MODE %s +%s %s",
139 channame, &modes[1], Client_ID(target));
144 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
145 const char *channame)
149 if (Client_Type(Client) != CLIENT_USER)
151 /* acknowledge join */
152 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
155 /* Send topic to client, if any */
156 topic = Channel_Topic(chan);
157 assert(topic != NULL);
159 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
160 Client_ID(Client), channame, topic))
163 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
164 Client_ID(Client), channame,
165 Channel_TopicWho(chan),
166 Channel_TopicTime(chan)))
170 /* send list of channel members to client */
171 if (!IRC_Send_NAMES(Client, chan))
173 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
178 IRC_JOIN( CLIENT *Client, REQUEST *Req )
180 char *channame, *channame_ptr, *key, *key_ptr, *flags;
184 assert( Client != NULL );
185 assert( Req != NULL );
187 /* Bad number of arguments? */
188 if (Req->argc < 1 || Req->argc > 2)
189 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
190 Client_ID(Client), Req->command);
192 /* Who is the sender? */
193 if (Client_Type(Client) == CLIENT_SERVER)
194 target = Client_Search(Req->prefix);
199 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
201 /* Is argument "0"? */
202 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
203 return part_from_all_channels(Client, target);
205 /* Are channel keys given? */
208 key_ptr = strchr(key, ',');
209 if (key_ptr) *key_ptr = '\0';
211 key = key_ptr = NULL;
213 channame = Req->argv[0];
214 channame_ptr = strchr(channame, ',');
215 if (channame_ptr) *channame_ptr = '\0';
220 /* Did the server include channel-user-modes? */
221 if (Client_Type(Client) == CLIENT_SERVER) {
222 flags = strchr(channame, 0x7);
229 chan = Channel_Search(channame);
230 if (!chan && Conf_PredefChannelsOnly) {
231 /* channel must be created, but server does not allow this */
232 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
237 if (Client_Type(Client) == CLIENT_USER) {
238 /* Test if the user has reached his maximum channel count */
239 if ((Conf_MaxJoins > 0) && (Channel_CountForUser(Client) >= Conf_MaxJoins))
240 return IRC_WriteStrClient(Client, ERR_TOOMANYCHANNELS_MSG,
241 Client_ID(Client), channame);
242 if (!chan) /* New Channel: first user will be channel operator */
245 if (!join_allowed(Client, target, chan, channame, key))
248 /* Remote server: we don't need to know whether the
249 * client is invited or not, but we have to make sure
250 * that the "one shot" entries (generated by INVITE
251 * commands) in this list become deleted when a user
252 * joins a channel this way. */
253 if (chan) (void)Lists_Check(Channel_GetListInvites(chan), target);
256 /* Join channel (and create channel if it doesn't exist) */
257 if (!Channel_Join(target, channame))
260 if (!chan) /* channel is new; it has been created above */
261 chan = Channel_Search(channame);
262 assert(chan != NULL);
264 join_set_channelmodes(chan, target, flags);
266 join_forward(Client, target, chan, channame);
268 if (!join_send_topic(Client, target, chan, channame))
269 break; /* write error */
272 channame = channame_ptr;
275 channame_ptr = strchr(channame, ',');
276 if (channame_ptr) *channame_ptr = '\0';
280 key_ptr = strchr(key, ',');
281 if (key_ptr) *key_ptr = '\0';
290 * Handler for the IRC "PART" command.
293 IRC_PART(CLIENT * Client, REQUEST * Req)
298 assert(Client != NULL);
301 if (Req->argc < 1 || Req->argc > 2)
302 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
303 Client_ID(Client), Req->command);
306 if (Client_Type(Client) == CLIENT_SERVER)
307 target = Client_Search(Req->prefix);
311 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
312 Client_ID(Client), Req->prefix);
314 /* Loop over all the given channel names */
315 chan = strtok(Req->argv[0], ",");
317 Channel_Part(target, Client, chan,
318 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
319 chan = strtok(NULL, ",");
326 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
333 assert( Client != NULL );
334 assert( Req != NULL );
336 /* Falsche Anzahl Parameter? */
337 if(( Req->argc < 1 ) || ( Req->argc > 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
339 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
341 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
343 /* Welcher Channel? */
344 chan = Channel_Search( Req->argv[0] );
345 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
347 /* Ist der User Mitglied in dem Channel? */
348 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
352 /* Request actual topic */
353 topic = Channel_Topic(chan);
355 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
356 Client_ID(Client), Channel_Name(chan), topic);
358 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
359 Client_ID(Client), Channel_Name(chan),
360 Channel_TopicWho(chan),
361 Channel_TopicTime(chan));
366 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
367 Client_ID(from), Channel_Name(chan));
370 if( strchr( Channel_Modes( chan ), 't' ))
372 /* Topic Lock. Ist der User ein Channel Operator? */
373 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
377 Channel_SetTopic(chan, from, Req->argv[1]);
378 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
379 Client_Mask(from), Channel_Name(chan),
380 Req->argv[1][0] ? Req->argv[1] : "<none>");
382 /* im Channel bekannt machen und an Server weiterleiten */
383 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
384 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
386 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
387 else return CONNECTED;
392 * Handler for the IRC "LIST" command.
393 * This implementation handles the local case as well as the forwarding of the
394 * LIST command to other servers in the IRC network.
397 IRC_LIST( CLIENT *Client, REQUEST *Req )
401 CLIENT *from, *target;
403 assert( Client != NULL );
404 assert( Req != NULL );
406 /* Bad number of prameters? */
408 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
409 Client_ID( Client ), Req->command );
412 pattern = strtok( Req->argv[0], "," );
416 /* Get sender from prefix, if any */
417 if( Client_Type( Client ) == CLIENT_SERVER )
418 from = Client_Search( Req->prefix );
423 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
424 Client_ID( Client ), Req->prefix );
428 /* Forward to other server? */
429 target = Client_Search( Req->argv[1] );
430 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
431 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
432 Client_ID( Client ), Req->argv[1] );
434 if( target != Client_ThisServer( ))
436 /* Target is indeed an other server, forward it! */
437 return IRC_WriteStrClientPrefix( target, from,
438 "LIST %s :%s", Client_ID( from ),
445 /* Loop through all the channels */
446 chan = Channel_First( );
449 /* Check search pattern */
450 if( Match( pattern, Channel_Name( chan )))
453 if( ! strchr( Channel_Modes( chan ), 's' ) ||
454 Channel_IsMemberOf( chan, from ))
456 if( ! IRC_WriteStrClient( from,
457 RPL_LIST_MSG, Client_ID( from ),
458 Channel_Name( chan ),
459 Channel_MemberCount( chan ),
460 Channel_Topic( chan )))
464 chan = Channel_Next( chan );
467 /* Get next name ... */
469 pattern = strtok( NULL, "," );
474 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
479 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
481 char modes_add[COMMAND_LEN], l[16], *ptr;
486 assert( Client != NULL );
487 assert( Req != NULL );
489 /* Bad number of parameters? */
490 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
492 /* Compatibility kludge */
493 if( Req->argc == 5 ) arg_topic = 4;
494 else if( Req->argc == 3 ) arg_topic = 2;
498 from = Client_Search( Req->prefix );
499 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
501 /* Search or create channel */
502 chan = Channel_Search( Req->argv[0] );
503 if( ! chan ) chan = Channel_Create( Req->argv[0] );
504 if( ! chan ) return CONNECTED;
506 if( Req->argv[1][0] == '+' )
508 ptr = Channel_Modes( chan );
511 /* OK, this channel doesn't have modes jet, set the received ones: */
512 Channel_SetModes( chan, &Req->argv[1][1] );
516 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
517 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
521 /* Delete modes which we never want to inherit */
522 Channel_ModeDel( chan, 'l' );
523 Channel_ModeDel( chan, 'k' );
526 strcpy( modes_add, "" );
527 ptr = Channel_Modes( chan );
532 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
533 strlcat( modes_add, l, sizeof( modes_add ));
537 strlcat( modes_add, " ", sizeof( modes_add ));
538 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
543 /* Inform members of this channel */
544 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
547 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
552 ptr = Channel_Topic( chan );
553 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
555 /* OK, there is no topic jet */
556 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
557 IRC_WriteStrChannelPrefix(Client, chan, from, false,
558 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
562 /* Forward CHANINFO to other serevrs */
563 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] );
564 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
565 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );