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 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
282 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
284 /* Forward command to other server? */
285 if (target != Client_ThisServer()) {
286 /* Send RPL_TRACELINK back to initiator */
287 idx = Client_Conn(Client);
289 idx2 = Client_Conn(Client_NextHop(target));
292 if (!IRC_WriteStrClient(from, RPL_TRACELINK_MSG,
293 Client_ID(from), PACKAGE_NAME,
294 PACKAGE_VERSION, Client_ID(target),
295 Client_ID(Client_NextHop(target)),
297 time(NULL) - Conn_StartTime(idx2),
298 Conn_SendQ(idx), Conn_SendQ(idx2)))
301 /* Forward command */
302 IRC_WriteStrClientPrefix(target, from, "TRACE %s", Req->argv[0]);
306 /* Infos about all connected servers */
309 if (Client_Conn(c) > NONE) {
311 if (Client_Type(c) == CLIENT_SERVER) {
313 strlcpy(user, Client_User(c), sizeof(user));
315 strlcpy(user, "unknown", sizeof(user));
316 if (!IRC_WriteStrClient(from,
318 Client_ID(from), Client_ID(c),
319 user, Client_Hostname(c),
320 Client_Mask(Client_ThisServer()),
321 Option_String(Client_Conn(c))))
324 if (Client_Type(c) == CLIENT_USER
325 && Client_HasMode(c, 'o')) {
327 if (!IRC_WriteStrClient(from,
328 RPL_TRACEOPERATOR_MSG,
329 Client_ID(from), Client_ID(c)))
333 c = Client_Next( c );
336 return IRC_WriteStrClient(from, RPL_TRACEEND_MSG, Client_ID(from),
337 Conf_ServerName, PACKAGE_NAME,
338 PACKAGE_VERSION, NGIRCd_DebugLevel);
342 * Handler for the IRC "HELP" command.
344 * @param Client The client from which this command has been received.
345 * @param Req Request structure with prefix and all parameters.
346 * @return CONNECTED or DISCONNECTED.
349 IRC_HELP(CLIENT *Client, REQUEST *Req)
353 assert(Client != NULL);
356 IRC_SetPenalty(Client, 2);
358 if ((Req->argc == 0 && array_bytes(&Conf_Helptext) > 0)
359 || (Req->argc >= 1 && strcasecmp(Req->argv[0], "Commands") != 0)) {
360 /* Help text available and requested */
362 return Help(Client, Req->argv[0]);
364 if (!Help(Client, "Intro"))
369 cmd = Parse_GetCommandStruct();
371 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
372 Client_ID(Client), cmd->name))
380 * Send help for a given topic to the client.
382 * @param Client The client requesting help.
383 * @param Topoc The help topic requested.
384 * @return CONNECTED or DISCONNECTED.
387 Help(CLIENT *Client, const char *Topic)
390 size_t helptext_len, len_str, idx_start, lines = 0;
391 bool in_article = false;
393 assert(Client != NULL);
394 assert(Topic != NULL);
396 helptext_len = array_bytes(&Conf_Helptext);
397 line = array_start(&Conf_Helptext);
398 while (helptext_len > 0) {
399 len_str = strlen(line) + 1;
400 assert(helptext_len >= len_str);
401 helptext_len -= len_str;
404 /* The first character in each article text line must
405 * be a TAB (ASCII 9) character which will be stripped
406 * in the output. If it is not a TAB, the end of the
407 * article has been reached. */
408 if (line[0] != '\t') {
415 /* A single '.' character indicates an empty line */
416 if (line[1] == '.' && line[2] == '\0')
421 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
428 if (line[0] == '-' && line[1] == ' '
429 && strcasecmp(&line[2], Topic) == 0)
436 /* Help topic not found (or empty)! */
437 if (!IRC_WriteStrClient(Client, "NOTICE %s :No help for \"%s\" found!",
438 Client_ID(Client), Topic))
445 * Get pointer to a static string representing the connection "options".
447 * @param Idx Connection index.
448 * @return Pointer to static (global) string buffer.
452 Option_String(CONN_ID Idx)
454 Option_String(UNUSED CONN_ID Idx)
457 static char option_txt[8];
464 options = Conn_Options(Idx);
465 strcpy(option_txt, "F"); /* No idea what this means, but the
466 * original ircd sends it ... */
468 if(options & CONN_SSL) /* SSL encrypted link */
469 strlcat(option_txt, "s", sizeof(option_txt));
472 if(options & CONN_ZIP) /* zlib compression enabled */
473 strlcat(option_txt, "z", sizeof(option_txt));
475 LogDebug(" *** %d: %d = %s", Idx, options, option_txt);
478 } /* Option_String */
481 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
486 char *currentTarget = Req->argv[0];
487 char *lastCurrentTarget = NULL;
488 char *message = NULL;
490 assert(Client != NULL);
493 if (Req->argc == 0) {
496 return IRC_WriteErrClient(Client, ERR_NORECIPIENT_MSG,
497 Client_ID(Client), Req->command);
499 if (Req->argc == 1) {
502 return IRC_WriteErrClient(Client, ERR_NOTEXTTOSEND_MSG,
508 IRC_SetPenalty(Client, 2);
509 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
510 Client_ID(Client), Req->command);
513 if (Client_Type(Client) == CLIENT_SERVER)
514 from = Client_Search(Req->prefix);
518 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
519 Client_ID(Client), Req->prefix);
522 if (Client_Conn(Client) > NONE)
523 message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]);
526 message = Req->argv[1];
528 /* handle msgtarget = msgto *("," msgto) */
529 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
530 ngt_UpperStr(Req->command);
532 while (currentTarget) {
533 /* Check for and handle valid <msgto> of form:
535 * msgto = channel / ( user [ "%" host ] "@" servername )
536 * msgto =/ ( user "%" host ) / targetmask
537 * msgto =/ nickname / ( nickname "!" user "@" host )
539 if (strchr(currentTarget, '!') == NULL)
541 cl = Client_Search(currentTarget);
546 /* If currentTarget isn't a nickname check for:
547 * user ["%" host] "@" servername
549 * nickname "!" user "@" host
551 char target[COMMAND_LEN];
555 char * server = NULL;
557 strlcpy(target, currentTarget, COMMAND_LEN);
558 server = strchr(target, '@');
563 host = strchr(target, '%');
568 user = strchr(target, '!');
570 /* msgto form: nick!user@host */
574 host = server; /* not "@server" but "@host" */
579 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
580 if (Client_Type(cl) != CLIENT_USER &&
581 Client_Type(cl) != CLIENT_SERVICE)
583 if (nick != NULL && host != NULL) {
584 if (strcasecmp(nick, Client_ID(cl)) == 0 &&
585 strcasecmp(user, Client_User(cl)) == 0 &&
586 strcasecmp(host, Client_HostnameDisplayed(cl)) == 0)
591 if (strcasecmp(user, Client_User(cl)) != 0)
593 if (host != NULL && strcasecmp(host,
594 Client_HostnameDisplayed(cl)) != 0)
596 if (server != NULL && strcasecmp(server,
597 Client_ID(Client_Introducer(cl))) != 0)
604 /* Target is a user, enforce type */
606 if (Client_Type(cl) != ForceType &&
607 !(ForceType == CLIENT_USER &&
608 (Client_Type(cl) == CLIENT_USER ||
609 Client_Type(cl) == CLIENT_SERVICE))) {
611 if (Client_Type(cl) != ForceType) {
613 if (SendErrors && !IRC_WriteErrClient(
614 from, ERR_NOSUCHNICK_MSG,Client_ID(from),
617 goto send_next_target;
621 if (ForceType == CLIENT_SERVICE &&
622 (Conn_Options(Client_Conn(Client_NextHop(cl)))
624 /* SQUERY command but RFC 1459 link: convert
625 * request to PRIVMSG command */
626 Req->command = "PRIVMSG";
629 if (Client_HasMode(cl, 'b') &&
630 !Client_HasMode(from, 'R') &&
631 !Client_HasMode(from, 'o') &&
632 !(Client_Type(from) == CLIENT_SERVER) &&
633 !(Client_Type(from) == CLIENT_SERVICE)) {
634 if (SendErrors && !IRC_WriteErrClient(from,
636 Client_ID(from), Client_ID(cl)))
638 goto send_next_target;
641 if (Client_HasMode(cl, 'C')) {
642 cl2chan = Channel_FirstChannelOf(cl);
644 chan = Channel_GetChannel(cl2chan);
645 if (Channel_IsMemberOf(chan, from))
647 cl2chan = Channel_NextChannelOf(cl, cl2chan);
650 if (SendErrors && !IRC_WriteErrClient(
651 from, ERR_NOTONSAMECHANNEL_MSG,
652 Client_ID(from), Client_ID(cl)))
654 goto send_next_target;
658 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
659 && Client_HasMode(cl, 'a')) {
661 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
667 if (Client_Conn(from) > NONE) {
668 Conn_UpdateIdle(Client_Conn(from));
670 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
671 Req->command, Client_ID(cl),
674 } else if (ForceType != CLIENT_SERVICE
675 && (chan = Channel_Search(currentTarget))) {
676 if (!Channel_Write(chan, from, Client, Req->command,
677 SendErrors, message))
679 } else if (ForceType != CLIENT_SERVICE
680 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
681 && strchr("$#", currentTarget[0])
682 && strchr(currentTarget, '.')) {
684 if (!Send_Message_Mask(from, Req->command, currentTarget,
685 message, SendErrors))
690 if (!IRC_WriteErrClient(from, ERR_NOSUCHNICK_MSG,
691 Client_ID(from), currentTarget))
696 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
698 Conn_SetPenalty(Client_Conn(Client), 1);
705 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
706 char * message, bool SendErrors)
710 char *mask = targetMask + 1;
711 const char *check_wildcards;
715 if (!Client_HasMode(from, 'o')) {
718 return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
723 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
724 * dot (".") and no wildcards ("*", "?") following the last one.
726 check_wildcards = strrchr(targetMask, '.');
727 assert(check_wildcards != NULL);
728 if (check_wildcards &&
729 check_wildcards[strcspn(check_wildcards, "*?")])
733 return IRC_WriteErrClient(from, ERR_WILDTOPLEVEL, targetMask);
736 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
737 if (targetMask[0] == '#') {
738 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
739 if (Client_Type(cl) != CLIENT_USER)
741 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
743 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
744 command, Client_ID(cl), message))
748 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
749 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
750 if (Client_Type(cl) != CLIENT_USER)
752 client_match = MatchCaseInsensitive(mask,
753 Client_ID(Client_Introducer(cl)));
755 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
756 command, Client_ID(cl), message))
761 } /* Send_Message_Mask */