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);
244 * New Channel: first user will be channel operator
245 * unless this is a modeless channel... */
247 if (*channame != '+')
251 if (!join_allowed(Client, target, chan, channame, key))
254 /* Remote server: we don't need to know whether the
255 * client is invited or not, but we have to make sure
256 * that the "one shot" entries (generated by INVITE
257 * commands) in this list become deleted when a user
258 * joins a channel this way. */
259 if (chan) (void)Lists_Check(Channel_GetListInvites(chan), target);
262 /* Join channel (and create channel if it doesn't exist) */
263 if (!Channel_Join(target, channame))
266 if (!chan) { /* channel is new; it has been created above */
267 chan = Channel_Search(channame);
268 assert(chan != NULL);
269 if (*channame == '+') { /* modeless channel... */
270 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
271 Channel_ModeAdd(chan, 'n'); /* no external msgs */
274 assert(chan != NULL);
276 join_set_channelmodes(chan, target, flags);
278 join_forward(Client, target, chan, channame);
280 if (!join_send_topic(Client, target, chan, channame))
281 break; /* write error */
284 channame = channame_ptr;
287 channame_ptr = strchr(channame, ',');
288 if (channame_ptr) *channame_ptr = '\0';
292 key_ptr = strchr(key, ',');
293 if (key_ptr) *key_ptr = '\0';
302 * Handler for the IRC "PART" command.
305 IRC_PART(CLIENT * Client, REQUEST * Req)
310 assert(Client != NULL);
313 if (Req->argc < 1 || Req->argc > 2)
314 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
315 Client_ID(Client), Req->command);
318 if (Client_Type(Client) == CLIENT_SERVER)
319 target = Client_Search(Req->prefix);
323 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
324 Client_ID(Client), Req->prefix);
326 /* Loop over all the given channel names */
327 chan = strtok(Req->argv[0], ",");
329 Channel_Part(target, Client, chan,
330 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
331 chan = strtok(NULL, ",");
338 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
345 assert( Client != NULL );
346 assert( Req != NULL );
348 if ((Req->argc < 1) || (Req->argc > 2))
349 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
351 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
353 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
355 /* Welcher Channel? */
356 chan = Channel_Search( Req->argv[0] );
357 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
359 /* Ist der User Mitglied in dem Channel? */
360 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
364 /* Request actual topic */
365 topic = Channel_Topic(chan);
367 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
368 Client_ID(Client), Channel_Name(chan), topic);
370 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
371 Client_ID(Client), Channel_Name(chan),
372 Channel_TopicWho(chan),
373 Channel_TopicTime(chan));
378 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
379 Client_ID(from), Channel_Name(chan));
382 if( strchr( Channel_Modes( chan ), 't' ))
384 /* Topic Lock. Ist der User ein Channel Operator? */
385 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
389 Channel_SetTopic(chan, from, Req->argv[1]);
390 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
391 Client_Mask(from), Channel_Name(chan),
392 Req->argv[1][0] ? Req->argv[1] : "<none>");
394 /* im Channel bekannt machen und an Server weiterleiten */
395 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
396 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
398 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
399 else return CONNECTED;
404 * Handler for the IRC "LIST" command.
405 * This implementation handles the local case as well as the forwarding of the
406 * LIST command to other servers in the IRC network.
409 IRC_LIST( CLIENT *Client, REQUEST *Req )
413 CLIENT *from, *target;
415 assert( Client != NULL );
416 assert( Req != NULL );
418 /* Bad number of prameters? */
420 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
421 Client_ID( Client ), Req->command );
424 pattern = strtok( Req->argv[0], "," );
428 /* Get sender from prefix, if any */
429 if( Client_Type( Client ) == CLIENT_SERVER )
430 from = Client_Search( Req->prefix );
435 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
436 Client_ID( Client ), Req->prefix );
440 /* Forward to other server? */
441 target = Client_Search( Req->argv[1] );
442 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
443 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
444 Client_ID( Client ), Req->argv[1] );
446 if( target != Client_ThisServer( ))
448 /* Target is indeed an other server, forward it! */
449 return IRC_WriteStrClientPrefix( target, from,
450 "LIST %s :%s", Client_ID( from ),
457 /* Loop through all the channels */
458 chan = Channel_First( );
461 /* Check search pattern */
462 if( Match( pattern, Channel_Name( chan )))
465 if( ! strchr( Channel_Modes( chan ), 's' ) ||
466 Channel_IsMemberOf( chan, from ))
468 if( ! IRC_WriteStrClient( from,
469 RPL_LIST_MSG, Client_ID( from ),
470 Channel_Name( chan ),
471 Channel_MemberCount( chan ),
472 Channel_Topic( chan )))
476 chan = Channel_Next( chan );
479 /* Get next name ... */
481 pattern = strtok( NULL, "," );
486 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
491 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
493 char modes_add[COMMAND_LEN], l[16], *ptr;
498 assert( Client != NULL );
499 assert( Req != NULL );
501 /* Bad number of parameters? */
502 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
504 /* Compatibility kludge */
505 if( Req->argc == 5 ) arg_topic = 4;
506 else if( Req->argc == 3 ) arg_topic = 2;
510 from = Client_Search( Req->prefix );
511 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
513 /* Search or create channel */
514 chan = Channel_Search( Req->argv[0] );
515 if( ! chan ) chan = Channel_Create( Req->argv[0] );
516 if( ! chan ) return CONNECTED;
518 if( Req->argv[1][0] == '+' )
520 ptr = Channel_Modes( chan );
523 /* OK, this channel doesn't have modes jet, set the received ones: */
524 Channel_SetModes( chan, &Req->argv[1][1] );
528 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
529 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
533 /* Delete modes which we never want to inherit */
534 Channel_ModeDel( chan, 'l' );
535 Channel_ModeDel( chan, 'k' );
538 strcpy( modes_add, "" );
539 ptr = Channel_Modes( chan );
544 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
545 strlcat( modes_add, l, sizeof( modes_add ));
549 strlcat( modes_add, " ", sizeof( modes_add ));
550 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
555 /* Inform members of this channel */
556 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
559 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
564 ptr = Channel_Topic( chan );
565 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
567 /* OK, there is no topic jet */
568 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
569 IRC_WriteStrChannelPrefix(Client, chan, from, false,
570 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
574 /* Forward CHANINFO to other serevrs */
575 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] );
576 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
577 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );