2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
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.
25 #include "conn-func.h"
28 #include "conn-encoding.h"
30 #include "irc-write.h"
41 static char *Option_String PARAMS((CONN_ID Idx));
42 static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType,
44 static bool Send_Message_Mask PARAMS((CLIENT *from, char *command,
45 char *targetMask, char *message,
47 static bool Help PARAMS((CLIENT *Client, const char *Topic));
51 * Check if a list limit is reached and inform client accordingly.
53 * @param From The client.
54 * @param Count Reply item count.
55 * @param Limit Reply limit.
56 * @param Name Name of the list.
57 * @return true if list limit has been reached; false otherwise.
60 IRC_CheckListTooBig(CLIENT *From, const int Count, const int Limit,
71 (void)IRC_WriteStrClient(From,
72 "NOTICE %s :%s list limit (%d) reached!",
73 Client_ID(From), Name, Limit);
74 IRC_SetPenalty(From, 2);
80 IRC_ERROR( CLIENT *Client, REQUEST *Req )
82 assert( Client != NULL );
83 assert( Req != NULL );
85 if (Client_Type(Client) != CLIENT_GOTPASS
86 && Client_Type(Client) != CLIENT_GOTPASS_2813
87 && Client_Type(Client) != CLIENT_UNKNOWNSERVER
88 && Client_Type(Client) != CLIENT_SERVER
89 && Client_Type(Client) != CLIENT_SERVICE) {
90 LogDebug("Ignored ERROR command from \"%s\" ...",
92 IRC_SetPenalty(Client, 2);
97 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
100 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
101 Client_Mask(Client), Req->argv[0]);
108 * Handler for the IRC "KILL" command.
110 * This function implements the IRC command "KILL" wich is used to selectively
111 * disconnect clients. It can be used by IRC operators and servers, for example
112 * to "solve" nick collisions after netsplits. See RFC 2812 section 3.7.1.
114 * Please note that this function is also called internally, without a real
115 * KILL command being received over the network! Client is Client_ThisServer()
116 * in this case, and the prefix in Req is NULL.
118 * @param Client The client from which this command has been received
119 * or Client_ThisServer() when generated interanlly.
120 * @param Req Request structure with prefix and all parameters.
121 * @returns CONNECTED or DISCONNECTED.
124 IRC_KILL( CLIENT *Client, REQUEST *Req )
127 char reason[COMMAND_LEN], *msg;
128 CONN_ID my_conn, conn;
130 assert (Client != NULL);
131 assert (Req != NULL);
133 if (Client_Type(Client) != CLIENT_SERVER && !Client_OperByMe(Client))
134 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
138 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
139 Client_ID(Client), Req->command);
141 /* Get prefix (origin); use the client if no prefix is given. */
143 prefix = Client_Search(Req->prefix);
147 /* Log a warning message and use this server as origin when the
148 * prefix (origin) is invalid. */
150 Log(LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
152 prefix = Client_ThisServer();
155 if (Client != Client_ThisServer())
156 Log(LOG_NOTICE|LOG_snotice,
157 "Got KILL command from \"%s\" for \"%s\": \"%s\".",
158 Client_Mask(prefix), Req->argv[0], Req->argv[1]);
160 /* Build reason string: Prefix the "reason" if the originator is a
161 * regular user, so users can't spoof KILLs of servers. */
162 if (Client_Type(Client) == CLIENT_USER)
163 snprintf(reason, sizeof(reason), "KILLed by %s: %s",
164 Client_ID(Client), Req->argv[1]);
166 strlcpy(reason, Req->argv[1], sizeof(reason));
168 /* Inform other servers */
169 IRC_WriteStrServersPrefix(Client, prefix, "KILL %s :%s",
170 Req->argv[0], reason);
172 /* Save ID of this connection */
173 my_conn = Client_Conn( Client );
175 /* Do we host such a client? */
176 c = Client_Search( Req->argv[0] );
179 if(( Client_Type( c ) != CLIENT_USER ) &&
180 ( Client_Type( c ) != CLIENT_GOTNICK ))
182 /* Target of this KILL is not a regular user, this is
183 * invalid! So we ignore this case if we received a
184 * regular KILL from the network and try to kill the
185 * client/connection anyway (but log an error!) if the
186 * origin is the local server. */
188 if( Client != Client_ThisServer( ))
190 /* Invalid KILL received from remote */
191 if( Client_Type( c ) == CLIENT_SERVER )
192 msg = ERR_CANTKILLSERVER_MSG;
194 msg = ERR_NOPRIVILEGES_MSG;
195 return IRC_WriteStrClient( Client, msg,
196 Client_ID( Client ));
199 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
200 Client_Type( c ), Req->argv[0] );
203 /* Kill the client NOW:
204 * - Close the local connection (if there is one),
205 * - Destroy the CLIENT structure for remote clients.
206 * Note: Conn_Close() removes the CLIENT structure as well. */
207 conn = Client_Conn( c );
209 Conn_Close(conn, NULL, reason, true);
211 Client_Destroy(c, NULL, reason, false);
214 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
216 /* Are we still connected or were we killed, too? */
217 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
225 * Handler for the IRC command NOTICE.
228 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
230 return Send_Message(Client, Req, CLIENT_USER, false);
235 * Handler for the IRC command PRIVMSG.
238 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
240 return Send_Message(Client, Req, CLIENT_USER, true);
245 * Handler for the IRC command SQUERY.
248 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
250 return Send_Message(Client, Req, CLIENT_SERVICE, true);
255 IRC_TRACE( CLIENT *Client, REQUEST *Req )
257 CLIENT *from, *target, *c;
259 char user[CLIENT_USER_LEN];
261 assert( Client != NULL );
262 assert( Req != NULL );
264 /* Bad number of arguments? */
265 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
268 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
270 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
273 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
274 else target = Client_ThisServer( );
276 /* Forward command to other server? */
277 if( target != Client_ThisServer( ))
279 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
281 /* Send RPL_TRACELINK back to initiator */
282 idx = Client_Conn( Client ); assert( idx > NONE );
283 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
284 if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), PACKAGE_NAME, PACKAGE_VERSION, Client_ID( target ), Client_ID( Client_NextHop( target )), Option_String( idx2 ), time( NULL ) - Conn_StartTime( idx2 ), Conn_SendQ( idx ), Conn_SendQ( idx2 ))) return DISCONNECTED;
286 /* Forward command */
287 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
291 /* Infos about all connected servers */
295 if( Client_Conn( c ) > NONE )
298 if( Client_Type( c ) == CLIENT_SERVER )
301 strlcpy( user, Client_User( c ), sizeof( user ));
302 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
303 if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), user, Client_Hostname( c ), Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( c )))) return DISCONNECTED;
305 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
308 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
311 c = Client_Next( c );
314 IRC_SetPenalty( Client, 3 );
315 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
320 * Handler for the IRC "HELP" command.
322 * @param Client The client from which this command has been received.
323 * @param Req Request structure with prefix and all parameters.
324 * @return CONNECTED or DISCONNECTED.
327 IRC_HELP(CLIENT *Client, REQUEST *Req)
331 assert(Client != NULL);
334 /* Bad number of arguments? */
336 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
337 Client_ID(Client), Req->command);
339 IRC_SetPenalty(Client, 2);
341 if ((Req->argc == 0 && array_bytes(&Conf_Helptext) > 0)
342 || (Req->argc >= 1 && strcasecmp(Req->argv[0], "Commands") != 0)) {
343 /* Help text available and requested */
345 return Help(Client, Req->argv[0]);
347 if (!Help(Client, "Intro"))
352 cmd = Parse_GetCommandStruct();
354 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
355 Client_ID(Client), cmd->name))
364 * Send help for a given topic to the client.
366 * @param Client The client requesting help.
367 * @param Topoc The help topic requested.
368 * @return CONNECTED or DISCONNECTED.
371 Help(CLIENT *Client, const char *Topic)
374 size_t helptext_len, len_str, idx_start, lines = 0;
375 bool in_article = false;
377 assert(Client != NULL);
378 assert(Topic != NULL);
380 helptext_len = array_bytes(&Conf_Helptext);
381 line = array_start(&Conf_Helptext);
382 while (helptext_len > 0) {
383 len_str = strlen(line) + 1;
384 assert(helptext_len >= len_str);
385 helptext_len -= len_str;
388 /* The first character in each article text line must
389 * be a TAB (ASCII 9) character which will be stripped
390 * in the output. If it is not a TAB, the end of the
391 * article has been reached. */
392 if (line[0] != '\t') {
399 /* A single '.' character indicates an empty line */
400 if (line[1] == '.' && line[2] == '\0')
405 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
412 if (line[0] == '-' && line[1] == ' '
413 && strcasecmp(&line[2], Topic) == 0)
420 /* Help topic not found (or empty)! */
421 if (!IRC_WriteStrClient(Client, "NOTICE %s :No help for \"%s\" found!",
422 Client_ID(Client), Topic))
431 Option_String(CONN_ID Idx)
433 Option_String(UNUSED CONN_ID Idx)
436 static char option_txt[8];
440 options = Conn_Options(Idx);
443 strcpy(option_txt, "F"); /* No idea what this means, but the
444 * original ircd sends it ... */
446 if(options & CONN_ZIP) /* zlib compression supported. */
447 strcat(option_txt, "z");
451 } /* Option_String */
455 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
460 char *currentTarget = Req->argv[0];
461 char *lastCurrentTarget = NULL;
462 char *message = NULL;
464 assert(Client != NULL);
467 if (Req->argc == 0) {
470 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
471 Client_ID(Client), Req->command);
473 if (Req->argc == 1) {
476 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
482 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
483 Client_ID(Client), Req->command);
486 if (Client_Type(Client) == CLIENT_SERVER)
487 from = Client_Search(Req->prefix);
491 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
492 Client_ID(Client), Req->prefix);
495 if (Client_Conn(Client) > NONE)
496 message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]);
499 message = Req->argv[1];
501 /* handle msgtarget = msgto *("," msgto) */
502 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
503 ngt_UpperStr(Req->command);
505 while (currentTarget) {
506 /* Check for and handle valid <msgto> of form:
508 * msgto = channel / ( user [ "%" host ] "@" servername )
509 * msgto =/ ( user "%" host ) / targetmask
510 * msgto =/ nickname / ( nickname "!" user "@" host )
512 if (strchr(currentTarget, '!') == NULL)
514 cl = Client_Search(currentTarget);
519 /* If currentTarget isn't a nickname check for:
520 * user ["%" host] "@" servername
522 * nickname "!" user "@" host
524 char target[COMMAND_LEN];
528 char * server = NULL;
530 strlcpy(target, currentTarget, COMMAND_LEN);
531 server = strchr(target, '@');
536 host = strchr(target, '%');
541 user = strchr(target, '!');
543 /* msgto form: nick!user@host */
547 host = server; /* not "@server" but "@host" */
552 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
553 if (Client_Type(cl) != CLIENT_USER &&
554 Client_Type(cl) != CLIENT_SERVICE)
556 if (nick != NULL && host != NULL) {
557 if (strcasecmp(nick, Client_ID(cl)) == 0 &&
558 strcasecmp(user, Client_User(cl)) == 0 &&
559 strcasecmp(host, Client_HostnameDisplayed(cl)) == 0)
564 if (strcasecmp(user, Client_User(cl)) != 0)
566 if (host != NULL && strcasecmp(host,
567 Client_HostnameDisplayed(cl)) != 0)
569 if (server != NULL && strcasecmp(server,
570 Client_ID(Client_Introducer(cl))) != 0)
577 /* Target is a user, enforce type */
579 if (Client_Type(cl) != ForceType &&
580 !(ForceType == CLIENT_USER &&
581 (Client_Type(cl) == CLIENT_USER ||
582 Client_Type(cl) == CLIENT_SERVICE))) {
584 if (Client_Type(cl) != ForceType) {
586 if (SendErrors && !IRC_WriteStrClient(
587 from, ERR_NOSUCHNICK_MSG,Client_ID(from),
590 goto send_next_target;
594 if (ForceType == CLIENT_SERVICE &&
595 (Conn_Options(Client_Conn(Client_NextHop(cl)))
597 /* SQUERY command but RFC 1459 link: convert
598 * request to PRIVMSG command */
599 Req->command = "PRIVMSG";
602 if (Client_HasMode(cl, 'b') &&
603 !Client_HasMode(from, 'R') &&
604 !Client_HasMode(from, 'o') &&
605 !(Client_Type(from) == CLIENT_SERVER) &&
606 !(Client_Type(from) == CLIENT_SERVICE)) {
607 if (SendErrors && !IRC_WriteStrClient(from,
609 Client_ID(from), Client_ID(cl)))
611 goto send_next_target;
614 if (Client_HasMode(cl, 'C')) {
615 cl2chan = Channel_FirstChannelOf(cl);
617 chan = Channel_GetChannel(cl2chan);
618 if (Channel_IsMemberOf(chan, from))
620 cl2chan = Channel_NextChannelOf(cl, cl2chan);
623 if (SendErrors && !IRC_WriteStrClient(
624 from, ERR_NOTONSAMECHANNEL_MSG,
625 Client_ID(from), Client_ID(cl)))
627 goto send_next_target;
631 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
632 && strchr(Client_Modes(cl), 'a')) {
634 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
640 if (Client_Conn(from) > NONE) {
641 Conn_UpdateIdle(Client_Conn(from));
643 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
644 Req->command, Client_ID(cl),
647 } else if (ForceType != CLIENT_SERVICE
648 && (chan = Channel_Search(currentTarget))) {
649 if (!Channel_Write(chan, from, Client, Req->command,
650 SendErrors, message))
652 } else if (ForceType != CLIENT_SERVICE
653 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
654 && strchr("$#", currentTarget[0])
655 && strchr(currentTarget, '.')) {
657 if (!Send_Message_Mask(from, Req->command, currentTarget,
658 message, SendErrors))
663 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
664 Client_ID(from), currentTarget))
669 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
671 Conn_SetPenalty(Client_Conn(Client), 1);
679 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
680 char * message, bool SendErrors)
684 char *mask = targetMask + 1;
685 const char *check_wildcards;
689 if (strchr(Client_Modes(from), 'o') == NULL) {
692 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
697 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
698 * dot (".") and no wildcards ("*", "?") following the last one.
700 check_wildcards = strrchr(targetMask, '.');
701 assert(check_wildcards != NULL);
702 if (check_wildcards &&
703 check_wildcards[strcspn(check_wildcards, "*?")])
707 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
710 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
711 if (targetMask[0] == '#') {
712 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
713 if (Client_Type(cl) != CLIENT_USER)
715 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
717 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
718 command, Client_ID(cl), message))
722 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
723 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
724 if (Client_Type(cl) != CLIENT_USER)
726 client_match = MatchCaseInsensitive(mask,
727 Client_ID(Client_Introducer(cl)));
729 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
730 command, Client_ID(cl), message))
735 } /* Send_Message_Mask */