2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2015 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.
26 #include "conn-func.h"
30 # include "conn-encoding.h"
32 #include "irc-macros.h"
33 #include "irc-write.h"
42 static char *Option_String PARAMS((CONN_ID Idx));
43 static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType,
45 static bool Send_Message_Mask PARAMS((CLIENT *from, char *command,
46 char *targetMask, char *message,
48 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);
79 * Handler for the IRC "ERROR" command.
81 * @param Client The client from which this command has been received.
82 * @param Req Request structure with prefix and all parameters.
83 * @return CONNECTED or DISCONNECTED.
86 IRC_ERROR(CLIENT *Client, REQUEST *Req)
88 assert( Client != NULL );
89 assert( Req != NULL );
91 if (Client_Type(Client) != CLIENT_GOTPASS
92 && Client_Type(Client) != CLIENT_GOTPASS_2813
93 && Client_Type(Client) != CLIENT_UNKNOWNSERVER
94 && Client_Type(Client) != CLIENT_SERVER
95 && Client_Type(Client) != CLIENT_SERVICE) {
96 LogDebug("Ignored ERROR command from \"%s\" ...",
98 IRC_SetPenalty(Client, 2);
103 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
104 Client_Mask(Client));
106 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
107 Client_Mask(Client), Req->argv[0]);
113 * Handler for the IRC "KILL" command.
115 * This function implements the IRC command "KILL" which is used to selectively
116 * disconnect clients. It can be used by IRC operators and servers, for example
117 * to "solve" nick collisions after netsplits. See RFC 2812 section 3.7.1.
119 * Please note that this function is also called internally, without a real
120 * KILL command being received over the network! Client is Client_ThisServer()
121 * in this case, and the prefix in Req is NULL.
123 * @param Client The client from which this command has been received or
124 * Client_ThisServer() when generated interanlly.
125 * @param Req Request structure with prefix and all parameters.
126 * @return CONNECTED or DISCONNECTED.
129 IRC_KILL(CLIENT *Client, REQUEST *Req)
132 char reason[COMMAND_LEN];
134 assert (Client != NULL);
135 assert (Req != NULL);
137 if (Client_Type(Client) != CLIENT_SERVER && !Op_Check(Client, Req))
138 return Op_NoPrivileges(Client, Req);
140 /* Get prefix (origin); use the client if no prefix is given. */
142 prefix = Client_Search(Req->prefix);
146 /* Log a warning message and use this server as origin when the
147 * prefix (origin) is invalid. And this is the reason why we don't
148 * use the _IRC_GET_SENDER_OR_RETURN_ macro above! */
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 return IRC_KillClient(Client, prefix, Req->argv[0], reason);
172 * Handler for the IRC "NOTICE" command.
174 * @param Client The client from which this command has been received.
175 * @param Req Request structure with prefix and all parameters.
176 * @return CONNECTED or DISCONNECTED.
179 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
181 return Send_Message(Client, Req, CLIENT_USER, false);
185 * Handler for the IRC "PRIVMSG" command.
187 * @param Client The client from which this command has been received.
188 * @param Req Request structure with prefix and all parameters.
189 * @return CONNECTED or DISCONNECTED.
192 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
194 return Send_Message(Client, Req, CLIENT_USER, true);
198 * Handler for the IRC "SQUERY" command.
200 * @param Client The client from which this command has been received.
201 * @param Req Request structure with prefix and all parameters.
202 * @return CONNECTED or DISCONNECTED.
205 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
207 return Send_Message(Client, Req, CLIENT_SERVICE, true);
211 * Handler for the IRC "TRACE" command.
213 * @param Client The client from which this command has been received.
214 * @param Req Request structure with prefix and all parameters.
215 * @return CONNECTED or DISCONNECTED.
218 IRC_TRACE(CLIENT *Client, REQUEST *Req)
220 CLIENT *from, *target, *c;
222 char user[CLIENT_USER_LEN];
224 assert(Client != NULL);
227 _IRC_GET_SENDER_OR_RETURN_(from, Req, Client)
228 _IRC_GET_TARGET_SERVER_OR_RETURN_(target, Req, 0, from)
230 /* Forward command to other server? */
231 if (target != Client_ThisServer()) {
232 /* Send RPL_TRACELINK back to initiator */
233 idx = Client_Conn(Client);
235 idx2 = Client_Conn(Client_NextHop(target));
238 if (!IRC_WriteStrClient(from, RPL_TRACELINK_MSG,
239 Client_ID(from), PACKAGE_NAME,
240 PACKAGE_VERSION, Client_ID(target),
241 Client_ID(Client_NextHop(target)),
243 (long)(time(NULL) - Conn_StartTime(idx2)),
244 Conn_SendQ(idx), Conn_SendQ(idx2)))
247 /* Forward command */
248 IRC_WriteStrClientPrefix(target, from, "TRACE %s", Req->argv[0]);
252 /* Infos about all connected servers */
255 if (Client_Conn(c) > NONE) {
257 if (Client_Type(c) == CLIENT_SERVER) {
259 strlcpy(user, Client_User(c), sizeof(user));
261 strlcpy(user, "unknown", sizeof(user));
262 if (!IRC_WriteStrClient(from,
264 Client_ID(from), Client_ID(c),
265 user, Client_Hostname(c),
266 Client_Mask(Client_ThisServer()),
267 Option_String(Client_Conn(c))))
270 if (Client_Type(c) == CLIENT_USER
271 && Client_HasMode(c, 'o')) {
273 if (!IRC_WriteStrClient(from,
274 RPL_TRACEOPERATOR_MSG,
275 Client_ID(from), Client_ID(c)))
279 c = Client_Next( c );
282 return IRC_WriteStrClient(from, RPL_TRACEEND_MSG, Client_ID(from),
283 Conf_ServerName, PACKAGE_NAME,
284 PACKAGE_VERSION, NGIRCd_DebugLevel);
288 * Handler for the IRC "HELP" command.
290 * @param Client The client from which this command has been received.
291 * @param Req Request structure with prefix and all parameters.
292 * @return CONNECTED or DISCONNECTED.
295 IRC_HELP(CLIENT *Client, REQUEST *Req)
299 assert(Client != NULL);
302 if ((Req->argc == 0 && array_bytes(&Conf_Helptext) > 0)
303 || (Req->argc >= 1 && strcasecmp(Req->argv[0], "Commands") != 0)) {
304 /* Help text available and requested */
306 return Help(Client, Req->argv[0]);
308 if (!Help(Client, "Intro"))
313 cmd = Parse_GetCommandStruct();
315 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
316 Client_ID(Client), cmd->name))
324 * Kill an client identified by its nick name.
326 * Please note that after killig a client, its CLIENT cond CONNECTION
327 * structures are invalid. So the caller must make sure on its own not to
328 * access data of probably killed clients after calling this function!
330 * @param Client The client from which the command leading to the KILL has
331 * been received, or NULL. The KILL will no be forwarded in this
332 * direction. Only relevant when From is set, too.
333 * @param From The client from which the command originated, or NULL for
335 * @param Nick The nick name to kill.
336 * @param Reason Text to send as reason to the client and other servers.
339 IRC_KillClient(CLIENT *Client, CLIENT *From, const char *Nick, const char *Reason)
342 CONN_ID my_conn = NONE, conn;
345 assert(Nick != NULL);
346 assert(Reason != NULL);
348 /* Do we know such a client in the network? */
349 c = Client_Search(Nick);
351 LogDebug("Client with nick \"%s\" is unknown, not forwaring.", Nick);
355 /* Inform other servers */
356 IRC_WriteStrServersPrefix(From ? Client : NULL,
357 From ? From : Client_ThisServer(),
358 "KILL %s :%s", Nick, Reason);
360 if (Client_Type(c) != CLIENT_USER && Client_Type(c) != CLIENT_GOTNICK) {
361 /* Target of this KILL is not a regular user, this is
362 * invalid! So we ignore this case if we received a
363 * regular KILL from the network and try to kill the
364 * client/connection anyway (but log an error!) if the
365 * origin is the local server. */
367 if (Client != Client_ThisServer()) {
368 /* Invalid KILL received from remote */
369 if (Client_Type(c) == CLIENT_SERVER)
370 msg = ERR_CANTKILLSERVER_MSG;
372 msg = ERR_NOPRIVILEGES_MSG;
373 return IRC_WriteErrClient(Client, msg, Client_ID(Client));
377 "Got KILL for invalid client type: %d, \"%s\"!",
378 Client_Type(c), Nick);
381 /* Save ID of this connection */
383 my_conn = Client_Conn(Client);
385 /* Kill the client NOW:
386 * - Close the local connection (if there is one),
387 * - Destroy the CLIENT structure for remote clients.
388 * Note: Conn_Close() removes the CLIENT structure as well. */
389 conn = Client_Conn(c);
391 Conn_Close(conn, NULL, Reason, true);
393 Client_Destroy(c, NULL, Reason, false);
395 /* Are we still connected or were we killed, too? */
396 if (my_conn > NONE && Conn_GetClient(my_conn))
403 * Send help for a given topic to the client.
405 * @param Client The client requesting help.
406 * @param Topoc The help topic requested.
407 * @return CONNECTED or DISCONNECTED.
410 Help(CLIENT *Client, const char *Topic)
413 size_t helptext_len, len_str, idx_start, lines = 0;
414 bool in_article = false;
416 assert(Client != NULL);
417 assert(Topic != NULL);
419 helptext_len = array_bytes(&Conf_Helptext);
420 line = array_start(&Conf_Helptext);
421 while (helptext_len > 0) {
422 len_str = strlen(line) + 1;
423 assert(helptext_len >= len_str);
424 helptext_len -= len_str;
427 /* The first character in each article text line must
428 * be a TAB (ASCII 9) character which will be stripped
429 * in the output. If it is not a TAB, the end of the
430 * article has been reached. */
431 if (line[0] != '\t') {
438 /* A single '.' character indicates an empty line */
439 if (line[1] == '.' && line[2] == '\0')
444 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
451 if (line[0] == '-' && line[1] == ' '
452 && strcasecmp(&line[2], Topic) == 0)
459 /* Help topic not found (or empty)! */
460 if (!IRC_WriteStrClient(Client, "NOTICE %s :No help for \"%s\" found!",
461 Client_ID(Client), Topic))
468 * Get pointer to a static string representing the connection "options".
470 * @param Idx Connection index.
471 * @return Pointer to static (global) string buffer.
475 Option_String(CONN_ID Idx)
477 Option_String(UNUSED CONN_ID Idx)
480 static char option_txt[8];
485 options = Conn_Options(Idx);
486 strcpy(option_txt, "F"); /* No idea what this means, but the
487 * original ircd sends it ... */
489 if(options & CONN_SSL) /* SSL encrypted link */
490 strlcat(option_txt, "s", sizeof(option_txt));
493 if(options & CONN_ZIP) /* zlib compression enabled */
494 strlcat(option_txt, "z", sizeof(option_txt));
496 LogDebug(" *** %d: %d = %s", Idx, options, option_txt);
499 } /* Option_String */
502 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
507 char *currentTarget = Req->argv[0];
508 char *lastCurrentTarget = NULL;
509 char *message = NULL;
511 assert(Client != NULL);
514 if (Req->argc == 0) {
517 return IRC_WriteErrClient(Client, ERR_NORECIPIENT_MSG,
518 Client_ID(Client), Req->command);
520 if (Req->argc == 1) {
523 return IRC_WriteErrClient(Client, ERR_NOTEXTTOSEND_MSG,
529 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
530 Client_ID(Client), Req->command);
533 if (Client_Type(Client) == CLIENT_SERVER && Req->prefix)
534 from = Client_Search(Req->prefix);
538 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
539 Client_ID(Client), Req->prefix);
542 if (Client_Conn(Client) > NONE)
543 message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]);
546 message = Req->argv[1];
548 /* handle msgtarget = msgto *("," msgto) */
549 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
550 ngt_UpperStr(Req->command);
552 while (currentTarget) {
553 /* Check for and handle valid <msgto> of form:
555 * msgto = channel / ( user [ "%" host ] "@" servername )
556 * msgto =/ ( user "%" host ) / targetmask
557 * msgto =/ nickname / ( nickname "!" user "@" host )
559 if (strchr(currentTarget, '!') == NULL)
561 cl = Client_Search(currentTarget);
566 /* If currentTarget isn't a nickname check for:
567 * user ["%" host] "@" servername
569 * nickname "!" user "@" host
571 char target[COMMAND_LEN];
575 char * server = NULL;
577 strlcpy(target, currentTarget, COMMAND_LEN);
578 server = strchr(target, '@');
583 host = strchr(target, '%');
588 user = strchr(target, '!');
590 /* msgto form: nick!user@host */
594 host = server; /* not "@server" but "@host" */
599 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
600 if (Client_Type(cl) != CLIENT_USER &&
601 Client_Type(cl) != CLIENT_SERVICE)
603 if (nick != NULL && host != NULL) {
604 if (strcasecmp(nick, Client_ID(cl)) == 0 &&
605 strcasecmp(user, Client_User(cl)) == 0 &&
606 strcasecmp(host, Client_HostnameDisplayed(cl)) == 0)
611 if (strcasecmp(user, Client_User(cl)) != 0)
613 if (host != NULL && strcasecmp(host,
614 Client_HostnameDisplayed(cl)) != 0)
616 if (server != NULL && strcasecmp(server,
617 Client_ID(Client_Introducer(cl))) != 0)
624 /* Target is a user, enforce type */
626 if (Client_Type(cl) != ForceType &&
627 !(ForceType == CLIENT_USER &&
628 (Client_Type(cl) == CLIENT_USER ||
629 Client_Type(cl) == CLIENT_SERVICE))) {
631 if (Client_Type(cl) != ForceType) {
633 if (SendErrors && !IRC_WriteErrClient(
634 from, ERR_NOSUCHNICK_MSG,Client_ID(from),
637 goto send_next_target;
641 if (ForceType == CLIENT_SERVICE &&
642 (Conn_Options(Client_Conn(Client_NextHop(cl)))
644 /* SQUERY command but RFC 1459 link: convert
645 * request to PRIVMSG command */
646 Req->command = "PRIVMSG";
649 if (Client_HasMode(cl, 'b') &&
650 !Client_HasMode(from, 'R') &&
651 !Client_HasMode(from, 'o') &&
652 !(Client_Type(from) == CLIENT_SERVER) &&
653 !(Client_Type(from) == CLIENT_SERVICE)) {
654 if (SendErrors && !IRC_WriteErrClient(from,
656 Client_ID(from), Client_ID(cl)))
658 goto send_next_target;
661 if (Client_HasMode(cl, 'C')) {
662 cl2chan = Channel_FirstChannelOf(cl);
664 chan = Channel_GetChannel(cl2chan);
665 if (Channel_IsMemberOf(chan, from))
667 cl2chan = Channel_NextChannelOf(cl, cl2chan);
670 if (SendErrors && !IRC_WriteErrClient(
671 from, ERR_NOTONSAMECHANNEL_MSG,
672 Client_ID(from), Client_ID(cl)))
674 goto send_next_target;
678 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
679 && Client_HasMode(cl, 'a')) {
681 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
687 if (Client_Conn(from) > NONE) {
688 Conn_UpdateIdle(Client_Conn(from));
690 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
691 Req->command, Client_ID(cl),
694 } else if (ForceType != CLIENT_SERVICE
695 && (chan = Channel_Search(currentTarget))) {
696 if (!Channel_Write(chan, from, Client, Req->command,
697 SendErrors, message))
699 } else if (ForceType != CLIENT_SERVICE
700 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
701 && strchr("$#", currentTarget[0])
702 && strchr(currentTarget, '.')) {
704 if (!Send_Message_Mask(from, Req->command, currentTarget,
705 message, SendErrors))
710 if (!IRC_WriteErrClient(from, ERR_NOSUCHNICK_MSG,
711 Client_ID(from), currentTarget))
716 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
718 Conn_SetPenalty(Client_Conn(Client), 1);
725 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
726 char * message, bool SendErrors)
730 char *mask = targetMask + 1;
731 const char *check_wildcards;
735 if (!Client_HasMode(from, 'o')) {
738 return IRC_WriteErrClient(from, ERR_NOPRIVILEGES_MSG,
743 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
744 * dot (".") and no wildcards ("*", "?") following the last one.
746 check_wildcards = strrchr(targetMask, '.');
747 assert(check_wildcards != NULL);
748 if (check_wildcards &&
749 check_wildcards[strcspn(check_wildcards, "*?")])
753 return IRC_WriteErrClient(from, ERR_WILDTOPLEVEL, targetMask);
756 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
757 if (targetMask[0] == '#') {
758 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
759 if (Client_Type(cl) != CLIENT_USER)
761 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
763 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
764 command, Client_ID(cl), message))
768 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
769 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
770 if (Client_Type(cl) != CLIENT_USER)
772 client_match = MatchCaseInsensitive(mask,
773 Client_ID(Client_Introducer(cl)));
775 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
776 command, Client_ID(cl), message))
781 } /* Send_Message_Mask */