2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 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"
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);
124 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
128 Channel_UserModeAdd(chan, target, *flags);
133 /* If channel persistent and client is ircop: make client chanop */
134 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
135 Channel_UserModeAdd(chan, target, 'o');
140 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
143 char str[COMMAND_LEN], *ptr = NULL;
145 strlcpy(str, (char *)Data, sizeof(str));
146 conn = Client_Conn(To);
148 if (Conn_Options(conn) & CONN_RFC1459) {
149 /* RFC 1459 compatibility mode, appended modes are NOT
150 * supported, so strip them off! */
151 ptr = strchr(str, 0x7);
156 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
158 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
160 } /* cb_join_forward */
164 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
165 const char *channame)
167 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
169 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
175 /* forward to other servers (if it is not a local channel) */
176 if (!Channel_IsLocal(chan)) {
177 snprintf(str, sizeof(str), "%s%s", channame, modes);
178 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
179 cb_join_forward, str);
182 /* tell users in this channel about the new client */
183 IRC_WriteStrChannelPrefix(Client, chan, target, false,
184 "JOIN :%s", channame);
186 /* syncronize channel modes */
188 IRC_WriteStrChannelPrefix(Client, chan, target, false,
189 "MODE %s +%s %s", channame,
190 &modes[1], Client_ID(target));
196 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
197 const char *channame)
201 if (Client_Type(Client) != CLIENT_USER)
203 /* acknowledge join */
204 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
207 /* Send topic to client, if any */
208 topic = Channel_Topic(chan);
209 assert(topic != NULL);
211 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
212 Client_ID(Client), channame, topic))
215 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
216 Client_ID(Client), channame,
217 Channel_TopicWho(chan),
218 Channel_TopicTime(chan)))
222 /* send list of channel members to client */
223 if (!IRC_Send_NAMES(Client, chan))
225 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
230 IRC_JOIN( CLIENT *Client, REQUEST *Req )
232 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
236 assert( Client != NULL );
237 assert( Req != NULL );
239 /* Bad number of arguments? */
240 if (Req->argc < 1 || Req->argc > 2)
241 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
242 Client_ID(Client), Req->command);
244 /* Who is the sender? */
245 if (Client_Type(Client) == CLIENT_SERVER)
246 target = Client_Search(Req->prefix);
251 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
253 /* Is argument "0"? */
254 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
255 return part_from_all_channels(Client, target);
257 /* Are channel keys given? */
259 key = strtok_r(Req->argv[1], ",", &lastkey);
261 channame = Req->argv[0];
262 channame = strtok_r(channame, ",", &lastchan);
264 /* Make sure that "channame" is not the empty string ("JOIN :") */
266 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
267 Client_ID(Client), Req->command);
272 /* Did the server include channel-user-modes? */
273 if (Client_Type(Client) == CLIENT_SERVER) {
274 flags = strchr(channame, 0x7);
281 chan = Channel_Search(channame);
282 if (!chan && Conf_PredefChannelsOnly) {
283 /* channel must be created, but server does not allow this */
284 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
289 if (Client_Type(Client) == CLIENT_USER) {
290 /* Test if the user has reached the channel limit */
291 if ((Conf_MaxJoins > 0) &&
292 (Channel_CountForUser(Client) >= Conf_MaxJoins))
293 return IRC_WriteStrClient(Client,
294 ERR_TOOMANYCHANNELS_MSG,
295 Client_ID(Client), channame);
297 /* Already existing channel: check if the
298 * client is allowed to join */
299 if (!join_allowed(Client, chan, channame, key))
302 /* New channel: first user will become channel
303 * operator unless this is a modeless channel */
304 if (*channame != '+')
308 /* Local client: update idle time */
309 Conn_UpdateIdle(Client_Conn(Client));
311 /* Remote server: we don't need to know whether the
312 * client is invited or not, but we have to make sure
313 * that the "one shot" entries (generated by INVITE
314 * commands) in this list become deleted when a user
315 * joins a channel this way. */
317 (void)Lists_Check(Channel_GetListInvites(chan),
321 /* Join channel (and create channel if it doesn't exist) */
322 if (!Channel_Join(target, channame))
325 if (!chan) { /* channel is new; it has been created above */
326 chan = Channel_Search(channame);
327 assert(chan != NULL);
328 if (Channel_IsModeless(chan)) {
329 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
330 Channel_ModeAdd(chan, 'n'); /* no external msgs */
333 assert(chan != NULL);
335 join_set_channelmodes(chan, target, flags);
337 join_forward(Client, target, chan, channame);
339 if (!join_send_topic(Client, target, chan, channame))
340 break; /* write error */
343 channame = strtok_r(NULL, ",", &lastchan);
345 key = strtok_r(NULL, ",", &lastkey);
352 * Handler for the IRC "PART" command.
355 IRC_PART(CLIENT * Client, REQUEST * Req)
360 assert(Client != NULL);
363 if (Req->argc < 1 || Req->argc > 2)
364 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
365 Client_ID(Client), Req->command);
368 if (Client_Type(Client) == CLIENT_SERVER)
369 target = Client_Search(Req->prefix);
373 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
374 Client_ID(Client), Req->prefix);
376 /* Loop over all the given channel names */
377 chan = strtok(Req->argv[0], ",");
379 /* Make sure that "chan" is not the empty string ("PART :") */
381 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
382 Client_ID(Client), Req->command);
385 Channel_Part(target, Client, chan,
386 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
387 chan = strtok(NULL, ",");
390 /* Update idle time, if local client */
391 if (Client_Conn(Client) > NONE)
392 Conn_UpdateIdle(Client_Conn(Client));
399 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
406 assert( Client != NULL );
407 assert( Req != NULL );
409 if ((Req->argc < 1) || (Req->argc > 2))
410 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
412 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
414 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
416 /* Welcher Channel? */
417 chan = Channel_Search( Req->argv[0] );
418 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
420 /* Ist der User Mitglied in dem Channel? */
421 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
425 /* Request actual topic */
426 topic = Channel_Topic(chan);
428 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
429 Client_ID(Client), Channel_Name(chan), topic);
431 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
432 Client_ID(Client), Channel_Name(chan),
433 Channel_TopicWho(chan),
434 Channel_TopicTime(chan));
439 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
440 Client_ID(from), Channel_Name(chan));
443 if( strchr( Channel_Modes( chan ), 't' ))
445 /* Topic Lock. Ist der User ein Channel Operator? */
446 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
450 Channel_SetTopic(chan, from, Req->argv[1]);
451 LogDebug("%s \"%s\" set topic on \"%s\": %s",
452 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
453 Req->argv[1][0] ? Req->argv[1] : "<none>");
455 /* Update channel and forward new topic to other servers */
456 if (!Channel_IsLocal(chan))
457 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
458 Req->argv[0], Req->argv[1]);
459 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
460 Req->argv[0], Req->argv[1]);
462 if (Client_Type(Client) == CLIENT_USER)
463 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
464 Req->argv[0], Req->argv[1]);
471 * Handler for the IRC "LIST" command.
472 * This implementation handles the local case as well as the forwarding of the
473 * LIST command to other servers in the IRC network.
476 IRC_LIST( CLIENT *Client, REQUEST *Req )
480 CLIENT *from, *target;
482 assert( Client != NULL );
483 assert( Req != NULL );
485 /* Bad number of prameters? */
487 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
488 Client_ID( Client ), Req->command );
491 pattern = strtok( Req->argv[0], "," );
495 /* Get sender from prefix, if any */
496 if( Client_Type( Client ) == CLIENT_SERVER )
497 from = Client_Search( Req->prefix );
502 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
503 Client_ID( Client ), Req->prefix );
507 /* Forward to other server? */
508 target = Client_Search( Req->argv[1] );
509 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
510 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
511 Client_ID( Client ), Req->argv[1] );
513 if( target != Client_ThisServer( ))
515 /* Target is indeed an other server, forward it! */
516 return IRC_WriteStrClientPrefix( target, from,
517 "LIST %s :%s", Client_ID( from ),
524 /* Loop through all the channels */
525 chan = Channel_First( );
528 /* Check search pattern */
529 if( Match( pattern, Channel_Name( chan )))
532 if( ! strchr( Channel_Modes( chan ), 's' ) ||
533 Channel_IsMemberOf( chan, from ))
535 if( ! IRC_WriteStrClient( from,
536 RPL_LIST_MSG, Client_ID( from ),
537 Channel_Name( chan ),
538 Channel_MemberCount( chan ),
539 Channel_Topic( chan )))
543 chan = Channel_Next( chan );
546 /* Get next name ... */
548 pattern = strtok( NULL, "," );
553 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
558 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
560 char modes_add[COMMAND_LEN], l[16], *ptr;
565 assert( Client != NULL );
566 assert( Req != NULL );
568 /* Bad number of parameters? */
569 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
571 /* Compatibility kludge */
572 if( Req->argc == 5 ) arg_topic = 4;
573 else if( Req->argc == 3 ) arg_topic = 2;
577 from = Client_Search( Req->prefix );
578 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
580 /* Search or create channel */
581 chan = Channel_Search( Req->argv[0] );
582 if( ! chan ) chan = Channel_Create( Req->argv[0] );
583 if( ! chan ) return CONNECTED;
585 if( Req->argv[1][0] == '+' )
587 ptr = Channel_Modes( chan );
590 /* OK, this channel doesn't have modes jet, set the received ones: */
591 Channel_SetModes( chan, &Req->argv[1][1] );
595 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
596 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
600 /* Delete modes which we never want to inherit */
601 Channel_ModeDel( chan, 'l' );
602 Channel_ModeDel( chan, 'k' );
605 strcpy( modes_add, "" );
606 ptr = Channel_Modes( chan );
611 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
612 strlcat( modes_add, l, sizeof( modes_add ));
616 strlcat( modes_add, " ", sizeof( modes_add ));
617 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
622 /* Inform members of this channel */
623 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
626 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
631 ptr = Channel_Topic( chan );
632 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
634 /* OK, there is no topic jet */
635 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
636 IRC_WriteStrChannelPrefix(Client, chan, from, false,
637 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
641 /* Forward CHANINFO to other serevrs */
642 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] );
643 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
644 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );