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
27 #include "conn-func.h"
34 #include "irc-write.h"
38 #include "irc-channel.h"
42 * RFC 2812, (3.2.1 Join message Command):
43 * Note that this message
44 * accepts a special argument ("0"), which is a special request to leave all
45 * channels the user is currently a member of. The server will process this
46 * message as if the user had sent a PART command (See Section 3.2.2) for
47 * each channel he is a member of.
50 part_from_all_channels(CLIENT* client, CLIENT *target)
55 while ((cl2chan = Channel_FirstChannelOf(target))) {
56 chan = Channel_GetChannel(cl2chan);
57 assert( chan != NULL );
58 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
65 * Check weather a local client is allowed to join an already existing
67 * @param Client Client that sent the JOIN command
68 * @param chan Channel to check
69 * @param channame Name of the channel
70 * @param key Provided channel key (or NULL if none has been provided)
71 * @return true if client is allowed to join channel, false otherwise
74 join_allowed(CLIENT *Client, CHANNEL *chan, const char *channame,
77 bool is_invited, is_banned;
78 const char *channel_modes;
80 /* Allow IRC operators to overwrite channel limits */
81 if (strchr(Client_Modes(Client), 'o'))
84 is_banned = Lists_Check(Channel_GetListBans(chan), Client);
85 is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
87 if (is_banned && !is_invited) {
88 /* Client is banned from channel (and not on invite list) */
89 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
90 Client_ID(Client), channame);
94 channel_modes = Channel_Modes(chan);
95 if (strchr(channel_modes, 'i') && !is_invited) {
96 /* Channel is "invite-only" and client is not on invite list */
97 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG,
98 Client_ID(Client), channame);
102 if (!Channel_CheckKey(chan, Client, key ? key : "")) {
103 /* Channel is protected by a channel key and the client
104 * didn't specify the correct one */
105 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
106 Client_ID(Client), channame);
110 if (strchr(channel_modes, 'l') &&
111 (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
112 /* There are more clints joined to this channel than allowed */
113 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG,
114 Client_ID(Client), channame);
118 if (strchr(channel_modes, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
119 /* Only "secure" clients are allowed, but clients doesn't
120 * use SSL encryption */
121 IRC_WriteStrClient(Client, ERR_SECURECHANNEL_MSG,
122 Client_ID(Client), channame);
131 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
135 Channel_UserModeAdd(chan, target, *flags);
140 /* If channel persistent and client is ircop: make client chanop */
141 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
142 Channel_UserModeAdd(chan, target, 'o');
147 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
150 char str[COMMAND_LEN], *ptr = NULL;
152 strlcpy(str, (char *)Data, sizeof(str));
153 conn = Client_Conn(To);
155 if (Conn_Options(conn) & CONN_RFC1459) {
156 /* RFC 1459 compatibility mode, appended modes are NOT
157 * supported, so strip them off! */
158 ptr = strchr(str, 0x7);
163 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
165 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
167 } /* cb_join_forward */
171 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
172 const char *channame)
174 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
176 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
182 /* forward to other servers (if it is not a local channel) */
183 if (!Channel_IsLocal(chan)) {
184 snprintf(str, sizeof(str), "%s%s", channame, modes);
185 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
186 cb_join_forward, str);
189 /* tell users in this channel about the new client */
190 IRC_WriteStrChannelPrefix(Client, chan, target, false,
191 "JOIN :%s", channame);
193 /* syncronize channel modes */
195 IRC_WriteStrChannelPrefix(Client, chan, target, false,
196 "MODE %s +%s %s", channame,
197 &modes[1], Client_ID(target));
203 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
204 const char *channame)
208 if (Client_Type(Client) != CLIENT_USER)
210 /* acknowledge join */
211 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
214 /* Send topic to client, if any */
215 topic = Channel_Topic(chan);
216 assert(topic != NULL);
218 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
219 Client_ID(Client), channame, topic))
222 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
223 Client_ID(Client), channame,
224 Channel_TopicWho(chan),
225 Channel_TopicTime(chan)))
229 /* send list of channel members to client */
230 if (!IRC_Send_NAMES(Client, chan))
232 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
237 IRC_JOIN( CLIENT *Client, REQUEST *Req )
239 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
243 assert( Client != NULL );
244 assert( Req != NULL );
246 /* Bad number of arguments? */
247 if (Req->argc < 1 || Req->argc > 2)
248 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
249 Client_ID(Client), Req->command);
251 /* Who is the sender? */
252 if (Client_Type(Client) == CLIENT_SERVER)
253 target = Client_Search(Req->prefix);
258 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
260 /* Is argument "0"? */
261 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
262 return part_from_all_channels(Client, target);
264 /* Are channel keys given? */
266 key = strtok_r(Req->argv[1], ",", &lastkey);
268 channame = Req->argv[0];
269 channame = strtok_r(channame, ",", &lastchan);
271 /* Make sure that "channame" is not the empty string ("JOIN :") */
273 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
274 Client_ID(Client), Req->command);
279 /* Did the server include channel-user-modes? */
280 if (Client_Type(Client) == CLIENT_SERVER) {
281 flags = strchr(channame, 0x7);
288 chan = Channel_Search(channame);
289 if (!chan && Conf_PredefChannelsOnly) {
290 /* channel must be created, but server does not allow this */
291 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
296 if (Client_Type(Client) == CLIENT_USER) {
297 /* Test if the user has reached the channel limit */
298 if ((Conf_MaxJoins > 0) &&
299 (Channel_CountForUser(Client) >= Conf_MaxJoins))
300 return IRC_WriteStrClient(Client,
301 ERR_TOOMANYCHANNELS_MSG,
302 Client_ID(Client), channame);
304 /* Already existing channel: check if the
305 * client is allowed to join */
306 if (!join_allowed(Client, chan, channame, key))
309 /* New channel: first user will become channel
310 * operator unless this is a modeless channel */
311 if (*channame != '+')
315 /* Local client: update idle time */
316 Conn_UpdateIdle(Client_Conn(Client));
318 /* Remote server: we don't need to know whether the
319 * client is invited or not, but we have to make sure
320 * that the "one shot" entries (generated by INVITE
321 * commands) in this list become deleted when a user
322 * joins a channel this way. */
324 (void)Lists_Check(Channel_GetListInvites(chan),
328 /* Join channel (and create channel if it doesn't exist) */
329 if (!Channel_Join(target, channame))
332 if (!chan) { /* channel is new; it has been created above */
333 chan = Channel_Search(channame);
334 assert(chan != NULL);
335 if (Channel_IsModeless(chan)) {
336 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
337 Channel_ModeAdd(chan, 'n'); /* no external msgs */
340 assert(chan != NULL);
342 join_set_channelmodes(chan, target, flags);
344 join_forward(Client, target, chan, channame);
346 if (!join_send_topic(Client, target, chan, channame))
347 break; /* write error */
350 channame = strtok_r(NULL, ",", &lastchan);
352 key = strtok_r(NULL, ",", &lastkey);
359 * Handler for the IRC "PART" command.
362 IRC_PART(CLIENT * Client, REQUEST * Req)
367 assert(Client != NULL);
370 if (Req->argc < 1 || Req->argc > 2)
371 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
372 Client_ID(Client), Req->command);
375 if (Client_Type(Client) == CLIENT_SERVER)
376 target = Client_Search(Req->prefix);
380 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
381 Client_ID(Client), Req->prefix);
383 /* Loop over all the given channel names */
384 chan = strtok(Req->argv[0], ",");
386 /* Make sure that "chan" is not the empty string ("PART :") */
388 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
389 Client_ID(Client), Req->command);
392 Channel_Part(target, Client, chan,
393 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
394 chan = strtok(NULL, ",");
397 /* Update idle time, if local client */
398 if (Client_Conn(Client) > NONE)
399 Conn_UpdateIdle(Client_Conn(Client));
406 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
413 assert( Client != NULL );
414 assert( Req != NULL );
416 if ((Req->argc < 1) || (Req->argc > 2))
417 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
419 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
421 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
423 /* Welcher Channel? */
424 chan = Channel_Search( Req->argv[0] );
425 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
427 /* Ist der User Mitglied in dem Channel? */
428 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
432 /* Request actual topic */
433 topic = Channel_Topic(chan);
435 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
436 Client_ID(Client), Channel_Name(chan), topic);
438 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
439 Client_ID(Client), Channel_Name(chan),
440 Channel_TopicWho(chan),
441 Channel_TopicTime(chan));
446 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
447 Client_ID(from), Channel_Name(chan));
450 if( strchr( Channel_Modes( chan ), 't' ))
452 /* Topic Lock. Ist der User ein Channel Operator? */
453 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
457 Channel_SetTopic(chan, from, Req->argv[1]);
458 LogDebug("%s \"%s\" set topic on \"%s\": %s",
459 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
460 Req->argv[1][0] ? Req->argv[1] : "<none>");
462 /* Update channel and forward new topic to other servers */
463 if (!Channel_IsLocal(chan))
464 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
465 Req->argv[0], Req->argv[1]);
466 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
467 Req->argv[0], Req->argv[1]);
469 if (Client_Type(Client) == CLIENT_USER)
470 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
471 Req->argv[0], Req->argv[1]);
478 * Handler for the IRC "LIST" command.
479 * This implementation handles the local case as well as the forwarding of the
480 * LIST command to other servers in the IRC network.
483 IRC_LIST( CLIENT *Client, REQUEST *Req )
487 CLIENT *from, *target;
489 assert( Client != NULL );
490 assert( Req != NULL );
492 /* Bad number of prameters? */
494 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
495 Client_ID( Client ), Req->command );
498 pattern = strtok( Req->argv[0], "," );
502 /* Get sender from prefix, if any */
503 if( Client_Type( Client ) == CLIENT_SERVER )
504 from = Client_Search( Req->prefix );
509 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
510 Client_ID( Client ), Req->prefix );
514 /* Forward to other server? */
515 target = Client_Search( Req->argv[1] );
516 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
517 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
518 Client_ID( Client ), Req->argv[1] );
520 if( target != Client_ThisServer( ))
522 /* Target is indeed an other server, forward it! */
523 return IRC_WriteStrClientPrefix( target, from,
524 "LIST %s :%s", Client_ID( from ),
531 /* Loop through all the channels */
532 chan = Channel_First( );
535 /* Check search pattern */
536 if( Match( pattern, Channel_Name( chan )))
539 if( ! strchr( Channel_Modes( chan ), 's' ) ||
540 Channel_IsMemberOf( chan, from ))
542 if( ! IRC_WriteStrClient( from,
543 RPL_LIST_MSG, Client_ID( from ),
544 Channel_Name( chan ),
545 Channel_MemberCount( chan ),
546 Channel_Topic( chan )))
550 chan = Channel_Next( chan );
553 /* Get next name ... */
555 pattern = strtok( NULL, "," );
560 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
565 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
567 char modes_add[COMMAND_LEN], l[16], *ptr;
572 assert( Client != NULL );
573 assert( Req != NULL );
575 /* Bad number of parameters? */
576 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
578 /* Compatibility kludge */
579 if( Req->argc == 5 ) arg_topic = 4;
580 else if( Req->argc == 3 ) arg_topic = 2;
584 from = Client_Search( Req->prefix );
585 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
587 /* Search or create channel */
588 chan = Channel_Search( Req->argv[0] );
589 if( ! chan ) chan = Channel_Create( Req->argv[0] );
590 if( ! chan ) return CONNECTED;
592 if( Req->argv[1][0] == '+' )
594 ptr = Channel_Modes( chan );
597 /* OK, this channel doesn't have modes jet, set the received ones: */
598 Channel_SetModes( chan, &Req->argv[1][1] );
602 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
603 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
607 /* Delete modes which we never want to inherit */
608 Channel_ModeDel( chan, 'l' );
609 Channel_ModeDel( chan, 'k' );
612 strcpy( modes_add, "" );
613 ptr = Channel_Modes( chan );
618 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
619 strlcat( modes_add, l, sizeof( modes_add ));
623 strlcat( modes_add, " ", sizeof( modes_add ));
624 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
629 /* Inform members of this channel */
630 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
633 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
638 ptr = Channel_Topic( chan );
639 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
641 /* OK, there is no topic jet */
642 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
643 IRC_WriteStrChannelPrefix(Client, chan, from, false,
644 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
648 /* Forward CHANINFO to other serevrs */
649 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] );
650 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
651 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );