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 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 2)
143 /* Get prefix (origin); use the client if no prefix is given. */
145 prefix = Client_Search(Req->prefix);
149 /* Log a warning message and use this server as origin when the
150 * prefix (origin) is invalid. And this is the reason why we don't
151 * use the _IRC_GET_SENDER_OR_RETURN_ macro above! */
153 Log(LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
155 prefix = Client_ThisServer();
158 if (Client != Client_ThisServer())
159 Log(LOG_NOTICE|LOG_snotice,
160 "Got KILL command from \"%s\" for \"%s\": \"%s\".",
161 Client_Mask(prefix), Req->argv[0], Req->argv[1]);
163 /* Build reason string: Prefix the "reason" if the originator is a
164 * regular user, so users can't spoof KILLs of servers. */
165 if (Client_Type(Client) == CLIENT_USER)
166 snprintf(reason, sizeof(reason), "KILLed by %s: %s",
167 Client_ID(Client), Req->argv[1]);
169 strlcpy(reason, Req->argv[1], sizeof(reason));
171 /* Inform other servers */
172 IRC_WriteStrServersPrefix(Client, prefix, "KILL %s :%s",
173 Req->argv[0], reason);
175 /* Save ID of this connection */
176 my_conn = Client_Conn( Client );
178 /* Do we host such a client? */
179 c = Client_Search( Req->argv[0] );
181 if (Client_Type(c) != CLIENT_USER
182 && Client_Type(c) != CLIENT_GOTNICK) {
183 /* Target of this KILL is not a regular user, this is
184 * invalid! So we ignore this case if we received a
185 * regular KILL from the network and try to kill the
186 * client/connection anyway (but log an error!) if the
187 * origin is the local server. */
189 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_WriteErrClient(Client, msg,
200 "Got KILL for invalid client type: %d, \"%s\"!",
201 Client_Type( c ), Req->argv[0] );
204 /* Kill the client NOW:
205 * - Close the local connection (if there is one),
206 * - Destroy the CLIENT structure for remote clients.
207 * Note: Conn_Close() removes the CLIENT structure as well. */
208 conn = Client_Conn( c );
210 Conn_Close(conn, NULL, reason, true);
212 Client_Destroy(c, NULL, reason, false);
215 Log(LOG_NOTICE, "Client with nick \"%s\" is unknown here.",
218 /* Are we still connected or were we killed, too? */
219 if (my_conn > NONE && Conn_GetClient(my_conn))
226 * Handler for the IRC "NOTICE" command.
228 * @param Client The client from which this command has been received.
229 * @param Req Request structure with prefix and all parameters.
230 * @return CONNECTED or DISCONNECTED.
233 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
235 return Send_Message(Client, Req, CLIENT_USER, false);
239 * Handler for the IRC "PRIVMSG" command.
241 * @param Client The client from which this command has been received.
242 * @param Req Request structure with prefix and all parameters.
243 * @return CONNECTED or DISCONNECTED.
246 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
248 return Send_Message(Client, Req, CLIENT_USER, true);
252 * Handler for the IRC "SQUERY" command.
254 * @param Client The client from which this command has been received.
255 * @param Req Request structure with prefix and all parameters.
256 * @return CONNECTED or DISCONNECTED.
259 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
261 return Send_Message(Client, Req, CLIENT_SERVICE, true);
265 * Handler for the IRC "TRACE" command.
267 * @param Client The client from which this command has been received.
268 * @param Req Request structure with prefix and all parameters.
269 * @return CONNECTED or DISCONNECTED.
272 IRC_TRACE(CLIENT *Client, REQUEST *Req)
274 CLIENT *from, *target, *c;
276 char user[CLIENT_USER_LEN];
278 assert(Client != NULL);
281 IRC_SetPenalty(Client, 3);
283 /* Bad number of arguments? */
285 return IRC_WriteErrClient(Client, ERR_NORECIPIENT_MSG,
286 Client_ID(Client), Req->command);
288 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
289 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
291 /* Forward command to other server? */
292 if (target != Client_ThisServer()) {
293 /* Send RPL_TRACELINK back to initiator */
294 idx = Client_Conn(Client);
296 idx2 = Client_Conn(Client_NextHop(target));
299 if (!IRC_WriteStrClient(from, RPL_TRACELINK_MSG,
300 Client_ID(from), PACKAGE_NAME,
301 PACKAGE_VERSION, Client_ID(target),
302 Client_ID(Client_NextHop(target)),
304 time(NULL) - Conn_StartTime(idx2),
305 Conn_SendQ(idx), Conn_SendQ(idx2)))
308 /* Forward command */
309 IRC_WriteStrClientPrefix(target, from, "TRACE %s", Req->argv[0]);
313 /* Infos about all connected servers */
316 if (Client_Conn(c) > NONE) {
318 if (Client_Type(c) == CLIENT_SERVER) {
320 strlcpy(user, Client_User(c), sizeof(user));
322 strlcpy(user, "unknown", sizeof(user));
323 if (!IRC_WriteStrClient(from,
325 Client_ID(from), Client_ID(c),
326 user, Client_Hostname(c),
327 Client_Mask(Client_ThisServer()),
328 Option_String(Client_Conn(c))))
331 if (Client_Type(c) == CLIENT_USER
332 && Client_HasMode(c, 'o')) {
334 if (!IRC_WriteStrClient(from,
335 RPL_TRACEOPERATOR_MSG,
336 Client_ID(from), Client_ID(c)))
340 c = Client_Next( c );
343 return IRC_WriteStrClient(from, RPL_TRACEEND_MSG, Client_ID(from),
344 Conf_ServerName, PACKAGE_NAME,
345 PACKAGE_VERSION, NGIRCd_DebugLevel);
349 * Handler for the IRC "HELP" command.
351 * @param Client The client from which this command has been received.
352 * @param Req Request structure with prefix and all parameters.
353 * @return CONNECTED or DISCONNECTED.
356 IRC_HELP(CLIENT *Client, REQUEST *Req)
360 assert(Client != NULL);
363 IRC_SetPenalty(Client, 2);
365 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
367 if ((Req->argc == 0 && array_bytes(&Conf_Helptext) > 0)
368 || (Req->argc >= 1 && strcasecmp(Req->argv[0], "Commands") != 0)) {
369 /* Help text available and requested */
371 return Help(Client, Req->argv[0]);
373 if (!Help(Client, "Intro"))
378 cmd = Parse_GetCommandStruct();
380 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
381 Client_ID(Client), cmd->name))
389 * Send help for a given topic to the client.
391 * @param Client The client requesting help.
392 * @param Topoc The help topic requested.
393 * @return CONNECTED or DISCONNECTED.
396 Help(CLIENT *Client, const char *Topic)
399 size_t helptext_len, len_str, idx_start, lines = 0;
400 bool in_article = false;
402 assert(Client != NULL);
403 assert(Topic != NULL);
405 helptext_len = array_bytes(&Conf_Helptext);
406 line = array_start(&Conf_Helptext);
407 while (helptext_len > 0) {
408 len_str = strlen(line) + 1;
409 assert(helptext_len >= len_str);
410 helptext_len -= len_str;
413 /* The first character in each article text line must
414 * be a TAB (ASCII 9) character which will be stripped
415 * in the output. If it is not a TAB, the end of the
416 * article has been reached. */
417 if (line[0] != '\t') {
424 /* A single '.' character indicates an empty line */
425 if (line[1] == '.' && line[2] == '\0')
430 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
437 if (line[0] == '-' && line[1] == ' '
438 && strcasecmp(&line[2], Topic) == 0)
445 /* Help topic not found (or empty)! */
446 if (!IRC_WriteStrClient(Client, "NOTICE %s :No help for \"%s\" found!",
447 Client_ID(Client), Topic))
454 * Get pointer to a static string representing the connection "options".
456 * @param Idx Connection index.
457 * @return Pointer to static (global) string buffer.
461 Option_String(CONN_ID Idx)
463 Option_String(UNUSED CONN_ID Idx)
466 static char option_txt[8];
473 options = Conn_Options(Idx);
474 strcpy(option_txt, "F"); /* No idea what this means, but the
475 * original ircd sends it ... */
477 if(options & CONN_SSL) /* SSL encrypted link */
478 strlcat(option_txt, "s", sizeof(option_txt));
481 if(options & CONN_ZIP) /* zlib compression enabled */
482 strlcat(option_txt, "z", sizeof(option_txt));
484 LogDebug(" *** %d: %d = %s", Idx, options, option_txt);
487 } /* Option_String */
490 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
495 char *currentTarget = Req->argv[0];
496 char *lastCurrentTarget = NULL;
497 char *message = NULL;
499 assert(Client != NULL);
502 if (Req->argc == 0) {
505 return IRC_WriteErrClient(Client, ERR_NORECIPIENT_MSG,
506 Client_ID(Client), Req->command);
508 if (Req->argc == 1) {
511 return IRC_WriteErrClient(Client, ERR_NOTEXTTOSEND_MSG,
517 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
518 Client_ID(Client), Req->command);
521 if (Client_Type(Client) == CLIENT_SERVER)
522 from = Client_Search(Req->prefix);
526 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
527 Client_ID(Client), Req->prefix);
530 if (Client_Conn(Client) > NONE)
531 message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]);
534 message = Req->argv[1];
536 /* handle msgtarget = msgto *("," msgto) */
537 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
538 ngt_UpperStr(Req->command);
540 while (currentTarget) {
541 /* Check for and handle valid <msgto> of form:
543 * msgto = channel / ( user [ "%" host ] "@" servername )
544 * msgto =/ ( user "%" host ) / targetmask
545 * msgto =/ nickname / ( nickname "!" user "@" host )
547 if (strchr(currentTarget, '!') == NULL)
549 cl = Client_Search(currentTarget);
554 /* If currentTarget isn't a nickname check for:
555 * user ["%" host] "@" servername
557 * nickname "!" user "@" host
559 char target[COMMAND_LEN];
563 char * server = NULL;
565 strlcpy(target, currentTarget, COMMAND_LEN);
566 server = strchr(target, '@');
571 host = strchr(target, '%');
576 user = strchr(target, '!');
578 /* msgto form: nick!user@host */
582 host = server; /* not "@server" but "@host" */
587 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
588 if (Client_Type(cl) != CLIENT_USER &&
589 Client_Type(cl) != CLIENT_SERVICE)
591 if (nick != NULL && host != NULL) {
592 if (strcasecmp(nick, Client_ID(cl)) == 0 &&
593 strcasecmp(user, Client_User(cl)) == 0 &&
594 strcasecmp(host, Client_HostnameDisplayed(cl)) == 0)
599 if (strcasecmp(user, Client_User(cl)) != 0)
601 if (host != NULL && strcasecmp(host,
602 Client_HostnameDisplayed(cl)) != 0)
604 if (server != NULL && strcasecmp(server,
605 Client_ID(Client_Introducer(cl))) != 0)
612 /* Target is a user, enforce type */
614 if (Client_Type(cl) != ForceType &&
615 !(ForceType == CLIENT_USER &&
616 (Client_Type(cl) == CLIENT_USER ||
617 Client_Type(cl) == CLIENT_SERVICE))) {
619 if (Client_Type(cl) != ForceType) {
621 if (SendErrors && !IRC_WriteErrClient(
622 from, ERR_NOSUCHNICK_MSG,Client_ID(from),
625 goto send_next_target;
629 if (ForceType == CLIENT_SERVICE &&
630 (Conn_Options(Client_Conn(Client_NextHop(cl)))
632 /* SQUERY command but RFC 1459 link: convert
633 * request to PRIVMSG command */
634 Req->command = "PRIVMSG";
637 if (Client_HasMode(cl, 'b') &&
638 !Client_HasMode(from, 'R') &&
639 !Client_HasMode(from, 'o') &&
640 !(Client_Type(from) == CLIENT_SERVER) &&
641 !(Client_Type(from) == CLIENT_SERVICE)) {
642 if (SendErrors && !IRC_WriteErrClient(from,
644 Client_ID(from), Client_ID(cl)))
646 goto send_next_target;
649 if (Client_HasMode(cl, 'C')) {
650 cl2chan = Channel_FirstChannelOf(cl);
652 chan = Channel_GetChannel(cl2chan);
653 if (Channel_IsMemberOf(chan, from))
655 cl2chan = Channel_NextChannelOf(cl, cl2chan);
658 if (SendErrors && !IRC_WriteErrClient(
659 from, ERR_NOTONSAMECHANNEL_MSG,
660 Client_ID(from), Client_ID(cl)))
662 goto send_next_target;
666 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
667 && Client_HasMode(cl, 'a')) {
669 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
675 if (Client_Conn(from) > NONE) {
676 Conn_UpdateIdle(Client_Conn(from));
678 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
679 Req->command, Client_ID(cl),
682 } else if (ForceType != CLIENT_SERVICE
683 && (chan = Channel_Search(currentTarget))) {
684 if (!Channel_Write(chan, from, Client, Req->command,
685 SendErrors, message))
687 } else if (ForceType != CLIENT_SERVICE
688 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
689 && strchr("$#", currentTarget[0])
690 && strchr(currentTarget, '.')) {
692 if (!Send_Message_Mask(from, Req->command, currentTarget,
693 message, SendErrors))
698 if (!IRC_WriteErrClient(from, ERR_NOSUCHNICK_MSG,
699 Client_ID(from), currentTarget))
704 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
706 Conn_SetPenalty(Client_Conn(Client), 1);
713 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
714 char * message, bool SendErrors)
718 char *mask = targetMask + 1;
719 const char *check_wildcards;
723 if (!Client_HasMode(from, 'o')) {
726 return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
731 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
732 * dot (".") and no wildcards ("*", "?") following the last one.
734 check_wildcards = strrchr(targetMask, '.');
735 assert(check_wildcards != NULL);
736 if (check_wildcards &&
737 check_wildcards[strcspn(check_wildcards, "*?")])
741 return IRC_WriteErrClient(from, ERR_WILDTOPLEVEL, targetMask);
744 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
745 if (targetMask[0] == '#') {
746 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
747 if (Client_Type(cl) != CLIENT_USER)
749 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
751 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
752 command, Client_ID(cl), message))
756 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
757 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
758 if (Client_Type(cl) != CLIENT_USER)
760 client_match = MatchCaseInsensitive(mask,
761 Client_ID(Client_Introducer(cl)));
763 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
764 command, Client_ID(cl), message))
769 } /* Send_Message_Mask */