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);
125 if (strchr(channel_modes, 'O') && !Client_OperByMe(Client)) {
126 /* Only IRC operators are allowed! */
127 IRC_WriteStrClient(Client, ERR_OPONLYCHANNEL_MSG,
128 Client_ID(Client), channame);
137 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
141 Channel_UserModeAdd(chan, target, *flags);
146 /* If channel persistent and client is ircop: make client chanop */
147 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
148 Channel_UserModeAdd(chan, target, 'o');
153 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
156 char str[COMMAND_LEN], *ptr = NULL;
158 strlcpy(str, (char *)Data, sizeof(str));
159 conn = Client_Conn(To);
161 if (Conn_Options(conn) & CONN_RFC1459) {
162 /* RFC 1459 compatibility mode, appended modes are NOT
163 * supported, so strip them off! */
164 ptr = strchr(str, 0x7);
169 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
171 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
173 } /* cb_join_forward */
177 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
178 const char *channame)
180 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
182 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
188 /* forward to other servers (if it is not a local channel) */
189 if (!Channel_IsLocal(chan)) {
190 snprintf(str, sizeof(str), "%s%s", channame, modes);
191 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
192 cb_join_forward, str);
195 /* tell users in this channel about the new client */
196 IRC_WriteStrChannelPrefix(Client, chan, target, false,
197 "JOIN :%s", channame);
199 /* syncronize channel modes */
201 IRC_WriteStrChannelPrefix(Client, chan, target, false,
202 "MODE %s +%s %s", channame,
203 &modes[1], Client_ID(target));
209 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
210 const char *channame)
214 if (Client_Type(Client) != CLIENT_USER)
216 /* acknowledge join */
217 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
220 /* Send topic to client, if any */
221 topic = Channel_Topic(chan);
222 assert(topic != NULL);
224 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
225 Client_ID(Client), channame, topic))
228 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
229 Client_ID(Client), channame,
230 Channel_TopicWho(chan),
231 Channel_TopicTime(chan)))
235 /* send list of channel members to client */
236 if (!IRC_Send_NAMES(Client, chan))
238 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
243 IRC_JOIN( CLIENT *Client, REQUEST *Req )
245 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
249 assert( Client != NULL );
250 assert( Req != NULL );
252 /* Bad number of arguments? */
253 if (Req->argc < 1 || Req->argc > 2)
254 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
255 Client_ID(Client), Req->command);
257 /* Who is the sender? */
258 if (Client_Type(Client) == CLIENT_SERVER)
259 target = Client_Search(Req->prefix);
264 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
266 /* Is argument "0"? */
267 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
268 return part_from_all_channels(Client, target);
270 /* Are channel keys given? */
272 key = strtok_r(Req->argv[1], ",", &lastkey);
274 channame = Req->argv[0];
275 channame = strtok_r(channame, ",", &lastchan);
277 /* Make sure that "channame" is not the empty string ("JOIN :") */
279 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
280 Client_ID(Client), Req->command);
285 /* Did the server include channel-user-modes? */
286 if (Client_Type(Client) == CLIENT_SERVER) {
287 flags = strchr(channame, 0x7);
294 chan = Channel_Search(channame);
295 if (!chan && Conf_PredefChannelsOnly) {
296 /* channel must be created, but server does not allow this */
297 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
302 if (Client_Type(Client) == CLIENT_USER) {
303 /* Test if the user has reached the channel limit */
304 if ((Conf_MaxJoins > 0) &&
305 (Channel_CountForUser(Client) >= Conf_MaxJoins))
306 return IRC_WriteStrClient(Client,
307 ERR_TOOMANYCHANNELS_MSG,
308 Client_ID(Client), channame);
310 /* Already existing channel: check if the
311 * client is allowed to join */
312 if (!join_allowed(Client, chan, channame, key))
315 /* New channel: first user will become channel
316 * operator unless this is a modeless channel */
317 if (*channame != '+')
321 /* Local client: update idle time */
322 Conn_UpdateIdle(Client_Conn(Client));
324 /* Remote server: we don't need to know whether the
325 * client is invited or not, but we have to make sure
326 * that the "one shot" entries (generated by INVITE
327 * commands) in this list become deleted when a user
328 * joins a channel this way. */
330 (void)Lists_Check(Channel_GetListInvites(chan),
334 /* Join channel (and create channel if it doesn't exist) */
335 if (!Channel_Join(target, channame))
338 if (!chan) { /* channel is new; it has been created above */
339 chan = Channel_Search(channame);
340 assert(chan != NULL);
341 if (Channel_IsModeless(chan)) {
342 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
343 Channel_ModeAdd(chan, 'n'); /* no external msgs */
346 assert(chan != NULL);
348 join_set_channelmodes(chan, target, flags);
350 join_forward(Client, target, chan, channame);
352 if (!join_send_topic(Client, target, chan, channame))
353 break; /* write error */
356 channame = strtok_r(NULL, ",", &lastchan);
358 key = strtok_r(NULL, ",", &lastkey);
365 * Handler for the IRC "PART" command.
368 IRC_PART(CLIENT * Client, REQUEST * Req)
373 assert(Client != NULL);
376 if (Req->argc < 1 || Req->argc > 2)
377 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
378 Client_ID(Client), Req->command);
381 if (Client_Type(Client) == CLIENT_SERVER)
382 target = Client_Search(Req->prefix);
386 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
387 Client_ID(Client), Req->prefix);
389 /* Loop over all the given channel names */
390 chan = strtok(Req->argv[0], ",");
392 /* Make sure that "chan" is not the empty string ("PART :") */
394 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
395 Client_ID(Client), Req->command);
398 Channel_Part(target, Client, chan,
399 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
400 chan = strtok(NULL, ",");
403 /* Update idle time, if local client */
404 if (Client_Conn(Client) > NONE)
405 Conn_UpdateIdle(Client_Conn(Client));
412 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
417 bool onchannel, topicok, use_servermode, r;
419 assert( Client != NULL );
420 assert( Req != NULL );
422 if (Req->argc < 1 || Req->argc > 2)
423 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
424 Client_ID(Client), Req->command);
426 if (Client_Type(Client) == CLIENT_SERVER)
427 from = Client_Search(Req->prefix);
432 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
433 Client_ID(Client), Req->prefix);
435 chan = Channel_Search(Req->argv[0]);
437 return IRC_WriteStrClient(from, ERR_NOSUCHCHANNEL_MSG,
438 Client_ID(from), Req->argv[0]);
440 Channel_CheckAdminRights(chan, Client, from,
441 &onchannel, &topicok, &use_servermode);
443 if (!onchannel && !topicok)
444 return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
445 Client_ID(from), Req->argv[0]);
447 if (Req->argc == 1) {
448 /* Request actual topic */
449 topic = Channel_Topic(chan);
451 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
453 Channel_Name(chan), topic);
455 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
458 Channel_TopicWho(chan),
459 Channel_TopicTime(chan));
464 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
469 if (strchr(Channel_Modes(chan), 't')) {
470 /* Topic Lock. Is the user a channel or IRC operator? */
472 return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
478 Channel_SetTopic(chan, from, Req->argv[1]);
479 LogDebug("%s \"%s\" set topic on \"%s\": %s",
480 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
481 Req->argv[1][0] ? Req->argv[1] : "<none>");
484 from = Client_ThisServer();
486 /* Update channel and forward new topic to other servers */
487 if (!Channel_IsLocal(chan))
488 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
489 Req->argv[0], Req->argv[1]);
490 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
491 Req->argv[0], Req->argv[1]);
493 if (Client_Type(Client) == CLIENT_USER)
494 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
495 Req->argv[0], Req->argv[1]);
502 * Handler for the IRC "LIST" command.
503 * This implementation handles the local case as well as the forwarding of the
504 * LIST command to other servers in the IRC network.
507 IRC_LIST( CLIENT *Client, REQUEST *Req )
511 CLIENT *from, *target;
513 assert( Client != NULL );
514 assert( Req != NULL );
516 /* Bad number of prameters? */
518 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
519 Client_ID( Client ), Req->command );
522 pattern = strtok( Req->argv[0], "," );
526 /* Get sender from prefix, if any */
527 if( Client_Type( Client ) == CLIENT_SERVER )
528 from = Client_Search( Req->prefix );
533 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
534 Client_ID( Client ), Req->prefix );
538 /* Forward to other server? */
539 target = Client_Search( Req->argv[1] );
540 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
541 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
542 Client_ID( Client ), Req->argv[1] );
544 if( target != Client_ThisServer( ))
546 /* Target is indeed an other server, forward it! */
547 return IRC_WriteStrClientPrefix( target, from,
548 "LIST %s :%s", Client_ID( from ),
555 /* Loop through all the channels */
556 chan = Channel_First( );
559 /* Check search pattern */
560 if( Match( pattern, Channel_Name( chan )))
563 if( ! strchr( Channel_Modes( chan ), 's' ) ||
564 Channel_IsMemberOf( chan, from ))
566 if( ! IRC_WriteStrClient( from,
567 RPL_LIST_MSG, Client_ID( from ),
568 Channel_Name( chan ),
569 Channel_MemberCount( chan ),
570 Channel_Topic( chan )))
574 chan = Channel_Next( chan );
577 /* Get next name ... */
579 pattern = strtok( NULL, "," );
584 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
589 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
591 char modes_add[COMMAND_LEN], l[16], *ptr;
596 assert( Client != NULL );
597 assert( Req != NULL );
599 /* Bad number of parameters? */
600 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
602 /* Compatibility kludge */
603 if( Req->argc == 5 ) arg_topic = 4;
604 else if( Req->argc == 3 ) arg_topic = 2;
608 from = Client_Search( Req->prefix );
609 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
611 /* Search or create channel */
612 chan = Channel_Search( Req->argv[0] );
613 if( ! chan ) chan = Channel_Create( Req->argv[0] );
614 if( ! chan ) return CONNECTED;
616 if( Req->argv[1][0] == '+' )
618 ptr = Channel_Modes( chan );
621 /* OK, this channel doesn't have modes jet, set the received ones: */
622 Channel_SetModes( chan, &Req->argv[1][1] );
626 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
627 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
631 /* Delete modes which we never want to inherit */
632 Channel_ModeDel( chan, 'l' );
633 Channel_ModeDel( chan, 'k' );
636 strcpy( modes_add, "" );
637 ptr = Channel_Modes( chan );
642 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
643 strlcat( modes_add, l, sizeof( modes_add ));
647 strlcat( modes_add, " ", sizeof( modes_add ));
648 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
653 /* Inform members of this channel */
654 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
657 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
662 ptr = Channel_Topic( chan );
663 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
665 /* OK, there is no topic jet */
666 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
667 IRC_WriteStrChannelPrefix(Client, chan, from, false,
668 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
672 /* Forward CHANINFO to other serevrs */
673 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] );
674 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
675 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );