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 (strchr(channel_modes, 'k') &&
93 strcmp(Channel_Key(chan), key ? key : ""))
95 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG, Client_ID(Client), channame);
98 /* Are there already too many members? */
99 if ((strchr(channel_modes, 'l')) && (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
100 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG, Client_ID(Client), channame);
108 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
112 Channel_UserModeAdd(chan, target, *flags);
117 /* If channel persistent and client is ircop: make client chanop */
118 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
119 Channel_UserModeAdd(chan, target, 'o');
124 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
127 char str[COMMAND_LEN], *ptr = NULL;
129 strlcpy(str, (char *)Data, sizeof(str));
130 conn = Client_Conn(To);
132 if (Conn_Options(conn) & CONN_RFC1459) {
133 /* RFC 1459 compatibility mode, appended modes are NOT
134 * supported, so strip them off! */
135 ptr = strchr(str, 0x7);
140 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
142 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
144 } /* cb_join_forward */
148 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
149 const char *channame)
151 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
153 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
159 /* forward to other servers */
160 snprintf(str, sizeof(str), "%s%s", channame, modes);
161 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0', cb_join_forward, str);
163 /* tell users in this channel about the new client */
164 IRC_WriteStrChannelPrefix(Client, chan, target, false, "JOIN :%s", channame);
166 IRC_WriteStrChannelPrefix(Client, chan, target, false, "MODE %s +%s %s",
167 channame, &modes[1], Client_ID(target));
172 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
173 const char *channame)
177 if (Client_Type(Client) != CLIENT_USER)
179 /* acknowledge join */
180 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
183 /* Send topic to client, if any */
184 topic = Channel_Topic(chan);
185 assert(topic != NULL);
187 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
188 Client_ID(Client), channame, topic))
191 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
192 Client_ID(Client), channame,
193 Channel_TopicWho(chan),
194 Channel_TopicTime(chan)))
198 /* send list of channel members to client */
199 if (!IRC_Send_NAMES(Client, chan))
201 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
206 IRC_JOIN( CLIENT *Client, REQUEST *Req )
208 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
212 assert( Client != NULL );
213 assert( Req != NULL );
215 /* Bad number of arguments? */
216 if (Req->argc < 1 || Req->argc > 2)
217 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
218 Client_ID(Client), Req->command);
220 /* Who is the sender? */
221 if (Client_Type(Client) == CLIENT_SERVER)
222 target = Client_Search(Req->prefix);
227 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
229 /* Is argument "0"? */
230 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
231 return part_from_all_channels(Client, target);
233 /* Are channel keys given? */
235 key = strtok_r(Req->argv[1], ",", &lastkey);
237 channame = Req->argv[0];
238 channame = strtok_r(channame, ",", &lastchan);
240 /* Make sure that "channame" is not the empty string ("JOIN :") */
242 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
243 Client_ID(Client), Req->command);
248 /* Did the server include channel-user-modes? */
249 if (Client_Type(Client) == CLIENT_SERVER) {
250 flags = strchr(channame, 0x7);
257 chan = Channel_Search(channame);
258 if (!chan && Conf_PredefChannelsOnly) {
259 /* channel must be created, but server does not allow this */
260 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
265 if (Client_Type(Client) == CLIENT_USER) {
266 /* Test if the user has reached his maximum channel count */
267 if ((Conf_MaxJoins > 0) && (Channel_CountForUser(Client) >= Conf_MaxJoins))
268 return IRC_WriteStrClient(Client, ERR_TOOMANYCHANNELS_MSG,
269 Client_ID(Client), channame);
272 * New Channel: first user will be channel operator
273 * unless this is a modeless channel.
275 if (*channame != '+')
278 if (!join_allowed(Client, target, chan, channame, key))
281 /* Local client: update idle time */
282 Conn_UpdateIdle(Client_Conn(Client));
284 /* Remote server: we don't need to know whether the
285 * client is invited or not, but we have to make sure
286 * that the "one shot" entries (generated by INVITE
287 * commands) in this list become deleted when a user
288 * joins a channel this way. */
289 if (chan) (void)Lists_Check(Channel_GetListInvites(chan), target);
292 /* Join channel (and create channel if it doesn't exist) */
293 if (!Channel_Join(target, channame))
296 if (!chan) { /* channel is new; it has been created above */
297 chan = Channel_Search(channame);
298 assert(chan != NULL);
299 if (*channame == '+') { /* modeless channel... */
300 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
301 Channel_ModeAdd(chan, 'n'); /* no external msgs */
304 assert(chan != NULL);
306 join_set_channelmodes(chan, target, flags);
308 join_forward(Client, target, chan, channame);
310 if (!join_send_topic(Client, target, chan, channame))
311 break; /* write error */
314 channame = strtok_r(NULL, ",", &lastchan);
316 key = strtok_r(NULL, ",", &lastkey);
323 * Handler for the IRC "PART" command.
326 IRC_PART(CLIENT * Client, REQUEST * Req)
331 assert(Client != NULL);
334 if (Req->argc < 1 || Req->argc > 2)
335 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
336 Client_ID(Client), Req->command);
339 if (Client_Type(Client) == CLIENT_SERVER)
340 target = Client_Search(Req->prefix);
344 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
345 Client_ID(Client), Req->prefix);
347 /* Loop over all the given channel names */
348 chan = strtok(Req->argv[0], ",");
350 /* Make sure that "chan" is not the empty string ("PART :") */
352 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
353 Client_ID(Client), Req->command);
356 Channel_Part(target, Client, chan,
357 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
358 chan = strtok(NULL, ",");
361 /* Update idle time, if local client */
362 if (Client_Conn(Client) > NONE)
363 Conn_UpdateIdle(Client_Conn(Client));
370 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
377 assert( Client != NULL );
378 assert( Req != NULL );
380 if ((Req->argc < 1) || (Req->argc > 2))
381 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
383 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
385 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
387 /* Welcher Channel? */
388 chan = Channel_Search( Req->argv[0] );
389 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
391 /* Ist der User Mitglied in dem Channel? */
392 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
396 /* Request actual topic */
397 topic = Channel_Topic(chan);
399 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
400 Client_ID(Client), Channel_Name(chan), topic);
402 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
403 Client_ID(Client), Channel_Name(chan),
404 Channel_TopicWho(chan),
405 Channel_TopicTime(chan));
410 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
411 Client_ID(from), Channel_Name(chan));
414 if( strchr( Channel_Modes( chan ), 't' ))
416 /* Topic Lock. Ist der User ein Channel Operator? */
417 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
421 Channel_SetTopic(chan, from, Req->argv[1]);
422 LogDebug("%s \"%s\" set topic on \"%s\": %s",
423 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
424 Req->argv[1][0] ? Req->argv[1] : "<none>");
426 /* im Channel bekannt machen und an Server weiterleiten */
427 IRC_WriteStrServersPrefix( Client, from, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
428 IRC_WriteStrChannelPrefix( Client, chan, from, false, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
430 if( Client_Type( Client ) == CLIENT_USER ) return IRC_WriteStrClientPrefix( Client, Client, "TOPIC %s :%s", Req->argv[0], Req->argv[1] );
431 else return CONNECTED;
436 * Handler for the IRC "LIST" command.
437 * This implementation handles the local case as well as the forwarding of the
438 * LIST command to other servers in the IRC network.
441 IRC_LIST( CLIENT *Client, REQUEST *Req )
445 CLIENT *from, *target;
447 assert( Client != NULL );
448 assert( Req != NULL );
450 /* Bad number of prameters? */
452 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
453 Client_ID( Client ), Req->command );
456 pattern = strtok( Req->argv[0], "," );
460 /* Get sender from prefix, if any */
461 if( Client_Type( Client ) == CLIENT_SERVER )
462 from = Client_Search( Req->prefix );
467 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
468 Client_ID( Client ), Req->prefix );
472 /* Forward to other server? */
473 target = Client_Search( Req->argv[1] );
474 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
475 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
476 Client_ID( Client ), Req->argv[1] );
478 if( target != Client_ThisServer( ))
480 /* Target is indeed an other server, forward it! */
481 return IRC_WriteStrClientPrefix( target, from,
482 "LIST %s :%s", Client_ID( from ),
489 /* Loop through all the channels */
490 chan = Channel_First( );
493 /* Check search pattern */
494 if( Match( pattern, Channel_Name( chan )))
497 if( ! strchr( Channel_Modes( chan ), 's' ) ||
498 Channel_IsMemberOf( chan, from ))
500 if( ! IRC_WriteStrClient( from,
501 RPL_LIST_MSG, Client_ID( from ),
502 Channel_Name( chan ),
503 Channel_MemberCount( chan ),
504 Channel_Topic( chan )))
508 chan = Channel_Next( chan );
511 /* Get next name ... */
513 pattern = strtok( NULL, "," );
518 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
523 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
525 char modes_add[COMMAND_LEN], l[16], *ptr;
530 assert( Client != NULL );
531 assert( Req != NULL );
533 /* Bad number of parameters? */
534 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
536 /* Compatibility kludge */
537 if( Req->argc == 5 ) arg_topic = 4;
538 else if( Req->argc == 3 ) arg_topic = 2;
542 from = Client_Search( Req->prefix );
543 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
545 /* Search or create channel */
546 chan = Channel_Search( Req->argv[0] );
547 if( ! chan ) chan = Channel_Create( Req->argv[0] );
548 if( ! chan ) return CONNECTED;
550 if( Req->argv[1][0] == '+' )
552 ptr = Channel_Modes( chan );
555 /* OK, this channel doesn't have modes jet, set the received ones: */
556 Channel_SetModes( chan, &Req->argv[1][1] );
560 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
561 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
565 /* Delete modes which we never want to inherit */
566 Channel_ModeDel( chan, 'l' );
567 Channel_ModeDel( chan, 'k' );
570 strcpy( modes_add, "" );
571 ptr = Channel_Modes( chan );
576 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
577 strlcat( modes_add, l, sizeof( modes_add ));
581 strlcat( modes_add, " ", sizeof( modes_add ));
582 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
587 /* Inform members of this channel */
588 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
591 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
596 ptr = Channel_Topic( chan );
597 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
599 /* OK, there is no topic jet */
600 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
601 IRC_WriteStrChannelPrefix(Client, chan, from, false,
602 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
606 /* Forward CHANINFO to other serevrs */
607 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] );
608 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
609 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );