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.
16 * IRC channel commands
28 #include "conn-func.h"
35 #include "irc-write.h"
39 #include "irc-channel.h"
43 * RFC 2812, (3.2.1 Join message Command):
44 * Note that this message
45 * accepts a special argument ("0"), which is a special request to leave all
46 * channels the user is currently a member of. The server will process this
47 * message as if the user had sent a PART command (See Section 3.2.2) for
48 * each channel he is a member of.
51 part_from_all_channels(CLIENT* client, CLIENT *target)
56 while ((cl2chan = Channel_FirstChannelOf(target))) {
57 chan = Channel_GetChannel(cl2chan);
58 assert( chan != NULL );
59 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
66 * Check weather a local client is allowed to join an already existing
68 * @param Client Client that sent the JOIN command
69 * @param chan Channel to check
70 * @param channame Name of the channel
71 * @param key Provided channel key (or NULL if none has been provided)
72 * @return true if client is allowed to join channel, false otherwise
75 join_allowed(CLIENT *Client, CHANNEL *chan, const char *channame,
78 bool is_invited, is_banned;
79 const char *channel_modes;
81 /* Allow IRC operators to overwrite channel limits */
82 if (strchr(Client_Modes(Client), 'o'))
85 is_banned = Lists_Check(Channel_GetListBans(chan), Client);
86 is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
88 if (is_banned && !is_invited) {
89 /* Client is banned from channel (and not on invite list) */
90 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
91 Client_ID(Client), channame);
95 channel_modes = Channel_Modes(chan);
96 if (strchr(channel_modes, 'i') && !is_invited) {
97 /* Channel is "invite-only" and client is not on invite list */
98 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG,
99 Client_ID(Client), channame);
103 if (!Channel_CheckKey(chan, Client, key ? key : "")) {
104 /* Channel is protected by a channel key and the client
105 * didn't specify the correct one */
106 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
107 Client_ID(Client), channame);
111 if (strchr(channel_modes, 'l') &&
112 (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
113 /* There are more clints joined to this channel than allowed */
114 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG,
115 Client_ID(Client), channame);
119 if (strchr(channel_modes, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
120 /* Only "secure" clients are allowed, but clients doesn't
121 * use SSL encryption */
122 IRC_WriteStrClient(Client, ERR_SECURECHANNEL_MSG,
123 Client_ID(Client), channame);
127 if (strchr(channel_modes, 'O') && !Client_OperByMe(Client)) {
128 /* Only IRC operators are allowed! */
129 IRC_WriteStrClient(Client, ERR_OPONLYCHANNEL_MSG,
130 Client_ID(Client), channame);
139 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
143 Channel_UserModeAdd(chan, target, *flags);
148 /* If channel persistent and client is ircop: make client chanop */
149 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
150 Channel_UserModeAdd(chan, target, 'o');
155 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
158 char str[COMMAND_LEN], *ptr = NULL;
160 strlcpy(str, (char *)Data, sizeof(str));
161 conn = Client_Conn(To);
163 if (Conn_Options(conn) & CONN_RFC1459) {
164 /* RFC 1459 compatibility mode, appended modes are NOT
165 * supported, so strip them off! */
166 ptr = strchr(str, 0x7);
171 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
173 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
175 } /* cb_join_forward */
179 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
180 const char *channame)
182 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
184 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
190 /* forward to other servers (if it is not a local channel) */
191 if (!Channel_IsLocal(chan)) {
192 snprintf(str, sizeof(str), "%s%s", channame, modes);
193 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
194 cb_join_forward, str);
197 /* tell users in this channel about the new client */
198 IRC_WriteStrChannelPrefix(Client, chan, target, false,
199 "JOIN :%s", channame);
201 /* syncronize channel modes */
203 IRC_WriteStrChannelPrefix(Client, chan, target, false,
204 "MODE %s +%s %s", channame,
205 &modes[1], Client_ID(target));
211 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
212 const char *channame)
216 if (Client_Type(Client) != CLIENT_USER)
218 /* acknowledge join */
219 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
222 /* Send topic to client, if any */
223 topic = Channel_Topic(chan);
224 assert(topic != NULL);
226 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
227 Client_ID(Client), channame, topic))
230 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
231 Client_ID(Client), channame,
232 Channel_TopicWho(chan),
233 Channel_TopicTime(chan)))
237 /* send list of channel members to client */
238 if (!IRC_Send_NAMES(Client, chan))
240 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
245 IRC_JOIN( CLIENT *Client, REQUEST *Req )
247 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
251 assert( Client != NULL );
252 assert( Req != NULL );
254 /* Bad number of arguments? */
255 if (Req->argc < 1 || Req->argc > 2)
256 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
257 Client_ID(Client), Req->command);
259 /* Who is the sender? */
260 if (Client_Type(Client) == CLIENT_SERVER)
261 target = Client_Search(Req->prefix);
266 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
268 /* Is argument "0"? */
269 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
270 return part_from_all_channels(Client, target);
272 /* Are channel keys given? */
274 key = strtok_r(Req->argv[1], ",", &lastkey);
276 channame = Req->argv[0];
277 channame = strtok_r(channame, ",", &lastchan);
279 /* Make sure that "channame" is not the empty string ("JOIN :") */
281 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
282 Client_ID(Client), Req->command);
287 /* Did the server include channel-user-modes? */
288 if (Client_Type(Client) == CLIENT_SERVER) {
289 flags = strchr(channame, 0x7);
296 chan = Channel_Search(channame);
297 if (!chan && Conf_PredefChannelsOnly) {
298 /* channel must be created, but server does not allow this */
299 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
304 if (Client_Type(Client) == CLIENT_USER) {
305 /* Test if the user has reached the channel limit */
306 if ((Conf_MaxJoins > 0) &&
307 (Channel_CountForUser(Client) >= Conf_MaxJoins))
308 return IRC_WriteStrClient(Client,
309 ERR_TOOMANYCHANNELS_MSG,
310 Client_ID(Client), channame);
312 /* Already existing channel: check if the
313 * client is allowed to join */
314 if (!join_allowed(Client, chan, channame, key))
317 /* New channel: first user will become channel
318 * operator unless this is a modeless channel */
319 if (*channame != '+')
323 /* Local client: update idle time */
324 Conn_UpdateIdle(Client_Conn(Client));
326 /* Remote server: we don't need to know whether the
327 * client is invited or not, but we have to make sure
328 * that the "one shot" entries (generated by INVITE
329 * commands) in this list become deleted when a user
330 * joins a channel this way. */
332 (void)Lists_Check(Channel_GetListInvites(chan),
336 /* Join channel (and create channel if it doesn't exist) */
337 if (!Channel_Join(target, channame))
340 if (!chan) { /* channel is new; it has been created above */
341 chan = Channel_Search(channame);
342 assert(chan != NULL);
343 if (Channel_IsModeless(chan)) {
344 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
345 Channel_ModeAdd(chan, 'n'); /* no external msgs */
348 assert(chan != NULL);
350 join_set_channelmodes(chan, target, flags);
352 join_forward(Client, target, chan, channame);
354 if (!join_send_topic(Client, target, chan, channame))
355 break; /* write error */
358 channame = strtok_r(NULL, ",", &lastchan);
360 key = strtok_r(NULL, ",", &lastkey);
367 * Handler for the IRC "PART" command.
370 IRC_PART(CLIENT * Client, REQUEST * Req)
375 assert(Client != NULL);
378 if (Req->argc < 1 || Req->argc > 2)
379 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
380 Client_ID(Client), Req->command);
383 if (Client_Type(Client) == CLIENT_SERVER)
384 target = Client_Search(Req->prefix);
388 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
389 Client_ID(Client), Req->prefix);
391 /* Loop over all the given channel names */
392 chan = strtok(Req->argv[0], ",");
394 /* Make sure that "chan" is not the empty string ("PART :") */
396 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
397 Client_ID(Client), Req->command);
400 Channel_Part(target, Client, chan,
401 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
402 chan = strtok(NULL, ",");
405 /* Update idle time, if local client */
406 if (Client_Conn(Client) > NONE)
407 Conn_UpdateIdle(Client_Conn(Client));
414 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
419 bool onchannel, topicok, use_servermode, r;
421 assert( Client != NULL );
422 assert( Req != NULL );
424 if (Req->argc < 1 || Req->argc > 2)
425 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
426 Client_ID(Client), Req->command);
428 if (Client_Type(Client) == CLIENT_SERVER)
429 from = Client_Search(Req->prefix);
434 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
435 Client_ID(Client), Req->prefix);
437 chan = Channel_Search(Req->argv[0]);
439 return IRC_WriteStrClient(from, ERR_NOSUCHCHANNEL_MSG,
440 Client_ID(from), Req->argv[0]);
442 Channel_CheckAdminRights(chan, Client, from,
443 &onchannel, &topicok, &use_servermode);
445 if (!onchannel && !topicok)
446 return IRC_WriteStrClient(from, ERR_NOTONCHANNEL_MSG,
447 Client_ID(from), Req->argv[0]);
449 if (Req->argc == 1) {
450 /* Request actual topic */
451 topic = Channel_Topic(chan);
453 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
455 Channel_Name(chan), topic);
457 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
460 Channel_TopicWho(chan),
461 Channel_TopicTime(chan));
466 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
471 if (strchr(Channel_Modes(chan), 't')) {
472 /* Topic Lock. Is the user a channel or IRC operator? */
474 return IRC_WriteStrClient(from, ERR_CHANOPRIVSNEEDED_MSG,
480 Channel_SetTopic(chan, from, Req->argv[1]);
481 LogDebug("%s \"%s\" set topic on \"%s\": %s",
482 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
483 Req->argv[1][0] ? Req->argv[1] : "<none>");
486 from = Client_ThisServer();
488 /* Update channel and forward new topic to other servers */
489 if (!Channel_IsLocal(chan))
490 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
491 Req->argv[0], Req->argv[1]);
492 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
493 Req->argv[0], Req->argv[1]);
495 if (Client_Type(Client) == CLIENT_USER)
496 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
497 Req->argv[0], Req->argv[1]);
504 * Handler for the IRC "LIST" command.
505 * This implementation handles the local case as well as the forwarding of the
506 * LIST command to other servers in the IRC network.
509 IRC_LIST( CLIENT *Client, REQUEST *Req )
513 CLIENT *from, *target;
515 assert( Client != NULL );
516 assert( Req != NULL );
518 /* Bad number of prameters? */
520 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
521 Client_ID( Client ), Req->command );
524 pattern = strtok( Req->argv[0], "," );
528 /* Get sender from prefix, if any */
529 if( Client_Type( Client ) == CLIENT_SERVER )
530 from = Client_Search( Req->prefix );
535 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
536 Client_ID( Client ), Req->prefix );
540 /* Forward to other server? */
541 target = Client_Search( Req->argv[1] );
542 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
543 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
544 Client_ID( Client ), Req->argv[1] );
546 if( target != Client_ThisServer( ))
548 /* Target is indeed an other server, forward it! */
549 return IRC_WriteStrClientPrefix( target, from,
550 "LIST %s :%s", Client_ID( from ),
557 /* Loop through all the channels */
558 chan = Channel_First( );
561 /* Check search pattern */
562 if( Match( pattern, Channel_Name( chan )))
565 if( ! strchr( Channel_Modes( chan ), 's' ) ||
566 Channel_IsMemberOf( chan, from ))
568 if( ! IRC_WriteStrClient( from,
569 RPL_LIST_MSG, Client_ID( from ),
570 Channel_Name( chan ),
571 Channel_MemberCount( chan ),
572 Channel_Topic( chan )))
576 chan = Channel_Next( chan );
579 /* Get next name ... */
581 pattern = strtok( NULL, "," );
586 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
591 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
593 char modes_add[COMMAND_LEN], l[16], *ptr;
598 assert( Client != NULL );
599 assert( Req != NULL );
601 /* Bad number of parameters? */
602 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
604 /* Compatibility kludge */
605 if( Req->argc == 5 ) arg_topic = 4;
606 else if( Req->argc == 3 ) arg_topic = 2;
610 from = Client_Search( Req->prefix );
611 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
613 /* Search or create channel */
614 chan = Channel_Search( Req->argv[0] );
615 if( ! chan ) chan = Channel_Create( Req->argv[0] );
616 if( ! chan ) return CONNECTED;
618 if( Req->argv[1][0] == '+' )
620 ptr = Channel_Modes( chan );
623 /* OK, this channel doesn't have modes jet, set the received ones: */
624 Channel_SetModes( chan, &Req->argv[1][1] );
628 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
629 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
633 /* Delete modes which we never want to inherit */
634 Channel_ModeDel( chan, 'l' );
635 Channel_ModeDel( chan, 'k' );
638 strcpy( modes_add, "" );
639 ptr = Channel_Modes( chan );
644 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
645 strlcat( modes_add, l, sizeof( modes_add ));
649 strlcat( modes_add, " ", sizeof( modes_add ));
650 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
655 /* Inform members of this channel */
656 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
659 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
664 ptr = Channel_Topic( chan );
665 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
667 /* OK, there is no topic jet */
668 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
669 IRC_WriteStrChannelPrefix(Client, chan, from, false,
670 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
674 /* Forward CHANINFO to other serevrs */
675 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] );
676 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
677 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );