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 $";
29 #include "conn-func.h"
36 #include "irc-write.h"
41 #include "irc-channel.h"
45 * RFC 2812, (3.2.1 Join message Command):
46 * Note that this message
47 * accepts a special argument ("0"), which is a special request to leave all
48 * channels the user is currently a member of. The server will process this
49 * message as if the user had sent a PART command (See Section 3.2.2) for
50 * each channel he is a member of.
53 part_from_all_channels(CLIENT* client, CLIENT *target)
58 while ((cl2chan = Channel_FirstChannelOf(target))) {
59 chan = Channel_GetChannel(cl2chan);
60 assert( chan != NULL );
61 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
68 join_allowed(CLIENT *Client, CLIENT *target, CHANNEL *chan,
69 const char *channame, const char *key)
71 bool is_invited, is_banned;
72 const char *channel_modes;
74 /* Allow IRC operators to overwrite channel limits */
75 if (strchr(Client_Modes(Client), 'o'))
78 is_banned = Lists_Check(Channel_GetListBans(chan), target);
79 is_invited = Lists_Check(Channel_GetListInvites(chan), target);
81 if (is_banned && !is_invited) {
82 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
86 channel_modes = Channel_Modes(chan);
87 if ((strchr(channel_modes, 'i')) && !is_invited) {
88 /* Channel is "invite-only" (and Client wasn't invited) */
89 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG, Client_ID(Client), channame);
93 /* Is the channel protected by a key? */
94 if (strchr(channel_modes, 'k') &&
95 strcmp(Channel_Key(chan), key ? key : ""))
97 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG, Client_ID(Client), channame);
100 /* Are there already too many members? */
101 if ((strchr(channel_modes, 'l')) && (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
102 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG, Client_ID(Client), channame);
110 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
114 Channel_UserModeAdd(chan, target, *flags);
119 /* If channel persistent and client is ircop: make client chanop */
120 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
121 Channel_UserModeAdd(chan, target, 'o');
126 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
127 const char *channame)
131 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
137 /* forward to other servers */
138 IRC_WriteStrServersPrefix(Client, target, "JOIN :%s%s", channame, modes);
140 /* tell users in this channel about the new client */
141 IRC_WriteStrChannelPrefix(Client, chan, target, false, "JOIN :%s", channame);
143 IRC_WriteStrChannelPrefix(Client, chan, target, false, "MODE %s +%s %s",
144 channame, &modes[1], Client_ID(target));
149 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
150 const char *channame)
154 if (Client_Type(Client) != CLIENT_USER)
156 /* acknowledge join */
157 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
160 /* Send topic to client, if any */
161 topic = Channel_Topic(chan);
162 assert(topic != NULL);
164 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
165 Client_ID(Client), channame, topic))
168 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
169 Client_ID(Client), channame,
170 Channel_TopicWho(chan),
171 Channel_TopicTime(chan)))
175 /* send list of channel members to client */
176 if (!IRC_Send_NAMES(Client, chan))
178 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
183 IRC_JOIN( CLIENT *Client, REQUEST *Req )
185 char *channame, *channame_ptr, *key, *key_ptr, *flags;
189 assert( Client != NULL );
190 assert( Req != NULL );
192 /* Bad number of arguments? */
193 if (Req->argc < 1 || Req->argc > 2)
194 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
195 Client_ID(Client), Req->command);
197 /* Who is the sender? */
198 if (Client_Type(Client) == CLIENT_SERVER)
199 target = Client_Search(Req->prefix);
204 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
206 /* Is argument "0"? */
207 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
208 return part_from_all_channels(Client, target);
210 /* Are channel keys given? */
213 key_ptr = strchr(key, ',');
214 if (key_ptr) *key_ptr = '\0';
216 key = key_ptr = NULL;
218 channame = Req->argv[0];
219 channame_ptr = strchr(channame, ',');
220 if (channame_ptr) *channame_ptr = '\0';
225 /* Did the server include channel-user-modes? */
226 if (Client_Type(Client) == CLIENT_SERVER) {
227 flags = strchr(channame, 0x7);
234 chan = Channel_Search(channame);
235 if (!chan && Conf_PredefChannelsOnly) {
236 /* channel must be created, but server does not allow this */
237 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
242 if (Client_Type(Client) == CLIENT_USER) {
243 /* Test if the user has reached his maximum channel count */
244 if ((Conf_MaxJoins > 0) && (Channel_CountForUser(Client) >= Conf_MaxJoins))
245 return IRC_WriteStrClient(Client, ERR_TOOMANYCHANNELS_MSG,
246 Client_ID(Client), channame);
249 * New Channel: first user will be channel operator
250 * unless this is a modeless channel.
252 if (*channame != '+')
255 if (!join_allowed(Client, target, chan, channame, key))
258 /* Local client: update idle time */
259 Conn_UpdateIdle(Client_Conn(Client));
261 /* Remote server: we don't need to know whether the
262 * client is invited or not, but we have to make sure
263 * that the "one shot" entries (generated by INVITE
264 * commands) in this list become deleted when a user
265 * joins a channel this way. */
266 if (chan) (void)Lists_Check(Channel_GetListInvites(chan), target);
269 /* Join channel (and create channel if it doesn't exist) */
270 if (!Channel_Join(target, channame))
273 if (!chan) { /* channel is new; it has been created above */
274 chan = Channel_Search(channame);
275 assert(chan != NULL);
276 if (*channame == '+') { /* modeless channel... */
277 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
278 Channel_ModeAdd(chan, 'n'); /* no external msgs */
281 assert(chan != NULL);
283 join_set_channelmodes(chan, target, flags);
285 join_forward(Client, target, chan, channame);
287 if (!join_send_topic(Client, target, chan, channame))
288 break; /* write error */
291 channame = channame_ptr;
294 channame_ptr = strchr(channame, ',');
295 if (channame_ptr) *channame_ptr = '\0';
299 key_ptr = strchr(key, ',');
300 if (key_ptr) *key_ptr = '\0';
309 * Handler for the IRC "PART" command.
312 IRC_PART(CLIENT * Client, REQUEST * Req)
317 assert(Client != NULL);
320 if (Req->argc < 1 || Req->argc > 2)
321 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
322 Client_ID(Client), Req->command);
325 if (Client_Type(Client) == CLIENT_SERVER)
326 target = Client_Search(Req->prefix);
330 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
331 Client_ID(Client), Req->prefix);
333 /* Loop over all the given channel names */
334 chan = strtok(Req->argv[0], ",");
336 Channel_Part(target, Client, chan,
337 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
338 chan = strtok(NULL, ",");
341 /* Update idle time, if local client */
342 if (Client_Conn(Client) > NONE)
343 Conn_UpdateIdle(Client_Conn(Client));
350 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
357 assert( Client != NULL );
358 assert( Req != NULL );
360 if ((Req->argc < 1) || (Req->argc > 2))
361 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
363 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
365 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
367 /* Welcher Channel? */
368 chan = Channel_Search( Req->argv[0] );
369 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
371 /* Ist der User Mitglied in dem Channel? */
372 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
376 /* Request actual topic */
377 topic = Channel_Topic(chan);
379 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
380 Client_ID(Client), Channel_Name(chan), topic);
382 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
383 Client_ID(Client), Channel_Name(chan),
384 Channel_TopicWho(chan),
385 Channel_TopicTime(chan));
390 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
391 Client_ID(from), Channel_Name(chan));
394 if( strchr( Channel_Modes( chan ), 't' ))
396 /* Topic Lock. Ist der User ein Channel Operator? */
397 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
401 Channel_SetTopic(chan, from, Req->argv[1]);
402 Log(LOG_DEBUG, "User \"%s\" set topic on \"%s\": %s",
403 Client_Mask(from), Channel_Name(chan),
404 Req->argv[1][0] ? Req->argv[1] : "<none>");
406 /* im Channel bekannt machen und an Server weiterleiten */
407 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
408 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
410 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
411 else return CONNECTED;
416 * Handler for the IRC "LIST" command.
417 * This implementation handles the local case as well as the forwarding of the
418 * LIST command to other servers in the IRC network.
421 IRC_LIST( CLIENT *Client, REQUEST *Req )
425 CLIENT *from, *target;
427 assert( Client != NULL );
428 assert( Req != NULL );
430 /* Bad number of prameters? */
432 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
433 Client_ID( Client ), Req->command );
436 pattern = strtok( Req->argv[0], "," );
440 /* Get sender from prefix, if any */
441 if( Client_Type( Client ) == CLIENT_SERVER )
442 from = Client_Search( Req->prefix );
447 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
448 Client_ID( Client ), Req->prefix );
452 /* Forward to other server? */
453 target = Client_Search( Req->argv[1] );
454 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
455 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
456 Client_ID( Client ), Req->argv[1] );
458 if( target != Client_ThisServer( ))
460 /* Target is indeed an other server, forward it! */
461 return IRC_WriteStrClientPrefix( target, from,
462 "LIST %s :%s", Client_ID( from ),
469 /* Loop through all the channels */
470 chan = Channel_First( );
473 /* Check search pattern */
474 if( Match( pattern, Channel_Name( chan )))
477 if( ! strchr( Channel_Modes( chan ), 's' ) ||
478 Channel_IsMemberOf( chan, from ))
480 if( ! IRC_WriteStrClient( from,
481 RPL_LIST_MSG, Client_ID( from ),
482 Channel_Name( chan ),
483 Channel_MemberCount( chan ),
484 Channel_Topic( chan )))
488 chan = Channel_Next( chan );
491 /* Get next name ... */
493 pattern = strtok( NULL, "," );
498 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
503 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
505 char modes_add[COMMAND_LEN], l[16], *ptr;
510 assert( Client != NULL );
511 assert( Req != NULL );
513 /* Bad number of parameters? */
514 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
516 /* Compatibility kludge */
517 if( Req->argc == 5 ) arg_topic = 4;
518 else if( Req->argc == 3 ) arg_topic = 2;
522 from = Client_Search( Req->prefix );
523 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
525 /* Search or create channel */
526 chan = Channel_Search( Req->argv[0] );
527 if( ! chan ) chan = Channel_Create( Req->argv[0] );
528 if( ! chan ) return CONNECTED;
530 if( Req->argv[1][0] == '+' )
532 ptr = Channel_Modes( chan );
535 /* OK, this channel doesn't have modes jet, set the received ones: */
536 Channel_SetModes( chan, &Req->argv[1][1] );
540 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
541 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
545 /* Delete modes which we never want to inherit */
546 Channel_ModeDel( chan, 'l' );
547 Channel_ModeDel( chan, 'k' );
550 strcpy( modes_add, "" );
551 ptr = Channel_Modes( chan );
556 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
557 strlcat( modes_add, l, sizeof( modes_add ));
561 strlcat( modes_add, " ", sizeof( modes_add ));
562 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
567 /* Inform members of this channel */
568 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
571 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
576 ptr = Channel_Topic( chan );
577 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
579 /* OK, there is no topic jet */
580 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
581 IRC_WriteStrChannelPrefix(Client, chan, from, false,
582 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
586 /* Forward CHANINFO to other serevrs */
587 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] );
588 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
589 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );