2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 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-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));
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);
80 IRC_ERROR( CLIENT *Client, REQUEST *Req )
82 assert( Client != NULL );
83 assert( Req != NULL );
86 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
89 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
90 Client_Mask(Client), Req->argv[0]);
97 * Handler for the IRC "KILL" command.
99 * This function implements the IRC command "KILL" wich is used to selectively
100 * disconnect clients. It can be used by IRC operators and servers, for example
101 * to "solve" nick collisions after netsplits. See RFC 2812 section 3.7.1.
103 * Please note that this function is also called internally, without a real
104 * KILL command being received over the network! Client is Client_ThisServer()
105 * in this case, and the prefix in Req is NULL.
107 * @param Client The client from which this command has been received
108 * or Client_ThisServer() when generated interanlly.
109 * @param Req Request structure with prefix and all parameters.
110 * @returns CONNECTED or DISCONNECTED.
113 IRC_KILL( CLIENT *Client, REQUEST *Req )
116 char reason[COMMAND_LEN], *msg;
117 CONN_ID my_conn, conn;
119 assert (Client != NULL);
120 assert (Req != NULL);
122 if (Client_Type(Client) != CLIENT_SERVER && !Client_OperByMe(Client))
123 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
127 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
128 Client_ID(Client), Req->command);
130 /* Get prefix (origin); use the client if no prefix is given. */
132 prefix = Client_Search(Req->prefix);
136 /* Log a warning message and use this server as origin when the
137 * prefix (origin) is invalid. */
139 Log(LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
141 prefix = Client_ThisServer();
144 if (Client != Client_ThisServer())
145 Log(LOG_NOTICE|LOG_snotice,
146 "Got KILL command from \"%s\" for \"%s\": %s",
147 Client_Mask(prefix), Req->argv[0], Req->argv[1]);
149 /* Build reason string: Prefix the "reason" if the originator is a
150 * regular user, so users can't spoof KILLs of servers. */
151 if (Client_Type(Client) == CLIENT_USER)
152 snprintf(reason, sizeof(reason), "KILLed by %s: %s",
153 Client_ID(Client), Req->argv[1]);
155 strlcpy(reason, Req->argv[1], sizeof(reason));
157 /* Inform other servers */
158 IRC_WriteStrServersPrefix(Client, prefix, "KILL %s :%s",
159 Req->argv[0], reason);
161 /* Save ID of this connection */
162 my_conn = Client_Conn( Client );
164 /* Do we host such a client? */
165 c = Client_Search( Req->argv[0] );
168 if(( Client_Type( c ) != CLIENT_USER ) &&
169 ( Client_Type( c ) != CLIENT_GOTNICK ))
171 /* Target of this KILL is not a regular user, this is
172 * invalid! So we ignore this case if we received a
173 * regular KILL from the network and try to kill the
174 * client/connection anyway (but log an error!) if the
175 * origin is the local server. */
177 if( Client != Client_ThisServer( ))
179 /* Invalid KILL received from remote */
180 if( Client_Type( c ) == CLIENT_SERVER )
181 msg = ERR_CANTKILLSERVER_MSG;
183 msg = ERR_NOPRIVILEGES_MSG;
184 return IRC_WriteStrClient( Client, msg,
185 Client_ID( Client ));
188 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
189 Client_Type( c ), Req->argv[0] );
192 /* Kill the client NOW:
193 * - Close the local connection (if there is one),
194 * - Destroy the CLIENT structure for remote clients.
195 * Note: Conn_Close() removes the CLIENT structure as well. */
196 conn = Client_Conn( c );
198 Conn_Close(conn, NULL, reason, true);
200 Client_Destroy(c, NULL, reason, false);
203 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
205 /* Are we still connected or were we killed, too? */
206 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
214 * Handler for the IRC command NOTICE.
217 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
219 return Send_Message(Client, Req, CLIENT_USER, false);
224 * Handler for the IRC command PRIVMSG.
227 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
229 return Send_Message(Client, Req, CLIENT_USER, true);
234 * Handler for the IRC command SQUERY.
237 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
239 return Send_Message(Client, Req, CLIENT_SERVICE, true);
244 IRC_TRACE( CLIENT *Client, REQUEST *Req )
246 CLIENT *from, *target, *c;
248 char user[CLIENT_USER_LEN];
250 assert( Client != NULL );
251 assert( Req != NULL );
253 /* Bad number of arguments? */
254 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
257 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
259 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
262 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
263 else target = Client_ThisServer( );
265 /* Forward command to other server? */
266 if( target != Client_ThisServer( ))
268 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
270 /* Send RPL_TRACELINK back to initiator */
271 idx = Client_Conn( Client ); assert( idx > NONE );
272 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
273 if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), PACKAGE_NAME, PACKAGE_VERSION, Client_ID( target ), Client_ID( Client_NextHop( target )), Option_String( idx2 ), time( NULL ) - Conn_StartTime( idx2 ), Conn_SendQ( idx ), Conn_SendQ( idx2 ))) return DISCONNECTED;
275 /* Forward command */
276 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
280 /* Infos about all connected servers */
284 if( Client_Conn( c ) > NONE )
287 if( Client_Type( c ) == CLIENT_SERVER )
290 strlcpy( user, Client_User( c ), sizeof( user ));
291 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
292 if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), user, Client_Hostname( c ), Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( c )))) return DISCONNECTED;
294 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
297 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
300 c = Client_Next( c );
303 IRC_SetPenalty( Client, 3 );
304 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
309 * Handler for the IRC "HELP" command.
311 * @param Client The client from which this command has been received.
312 * @param Req Request structure with prefix and all parameters.
313 * @return CONNECTED or DISCONNECTED.
316 IRC_HELP(CLIENT *Client, REQUEST *Req)
320 assert(Client != NULL);
323 /* Bad number of arguments? */
325 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
326 Client_ID(Client), Req->command);
328 IRC_SetPenalty(Client, 2);
330 if ((Req->argc == 0 && array_bytes(&Conf_Helptext) > 0)
331 || (Req->argc >= 1 && strcasecmp(Req->argv[0], "Commands") != 0)) {
332 /* Help text available and requested */
334 return Help(Client, Req->argv[0]);
336 if (!Help(Client, "Intro"))
341 cmd = Parse_GetCommandStruct();
343 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
344 Client_ID(Client), cmd->name))
353 * Send help for a given topic to the client.
355 * @param Client The client requesting help.
356 * @param Topoc The help topic requested.
357 * @return CONNECTED or DISCONNECTED.
360 Help(CLIENT *Client, const char *Topic)
363 size_t helptext_len, len_str, idx_start, lines = 0;
364 bool in_article = false;
366 assert(Client != NULL);
367 assert(Topic != NULL);
369 helptext_len = array_bytes(&Conf_Helptext);
370 line = array_start(&Conf_Helptext);
371 while (helptext_len > 0) {
372 len_str = strlen(line) + 1;
373 assert(helptext_len >= len_str);
374 helptext_len -= len_str;
377 /* The first character in each article text line must
378 * be a TAB (ASCII 9) character which will be stripped
379 * in the output. If it is not a TAB, the end of the
380 * article has been reached. */
381 if (line[0] != '\t') {
388 /* A single '.' character indicates an empty line */
389 if (line[1] == '.' && line[2] == '\0')
394 if (!IRC_WriteStrClient(Client, "NOTICE %s :%s",
401 if (line[0] == '-' && line[1] == ' '
402 && strcasecmp(&line[2], Topic) == 0)
409 /* Help topic not found (or empty)! */
410 if (!IRC_WriteStrClient(Client, "NOTICE %s :No help for \"%s\" found!",
411 Client_ID(Client), Topic))
420 Option_String(CONN_ID Idx)
422 Option_String(UNUSED CONN_ID Idx)
425 static char option_txt[8];
429 options = Conn_Options(Idx);
432 strcpy(option_txt, "F"); /* No idea what this means, but the
433 * original ircd sends it ... */
435 if(options & CONN_ZIP) /* zlib compression supported. */
436 strcat(option_txt, "z");
440 } /* Option_String */
444 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
449 char *currentTarget = Req->argv[0];
450 char *lastCurrentTarget = NULL;
451 char *message = NULL;
453 assert(Client != NULL);
456 if (Req->argc == 0) {
459 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
460 Client_ID(Client), Req->command);
462 if (Req->argc == 1) {
465 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
471 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
472 Client_ID(Client), Req->command);
475 if (Client_Type(Client) == CLIENT_SERVER)
476 from = Client_Search(Req->prefix);
480 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
481 Client_ID(Client), Req->prefix);
484 if (Client_Conn(Client) > NONE)
485 message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]);
488 message = Req->argv[1];
490 /* handle msgtarget = msgto *("," msgto) */
491 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
492 ngt_UpperStr(Req->command);
494 while (currentTarget) {
495 /* Check for and handle valid <msgto> of form:
497 * msgto = channel / ( user [ "%" host ] "@" servername )
498 * msgto =/ ( user "%" host ) / targetmask
499 * msgto =/ nickname / ( nickname "!" user "@" host )
501 if (strchr(currentTarget, '!') == NULL)
503 cl = Client_Search(currentTarget);
508 /* If currentTarget isn't a nickname check for:
509 * user ["%" host] "@" servername
511 * nickname "!" user "@" host
513 char target[COMMAND_LEN];
517 char * server = NULL;
519 strlcpy(target, currentTarget, COMMAND_LEN);
520 server = strchr(target, '@');
525 host = strchr(target, '%');
530 user = strchr(target, '!');
532 /* msgto form: nick!user@host */
536 host = server; /* not "@server" but "@host" */
541 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
542 if (Client_Type(cl) != CLIENT_USER &&
543 Client_Type(cl) != CLIENT_SERVICE)
545 if (nick != NULL && host != NULL) {
546 if (strcasecmp(nick, Client_ID(cl)) == 0 &&
547 strcasecmp(user, Client_User(cl)) == 0 &&
548 strcasecmp(host, Client_HostnameDisplayed(cl)) == 0)
553 if (strcasecmp(user, Client_User(cl)) != 0)
555 if (host != NULL && strcasecmp(host,
556 Client_HostnameDisplayed(cl)) != 0)
558 if (server != NULL && strcasecmp(server,
559 Client_ID(Client_Introducer(cl))) != 0)
566 /* Target is a user, enforce type */
568 if (Client_Type(cl) != ForceType &&
569 !(ForceType == CLIENT_USER &&
570 (Client_Type(cl) == CLIENT_USER ||
571 Client_Type(cl) == CLIENT_SERVICE))) {
573 if (Client_Type(cl) != ForceType) {
575 if (SendErrors && !IRC_WriteStrClient(
576 from, ERR_NOSUCHNICK_MSG,Client_ID(from),
579 goto send_next_target;
583 if (ForceType == CLIENT_SERVICE &&
584 (Conn_Options(Client_Conn(Client_NextHop(cl)))
586 /* SQUERY command but RFC 1459 link: convert
587 * request to PRIVMSG command */
588 Req->command = "PRIVMSG";
591 if (Client_HasMode(cl, 'b') &&
592 !Client_HasMode(from, 'R') &&
593 !Client_HasMode(from, 'o') &&
594 !(Client_Type(from) == CLIENT_SERVER) &&
595 !(Client_Type(from) == CLIENT_SERVICE)) {
596 if (SendErrors && !IRC_WriteStrClient(from,
598 Client_ID(from), Client_ID(cl)))
600 goto send_next_target;
603 if (Client_HasMode(cl, 'C')) {
604 cl2chan = Channel_FirstChannelOf(cl);
606 chan = Channel_GetChannel(cl2chan);
607 if (Channel_IsMemberOf(chan, from))
609 cl2chan = Channel_NextChannelOf(cl, cl2chan);
612 if (SendErrors && !IRC_WriteStrClient(
613 from, ERR_NOTONSAMECHANNEL_MSG,
614 Client_ID(from), Client_ID(cl)))
616 goto send_next_target;
620 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
621 && strchr(Client_Modes(cl), 'a')) {
623 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
629 if (Client_Conn(from) > NONE) {
630 Conn_UpdateIdle(Client_Conn(from));
632 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
633 Req->command, Client_ID(cl),
636 } else if (ForceType != CLIENT_SERVICE
637 && (chan = Channel_Search(currentTarget))) {
638 if (!Channel_Write(chan, from, Client, Req->command,
639 SendErrors, message))
641 } else if (ForceType != CLIENT_SERVICE
642 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
643 && strchr("$#", currentTarget[0])
644 && strchr(currentTarget, '.')) {
646 if (!Send_Message_Mask(from, Req->command, currentTarget,
647 message, SendErrors))
652 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
653 Client_ID(from), currentTarget))
658 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
660 Conn_SetPenalty(Client_Conn(Client), 1);
668 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
669 char * message, bool SendErrors)
673 char *mask = targetMask + 1;
674 const char *check_wildcards;
678 if (strchr(Client_Modes(from), 'o') == NULL) {
681 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
686 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
687 * dot (".") and no wildcards ("*", "?") following the last one.
689 check_wildcards = strrchr(targetMask, '.');
690 assert(check_wildcards != NULL);
691 if (check_wildcards &&
692 check_wildcards[strcspn(check_wildcards, "*?")])
696 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
699 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
700 if (targetMask[0] == '#') {
701 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
702 if (Client_Type(cl) != CLIENT_USER)
704 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
706 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
707 command, Client_ID(cl), message))
711 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
712 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
713 if (Client_Type(cl) != CLIENT_USER)
715 client_match = MatchCaseInsensitive(mask,
716 Client_ID(Client_Introducer(cl)));
718 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
719 command, Client_ID(cl), message))
724 } /* Send_Message_Mask */