2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2010 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.
17 * IRC channel commands
29 #include "conn-func.h"
36 #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 * Check weather a local client is allowed to join an already existing
69 * @param Client Client that sent the JOIN command
70 * @param chan Channel to check
71 * @param channame Name of the channel
72 * @param key Provided channel key (or NULL if none has been provided)
73 * @return true if client is allowed to join channel, false otherwise
76 join_allowed(CLIENT *Client, CHANNEL *chan, const char *channame,
79 bool is_invited, is_banned;
80 const char *channel_modes;
82 /* Allow IRC operators to overwrite channel limits */
83 if (strchr(Client_Modes(Client), 'o'))
86 is_banned = Lists_Check(Channel_GetListBans(chan), Client);
87 is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
89 if (is_banned && !is_invited) {
90 /* Client is banned from channel (and not on invite list) */
91 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
92 Client_ID(Client), channame);
96 channel_modes = Channel_Modes(chan);
97 if (strchr(channel_modes, 'i') && !is_invited) {
98 /* Channel is "invite-only" and client is not on invite list */
99 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG,
100 Client_ID(Client), channame);
104 if (!Channel_CheckKey(chan, Client, key ? key : "")) {
105 /* Channel is protected by a channel key and the client
106 * didn't specify the correct one */
107 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
108 Client_ID(Client), channame);
112 if (strchr(channel_modes, 'l') &&
113 (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
114 /* There are more clints joined to this channel than allowed */
115 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG,
116 Client_ID(Client), channame);
120 if (strchr(channel_modes, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
121 /* Only "secure" clients are allowed, but clients doesn't
122 * use SSL encryption */
123 IRC_WriteStrClient(Client, ERR_SECURECHANNEL_MSG,
124 Client_ID(Client), channame);
128 if (strchr(channel_modes, 'O') && !Client_OperByMe(Client)) {
129 /* Only IRC operators are allowed! */
130 IRC_WriteStrClient(Client, ERR_OPONLYCHANNEL_MSG,
131 Client_ID(Client), channame);
140 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
144 Channel_UserModeAdd(chan, target, *flags);
149 /* If channel persistent and client is ircop: make client chanop */
150 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
151 Channel_UserModeAdd(chan, target, 'o');
156 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
159 char str[COMMAND_LEN], *ptr = NULL;
161 strlcpy(str, (char *)Data, sizeof(str));
162 conn = Client_Conn(To);
164 if (Conn_Options(conn) & CONN_RFC1459) {
165 /* RFC 1459 compatibility mode, appended modes are NOT
166 * supported, so strip them off! */
167 ptr = strchr(str, 0x7);
172 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
174 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
176 } /* cb_join_forward */
180 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
181 const char *channame)
183 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
185 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
191 /* forward to other servers (if it is not a local channel) */
192 if (!Channel_IsLocal(chan)) {
193 snprintf(str, sizeof(str), "%s%s", channame, modes);
194 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
195 cb_join_forward, str);
198 /* tell users in this channel about the new client */
199 IRC_WriteStrChannelPrefix(Client, chan, target, false,
200 "JOIN :%s", channame);
202 /* syncronize channel modes */
204 IRC_WriteStrChannelPrefix(Client, chan, target, false,
205 "MODE %s +%s %s", channame,
206 &modes[1], Client_ID(target));
212 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
213 const char *channame)
217 if (Client_Type(Client) != CLIENT_USER)
219 /* acknowledge join */
220 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
223 /* Send topic to client, if any */
224 topic = Channel_Topic(chan);
225 assert(topic != NULL);
227 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
228 Client_ID(Client), channame, topic))
231 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
232 Client_ID(Client), channame,
233 Channel_TopicWho(chan),
234 Channel_TopicTime(chan)))
238 /* send list of channel members to client */
239 if (!IRC_Send_NAMES(Client, chan))
241 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
246 IRC_JOIN( CLIENT *Client, REQUEST *Req )
248 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
252 assert( Client != NULL );
253 assert( Req != NULL );
255 /* Bad number of arguments? */
256 if (Req->argc < 1 || Req->argc > 2)
257 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
258 Client_ID(Client), Req->command);
260 /* Who is the sender? */
261 if (Client_Type(Client) == CLIENT_SERVER)
262 target = Client_Search(Req->prefix);
267 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
269 /* Is argument "0"? */
270 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
271 return part_from_all_channels(Client, target);
273 /* Are channel keys given? */
275 key = strtok_r(Req->argv[1], ",", &lastkey);
277 channame = Req->argv[0];
278 channame = strtok_r(channame, ",", &lastchan);
280 /* Make sure that "channame" is not the empty string ("JOIN :") */
282 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
283 Client_ID(Client), Req->command);
288 /* Did the server include channel-user-modes? */
289 if (Client_Type(Client) == CLIENT_SERVER) {
290 flags = strchr(channame, 0x7);
297 chan = Channel_Search(channame);
298 if (!chan && Conf_PredefChannelsOnly) {
299 /* channel must be created, but server does not allow this */
300 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
305 if (Client_Type(Client) == CLIENT_USER) {
306 /* Test if the user has reached the channel limit */
307 if ((Conf_MaxJoins > 0) &&
308 (Channel_CountForUser(Client) >= Conf_MaxJoins))
309 return IRC_WriteStrClient(Client,
310 ERR_TOOMANYCHANNELS_MSG,
311 Client_ID(Client), channame);
313 /* Already existing channel: check if the
314 * client is allowed to join */
315 if (!join_allowed(Client, chan, channame, key))
318 /* New channel: first user will become channel
319 * operator unless this is a modeless channel */
320 if (*channame != '+')
324 /* Local client: update idle time */
325 Conn_UpdateIdle(Client_Conn(Client));
327 /* Remote server: we don't need to know whether the
328 * client is invited or not, but we have to make sure
329 * that the "one shot" entries (generated by INVITE
330 * commands) in this list become deleted when a user
331 * joins a channel this way. */
333 (void)Lists_Check(Channel_GetListInvites(chan),
337 /* Join channel (and create channel if it doesn't exist) */
338 if (!Channel_Join(target, channame))
341 if (!chan) { /* channel is new; it has been created above */
342 chan = Channel_Search(channame);
343 assert(chan != NULL);
344 if (Channel_IsModeless(chan)) {
345 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
346 Channel_ModeAdd(chan, 'n'); /* no external msgs */
349 assert(chan != NULL);
351 join_set_channelmodes(chan, target, flags);
353 join_forward(Client, target, chan, channame);
355 if (!join_send_topic(Client, target, chan, channame))
356 break; /* write error */
359 channame = strtok_r(NULL, ",", &lastchan);
361 key = strtok_r(NULL, ",", &lastkey);
368 * Handler for the IRC "PART" command.
371 IRC_PART(CLIENT * Client, REQUEST * Req)
376 assert(Client != NULL);
379 if (Req->argc < 1 || Req->argc > 2)
380 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
381 Client_ID(Client), Req->command);
384 if (Client_Type(Client) == CLIENT_SERVER)
385 target = Client_Search(Req->prefix);
389 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
390 Client_ID(Client), Req->prefix);
392 /* Loop over all the given channel names */
393 chan = strtok(Req->argv[0], ",");
395 /* Make sure that "chan" is not the empty string ("PART :") */
397 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
398 Client_ID(Client), Req->command);
401 Channel_Part(target, Client, chan,
402 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
403 chan = strtok(NULL, ",");
406 /* Update idle time, if local client */
407 if (Client_Conn(Client) > NONE)
408 Conn_UpdateIdle(Client_Conn(Client));
415 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
420 bool onchannel, topicok, use_servermode, r;
422 assert( Client != NULL );
423 assert( Req != NULL );
425 if (Req->argc < 1 || Req->argc > 2)
426 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
427 Client_ID(Client), Req->command);
429 if (Client_Type(Client) == CLIENT_SERVER)
430 from = Client_Search(Req->prefix);
435 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
436 Client_ID(Client), Req->prefix);
438 chan = Channel_Search(Req->argv[0]);
440 return IRC_WriteStrClient(from, ERR_NOSUCHCHANNEL_MSG,
441 Client_ID(from), Req->argv[0]);
443 Channel_CheckAdminRights(chan, Client, from,
444 &onchannel, &topicok, &use_servermode);
446 if (!onchannel && !topicok)
447 return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
448 Client_ID(from), Req->argv[0]);
450 if (Req->argc == 1) {
451 /* Request actual topic */
452 topic = Channel_Topic(chan);
454 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
456 Channel_Name(chan), topic);
458 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
461 Channel_TopicWho(chan),
462 Channel_TopicTime(chan));
467 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
472 if (strchr(Channel_Modes(chan), 't')) {
473 /* Topic Lock. Is the user a channel or IRC operator? */
475 return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
481 Channel_SetTopic(chan, from, Req->argv[1]);
482 LogDebug("%s \"%s\" set topic on \"%s\": %s",
483 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
484 Req->argv[1][0] ? Req->argv[1] : "<none>");
487 from = Client_ThisServer();
489 /* Update channel and forward new topic to other servers */
490 if (!Channel_IsLocal(chan))
491 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
492 Req->argv[0], Req->argv[1]);
493 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
494 Req->argv[0], Req->argv[1]);
496 if (Client_Type(Client) == CLIENT_USER)
497 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
498 Req->argv[0], Req->argv[1]);
505 * Handler for the IRC "LIST" command.
506 * This implementation handles the local case as well as the forwarding of the
507 * LIST command to other servers in the IRC network.
510 IRC_LIST( CLIENT *Client, REQUEST *Req )
514 CLIENT *from, *target;
516 assert( Client != NULL );
517 assert( Req != NULL );
519 /* Bad number of prameters? */
521 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
522 Client_ID( Client ), Req->command );
525 pattern = strtok( Req->argv[0], "," );
529 /* Get sender from prefix, if any */
530 if( Client_Type( Client ) == CLIENT_SERVER )
531 from = Client_Search( Req->prefix );
536 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
537 Client_ID( Client ), Req->prefix );
541 /* Forward to other server? */
542 target = Client_Search( Req->argv[1] );
543 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
544 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
545 Client_ID( Client ), Req->argv[1] );
547 if( target != Client_ThisServer( ))
549 /* Target is indeed an other server, forward it! */
550 return IRC_WriteStrClientPrefix( target, from,
551 "LIST %s :%s", Client_ID( from ),
558 /* Loop through all the channels */
559 chan = Channel_First( );
562 /* Check search pattern */
563 if( Match( pattern, Channel_Name( chan )))
566 if( ! strchr( Channel_Modes( chan ), 's' ) ||
567 Channel_IsMemberOf( chan, from ))
569 if( ! IRC_WriteStrClient( from,
570 RPL_LIST_MSG, Client_ID( from ),
571 Channel_Name( chan ),
572 Channel_MemberCount( chan ),
573 Channel_Topic( chan )))
577 chan = Channel_Next( chan );
580 /* Get next name ... */
582 pattern = strtok( NULL, "," );
587 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
592 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
594 char modes_add[COMMAND_LEN], l[16], *ptr;
599 assert( Client != NULL );
600 assert( Req != NULL );
602 /* Bad number of parameters? */
603 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
605 /* Compatibility kludge */
606 if( Req->argc == 5 ) arg_topic = 4;
607 else if( Req->argc == 3 ) arg_topic = 2;
611 from = Client_Search( Req->prefix );
612 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
614 /* Search or create channel */
615 chan = Channel_Search( Req->argv[0] );
616 if( ! chan ) chan = Channel_Create( Req->argv[0] );
617 if( ! chan ) return CONNECTED;
619 if( Req->argv[1][0] == '+' )
621 ptr = Channel_Modes( chan );
624 /* OK, this channel doesn't have modes jet, set the received ones: */
625 Channel_SetModes( chan, &Req->argv[1][1] );
629 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
630 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
634 /* Delete modes which we never want to inherit */
635 Channel_ModeDel( chan, 'l' );
636 Channel_ModeDel( chan, 'k' );
639 strcpy( modes_add, "" );
640 ptr = Channel_Modes( chan );
645 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
646 strlcat( modes_add, l, sizeof( modes_add ));
650 strlcat( modes_add, " ", sizeof( modes_add ));
651 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
656 /* Inform members of this channel */
657 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
660 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
665 ptr = Channel_Topic( chan );
666 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
668 /* OK, there is no topic jet */
669 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
670 IRC_WriteStrChannelPrefix(Client, chan, from, false,
671 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
675 /* Forward CHANINFO to other serevrs */
676 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] );
677 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
678 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );