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.
24 #include "conn-func.h"
27 #include "conn-encoding.h"
29 #include "irc-macros.h"
30 #include "irc-write.h"
40 static char *Option_String PARAMS((CONN_ID Idx));
41 static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType,
43 static bool Send_Message_Mask PARAMS((CLIENT *from, char *command,
44 char *targetMask, char *message,
46 static bool Help PARAMS((CLIENT *Client, const char *Topic));
49 * Check if a list limit is reached and inform client accordingly.
51 * @param From The client.
52 * @param Count Reply item count.
53 * @param Limit Reply limit.
54 * @param Name Name of the list.
55 * @return true if list limit has been reached; false otherwise.
58 IRC_CheckListTooBig(CLIENT *From, const int Count, const int Limit,
69 (void)IRC_WriteStrClient(From,
70 "NOTICE %s :%s list limit (%d) reached!",
71 Client_ID(From), Name, Limit);
72 IRC_SetPenalty(From, 2);
77 * Handler for the IRC "ERROR" command.
79 * @param Client The client from which this command has been received.
80 * @param Req Request structure with prefix and all parameters.
81 * @return CONNECTED or DISCONNECTED.
84 IRC_ERROR(CLIENT *Client, REQUEST *Req)
86 assert( Client != NULL );
87 assert( Req != NULL );
89 if (Client_Type(Client) != CLIENT_GOTPASS
90 && Client_Type(Client) != CLIENT_GOTPASS_2813
91 && Client_Type(Client) != CLIENT_UNKNOWNSERVER
92 && Client_Type(Client) != CLIENT_SERVER
93 && Client_Type(Client) != CLIENT_SERVICE) {
94 LogDebug("Ignored ERROR command from \"%s\" ...",
96 IRC_SetPenalty(Client, 2);
101 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
102 Client_Mask(Client));
104 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
105 Client_Mask(Client), Req->argv[0]);
111 * Handler for the IRC "KILL" command.
113 * This function implements the IRC command "KILL" which is used to selectively
114 * disconnect clients. It can be used by IRC operators and servers, for example
115 * to "solve" nick collisions after netsplits. See RFC 2812 section 3.7.1.
117 * Please note that this function is also called internally, without a real
118 * KILL command being received over the network! Client is Client_ThisServer()
119 * in this case, and the prefix in Req is NULL.
121 * @param Client The client from which this command has been received or
122 * Client_ThisServer() when generated interanlly.
123 * @param Req Request structure with prefix and all parameters.
124 * @return CONNECTED or DISCONNECTED.
127 IRC_KILL(CLIENT *Client, REQUEST *Req)
130 char reason[COMMAND_LEN];
132 assert (Client != NULL);
133 assert (Req != NULL);
135 if (Client_Type(Client) != CLIENT_SERVER && !Op_Check(Client, Req))
136 return Op_NoPrivileges(Client, Req);
138 /* Get prefix (origin); use the client if no prefix is given. */
140 prefix = Client_Search(Req->prefix);
144 /* Log a warning message and use this server as origin when the
145 * prefix (origin) is invalid. And this is the reason why we don't
146 * use the _IRC_GET_SENDER_OR_RETURN_ macro above! */
148 Log(LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
150 prefix = Client_ThisServer();
153 if (Client != Client_ThisServer())
154 Log(LOG_NOTICE|LOG_snotice,
155 "Got KILL command from \"%s\" for \"%s\": \"%s\".",
156 Client_Mask(prefix), Req->argv[0], Req->argv[1]);
158 /* Build reason string: Prefix the "reason" if the originator is a
159 * regular user, so users can't spoof KILLs of servers. */
160 if (Client_Type(Client) == CLIENT_USER)
161 snprintf(reason, sizeof(reason), "KILLed by %s: %s",
162 Client_ID(Client), Req->argv[1]);
164 strlcpy(reason, Req->argv[1], sizeof(reason));
166 return IRC_KillClient(Client, prefix, Req->argv[0], reason);
170 * Handler for the IRC "NOTICE" command.
172 * @param Client The client from which this command has been received.
173 * @param Req Request structure with prefix and all parameters.
174 * @return CONNECTED or DISCONNECTED.
177 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
179 return Send_Message(Client, Req, CLIENT_USER, false);
183 * Handler for the IRC "PRIVMSG" command.
185 * @param Client The client from which this command has been received.
186 * @param Req Request structure with prefix and all parameters.
187 * @return CONNECTED or DISCONNECTED.
190 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
192 return Send_Message(Client, Req, CLIENT_USER, true);
196 * Handler for the IRC "SQUERY" command.
198 * @param Client The client from which this command has been received.
199 * @param Req Request structure with prefix and all parameters.
200 * @return CONNECTED or DISCONNECTED.
203 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
205 return Send_Message(Client, Req, CLIENT_SERVICE, true);
209 * Handler for the IRC "TRACE" command.
211 * @param Client The client from which this command has been received.
212 * @param Req Request structure with prefix and all parameters.
213 * @return CONNECTED or DISCONNECTED.
216 IRC_TRACE(CLIENT *Client, REQUEST *Req)
218 CLIENT *from, *target, *c;
220 char user[CLIENT_USER_LEN];
222 assert(Client != NULL);
225 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
226 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
228 /* Forward command to other server? */
229 if (target != Client_ThisServer()) {
230 /* Send RPL_TRACELINK back to initiator */
231 idx = Client_Conn(Client);
233 idx2 = Client_Conn(Client_NextHop(target));
236 if (!IRC_WriteStrClient(from, RPL_TRACELINK_MSG,
237 Client_ID(from), PACKAGE_NAME,
238 PACKAGE_VERSION, Client_ID(target),
239 Client_ID(Client_NextHop(target)),
241 time(NULL) - Conn_StartTime(idx2),
242 Conn_SendQ(idx), Conn_SendQ(idx2)))
245 /* Forward command */
246 IRC_WriteStrClientPrefix(target, from, "TRACE %s", Req->argv[0]);
250 /* Infos about all connected servers */
253 if (Client_Conn(c) > NONE) {
255 if (Client_Type(c) == CLIENT_SERVER) {
257 strlcpy(user, Client_User(c), sizeof(user));
259 strlcpy(user, "unknown", sizeof(user));
260 if (!IRC_WriteStrClient(from,
262 Client_ID(from), Client_ID(c),
263 user, Client_Hostname(c),
264 Client_Mask(Client_ThisServer()),
265 Option_String(Client_Conn(c))))
268 if (Client_Type(c) == CLIENT_USER
269 && Client_HasMode(c, 'o')) {
271 if (!IRC_WriteStrClient(from,
272 RPL_TRACEOPERATOR_MSG,
273 Client_ID(from), Client_ID(c)))
277 c = Client_Next( c );
280 return IRC_WriteStrClient(from, RPL_TRACEEND_MSG, Client_ID(from),
281 Conf_ServerName, PACKAGE_NAME,
282 PACKAGE_VERSION, NGIRCd_DebugLevel);
286 * Handler for the IRC "HELP" command.
288 * @param Client The client from which this command has been received.
289 * @param Req Request structure with prefix and all parameters.
290 * @return CONNECTED or DISCONNECTED.
293 IRC_HELP(CLIENT *Client, REQUEST *Req)
297 assert(Client != NULL);
300 if ((Req->argc == 0 && array_bytes(&Conf_Helptext) > 0)
301 || (Req->argc >= 1 && strcasecmp(Req->argv[0], "Commands") != 0)) {
302 /* Help text available and requested */
304 return Help(Client, Req->argv[0]);
306 if (!Help(Client, "Intro"))
311 cmd = Parse_GetCommandStruct();
313 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
314 Client_ID(Client), cmd->name))
322 * Kill an client identified by its nick name.
324 * Please note that after killig a client, its CLIENT cond CONNECTION
325 * structures are invalid. So the caller must make sure on its own not to
326 * access data of probably killed clients after calling this function!
328 * @param Client The client from which the command leading to the KILL has
329 * been received, or NULL. The KILL will no be forwarded in this
330 * direction. Only relevant when From is set, too.
331 * @param From The client from which the command originated, or NULL for
333 * @param Nick The nick name to kill.
334 * @param Reason Text to send as reason to the client and other servers.
337 IRC_KillClient(CLIENT *Client, CLIENT *From, const char *Nick, const char *Reason)
340 CONN_ID my_conn, conn;
343 /* Do we know such a client in the network? */
344 c = Client_Search(Nick);
346 LogDebug("Client with nick \"%s\" is unknown, not forwaring.", Nick);
350 /* Inform other servers */
351 IRC_WriteStrServersPrefix(From ? Client : NULL,
352 From ? From : Client_ThisServer(),
353 "KILL %s :%s", Nick, Reason);
355 if (Client_Type(c) != CLIENT_USER && Client_Type(c) != CLIENT_GOTNICK) {
356 /* Target of this KILL is not a regular user, this is
357 * invalid! So we ignore this case if we received a
358 * regular KILL from the network and try to kill the
359 * client/connection anyway (but log an error!) if the
360 * origin is the local server. */
362 if (Client != Client_ThisServer()) {
363 /* Invalid KILL received from remote */
364 if (Client_Type(c) == CLIENT_SERVER)
365 msg = ERR_CANTKILLSERVER_MSG;
367 msg = ERR_NOPRIVILEGES_MSG;
368 return IRC_WriteErrClient(Client, msg, Client_ID(Client));
372 "Got KILL for invalid client type: %d, \"%s\"!",
373 Client_Type(c), Nick);
376 /* Save ID of this connection */
377 my_conn = Client_Conn(Client);
379 /* Kill the client NOW:
380 * - Close the local connection (if there is one),
381 * - Destroy the CLIENT structure for remote clients.
382 * Note: Conn_Close() removes the CLIENT structure as well. */
383 conn = Client_Conn(c);
385 Conn_Close(conn, NULL, Reason, true);
387 Client_Destroy(c, NULL, Reason, false);
389 /* Are we still connected or were we killed, too? */
390 if (my_conn > NONE && Conn_GetClient(my_conn))
397 * Send help for a given topic to the client.
399 * @param Client The client requesting help.
400 * @param Topoc The help topic requested.
401 * @return CONNECTED or DISCONNECTED.
404 Help(CLIENT *Client, const char *Topic)
407 size_t helptext_len, len_str, idx_start, lines = 0;
408 bool in_article = false;
410 assert(Client != NULL);
411 assert(Topic != NULL);
413 helptext_len = array_bytes(&Conf_Helptext);
414 line = array_start(&Conf_Helptext);
415 while (helptext_len > 0) {
416 len_str = strlen(line) + 1;
417 assert(helptext_len >= len_str);
418 helptext_len -= len_str;
421 /* The first character in each article text line must
422 * be a TAB (ASCII 9) character which will be stripped
423 * in the output. If it is not a TAB, the end of the
424 * article has been reached. */
425 if (line[0] != '\t') {
432 /* A single '.' character indicates an empty line */
433 if (line[1] == '.' && line[2] == '\0')
438 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
445 if (line[0] == '-' && line[1] == ' '
446 && strcasecmp(&line[2], Topic) == 0)
453 /* Help topic not found (or empty)! */
454 if (!IRC_WriteStrClient(Client, "NOTICE %s :No help for \"%s\" found!",
455 Client_ID(Client), Topic))
462 * Get pointer to a static string representing the connection "options".
464 * @param Idx Connection index.
465 * @return Pointer to static (global) string buffer.
469 Option_String(CONN_ID Idx)
471 Option_String(UNUSED CONN_ID Idx)
474 static char option_txt[8];
479 options = Conn_Options(Idx);
480 strcpy(option_txt, "F"); /* No idea what this means, but the
481 * original ircd sends it ... */
483 if(options & CONN_SSL) /* SSL encrypted link */
484 strlcat(option_txt, "s", sizeof(option_txt));
487 if(options & CONN_ZIP) /* zlib compression enabled */
488 strlcat(option_txt, "z", sizeof(option_txt));
490 LogDebug(" *** %d: %d = %s", Idx, options, option_txt);
493 } /* Option_String */
496 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
501 char *currentTarget = Req->argv[0];
502 char *lastCurrentTarget = NULL;
503 char *message = NULL;
505 assert(Client != NULL);
508 if (Req->argc == 0) {
511 return IRC_WriteErrClient(Client, ERR_NORECIPIENT_MSG,
512 Client_ID(Client), Req->command);
514 if (Req->argc == 1) {
517 return IRC_WriteErrClient(Client, ERR_NOTEXTTOSEND_MSG,
523 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
524 Client_ID(Client), Req->command);
527 if (Client_Type(Client) == CLIENT_SERVER)
528 from = Client_Search(Req->prefix);
532 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
533 Client_ID(Client), Req->prefix);
536 if (Client_Conn(Client) > NONE)
537 message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]);
540 message = Req->argv[1];
542 /* handle msgtarget = msgto *("," msgto) */
543 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
544 ngt_UpperStr(Req->command);
546 while (currentTarget) {
547 /* Check for and handle valid <msgto> of form:
549 * msgto = channel / ( user [ "%" host ] "@" servername )
550 * msgto =/ ( user "%" host ) / targetmask
551 * msgto =/ nickname / ( nickname "!" user "@" host )
553 if (strchr(currentTarget, '!') == NULL)
555 cl = Client_Search(currentTarget);
560 /* If currentTarget isn't a nickname check for:
561 * user ["%" host] "@" servername
563 * nickname "!" user "@" host
565 char target[COMMAND_LEN];
569 char * server = NULL;
571 strlcpy(target, currentTarget, COMMAND_LEN);
572 server = strchr(target, '@');
577 host = strchr(target, '%');
582 user = strchr(target, '!');
584 /* msgto form: nick!user@host */
588 host = server; /* not "@server" but "@host" */
593 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
594 if (Client_Type(cl) != CLIENT_USER &&
595 Client_Type(cl) != CLIENT_SERVICE)
597 if (nick != NULL && host != NULL) {
598 if (strcasecmp(nick, Client_ID(cl)) == 0 &&
599 strcasecmp(user, Client_User(cl)) == 0 &&
600 strcasecmp(host, Client_HostnameDisplayed(cl)) == 0)
605 if (strcasecmp(user, Client_User(cl)) != 0)
607 if (host != NULL && strcasecmp(host,
608 Client_HostnameDisplayed(cl)) != 0)
610 if (server != NULL && strcasecmp(server,
611 Client_ID(Client_Introducer(cl))) != 0)
618 /* Target is a user, enforce type */
620 if (Client_Type(cl) != ForceType &&
621 !(ForceType == CLIENT_USER &&
622 (Client_Type(cl) == CLIENT_USER ||
623 Client_Type(cl) == CLIENT_SERVICE))) {
625 if (Client_Type(cl) != ForceType) {
627 if (SendErrors && !IRC_WriteErrClient(
628 from, ERR_NOSUCHNICK_MSG,Client_ID(from),
631 goto send_next_target;
635 if (ForceType == CLIENT_SERVICE &&
636 (Conn_Options(Client_Conn(Client_NextHop(cl)))
638 /* SQUERY command but RFC 1459 link: convert
639 * request to PRIVMSG command */
640 Req->command = "PRIVMSG";
643 if (Client_HasMode(cl, 'b') &&
644 !Client_HasMode(from, 'R') &&
645 !Client_HasMode(from, 'o') &&
646 !(Client_Type(from) == CLIENT_SERVER) &&
647 !(Client_Type(from) == CLIENT_SERVICE)) {
648 if (SendErrors && !IRC_WriteErrClient(from,
650 Client_ID(from), Client_ID(cl)))
652 goto send_next_target;
655 if (Client_HasMode(cl, 'C')) {
656 cl2chan = Channel_FirstChannelOf(cl);
658 chan = Channel_GetChannel(cl2chan);
659 if (Channel_IsMemberOf(chan, from))
661 cl2chan = Channel_NextChannelOf(cl, cl2chan);
664 if (SendErrors && !IRC_WriteErrClient(
665 from, ERR_NOTONSAMECHANNEL_MSG,
666 Client_ID(from), Client_ID(cl)))
668 goto send_next_target;
672 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
673 && Client_HasMode(cl, 'a')) {
675 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
681 if (Client_Conn(from) > NONE) {
682 Conn_UpdateIdle(Client_Conn(from));
684 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
685 Req->command, Client_ID(cl),
688 } else if (ForceType != CLIENT_SERVICE
689 && (chan = Channel_Search(currentTarget))) {
690 if (!Channel_Write(chan, from, Client, Req->command,
691 SendErrors, message))
693 } else if (ForceType != CLIENT_SERVICE
694 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
695 && strchr("$#", currentTarget[0])
696 && strchr(currentTarget, '.')) {
698 if (!Send_Message_Mask(from, Req->command, currentTarget,
699 message, SendErrors))
704 if (!IRC_WriteErrClient(from, ERR_NOSUCHNICK_MSG,
705 Client_ID(from), currentTarget))
710 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
712 Conn_SetPenalty(Client_Conn(Client), 1);
719 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
720 char * message, bool SendErrors)
724 char *mask = targetMask + 1;
725 const char *check_wildcards;
729 if (!Client_HasMode(from, 'o')) {
732 return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
737 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
738 * dot (".") and no wildcards ("*", "?") following the last one.
740 check_wildcards = strrchr(targetMask, '.');
741 assert(check_wildcards != NULL);
742 if (check_wildcards &&
743 check_wildcards[strcspn(check_wildcards, "*?")])
747 return IRC_WriteErrClient(from, ERR_WILDTOPLEVEL, targetMask);
750 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
751 if (targetMask[0] == '#') {
752 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
753 if (Client_Type(cl) != CLIENT_USER)
755 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
757 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
758 command, Client_ID(cl), message))
762 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
763 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
764 if (Client_Type(cl) != CLIENT_USER)
766 client_match = MatchCaseInsensitive(mask,
767 Client_ID(Client_Introducer(cl)));
769 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
770 command, Client_ID(cl), message))
775 } /* Send_Message_Mask */