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.
11 * IRC channel commands
26 #include "conn-func.h"
33 #include "irc-write.h"
37 #include "irc-channel.h"
41 * RFC 2812, (3.2.1 Join message Command):
42 * Note that this message
43 * accepts a special argument ("0"), which is a special request to leave all
44 * channels the user is currently a member of. The server will process this
45 * message as if the user had sent a PART command (See Section 3.2.2) for
46 * each channel he is a member of.
49 part_from_all_channels(CLIENT* client, CLIENT *target)
54 while ((cl2chan = Channel_FirstChannelOf(target))) {
55 chan = Channel_GetChannel(cl2chan);
56 assert( chan != NULL );
57 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
64 * Check weather a local client is allowed to join an already existing
66 * @param Client Client that sent the JOIN command
67 * @param chan Channel to check
68 * @param channame Name of the channel
69 * @param key Provided channel key (or NULL if none has been provided)
70 * @return true if client is allowed to join channel, false otherwise
73 join_allowed(CLIENT *Client, CHANNEL *chan, const char *channame,
76 bool is_invited, is_banned;
77 const char *channel_modes;
79 /* Allow IRC operators to overwrite channel limits */
80 if (strchr(Client_Modes(Client), 'o'))
83 is_banned = Lists_Check(Channel_GetListBans(chan), Client);
84 is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
86 if (is_banned && !is_invited) {
87 /* Client is banned from channel (and not on invite list) */
88 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
89 Client_ID(Client), channame);
93 channel_modes = Channel_Modes(chan);
94 if (strchr(channel_modes, 'i') && !is_invited) {
95 /* Channel is "invite-only" and client is not on invite list */
96 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG,
97 Client_ID(Client), channame);
101 if (!Channel_CheckKey(chan, Client, key ? key : "")) {
102 /* Channel is protected by a channel key and the client
103 * didn't specify the correct one */
104 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
105 Client_ID(Client), channame);
109 if (strchr(channel_modes, 'l') &&
110 (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
111 /* There are more clints joined to this channel than allowed */
112 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG,
113 Client_ID(Client), channame);
117 if (strchr(channel_modes, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
118 /* Only "secure" clients are allowed, but clients doesn't
119 * use SSL encryption */
120 IRC_WriteStrClient(Client, ERR_SECURECHANNEL_MSG,
121 Client_ID(Client), channame);
130 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
134 Channel_UserModeAdd(chan, target, *flags);
139 /* If channel persistent and client is ircop: make client chanop */
140 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
141 Channel_UserModeAdd(chan, target, 'o');
146 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
149 char str[COMMAND_LEN], *ptr = NULL;
151 strlcpy(str, (char *)Data, sizeof(str));
152 conn = Client_Conn(To);
154 if (Conn_Options(conn) & CONN_RFC1459) {
155 /* RFC 1459 compatibility mode, appended modes are NOT
156 * supported, so strip them off! */
157 ptr = strchr(str, 0x7);
162 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
164 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
166 } /* cb_join_forward */
170 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
171 const char *channame)
173 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
175 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
181 /* forward to other servers (if it is not a local channel) */
182 if (!Channel_IsLocal(chan)) {
183 snprintf(str, sizeof(str), "%s%s", channame, modes);
184 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
185 cb_join_forward, str);
188 /* tell users in this channel about the new client */
189 IRC_WriteStrChannelPrefix(Client, chan, target, false,
190 "JOIN :%s", channame);
192 /* syncronize channel modes */
194 IRC_WriteStrChannelPrefix(Client, chan, target, false,
195 "MODE %s +%s %s", channame,
196 &modes[1], Client_ID(target));
202 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
203 const char *channame)
207 if (Client_Type(Client) != CLIENT_USER)
209 /* acknowledge join */
210 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
213 /* Send topic to client, if any */
214 topic = Channel_Topic(chan);
215 assert(topic != NULL);
217 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
218 Client_ID(Client), channame, topic))
221 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
222 Client_ID(Client), channame,
223 Channel_TopicWho(chan),
224 Channel_TopicTime(chan)))
228 /* send list of channel members to client */
229 if (!IRC_Send_NAMES(Client, chan))
231 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
236 IRC_JOIN( CLIENT *Client, REQUEST *Req )
238 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
242 assert( Client != NULL );
243 assert( Req != NULL );
245 /* Bad number of arguments? */
246 if (Req->argc < 1 || Req->argc > 2)
247 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
248 Client_ID(Client), Req->command);
250 /* Who is the sender? */
251 if (Client_Type(Client) == CLIENT_SERVER)
252 target = Client_Search(Req->prefix);
257 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
259 /* Is argument "0"? */
260 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
261 return part_from_all_channels(Client, target);
263 /* Are channel keys given? */
265 key = strtok_r(Req->argv[1], ",", &lastkey);
267 channame = Req->argv[0];
268 channame = strtok_r(channame, ",", &lastchan);
270 /* Make sure that "channame" is not the empty string ("JOIN :") */
272 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
273 Client_ID(Client), Req->command);
278 /* Did the server include channel-user-modes? */
279 if (Client_Type(Client) == CLIENT_SERVER) {
280 flags = strchr(channame, 0x7);
287 chan = Channel_Search(channame);
288 if (!chan && Conf_PredefChannelsOnly) {
289 /* channel must be created, but server does not allow this */
290 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
295 if (Client_Type(Client) == CLIENT_USER) {
296 /* Test if the user has reached the channel limit */
297 if ((Conf_MaxJoins > 0) &&
298 (Channel_CountForUser(Client) >= Conf_MaxJoins))
299 return IRC_WriteStrClient(Client,
300 ERR_TOOMANYCHANNELS_MSG,
301 Client_ID(Client), channame);
303 /* Already existing channel: check if the
304 * client is allowed to join */
305 if (!join_allowed(Client, chan, channame, key))
308 /* New channel: first user will become channel
309 * operator unless this is a modeless channel */
310 if (*channame != '+')
314 /* Local client: update idle time */
315 Conn_UpdateIdle(Client_Conn(Client));
317 /* Remote server: we don't need to know whether the
318 * client is invited or not, but we have to make sure
319 * that the "one shot" entries (generated by INVITE
320 * commands) in this list become deleted when a user
321 * joins a channel this way. */
323 (void)Lists_Check(Channel_GetListInvites(chan),
327 /* Join channel (and create channel if it doesn't exist) */
328 if (!Channel_Join(target, channame))
331 if (!chan) { /* channel is new; it has been created above */
332 chan = Channel_Search(channame);
333 assert(chan != NULL);
334 if (Channel_IsModeless(chan)) {
335 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
336 Channel_ModeAdd(chan, 'n'); /* no external msgs */
339 assert(chan != NULL);
341 join_set_channelmodes(chan, target, flags);
343 join_forward(Client, target, chan, channame);
345 if (!join_send_topic(Client, target, chan, channame))
346 break; /* write error */
349 channame = strtok_r(NULL, ",", &lastchan);
351 key = strtok_r(NULL, ",", &lastkey);
358 * Handler for the IRC "PART" command.
361 IRC_PART(CLIENT * Client, REQUEST * Req)
366 assert(Client != NULL);
369 if (Req->argc < 1 || Req->argc > 2)
370 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
371 Client_ID(Client), Req->command);
374 if (Client_Type(Client) == CLIENT_SERVER)
375 target = Client_Search(Req->prefix);
379 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
380 Client_ID(Client), Req->prefix);
382 /* Loop over all the given channel names */
383 chan = strtok(Req->argv[0], ",");
385 /* Make sure that "chan" is not the empty string ("PART :") */
387 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
388 Client_ID(Client), Req->command);
391 Channel_Part(target, Client, chan,
392 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
393 chan = strtok(NULL, ",");
396 /* Update idle time, if local client */
397 if (Client_Conn(Client) > NONE)
398 Conn_UpdateIdle(Client_Conn(Client));
405 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
410 bool onchannel, topicok, use_servermode, r;
412 assert( Client != NULL );
413 assert( Req != NULL );
415 if (Req->argc < 1 || Req->argc > 2)
416 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
417 Client_ID(Client), Req->command);
419 if (Client_Type(Client) == CLIENT_SERVER)
420 from = Client_Search(Req->prefix);
425 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
426 Client_ID(Client), Req->prefix);
428 chan = Channel_Search(Req->argv[0]);
430 return IRC_WriteStrClient(from, ERR_NOSUCHCHANNEL_MSG,
431 Client_ID(from), Req->argv[0]);
433 Channel_CheckAdminRights(chan, Client, from,
434 &onchannel, &topicok, &use_servermode);
436 if (!onchannel && !topicok)
437 return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
438 Client_ID(from), Req->argv[0]);
440 if (Req->argc == 1) {
441 /* Request actual topic */
442 topic = Channel_Topic(chan);
444 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
446 Channel_Name(chan), topic);
448 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
451 Channel_TopicWho(chan),
452 Channel_TopicTime(chan));
457 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
462 if (strchr(Channel_Modes(chan), 't')) {
463 /* Topic Lock. Is the user a channel or IRC operator? */
465 return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
471 Channel_SetTopic(chan, from, Req->argv[1]);
472 LogDebug("%s \"%s\" set topic on \"%s\": %s",
473 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
474 Req->argv[1][0] ? Req->argv[1] : "<none>");
477 from = Client_ThisServer();
479 /* Update channel and forward new topic to other servers */
480 if (!Channel_IsLocal(chan))
481 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
482 Req->argv[0], Req->argv[1]);
483 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
484 Req->argv[0], Req->argv[1]);
486 if (Client_Type(Client) == CLIENT_USER)
487 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
488 Req->argv[0], Req->argv[1]);
495 * Handler for the IRC "LIST" command.
496 * This implementation handles the local case as well as the forwarding of the
497 * LIST command to other servers in the IRC network.
500 IRC_LIST( CLIENT *Client, REQUEST *Req )
504 CLIENT *from, *target;
506 assert( Client != NULL );
507 assert( Req != NULL );
509 /* Bad number of prameters? */
511 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
512 Client_ID( Client ), Req->command );
515 pattern = strtok( Req->argv[0], "," );
519 /* Get sender from prefix, if any */
520 if( Client_Type( Client ) == CLIENT_SERVER )
521 from = Client_Search( Req->prefix );
526 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
527 Client_ID( Client ), Req->prefix );
531 /* Forward to other server? */
532 target = Client_Search( Req->argv[1] );
533 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
534 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
535 Client_ID( Client ), Req->argv[1] );
537 if( target != Client_ThisServer( ))
539 /* Target is indeed an other server, forward it! */
540 return IRC_WriteStrClientPrefix( target, from,
541 "LIST %s :%s", Client_ID( from ),
548 /* Loop through all the channels */
549 chan = Channel_First( );
552 /* Check search pattern */
553 if( Match( pattern, Channel_Name( chan )))
556 if( ! strchr( Channel_Modes( chan ), 's' ) ||
557 Channel_IsMemberOf( chan, from ))
559 if( ! IRC_WriteStrClient( from,
560 RPL_LIST_MSG, Client_ID( from ),
561 Channel_Name( chan ),
562 Channel_MemberCount( chan ),
563 Channel_Topic( chan )))
567 chan = Channel_Next( chan );
570 /* Get next name ... */
572 pattern = strtok( NULL, "," );
577 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
582 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
584 char modes_add[COMMAND_LEN], l[16], *ptr;
589 assert( Client != NULL );
590 assert( Req != NULL );
592 /* Bad number of parameters? */
593 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
595 /* Compatibility kludge */
596 if( Req->argc == 5 ) arg_topic = 4;
597 else if( Req->argc == 3 ) arg_topic = 2;
601 from = Client_Search( Req->prefix );
602 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
604 /* Search or create channel */
605 chan = Channel_Search( Req->argv[0] );
606 if( ! chan ) chan = Channel_Create( Req->argv[0] );
607 if( ! chan ) return CONNECTED;
609 if( Req->argv[1][0] == '+' )
611 ptr = Channel_Modes( chan );
614 /* OK, this channel doesn't have modes jet, set the received ones: */
615 Channel_SetModes( chan, &Req->argv[1][1] );
619 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
620 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
624 /* Delete modes which we never want to inherit */
625 Channel_ModeDel( chan, 'l' );
626 Channel_ModeDel( chan, 'k' );
629 strcpy( modes_add, "" );
630 ptr = Channel_Modes( chan );
635 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
636 strlcat( modes_add, l, sizeof( modes_add ));
640 strlcat( modes_add, " ", sizeof( modes_add ));
641 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
646 /* Inform members of this channel */
647 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
650 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
655 ptr = Channel_Topic( chan );
656 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
658 /* OK, there is no topic jet */
659 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
660 IRC_WriteStrChannelPrefix(Client, chan, from, false,
661 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
665 /* Forward CHANINFO to other serevrs */
666 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] );
667 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
668 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );