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,
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);
79 IRC_ERROR( CLIENT *Client, REQUEST *Req )
81 assert( Client != NULL );
82 assert( Req != NULL );
85 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
88 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
89 Client_Mask(Client), Req->argv[0]);
96 * Handler for the IRC "KILL" command.
98 * This function implements the IRC command "KILL" wich is used to selectively
99 * disconnect clients. It can be used by IRC operators and servers, for example
100 * to "solve" nick collisions after netsplits. See RFC 2812 section 3.7.1.
102 * Please note that this function is also called internally, without a real
103 * KILL command being received over the network! Client is Client_ThisServer()
104 * in this case, and the prefix in Req is NULL.
106 * @param Client The client from which this command has been received
107 * or Client_ThisServer() when generated interanlly.
108 * @param Req Request structure with prefix and all parameters.
109 * @returns CONNECTED or DISCONNECTED.
112 IRC_KILL( CLIENT *Client, REQUEST *Req )
115 char reason[COMMAND_LEN], *msg;
116 CONN_ID my_conn, conn;
118 assert (Client != NULL);
119 assert (Req != NULL);
121 if (Client_Type(Client) != CLIENT_SERVER && !Client_OperByMe(Client))
122 return IRC_WriteStrClient(Client, ERR_NOPRIVILEGES_MSG,
126 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
127 Client_ID(Client), Req->command);
129 /* Get prefix (origin); use the client if no prefix is given. */
131 prefix = Client_Search(Req->prefix);
135 /* Log a warning message and use this server as origin when the
136 * prefix (origin) is invalid. */
138 Log(LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
140 prefix = Client_ThisServer();
143 if (Client != Client_ThisServer())
144 Log(LOG_NOTICE|LOG_snotice,
145 "Got KILL command from \"%s\" for \"%s\": %s",
146 Client_Mask(prefix), Req->argv[0], Req->argv[1]);
148 /* Build reason string: Prefix the "reason" if the originator is a
149 * regular user, so users can't spoof KILLs of servers. */
150 if (Client_Type(Client) == CLIENT_USER)
151 snprintf(reason, sizeof(reason), "KILLed by %s: %s",
152 Client_ID(Client), Req->argv[1]);
154 strlcpy(reason, Req->argv[1], sizeof(reason));
156 /* Inform other servers */
157 IRC_WriteStrServersPrefix(Client, prefix, "KILL %s :%s",
158 Req->argv[0], reason);
160 /* Save ID of this connection */
161 my_conn = Client_Conn( Client );
163 /* Do we host such a client? */
164 c = Client_Search( Req->argv[0] );
167 if(( Client_Type( c ) != CLIENT_USER ) &&
168 ( Client_Type( c ) != CLIENT_GOTNICK ))
170 /* Target of this KILL is not a regular user, this is
171 * invalid! So we ignore this case if we received a
172 * regular KILL from the network and try to kill the
173 * client/connection anyway (but log an error!) if the
174 * origin is the local server. */
176 if( Client != Client_ThisServer( ))
178 /* Invalid KILL received from remote */
179 if( Client_Type( c ) == CLIENT_SERVER )
180 msg = ERR_CANTKILLSERVER_MSG;
182 msg = ERR_NOPRIVILEGES_MSG;
183 return IRC_WriteStrClient( Client, msg,
184 Client_ID( Client ));
187 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
188 Client_Type( c ), Req->argv[0] );
191 /* Kill the client NOW:
192 * - Close the local connection (if there is one),
193 * - Destroy the CLIENT structure for remote clients.
194 * Note: Conn_Close() removes the CLIENT structure as well. */
195 conn = Client_Conn( c );
197 Conn_Close(conn, NULL, reason, true);
199 Client_Destroy(c, NULL, reason, false);
202 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
204 /* Are we still connected or were we killed, too? */
205 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
213 * Handler for the IRC command NOTICE.
216 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
218 return Send_Message(Client, Req, CLIENT_USER, false);
223 * Handler for the IRC command PRIVMSG.
226 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
228 return Send_Message(Client, Req, CLIENT_USER, true);
233 * Handler for the IRC command SQUERY.
236 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
238 return Send_Message(Client, Req, CLIENT_SERVICE, true);
243 IRC_TRACE( CLIENT *Client, REQUEST *Req )
245 CLIENT *from, *target, *c;
247 char user[CLIENT_USER_LEN];
249 assert( Client != NULL );
250 assert( Req != NULL );
252 /* Bad number of arguments? */
253 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
256 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
258 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
261 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
262 else target = Client_ThisServer( );
264 /* Forward command to other server? */
265 if( target != Client_ThisServer( ))
267 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
269 /* Send RPL_TRACELINK back to initiator */
270 idx = Client_Conn( Client ); assert( idx > NONE );
271 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
272 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;
274 /* Forward command */
275 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
279 /* Infos about all connected servers */
283 if( Client_Conn( c ) > NONE )
286 if( Client_Type( c ) == CLIENT_SERVER )
289 strlcpy( user, Client_User( c ), sizeof( user ));
290 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
291 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;
293 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
296 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
299 c = Client_Next( c );
302 IRC_SetPenalty( Client, 3 );
303 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
308 IRC_HELP( CLIENT *Client, REQUEST *Req )
312 assert( Client != NULL );
313 assert( Req != NULL );
315 /* Bad number of arguments? */
316 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
318 cmd = Parse_GetCommandStruct( );
321 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
325 IRC_SetPenalty( Client, 2 );
332 Option_String(CONN_ID Idx)
334 Option_String(UNUSED CONN_ID Idx)
337 static char option_txt[8];
341 options = Conn_Options(Idx);
344 strcpy(option_txt, "F"); /* No idea what this means, but the
345 * original ircd sends it ... */
347 if(options & CONN_ZIP) /* zlib compression supported. */
348 strcat(option_txt, "z");
352 } /* Option_String */
356 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
361 char *currentTarget = Req->argv[0];
362 char *lastCurrentTarget = NULL;
363 char *message = NULL;
365 assert(Client != NULL);
368 if (Req->argc == 0) {
371 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
372 Client_ID(Client), Req->command);
374 if (Req->argc == 1) {
377 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
383 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
384 Client_ID(Client), Req->command);
387 if (Client_Type(Client) == CLIENT_SERVER)
388 from = Client_Search(Req->prefix);
392 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
393 Client_ID(Client), Req->prefix);
396 if (Client_Conn(Client) > NONE)
397 message = Conn_EncodingFrom(Client_Conn(Client), Req->argv[1]);
400 message = Req->argv[1];
402 /* handle msgtarget = msgto *("," msgto) */
403 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
404 ngt_UpperStr(Req->command);
406 while (currentTarget) {
407 /* Check for and handle valid <msgto> of form:
409 * msgto = channel / ( user [ "%" host ] "@" servername )
410 * msgto =/ ( user "%" host ) / targetmask
411 * msgto =/ nickname / ( nickname "!" user "@" host )
413 if (strchr(currentTarget, '!') == NULL)
415 cl = Client_Search(currentTarget);
420 /* If currentTarget isn't a nickname check for:
421 * user ["%" host] "@" servername
423 * nickname "!" user "@" host
425 char target[COMMAND_LEN];
429 char * server = NULL;
431 strlcpy(target, currentTarget, COMMAND_LEN);
432 server = strchr(target, '@');
437 host = strchr(target, '%');
442 user = strchr(target, '!');
444 /* msgto form: nick!user@host */
448 host = server; /* not "@server" but "@host" */
453 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
454 if (Client_Type(cl) != CLIENT_USER &&
455 Client_Type(cl) != CLIENT_SERVICE)
457 if (nick != NULL && host != NULL) {
458 if (strcasecmp(nick, Client_ID(cl)) == 0 &&
459 strcasecmp(user, Client_User(cl)) == 0 &&
460 strcasecmp(host, Client_HostnameCloaked(cl)) == 0)
465 if (strcasecmp(user, Client_User(cl)) != 0)
467 if (host != NULL && strcasecmp(host,
468 Client_HostnameCloaked(cl)) != 0)
470 if (server != NULL && strcasecmp(server,
471 Client_ID(Client_Introducer(cl))) != 0)
478 /* Target is a user, enforce type */
480 if (Client_Type(cl) != ForceType &&
481 !(ForceType == CLIENT_USER &&
482 (Client_Type(cl) == CLIENT_USER ||
483 Client_Type(cl) == CLIENT_SERVICE))) {
485 if (Client_Type(cl) != ForceType) {
487 if (SendErrors && !IRC_WriteStrClient(
488 from, ERR_NOSUCHNICK_MSG,Client_ID(from),
491 goto send_next_target;
495 if (ForceType == CLIENT_SERVICE &&
496 (Conn_Options(Client_Conn(Client_NextHop(cl)))
498 /* SQUERY command but RFC 1459 link: convert
499 * request to PRIVMSG command */
500 Req->command = "PRIVMSG";
503 if (Client_HasMode(cl, 'b') &&
504 !Client_HasMode(from, 'R') &&
505 !Client_HasMode(from, 'o') &&
506 !(Client_Type(from) == CLIENT_SERVER) &&
507 !(Client_Type(from) == CLIENT_SERVICE)) {
508 if (SendErrors && !IRC_WriteStrClient(from,
510 Client_ID(from), Client_ID(cl)))
512 goto send_next_target;
515 if (Client_HasMode(cl, 'C')) {
516 cl2chan = Channel_FirstChannelOf(cl);
518 chan = Channel_GetChannel(cl2chan);
519 if (Channel_IsMemberOf(chan, from))
521 cl2chan = Channel_NextChannelOf(cl, cl2chan);
524 if (SendErrors && !IRC_WriteStrClient(
525 from, ERR_NOTONSAMECHANNEL_MSG,
526 Client_ID(from), Client_ID(cl)))
528 goto send_next_target;
532 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
533 && strchr(Client_Modes(cl), 'a')) {
535 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
541 if (Client_Conn(from) > NONE) {
542 Conn_UpdateIdle(Client_Conn(from));
544 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
545 Req->command, Client_ID(cl),
548 } else if (ForceType != CLIENT_SERVICE
549 && (chan = Channel_Search(currentTarget))) {
550 if (!Channel_Write(chan, from, Client, Req->command,
551 SendErrors, message))
553 } else if (ForceType != CLIENT_SERVICE
554 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
555 && strchr("$#", currentTarget[0])
556 && strchr(currentTarget, '.')) {
558 if (!Send_Message_Mask(from, Req->command, currentTarget,
559 message, SendErrors))
564 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
565 Client_ID(from), currentTarget))
570 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
572 Conn_SetPenalty(Client_Conn(Client), 1);
580 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
581 char * message, bool SendErrors)
585 char *mask = targetMask + 1;
586 const char *check_wildcards;
590 if (strchr(Client_Modes(from), 'o') == NULL) {
593 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
598 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
599 * dot (".") and no wildcards ("*", "?") following the last one.
601 check_wildcards = strrchr(targetMask, '.');
602 assert(check_wildcards != NULL);
603 if (check_wildcards &&
604 check_wildcards[strcspn(check_wildcards, "*?")])
608 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
611 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
612 if (targetMask[0] == '#') {
613 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
614 if (Client_Type(cl) != CLIENT_USER)
616 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
618 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
619 command, Client_ID(cl), message))
623 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
624 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
625 if (Client_Type(cl) != CLIENT_USER)
627 client_match = MatchCaseInsensitive(mask,
628 Client_ID(Client_Introducer(cl)));
630 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
631 command, Client_ID(cl), message))
636 } /* Send_Message_Mask */