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 join_allowed(CLIENT *Client, CLIENT *target, CHANNEL *chan,
67 const char *channame, const char *key)
69 bool is_invited, is_banned;
70 const char *channel_modes;
72 /* Allow IRC operators to overwrite channel limits */
73 if (strchr(Client_Modes(Client), 'o'))
76 is_banned = Lists_Check(Channel_GetListBans(chan), target);
77 is_invited = Lists_Check(Channel_GetListInvites(chan), target);
79 if (is_banned && !is_invited) {
80 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
84 channel_modes = Channel_Modes(chan);
85 if ((strchr(channel_modes, 'i')) && !is_invited) {
86 /* Channel is "invite-only" (and Client wasn't invited) */
87 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG, Client_ID(Client), channame);
91 /* Is the channel protected by a key? */
92 if (!Channel_CheckKey(chan, target, key ? key : "")) {
93 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
94 Client_ID(Client), channame);
97 /* Are there already too many members? */
98 if ((strchr(channel_modes, 'l')) && (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
99 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG, Client_ID(Client), channame);
107 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
111 Channel_UserModeAdd(chan, target, *flags);
116 /* If channel persistent and client is ircop: make client chanop */
117 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
118 Channel_UserModeAdd(chan, target, 'o');
123 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
126 char str[COMMAND_LEN], *ptr = NULL;
128 strlcpy(str, (char *)Data, sizeof(str));
129 conn = Client_Conn(To);
131 if (Conn_Options(conn) & CONN_RFC1459) {
132 /* RFC 1459 compatibility mode, appended modes are NOT
133 * supported, so strip them off! */
134 ptr = strchr(str, 0x7);
139 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
141 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
143 } /* cb_join_forward */
147 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
148 const char *channame)
150 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
152 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
158 /* forward to other servers (if it is not a local channel) */
159 if (!Channel_IsLocal(chan)) {
160 snprintf(str, sizeof(str), "%s%s", channame, modes);
161 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
162 cb_join_forward, str);
165 /* tell users in this channel about the new client */
166 IRC_WriteStrChannelPrefix(Client, chan, target, false,
167 "JOIN :%s", channame);
169 /* syncronize channel modes */
171 IRC_WriteStrChannelPrefix(Client, chan, target, false,
172 "MODE %s +%s %s", channame,
173 &modes[1], Client_ID(target));
179 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
180 const char *channame)
184 if (Client_Type(Client) != CLIENT_USER)
186 /* acknowledge join */
187 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
190 /* Send topic to client, if any */
191 topic = Channel_Topic(chan);
192 assert(topic != NULL);
194 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
195 Client_ID(Client), channame, topic))
198 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
199 Client_ID(Client), channame,
200 Channel_TopicWho(chan),
201 Channel_TopicTime(chan)))
205 /* send list of channel members to client */
206 if (!IRC_Send_NAMES(Client, chan))
208 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
213 IRC_JOIN( CLIENT *Client, REQUEST *Req )
215 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
219 assert( Client != NULL );
220 assert( Req != NULL );
222 /* Bad number of arguments? */
223 if (Req->argc < 1 || Req->argc > 2)
224 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
225 Client_ID(Client), Req->command);
227 /* Who is the sender? */
228 if (Client_Type(Client) == CLIENT_SERVER)
229 target = Client_Search(Req->prefix);
234 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
236 /* Is argument "0"? */
237 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
238 return part_from_all_channels(Client, target);
240 /* Are channel keys given? */
242 key = strtok_r(Req->argv[1], ",", &lastkey);
244 channame = Req->argv[0];
245 channame = strtok_r(channame, ",", &lastchan);
247 /* Make sure that "channame" is not the empty string ("JOIN :") */
249 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
250 Client_ID(Client), Req->command);
255 /* Did the server include channel-user-modes? */
256 if (Client_Type(Client) == CLIENT_SERVER) {
257 flags = strchr(channame, 0x7);
264 chan = Channel_Search(channame);
265 if (!chan && Conf_PredefChannelsOnly) {
266 /* channel must be created, but server does not allow this */
267 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
272 if (Client_Type(Client) == CLIENT_USER) {
273 /* Test if the user has reached his maximum channel count */
274 if ((Conf_MaxJoins > 0) && (Channel_CountForUser(Client) >= Conf_MaxJoins))
275 return IRC_WriteStrClient(Client, ERR_TOOMANYCHANNELS_MSG,
276 Client_ID(Client), channame);
279 * New Channel: first user will be channel operator
280 * unless this is a modeless channel.
282 if (*channame != '+')
285 if (!join_allowed(Client, target, chan, channame, key))
288 /* Local client: update idle time */
289 Conn_UpdateIdle(Client_Conn(Client));
291 /* Remote server: we don't need to know whether the
292 * client is invited or not, but we have to make sure
293 * that the "one shot" entries (generated by INVITE
294 * commands) in this list become deleted when a user
295 * joins a channel this way. */
296 if (chan) (void)Lists_Check(Channel_GetListInvites(chan), target);
299 /* Join channel (and create channel if it doesn't exist) */
300 if (!Channel_Join(target, channame))
303 if (!chan) { /* channel is new; it has been created above */
304 chan = Channel_Search(channame);
305 assert(chan != NULL);
306 if (Channel_IsModeless(chan)) {
307 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
308 Channel_ModeAdd(chan, 'n'); /* no external msgs */
311 assert(chan != NULL);
313 join_set_channelmodes(chan, target, flags);
315 join_forward(Client, target, chan, channame);
317 if (!join_send_topic(Client, target, chan, channame))
318 break; /* write error */
321 channame = strtok_r(NULL, ",", &lastchan);
323 key = strtok_r(NULL, ",", &lastkey);
330 * Handler for the IRC "PART" command.
333 IRC_PART(CLIENT * Client, REQUEST * Req)
338 assert(Client != NULL);
341 if (Req->argc < 1 || Req->argc > 2)
342 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
343 Client_ID(Client), Req->command);
346 if (Client_Type(Client) == CLIENT_SERVER)
347 target = Client_Search(Req->prefix);
351 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
352 Client_ID(Client), Req->prefix);
354 /* Loop over all the given channel names */
355 chan = strtok(Req->argv[0], ",");
357 /* Make sure that "chan" is not the empty string ("PART :") */
359 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
360 Client_ID(Client), Req->command);
363 Channel_Part(target, Client, chan,
364 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
365 chan = strtok(NULL, ",");
368 /* Update idle time, if local client */
369 if (Client_Conn(Client) > NONE)
370 Conn_UpdateIdle(Client_Conn(Client));
377 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
384 assert( Client != NULL );
385 assert( Req != NULL );
387 if ((Req->argc < 1) || (Req->argc > 2))
388 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
390 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
392 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
394 /* Welcher Channel? */
395 chan = Channel_Search( Req->argv[0] );
396 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
398 /* Ist der User Mitglied in dem Channel? */
399 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
403 /* Request actual topic */
404 topic = Channel_Topic(chan);
406 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
407 Client_ID(Client), Channel_Name(chan), topic);
409 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
410 Client_ID(Client), Channel_Name(chan),
411 Channel_TopicWho(chan),
412 Channel_TopicTime(chan));
417 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
418 Client_ID(from), Channel_Name(chan));
421 if( strchr( Channel_Modes( chan ), 't' ))
423 /* Topic Lock. Ist der User ein Channel Operator? */
424 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
428 Channel_SetTopic(chan, from, Req->argv[1]);
429 LogDebug("%s \"%s\" set topic on \"%s\": %s",
430 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
431 Req->argv[1][0] ? Req->argv[1] : "<none>");
433 /* Update channel and forward new topic to other servers */
434 if (!Channel_IsLocal(chan))
435 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
436 Req->argv[0], Req->argv[1]);
437 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
438 Req->argv[0], Req->argv[1]);
440 if (Client_Type(Client) == CLIENT_USER)
441 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
442 Req->argv[0], Req->argv[1]);
449 * Handler for the IRC "LIST" command.
450 * This implementation handles the local case as well as the forwarding of the
451 * LIST command to other servers in the IRC network.
454 IRC_LIST( CLIENT *Client, REQUEST *Req )
458 CLIENT *from, *target;
460 assert( Client != NULL );
461 assert( Req != NULL );
463 /* Bad number of prameters? */
465 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
466 Client_ID( Client ), Req->command );
469 pattern = strtok( Req->argv[0], "," );
473 /* Get sender from prefix, if any */
474 if( Client_Type( Client ) == CLIENT_SERVER )
475 from = Client_Search( Req->prefix );
480 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
481 Client_ID( Client ), Req->prefix );
485 /* Forward to other server? */
486 target = Client_Search( Req->argv[1] );
487 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
488 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
489 Client_ID( Client ), Req->argv[1] );
491 if( target != Client_ThisServer( ))
493 /* Target is indeed an other server, forward it! */
494 return IRC_WriteStrClientPrefix( target, from,
495 "LIST %s :%s", Client_ID( from ),
502 /* Loop through all the channels */
503 chan = Channel_First( );
506 /* Check search pattern */
507 if( Match( pattern, Channel_Name( chan )))
510 if( ! strchr( Channel_Modes( chan ), 's' ) ||
511 Channel_IsMemberOf( chan, from ))
513 if( ! IRC_WriteStrClient( from,
514 RPL_LIST_MSG, Client_ID( from ),
515 Channel_Name( chan ),
516 Channel_MemberCount( chan ),
517 Channel_Topic( chan )))
521 chan = Channel_Next( chan );
524 /* Get next name ... */
526 pattern = strtok( NULL, "," );
531 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
536 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
538 char modes_add[COMMAND_LEN], l[16], *ptr;
543 assert( Client != NULL );
544 assert( Req != NULL );
546 /* Bad number of parameters? */
547 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
549 /* Compatibility kludge */
550 if( Req->argc == 5 ) arg_topic = 4;
551 else if( Req->argc == 3 ) arg_topic = 2;
555 from = Client_Search( Req->prefix );
556 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
558 /* Search or create channel */
559 chan = Channel_Search( Req->argv[0] );
560 if( ! chan ) chan = Channel_Create( Req->argv[0] );
561 if( ! chan ) return CONNECTED;
563 if( Req->argv[1][0] == '+' )
565 ptr = Channel_Modes( chan );
568 /* OK, this channel doesn't have modes jet, set the received ones: */
569 Channel_SetModes( chan, &Req->argv[1][1] );
573 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
574 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
578 /* Delete modes which we never want to inherit */
579 Channel_ModeDel( chan, 'l' );
580 Channel_ModeDel( chan, 'k' );
583 strcpy( modes_add, "" );
584 ptr = Channel_Modes( chan );
589 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
590 strlcat( modes_add, l, sizeof( modes_add ));
594 strlcat( modes_add, " ", sizeof( modes_add ));
595 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
600 /* Inform members of this channel */
601 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
604 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
609 ptr = Channel_Topic( chan );
610 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
612 /* OK, there is no topic jet */
613 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
614 IRC_WriteStrChannelPrefix(Client, chan, from, false,
615 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
619 /* Forward CHANINFO to other serevrs */
620 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] );
621 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
622 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );