2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2013 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-macros.h"
31 #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));
50 * Check if a list limit is reached and inform client accordingly.
52 * @param From The client.
53 * @param Count Reply item count.
54 * @param Limit Reply limit.
55 * @param Name Name of the list.
56 * @return true if list limit has been reached; false otherwise.
59 IRC_CheckListTooBig(CLIENT *From, const int Count, const int Limit,
70 (void)IRC_WriteStrClient(From,
71 "NOTICE %s :%s list limit (%d) reached!",
72 Client_ID(From), Name, Limit);
73 IRC_SetPenalty(From, 2);
78 * Handler for the IRC "ERROR" command.
80 * @param Client The client from which this command has been received.
81 * @param Req Request structure with prefix and all parameters.
82 * @return CONNECTED or DISCONNECTED.
85 IRC_ERROR(CLIENT *Client, REQUEST *Req)
87 assert( Client != NULL );
88 assert( Req != NULL );
90 if (Client_Type(Client) != CLIENT_GOTPASS
91 && Client_Type(Client) != CLIENT_GOTPASS_2813
92 && Client_Type(Client) != CLIENT_UNKNOWNSERVER
93 && Client_Type(Client) != CLIENT_SERVER
94 && Client_Type(Client) != CLIENT_SERVICE) {
95 LogDebug("Ignored ERROR command from \"%s\" ...",
97 IRC_SetPenalty(Client, 2);
102 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
103 Client_Mask(Client));
105 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
106 Client_Mask(Client), Req->argv[0]);
112 * Handler for the IRC "KILL" command.
114 * This function implements the IRC command "KILL" which is used to selectively
115 * disconnect clients. It can be used by IRC operators and servers, for example
116 * to "solve" nick collisions after netsplits. See RFC 2812 section 3.7.1.
118 * Please note that this function is also called internally, without a real
119 * KILL command being received over the network! Client is Client_ThisServer()
120 * in this case, and the prefix in Req is NULL.
122 * @param Client The client from which this command has been received or
123 * Client_ThisServer() when generated interanlly.
124 * @param Req Request structure with prefix and all parameters.
125 * @return CONNECTED or DISCONNECTED.
128 IRC_KILL(CLIENT *Client, REQUEST *Req)
131 char reason[COMMAND_LEN], *msg;
132 CONN_ID my_conn, conn;
134 assert (Client != NULL);
135 assert (Req != NULL);
137 if (Client_Type(Client) != CLIENT_SERVER && !Client_OperByMe(Client))
138 return IRC_WriteErrClient(Client, ERR_NOPRIVILEGES_MSG,
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. And this is the reason why we don't
149 * use the _IRC_GET_SENDER_OR_RETURN_ macro above! */
151 Log(LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
153 prefix = Client_ThisServer();
156 if (Client != Client_ThisServer())
157 Log(LOG_NOTICE|LOG_snotice,
158 "Got KILL command from \"%s\" for \"%s\": \"%s\".",
159 Client_Mask(prefix), Req->argv[0], Req->argv[1]);
161 /* Build reason string: Prefix the "reason" if the originator is a
162 * regular user, so users can't spoof KILLs of servers. */
163 if (Client_Type(Client) == CLIENT_USER)
164 snprintf(reason, sizeof(reason), "KILLed by %s: %s",
165 Client_ID(Client), Req->argv[1]);
167 strlcpy(reason, Req->argv[1], sizeof(reason));
169 /* Inform other servers */
170 IRC_WriteStrServersPrefix(Client, prefix, "KILL %s :%s",
171 Req->argv[0], reason);
173 /* Save ID of this connection */
174 my_conn = Client_Conn( Client );
176 /* Do we host such a client? */
177 c = Client_Search( Req->argv[0] );
179 if (Client_Type(c) != CLIENT_USER
180 && Client_Type(c) != CLIENT_GOTNICK) {
181 /* Target of this KILL is not a regular user, this is
182 * invalid! So we ignore this case if we received a
183 * regular KILL from the network and try to kill the
184 * client/connection anyway (but log an error!) if the
185 * origin is the local server. */
187 if (Client != Client_ThisServer()) {
188 /* Invalid KILL received from remote */
189 if (Client_Type(c) == CLIENT_SERVER)
190 msg = ERR_CANTKILLSERVER_MSG;
192 msg = ERR_NOPRIVILEGES_MSG;
193 return IRC_WriteErrClient(Client, msg,
198 "Got KILL for invalid client type: %d, \"%s\"!",
199 Client_Type( c ), Req->argv[0] );
202 /* Kill the client NOW:
203 * - Close the local connection (if there is one),
204 * - Destroy the CLIENT structure for remote clients.
205 * Note: Conn_Close() removes the CLIENT structure as well. */
206 conn = Client_Conn( c );
208 Conn_Close(conn, NULL, reason, true);
210 Client_Destroy(c, NULL, reason, false);
213 Log(LOG_NOTICE, "Client with nick \"%s\" is unknown here.",
216 /* Are we still connected or were we killed, too? */
217 if (my_conn > NONE && Conn_GetClient(my_conn))
224 * Handler for the IRC "NOTICE" command.
226 * @param Client The client from which this command has been received.
227 * @param Req Request structure with prefix and all parameters.
228 * @return CONNECTED or DISCONNECTED.
231 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
233 return Send_Message(Client, Req, CLIENT_USER, false);
237 * Handler for the IRC "PRIVMSG" command.
239 * @param Client The client from which this command has been received.
240 * @param Req Request structure with prefix and all parameters.
241 * @return CONNECTED or DISCONNECTED.
244 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
246 return Send_Message(Client, Req, CLIENT_USER, true);
250 * Handler for the IRC "SQUERY" command.
252 * @param Client The client from which this command has been received.
253 * @param Req Request structure with prefix and all parameters.
254 * @return CONNECTED or DISCONNECTED.
257 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
259 return Send_Message(Client, Req, CLIENT_SERVICE, true);
263 * Handler for the IRC "TRACE" command.
265 * @param Client The client from which this command has been received.
266 * @param Req Request structure with prefix and all parameters.
267 * @return CONNECTED or DISCONNECTED.
270 IRC_TRACE(CLIENT *Client, REQUEST *Req)
272 CLIENT *from, *target, *c;
274 char user[CLIENT_USER_LEN];
276 assert(Client != NULL);
279 IRC_SetPenalty(Client, 3);
281 /* Bad number of arguments? */
283 return IRC_WriteErrClient(Client, ERR_NORECIPIENT_MSG,
284 Client_ID(Client), Req->command);
286 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
287 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
289 /* Forward command to other server? */
290 if (target != Client_ThisServer()) {
291 /* Send RPL_TRACELINK back to initiator */
292 idx = Client_Conn(Client);
294 idx2 = Client_Conn(Client_NextHop(target));
297 if (!IRC_WriteStrClient(from, RPL_TRACELINK_MSG,
298 Client_ID(from), PACKAGE_NAME,
299 PACKAGE_VERSION, Client_ID(target),
300 Client_ID(Client_NextHop(target)),
302 time(NULL) - Conn_StartTime(idx2),
303 Conn_SendQ(idx), Conn_SendQ(idx2)))
306 /* Forward command */
307 IRC_WriteStrClientPrefix(target, from, "TRACE %s", Req->argv[0]);
311 /* Infos about all connected servers */
314 if (Client_Conn(c) > NONE) {
316 if (Client_Type(c) == CLIENT_SERVER) {
318 strlcpy(user, Client_User(c), sizeof(user));
320 strlcpy(user, "unknown", sizeof(user));
321 if (!IRC_WriteStrClient(from,
323 Client_ID(from), Client_ID(c),
324 user, Client_Hostname(c),
325 Client_Mask(Client_ThisServer()),
326 Option_String(Client_Conn(c))))
329 if (Client_Type(c) == CLIENT_USER
330 && Client_HasMode(c, 'o')) {
332 if (!IRC_WriteStrClient(from,
333 RPL_TRACEOPERATOR_MSG,
334 Client_ID(from), Client_ID(c)))
338 c = Client_Next( c );
341 return IRC_WriteStrClient(from, RPL_TRACEEND_MSG, Client_ID(from),
342 Conf_ServerName, PACKAGE_NAME,
343 PACKAGE_VERSION, NGIRCd_DebugLevel);
347 * Handler for the IRC "HELP" command.
349 * @param Client The client from which this command has been received.
350 * @param Req Request structure with prefix and all parameters.
351 * @return CONNECTED or DISCONNECTED.
354 IRC_HELP(CLIENT *Client, REQUEST *Req)
358 assert(Client != NULL);
361 IRC_SetPenalty(Client, 2);
363 if ((Req->argc == 0 && array_bytes(&Conf_Helptext) > 0)
364 || (Req->argc >= 1 && strcasecmp(Req->argv[0], "Commands") != 0)) {
365 /* Help text available and requested */
367 return Help(Client, Req->argv[0]);
369 if (!Help(Client, "Intro"))
374 cmd = Parse_GetCommandStruct();
376 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
377 Client_ID(Client), cmd->name))
385 * Send help for a given topic to the client.
387 * @param Client The client requesting help.
388 * @param Topoc The help topic requested.
389 * @return CONNECTED or DISCONNECTED.
392 Help(CLIENT *Client, const char *Topic)
395 size_t helptext_len, len_str, idx_start, lines = 0;
396 bool in_article = false;
398 assert(Client != NULL);
399 assert(Topic != NULL);
401 helptext_len = array_bytes(&Conf_Helptext);
402 line = array_start(&Conf_Helptext);
403 while (helptext_len > 0) {
404 len_str = strlen(line) + 1;
405 assert(helptext_len >= len_str);
406 helptext_len -= len_str;
409 /* The first character in each article text line must
410 * be a TAB (ASCII 9) character which will be stripped
411 * in the output. If it is not a TAB, the end of the
412 * article has been reached. */
413 if (line[0] != '\t') {
420 /* A single '.' character indicates an empty line */
421 if (line[1] == '.' && line[2] == '\0')
426 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
433 if (line[0] == '-' && line[1] == ' '
434 && strcasecmp(&line[2], Topic) == 0)
441 /* Help topic not found (or empty)! */
442 if (!IRC_WriteStrClient(Client, "NOTICE %s :No help for \"%s\" found!",
443 Client_ID(Client), Topic))
450 * Get pointer to a static string representing the connection "options".
452 * @param Idx Connection index.
453 * @return Pointer to static (global) string buffer.
457 Option_String(CONN_ID Idx)
459 Option_String(UNUSED CONN_ID Idx)
462 static char option_txt[8];
469 options = Conn_Options(Idx);
470 strcpy(option_txt, "F"); /* No idea what this means, but the
471 * original ircd sends it ... */
473 if(options & CONN_SSL) /* SSL encrypted link */
474 strlcat(option_txt, "s", sizeof(option_txt));
477 if(options & CONN_ZIP) /* zlib compression enabled */
478 strlcat(option_txt, "z", sizeof(option_txt));
480 LogDebug(" *** %d: %d = %s", Idx, options, option_txt);
483 } /* Option_String */
486 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
491 char *currentTarget = Req->argv[0];
492 char *lastCurrentTarget = NULL;
493 char *message = NULL;
495 assert(Client != NULL);
498 if (Req->argc == 0) {
501 return IRC_WriteErrClient(Client, ERR_NORECIPIENT_MSG,
502 Client_ID(Client), Req->command);
504 if (Req->argc == 1) {
507 return IRC_WriteErrClient(Client, ERR_NOTEXTTOSEND_MSG,
513 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
514 Client_ID(Client), Req->command);
517 if (Client_Type(Client) == CLIENT_SERVER)
518 from = Client_Search(Req->prefix);
522 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
523 Client_ID(Client), Req->prefix);
526 if (Client_Conn(Client) > NONE)
527 message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]);
530 message = Req->argv[1];
532 /* handle msgtarget = msgto *("," msgto) */
533 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
534 ngt_UpperStr(Req->command);
536 while (currentTarget) {
537 /* Check for and handle valid <msgto> of form:
539 * msgto = channel / ( user [ "%" host ] "@" servername )
540 * msgto =/ ( user "%" host ) / targetmask
541 * msgto =/ nickname / ( nickname "!" user "@" host )
543 if (strchr(currentTarget, '!') == NULL)
545 cl = Client_Search(currentTarget);
550 /* If currentTarget isn't a nickname check for:
551 * user ["%" host] "@" servername
553 * nickname "!" user "@" host
555 char target[COMMAND_LEN];
559 char * server = NULL;
561 strlcpy(target, currentTarget, COMMAND_LEN);
562 server = strchr(target, '@');
567 host = strchr(target, '%');
572 user = strchr(target, '!');
574 /* msgto form: nick!user@host */
578 host = server; /* not "@server" but "@host" */
583 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
584 if (Client_Type(cl) != CLIENT_USER &&
585 Client_Type(cl) != CLIENT_SERVICE)
587 if (nick != NULL && host != NULL) {
588 if (strcasecmp(nick, Client_ID(cl)) == 0 &&
589 strcasecmp(user, Client_User(cl)) == 0 &&
590 strcasecmp(host, Client_HostnameDisplayed(cl)) == 0)
595 if (strcasecmp(user, Client_User(cl)) != 0)
597 if (host != NULL && strcasecmp(host,
598 Client_HostnameDisplayed(cl)) != 0)
600 if (server != NULL && strcasecmp(server,
601 Client_ID(Client_Introducer(cl))) != 0)
608 /* Target is a user, enforce type */
610 if (Client_Type(cl) != ForceType &&
611 !(ForceType == CLIENT_USER &&
612 (Client_Type(cl) == CLIENT_USER ||
613 Client_Type(cl) == CLIENT_SERVICE))) {
615 if (Client_Type(cl) != ForceType) {
617 if (SendErrors && !IRC_WriteErrClient(
618 from, ERR_NOSUCHNICK_MSG,Client_ID(from),
621 goto send_next_target;
625 if (ForceType == CLIENT_SERVICE &&
626 (Conn_Options(Client_Conn(Client_NextHop(cl)))
628 /* SQUERY command but RFC 1459 link: convert
629 * request to PRIVMSG command */
630 Req->command = "PRIVMSG";
633 if (Client_HasMode(cl, 'b') &&
634 !Client_HasMode(from, 'R') &&
635 !Client_HasMode(from, 'o') &&
636 !(Client_Type(from) == CLIENT_SERVER) &&
637 !(Client_Type(from) == CLIENT_SERVICE)) {
638 if (SendErrors && !IRC_WriteErrClient(from,
640 Client_ID(from), Client_ID(cl)))
642 goto send_next_target;
645 if (Client_HasMode(cl, 'C')) {
646 cl2chan = Channel_FirstChannelOf(cl);
648 chan = Channel_GetChannel(cl2chan);
649 if (Channel_IsMemberOf(chan, from))
651 cl2chan = Channel_NextChannelOf(cl, cl2chan);
654 if (SendErrors && !IRC_WriteErrClient(
655 from, ERR_NOTONSAMECHANNEL_MSG,
656 Client_ID(from), Client_ID(cl)))
658 goto send_next_target;
662 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
663 && Client_HasMode(cl, 'a')) {
665 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
671 if (Client_Conn(from) > NONE) {
672 Conn_UpdateIdle(Client_Conn(from));
674 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
675 Req->command, Client_ID(cl),
678 } else if (ForceType != CLIENT_SERVICE
679 && (chan = Channel_Search(currentTarget))) {
680 if (!Channel_Write(chan, from, Client, Req->command,
681 SendErrors, message))
683 } else if (ForceType != CLIENT_SERVICE
684 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
685 && strchr("$#", currentTarget[0])
686 && strchr(currentTarget, '.')) {
688 if (!Send_Message_Mask(from, Req->command, currentTarget,
689 message, SendErrors))
694 if (!IRC_WriteErrClient(from, ERR_NOSUCHNICK_MSG,
695 Client_ID(from), currentTarget))
700 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
702 Conn_SetPenalty(Client_Conn(Client), 1);
709 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
710 char * message, bool SendErrors)
714 char *mask = targetMask + 1;
715 const char *check_wildcards;
719 if (!Client_HasMode(from, 'o')) {
722 return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
727 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
728 * dot (".") and no wildcards ("*", "?") following the last one.
730 check_wildcards = strrchr(targetMask, '.');
731 assert(check_wildcards != NULL);
732 if (check_wildcards &&
733 check_wildcards[strcspn(check_wildcards, "*?")])
737 return IRC_WriteErrClient(from, ERR_WILDTOPLEVEL, targetMask);
740 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
741 if (targetMask[0] == '#') {
742 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
743 if (Client_Type(cl) != CLIENT_USER)
745 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
747 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
748 command, Client_ID(cl), message))
752 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
753 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
754 if (Client_Type(cl) != CLIENT_USER)
756 client_match = MatchCaseInsensitive(mask,
757 Client_ID(Client_Introducer(cl)));
759 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
760 command, Client_ID(cl), message))
765 } /* Send_Message_Mask */