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
26 #include "conn-func.h"
33 #include "irc-write.h"
37 #include "irc-channel.h"
41 * RFC 2812, (3.2.1 Join message Command):
42 * Note that this message
43 * accepts a special argument ("0"), which is a special request to leave all
44 * channels the user is currently a member of. The server will process this
45 * message as if the user had sent a PART command (See Section 3.2.2) for
46 * each channel he is a member of.
49 part_from_all_channels(CLIENT* client, CLIENT *target)
54 while ((cl2chan = Channel_FirstChannelOf(target))) {
55 chan = Channel_GetChannel(cl2chan);
56 assert( chan != NULL );
57 Channel_Part(target, client, Channel_Name(chan), Client_ID(target));
64 * Check weather a local client is allowed to join an already existing
66 * @param Client Client that sent the JOIN command
67 * @param chan Channel to check
68 * @param channame Name of the channel
69 * @param key Provided channel key (or NULL if none has been provided)
70 * @return true if client is allowed to join channel, false otherwise
73 join_allowed(CLIENT *Client, CHANNEL *chan, const char *channame,
76 bool is_invited, is_banned;
77 const char *channel_modes;
79 /* Allow IRC operators to overwrite channel limits */
80 if (strchr(Client_Modes(Client), 'o'))
83 is_banned = Lists_Check(Channel_GetListBans(chan), Client);
84 is_invited = Lists_Check(Channel_GetListInvites(chan), Client);
86 if (is_banned && !is_invited) {
87 /* Client is banned from channel (and not on invite list) */
88 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG,
89 Client_ID(Client), channame);
93 channel_modes = Channel_Modes(chan);
94 if (strchr(channel_modes, 'i') && !is_invited) {
95 /* Channel is "invite-only" and client is not on invite list */
96 IRC_WriteStrClient(Client, ERR_INVITEONLYCHAN_MSG,
97 Client_ID(Client), channame);
101 if (!Channel_CheckKey(chan, Client, key ? key : "")) {
102 /* Channel is protected by a channel key and the client
103 * didn't specify the correct one */
104 IRC_WriteStrClient(Client, ERR_BADCHANNELKEY_MSG,
105 Client_ID(Client), channame);
109 if (strchr(channel_modes, 'l') &&
110 (Channel_MaxUsers(chan) <= Channel_MemberCount(chan))) {
111 /* There are more clints joined to this channel than allowed */
112 IRC_WriteStrClient(Client, ERR_CHANNELISFULL_MSG,
113 Client_ID(Client), channame);
117 if (strchr(channel_modes, 'z') && !Conn_UsesSSL(Client_Conn(Client))) {
118 /* Only "secure" clients are allowed, but clients doesn't
119 * use SSL encryption */
120 IRC_WriteStrClient(Client, ERR_SECURECHANNEL_MSG,
121 Client_ID(Client), channame);
130 join_set_channelmodes(CHANNEL *chan, CLIENT *target, const char *flags)
134 Channel_UserModeAdd(chan, target, *flags);
139 /* If channel persistent and client is ircop: make client chanop */
140 if (strchr(Channel_Modes(chan), 'P') && strchr(Client_Modes(target), 'o'))
141 Channel_UserModeAdd(chan, target, 'o');
146 cb_join_forward(CLIENT *To, CLIENT *Prefix, void *Data)
149 char str[COMMAND_LEN], *ptr = NULL;
151 strlcpy(str, (char *)Data, sizeof(str));
152 conn = Client_Conn(To);
154 if (Conn_Options(conn) & CONN_RFC1459) {
155 /* RFC 1459 compatibility mode, appended modes are NOT
156 * supported, so strip them off! */
157 ptr = strchr(str, 0x7);
162 IRC_WriteStrClientPrefix(To, Prefix, "JOIN %s", str);
164 IRC_WriteStrClientPrefix(To, Prefix, "MODE %s +%s %s", str, ptr,
166 } /* cb_join_forward */
170 join_forward(CLIENT *Client, CLIENT *target, CHANNEL *chan,
171 const char *channame)
173 char modes[CHANNEL_MODE_LEN], str[COMMAND_LEN];
175 strlcpy(&modes[1], Channel_UserModes(chan, target), sizeof(modes) - 1);
181 /* forward to other servers (if it is not a local channel) */
182 if (!Channel_IsLocal(chan)) {
183 snprintf(str, sizeof(str), "%s%s", channame, modes);
184 IRC_WriteStrServersPrefixFlag_CB(Client, target, '\0',
185 cb_join_forward, str);
188 /* tell users in this channel about the new client */
189 IRC_WriteStrChannelPrefix(Client, chan, target, false,
190 "JOIN :%s", channame);
192 /* syncronize channel modes */
194 IRC_WriteStrChannelPrefix(Client, chan, target, false,
195 "MODE %s +%s %s", channame,
196 &modes[1], Client_ID(target));
202 join_send_topic(CLIENT *Client, CLIENT *target, CHANNEL *chan,
203 const char *channame)
207 if (Client_Type(Client) != CLIENT_USER)
209 /* acknowledge join */
210 if (!IRC_WriteStrClientPrefix(Client, target, "JOIN :%s", channame))
213 /* Send topic to client, if any */
214 topic = Channel_Topic(chan);
215 assert(topic != NULL);
217 if (!IRC_WriteStrClient(Client, RPL_TOPIC_MSG,
218 Client_ID(Client), channame, topic))
221 if (!IRC_WriteStrClient(Client, RPL_TOPICSETBY_MSG,
222 Client_ID(Client), channame,
223 Channel_TopicWho(chan),
224 Channel_TopicTime(chan)))
228 /* send list of channel members to client */
229 if (!IRC_Send_NAMES(Client, chan))
231 return IRC_WriteStrClient(Client, RPL_ENDOFNAMES_MSG, Client_ID(Client), Channel_Name(chan));
236 IRC_JOIN( CLIENT *Client, REQUEST *Req )
238 char *channame, *key = NULL, *flags, *lastkey = NULL, *lastchan = NULL;
242 assert( Client != NULL );
243 assert( Req != NULL );
245 /* Bad number of arguments? */
246 if (Req->argc < 1 || Req->argc > 2)
247 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
248 Client_ID(Client), Req->command);
250 /* Who is the sender? */
251 if (Client_Type(Client) == CLIENT_SERVER)
252 target = Client_Search(Req->prefix);
257 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG, Client_ID(Client), Req->prefix);
259 /* Is argument "0"? */
260 if (Req->argc == 1 && !strncmp("0", Req->argv[0], 2))
261 return part_from_all_channels(Client, target);
263 /* Are channel keys given? */
265 key = strtok_r(Req->argv[1], ",", &lastkey);
267 channame = Req->argv[0];
268 channame = strtok_r(channame, ",", &lastchan);
270 /* Make sure that "channame" is not the empty string ("JOIN :") */
272 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
273 Client_ID(Client), Req->command);
278 /* Did the server include channel-user-modes? */
279 if (Client_Type(Client) == CLIENT_SERVER) {
280 flags = strchr(channame, 0x7);
287 chan = Channel_Search(channame);
288 if (!chan && Conf_PredefChannelsOnly) {
289 /* channel must be created, but server does not allow this */
290 IRC_WriteStrClient(Client, ERR_BANNEDFROMCHAN_MSG, Client_ID(Client), channame);
295 if (Client_Type(Client) == CLIENT_USER) {
296 /* Test if the user has reached the channel limit */
297 if ((Conf_MaxJoins > 0) &&
298 (Channel_CountForUser(Client) >= Conf_MaxJoins))
299 return IRC_WriteStrClient(Client,
300 ERR_TOOMANYCHANNELS_MSG,
301 Client_ID(Client), channame);
303 /* Already existing channel: check if the
304 * client is allowed to join */
305 if (!join_allowed(Client, chan, channame, key))
308 /* New channel: first user will become channel
309 * operator unless this is a modeless channel */
310 if (*channame != '+')
314 /* Local client: update idle time */
315 Conn_UpdateIdle(Client_Conn(Client));
317 /* Remote server: we don't need to know whether the
318 * client is invited or not, but we have to make sure
319 * that the "one shot" entries (generated by INVITE
320 * commands) in this list become deleted when a user
321 * joins a channel this way. */
323 (void)Lists_Check(Channel_GetListInvites(chan),
327 /* Join channel (and create channel if it doesn't exist) */
328 if (!Channel_Join(target, channame))
331 if (!chan) { /* channel is new; it has been created above */
332 chan = Channel_Search(channame);
333 assert(chan != NULL);
334 if (Channel_IsModeless(chan)) {
335 Channel_ModeAdd(chan, 't'); /* /TOPIC not allowed */
336 Channel_ModeAdd(chan, 'n'); /* no external msgs */
339 assert(chan != NULL);
341 join_set_channelmodes(chan, target, flags);
343 join_forward(Client, target, chan, channame);
345 if (!join_send_topic(Client, target, chan, channame))
346 break; /* write error */
349 channame = strtok_r(NULL, ",", &lastchan);
351 key = strtok_r(NULL, ",", &lastkey);
358 * Handler for the IRC "PART" command.
361 IRC_PART(CLIENT * Client, REQUEST * Req)
366 assert(Client != NULL);
369 if (Req->argc < 1 || Req->argc > 2)
370 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
371 Client_ID(Client), Req->command);
374 if (Client_Type(Client) == CLIENT_SERVER)
375 target = Client_Search(Req->prefix);
379 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
380 Client_ID(Client), Req->prefix);
382 /* Loop over all the given channel names */
383 chan = strtok(Req->argv[0], ",");
385 /* Make sure that "chan" is not the empty string ("PART :") */
387 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
388 Client_ID(Client), Req->command);
391 Channel_Part(target, Client, chan,
392 Req->argc > 1 ? Req->argv[1] : Client_ID(target));
393 chan = strtok(NULL, ",");
396 /* Update idle time, if local client */
397 if (Client_Conn(Client) > NONE)
398 Conn_UpdateIdle(Client_Conn(Client));
405 IRC_TOPIC( CLIENT *Client, REQUEST *Req )
412 assert( Client != NULL );
413 assert( Req != NULL );
415 if ((Req->argc < 1) || (Req->argc > 2))
416 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG, Client_ID(Client), Req->command);
418 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
420 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
422 /* Welcher Channel? */
423 chan = Channel_Search( Req->argv[0] );
424 if( ! chan ) return IRC_WriteStrClient( from, ERR_NOSUCHCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
426 /* Ist der User Mitglied in dem Channel? */
427 if( ! Channel_IsMemberOf( chan, from )) return IRC_WriteStrClient( from, ERR_NOTONCHANNEL_MSG, Client_ID( from ), Req->argv[0] );
431 /* Request actual topic */
432 topic = Channel_Topic(chan);
434 r = IRC_WriteStrClient(from, RPL_TOPIC_MSG,
435 Client_ID(Client), Channel_Name(chan), topic);
437 r = IRC_WriteStrClient(from, RPL_TOPICSETBY_MSG,
438 Client_ID(Client), Channel_Name(chan),
439 Channel_TopicWho(chan),
440 Channel_TopicTime(chan));
445 return IRC_WriteStrClient(from, RPL_NOTOPIC_MSG,
446 Client_ID(from), Channel_Name(chan));
449 if( strchr( Channel_Modes( chan ), 't' ))
451 /* Topic Lock. Ist der User ein Channel Operator? */
452 if( ! strchr( Channel_UserModes( chan, from ), 'o' )) return IRC_WriteStrClient( from, ERR_CHANOPRIVSNEEDED_MSG, Client_ID( from ), Channel_Name( chan ));
456 Channel_SetTopic(chan, from, Req->argv[1]);
457 LogDebug("%s \"%s\" set topic on \"%s\": %s",
458 Client_TypeText(from), Client_Mask(from), Channel_Name(chan),
459 Req->argv[1][0] ? Req->argv[1] : "<none>");
461 /* Update channel and forward new topic to other servers */
462 if (!Channel_IsLocal(chan))
463 IRC_WriteStrServersPrefix(Client, from, "TOPIC %s :%s",
464 Req->argv[0], Req->argv[1]);
465 IRC_WriteStrChannelPrefix(Client, chan, from, false, "TOPIC %s :%s",
466 Req->argv[0], Req->argv[1]);
468 if (Client_Type(Client) == CLIENT_USER)
469 return IRC_WriteStrClientPrefix(Client, Client, "TOPIC %s :%s",
470 Req->argv[0], Req->argv[1]);
477 * Handler for the IRC "LIST" command.
478 * This implementation handles the local case as well as the forwarding of the
479 * LIST command to other servers in the IRC network.
482 IRC_LIST( CLIENT *Client, REQUEST *Req )
486 CLIENT *from, *target;
488 assert( Client != NULL );
489 assert( Req != NULL );
491 /* Bad number of prameters? */
493 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
494 Client_ID( Client ), Req->command );
497 pattern = strtok( Req->argv[0], "," );
501 /* Get sender from prefix, if any */
502 if( Client_Type( Client ) == CLIENT_SERVER )
503 from = Client_Search( Req->prefix );
508 return IRC_WriteStrClient( Client, ERR_NOSUCHSERVER_MSG,
509 Client_ID( Client ), Req->prefix );
513 /* Forward to other server? */
514 target = Client_Search( Req->argv[1] );
515 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER ))
516 return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG,
517 Client_ID( Client ), Req->argv[1] );
519 if( target != Client_ThisServer( ))
521 /* Target is indeed an other server, forward it! */
522 return IRC_WriteStrClientPrefix( target, from,
523 "LIST %s :%s", Client_ID( from ),
530 /* Loop through all the channels */
531 chan = Channel_First( );
534 /* Check search pattern */
535 if( Match( pattern, Channel_Name( chan )))
538 if( ! strchr( Channel_Modes( chan ), 's' ) ||
539 Channel_IsMemberOf( chan, from ))
541 if( ! IRC_WriteStrClient( from,
542 RPL_LIST_MSG, Client_ID( from ),
543 Channel_Name( chan ),
544 Channel_MemberCount( chan ),
545 Channel_Topic( chan )))
549 chan = Channel_Next( chan );
552 /* Get next name ... */
554 pattern = strtok( NULL, "," );
559 return IRC_WriteStrClient( from, RPL_LISTEND_MSG, Client_ID( from ));
564 IRC_CHANINFO( CLIENT *Client, REQUEST *Req )
566 char modes_add[COMMAND_LEN], l[16], *ptr;
571 assert( Client != NULL );
572 assert( Req != NULL );
574 /* Bad number of parameters? */
575 if(( Req->argc < 2 ) || ( Req->argc == 4 ) || ( Req->argc > 5 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
577 /* Compatibility kludge */
578 if( Req->argc == 5 ) arg_topic = 4;
579 else if( Req->argc == 3 ) arg_topic = 2;
583 from = Client_Search( Req->prefix );
584 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
586 /* Search or create channel */
587 chan = Channel_Search( Req->argv[0] );
588 if( ! chan ) chan = Channel_Create( Req->argv[0] );
589 if( ! chan ) return CONNECTED;
591 if( Req->argv[1][0] == '+' )
593 ptr = Channel_Modes( chan );
596 /* OK, this channel doesn't have modes jet, set the received ones: */
597 Channel_SetModes( chan, &Req->argv[1][1] );
601 if( strchr( Channel_Modes( chan ), 'k' )) Channel_SetKey( chan, Req->argv[2] );
602 if( strchr( Channel_Modes( chan ), 'l' )) Channel_SetMaxUsers( chan, atol( Req->argv[3] ));
606 /* Delete modes which we never want to inherit */
607 Channel_ModeDel( chan, 'l' );
608 Channel_ModeDel( chan, 'k' );
611 strcpy( modes_add, "" );
612 ptr = Channel_Modes( chan );
617 snprintf( l, sizeof( l ), " %lu", Channel_MaxUsers( chan ));
618 strlcat( modes_add, l, sizeof( modes_add ));
622 strlcat( modes_add, " ", sizeof( modes_add ));
623 strlcat( modes_add, Channel_Key( chan ), sizeof( modes_add ));
628 /* Inform members of this channel */
629 IRC_WriteStrChannelPrefix( Client, chan, from, false, "MODE %s +%s%s", Req->argv[0], Channel_Modes( chan ), modes_add );
632 else Log( LOG_WARNING, "CHANINFO: invalid MODE format ignored!" );
637 ptr = Channel_Topic( chan );
638 if(( ! *ptr ) && ( Req->argv[arg_topic][0] ))
640 /* OK, there is no topic jet */
641 Channel_SetTopic(chan, Client, Req->argv[arg_topic]);
642 IRC_WriteStrChannelPrefix(Client, chan, from, false,
643 "TOPIC %s :%s", Req->argv[0], Channel_Topic(chan));
647 /* Forward CHANINFO to other serevrs */
648 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] );
649 else if( Req->argc == 3 ) IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s :%s", Req->argv[0], Req->argv[1], Req->argv[2] );
650 else IRC_WriteStrServersPrefixFlag( Client, from, 'C', "CHANINFO %s %s", Req->argv[0], Req->argv[1] );