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 /* Allow IRC operators to overwrite channel limits */
74 if (strchr(Client_Modes(Client), 'o'))
77 is_banned = Lists_Check(Channel_GetListBans(chan), target);
78 is_invited = Lists_Check(Channel_GetListInvites(chan), target);
80 if (is_banned && !is_invited) {
81 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
85 channel_modes = Channel_Modes(chan);
86 if ((strchr(channel_modes, 'i')) && !is_invited) {
87 /* Channel is "invite-only" (and Client wasn't invited) */
88 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG, Client_ID(Client), channame);
92 /* Is the channel protected by a key? */
93 if (strchr(channel_modes, 'k') &&
94 strcmp(Channel_Key(chan), key ? key : ""))
96 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG, Client_ID(Client), channame);
99 /* Are there already too many members? */
100 if ((strchr(channel_modes, 'l')) && (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
101 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG, Client_ID(Client), channame);
109 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
113 Channel_UserModeAdd(chan, target, *flags);
118 /* If channel persistent and client is ircop: make client chanop */
119 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
120 Channel_UserModeAdd(chan, target, 'o');
125 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
126 const char *channame)
130 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
136 /* forward to other servers */
137 IRC_WriteStrServersPrefix(Client, target, "JOIN :%s%s", channame, modes);
139 /* tell users in this channel about the new client */
140 IRC_WriteStrChannelPrefix(Client, chan, target, false, "JOIN :%s", channame);
142 IRC_WriteStrChannelPrefix(Client, chan, target, false, "MODE %s +%s %s",
143 channame, &modes[1], Client_ID(target));
148 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
149 const char *channame)
153 if (Client_Type(Client) != CLIENT_USER)
155 /* acknowledge join */
156 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
159 /* Send topic to client, if any */
160 topic = Channel_Topic(chan);
161 assert(topic != NULL);
163 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
164 Client_ID(Client), channame, topic))
167 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
168 Client_ID(Client), channame,
169 Channel_TopicWho(chan),
170 Channel_TopicTime(chan)))
174 /* send list of channel members to client */
175 if (!IRC_Send_NAMES(Client, chan))
177 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
182 IRC_JOIN( CLIENT *Client, REQUEST *Req )
184 char *channame, *channame_ptr, *key, *key_ptr, *flags;
188 assert( Client != NULL );
189 assert( Req != NULL );
191 /* Bad number of arguments? */
192 if (Req->argc < 1 || Req->argc > 2)
193 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
194 Client_ID(Client), Req->command);
196 /* Who is the sender? */
197 if (Client_Type(Client) == CLIENT_SERVER)
198 target = Client_Search(Req->prefix);
203 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
205 /* Is argument "0"? */
206 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
207 return part_from_all_channels(Client, target);
209 /* Are channel keys given? */
212 key_ptr = strchr(key, ',');
213 if (key_ptr) *key_ptr = '\0';
215 key = key_ptr = NULL;
217 channame = Req->argv[0];
218 channame_ptr = strchr(channame, ',');
219 if (channame_ptr) *channame_ptr = '\0';
224 /* Did the server include channel-user-modes? */
225 if (Client_Type(Client) == CLIENT_SERVER) {
226 flags = strchr(channame, 0x7);
233 chan = Channel_Search(channame);
234 if (!chan && Conf_PredefChannelsOnly) {
235 /* channel must be created, but server does not allow this */
236 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
241 if (Client_Type(Client) == CLIENT_USER) {
242 /* Test if the user has reached his maximum channel count */
243 if ((Conf_MaxJoins > 0) && (Channel_CountForUser(Client) >= Conf_MaxJoins))
244 return IRC_WriteStrClient(Client, ERR_TOOMANYCHANNELS_MSG,
245 Client_ID(Client), channame);
248 * New Channel: first user will be channel operator
249 * unless this is a modeless channel.
251 if (*channame != '+')
254 if (!join_allowed(Client, target, chan, channame, key))
257 /* Remote server: we don't need to know whether the
258 * client is invited or not, but we have to make sure
259 * that the "one shot" entries (generated by INVITE
260 * commands) in this list become deleted when a user
261 * joins a channel this way. */
262 if (chan) (void)Lists_Check(Channel_GetListInvites(chan), target);
265 /* Join channel (and create channel if it doesn't exist) */
266 if (!Channel_Join(target, channame))
269 if (!chan) { /* channel is new; it has been created above */
270 chan = Channel_Search(channame);
271 assert(chan != NULL);
272 if (*channame == '+') { /* modeless channel... */
273 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
274 Channel_ModeAdd(chan, 'n'); /* no external msgs */
277 assert(chan != NULL);
279 join_set_channelmodes(chan, target, flags);
281 join_forward(Client, target, chan, channame);
283 if (!join_send_topic(Client, target, chan, channame))
284 break; /* write error */
287 channame = channame_ptr;
290 channame_ptr = strchr(channame, ',');
291 if (channame_ptr) *channame_ptr = '\0';
295 key_ptr = strchr(key, ',');
296 if (key_ptr) *key_ptr = '\0';
305 * Handler for the IRC "PART" command.
308 IRC_PART(CLIENT * Client, REQUEST * Req)
313 assert(Client != NULL);
316 if (Req->argc < 1 || Req->argc > 2)
317 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
318 Client_ID(Client), Req->command);
321 if (Client_Type(Client) == CLIENT_SERVER)
322 target = Client_Search(Req->prefix);
326 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
327 Client_ID(Client), Req->prefix);
329 /* Loop over all the given channel names */
330 chan = strtok(Req->argv[0], ",");
332 Channel_Part(target, Client, chan,
333 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
334 chan = strtok(NULL, ",");
341 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
348 assert( Client != NULL );
349 assert( Req != NULL );
351 if ((Req->argc < 1) || (Req->argc > 2))
352 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
354 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
356 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
358 /* Welcher Channel? */
359 chan = Channel_Search( Req->argv[0] );
360 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
362 /* Ist der User Mitglied in dem Channel? */
363 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
367 /* Request actual topic */
368 topic = Channel_Topic(chan);
370 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
371 Client_ID(Client), Channel_Name(chan), topic);
373 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
374 Client_ID(Client), Channel_Name(chan),
375 Channel_TopicWho(chan),
376 Channel_TopicTime(chan));
381 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
382 Client_ID(from), Channel_Name(chan));
385 if( strchr( Channel_Modes( chan ), 't' ))
387 /* Topic Lock. Ist der User ein Channel Operator? */
388 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
392 Channel_SetTopic(chan, from, Req->argv[1]);
393 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
394 Client_Mask(from), Channel_Name(chan),
395 Req->argv[1][0] ? Req->argv[1] : "<none>");
397 /* im Channel bekannt machen und an Server weiterleiten */
398 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
399 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
401 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
402 else return CONNECTED;
407 * Handler for the IRC "LIST" command.
408 * This implementation handles the local case as well as the forwarding of the
409 * LIST command to other servers in the IRC network.
412 IRC_LIST( CLIENT *Client, REQUEST *Req )
416 CLIENT *from, *target;
418 assert( Client != NULL );
419 assert( Req != NULL );
421 /* Bad number of prameters? */
423 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
424 Client_ID( Client ), Req->command );
427 pattern = strtok( Req->argv[0], "," );
431 /* Get sender from prefix, if any */
432 if( Client_Type( Client ) == CLIENT_SERVER )
433 from = Client_Search( Req->prefix );
438 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
439 Client_ID( Client ), Req->prefix );
443 /* Forward to other server? */
444 target = Client_Search( Req->argv[1] );
445 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
446 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
447 Client_ID( Client ), Req->argv[1] );
449 if( target != Client_ThisServer( ))
451 /* Target is indeed an other server, forward it! */
452 return IRC_WriteStrClientPrefix( target, from,
453 "LIST %s :%s", Client_ID( from ),
460 /* Loop through all the channels */
461 chan = Channel_First( );
464 /* Check search pattern */
465 if( Match( pattern, Channel_Name( chan )))
468 if( ! strchr( Channel_Modes( chan ), 's' ) ||
469 Channel_IsMemberOf( chan, from ))
471 if( ! IRC_WriteStrClient( from,
472 RPL_LIST_MSG, Client_ID( from ),
473 Channel_Name( chan ),
474 Channel_MemberCount( chan ),
475 Channel_Topic( chan )))
479 chan = Channel_Next( chan );
482 /* Get next name ... */
484 pattern = strtok( NULL, "," );
489 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
494 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
496 char modes_add[COMMAND_LEN], l[16], *ptr;
501 assert( Client != NULL );
502 assert( Req != NULL );
504 /* Bad number of parameters? */
505 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
507 /* Compatibility kludge */
508 if( Req->argc == 5 ) arg_topic = 4;
509 else if( Req->argc == 3 ) arg_topic = 2;
513 from = Client_Search( Req->prefix );
514 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
516 /* Search or create channel */
517 chan = Channel_Search( Req->argv[0] );
518 if( ! chan ) chan = Channel_Create( Req->argv[0] );
519 if( ! chan ) return CONNECTED;
521 if( Req->argv[1][0] == '+' )
523 ptr = Channel_Modes( chan );
526 /* OK, this channel doesn't have modes jet, set the received ones: */
527 Channel_SetModes( chan, &Req->argv[1][1] );
531 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
532 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
536 /* Delete modes which we never want to inherit */
537 Channel_ModeDel( chan, 'l' );
538 Channel_ModeDel( chan, 'k' );
541 strcpy( modes_add, "" );
542 ptr = Channel_Modes( chan );
547 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
548 strlcat( modes_add, l, sizeof( modes_add ));
552 strlcat( modes_add, " ", sizeof( modes_add ));
553 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
558 /* Inform members of this channel */
559 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
562 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
567 ptr = Channel_Topic( chan );
568 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
570 /* OK, there is no topic jet */
571 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
572 IRC_WriteStrChannelPrefix(Client, chan, from, false,
573 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
577 /* Forward CHANINFO to other serevrs */
578 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] );
579 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
580 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );