2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2004 Alexander Barton <alex@barton.de>
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.
17 static char UNUSED id[] = "$Id: irc.c,v 1.132 2008/01/15 22:28:14 fw Exp $";
26 #include "conn-func.h"
31 #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,
51 IRC_ERROR( CLIENT *Client, REQUEST *Req )
53 assert( Client != NULL );
54 assert( Req != NULL );
57 Log(LOG_NOTICE, "Got ERROR from \"%s\"!",
60 Log(LOG_NOTICE, "Got ERROR from \"%s\": \"%s\"!",
61 Client_Mask(Client), Req->argv[0]);
68 * Kill client on request.
69 * This function implements the IRC command "KILL" wich is used to selectively
70 * disconnect clients. It can be used by IRC operators and servers, for example
71 * to "solve" nick collisions after netsplits.
72 * Please note that this function is also called internally, without a real
73 * KILL command beeing received over the network! Client is Client_ThisServer()
76 IRC_KILL( CLIENT *Client, REQUEST *Req )
79 char reason[COMMAND_LEN], *msg;
80 CONN_ID my_conn, conn;
82 assert( Client != NULL );
83 assert( Req != NULL );
85 if(( Client_Type( Client ) != CLIENT_SERVER ) &&
86 ( ! Client_OperByMe( Client )))
88 /* The originator of the KILL is neither an IRC operator of
89 * this server nor a server. */
90 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
96 /* This command requires exactly 2 parameters! */
97 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
98 Client_ID( Client ), Req->command );
101 if( Req->prefix ) prefix = Client_Search( Req->prefix );
102 else prefix = Client;
105 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
107 prefix = Client_ThisServer( );
110 if( Client != Client_ThisServer( ))
112 /* This is a "real" KILL received from the network. */
113 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
114 Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
117 /* Build reason string */
118 if( Client_Type( Client ) == CLIENT_USER )
120 /* Prefix the "reason" if the originator is a regular user,
121 * so users can't spoof KILLs of servers. */
122 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
123 Client_ID( Client ), Req->argv[1] );
126 strlcpy( reason, Req->argv[1], sizeof( reason ));
128 /* Inform other servers */
129 IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
130 Req->argv[0], reason );
132 /* Save ID of this connection */
133 my_conn = Client_Conn( Client );
135 /* Do we host such a client? */
136 c = Client_Search( Req->argv[0] );
139 if(( Client_Type( c ) != CLIENT_USER ) &&
140 ( Client_Type( c ) != CLIENT_GOTNICK ))
142 /* Target of this KILL is not a regular user, this is
143 * invalid! So we ignore this case if we received a
144 * regular KILL from the network and try to kill the
145 * client/connection anyway (but log an error!) if the
146 * origin is the local server. */
148 if( Client != Client_ThisServer( ))
150 /* Invalid KILL received from remote */
151 if( Client_Type( c ) == CLIENT_SERVER )
152 msg = ERR_CANTKILLSERVER_MSG;
154 msg = ERR_NOPRIVILEGES_MSG;
155 return IRC_WriteStrClient( Client, msg,
156 Client_ID( Client ));
159 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
160 Client_Type( c ), Req->argv[0] );
163 /* Kill client NOW! */
164 conn = Client_Conn( c );
165 Client_Destroy( c, NULL, reason, false );
167 Conn_Close( conn, NULL, reason, true );
170 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
172 /* Are we still connected or were we killed, too? */
173 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
181 * Handler for the IRC command NOTICE.
184 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
186 return Send_Message(Client, Req, CLIENT_USER, false);
191 * Handler for the IRC command PRIVMSG.
194 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
196 return Send_Message(Client, Req, CLIENT_USER, true);
201 * Handler for the IRC command SQUERY.
204 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
206 return Send_Message(Client, Req, CLIENT_SERVICE, true);
211 IRC_TRACE( CLIENT *Client, REQUEST *Req )
213 CLIENT *from, *target, *c;
215 char user[CLIENT_USER_LEN];
217 assert( Client != NULL );
218 assert( Req != NULL );
220 /* Bad number of arguments? */
221 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
224 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
226 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
229 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
230 else target = Client_ThisServer( );
232 /* Forward command to other server? */
233 if( target != Client_ThisServer( ))
235 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
237 /* Send RPL_TRACELINK back to initiator */
238 idx = Client_Conn( Client ); assert( idx > NONE );
239 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
240 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;
242 /* Forward command */
243 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
247 /* Infos about all connected servers */
251 if( Client_Conn( c ) > NONE )
254 if( Client_Type( c ) == CLIENT_SERVER )
257 strlcpy( user, Client_User( c ), sizeof( user ));
258 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
259 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;
261 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
264 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
267 c = Client_Next( c );
270 IRC_SetPenalty( Client, 3 );
271 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
276 IRC_HELP( CLIENT *Client, REQUEST *Req )
280 assert( Client != NULL );
281 assert( Req != NULL );
283 /* Bad number of arguments? */
284 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
286 cmd = Parse_GetCommandStruct( );
289 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
293 IRC_SetPenalty( Client, 2 );
299 Option_String( CONN_ID Idx )
301 static char option_txt[8];
304 options = Conn_Options(Idx);
306 strcpy(option_txt, "F"); /* No idea what this means, but the
307 * original ircd sends it ... */
309 if(options & CONN_ZIP) /* zlib compression supported. */
310 strcat(option_txt, "z");
314 } /* Option_String */
318 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
322 char *currentTarget = Req->argv[0];
323 char *lastCurrentTarget = NULL;
325 assert(Client != NULL);
328 if (Req->argc == 0) {
331 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
332 Client_ID(Client), Req->command);
334 if (Req->argc == 1) {
337 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
343 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
344 Client_ID(Client), Req->command);
347 if (Client_Type(Client) == CLIENT_SERVER)
348 from = Client_Search(Req->prefix);
352 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
353 Client_ID(Client), Req->prefix);
355 /* handle msgtarget = msgto *("," msgto) */
356 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
357 ngt_UpperStr(Req->command);
359 while (currentTarget) {
360 /* Check for and handle valid <msgto> of form:
362 * msgto = channel / ( user [ "%" host ] "@" servername )
363 * msgto =/ ( user "%" host ) / targetmask
364 * msgto =/ nickname / ( nickname "!" user "@" host )
366 if (strchr(currentTarget, '!') == NULL)
368 cl = Client_Search(currentTarget);
373 /* If currentTarget isn't a nickname check for:
374 * user ["%" host] "@" servername
376 * nickname "!" user "@" host
378 char target[COMMAND_LEN];
382 char * server = NULL;
384 strlcpy(target, currentTarget, COMMAND_LEN);
385 server = strchr(target, '@');
390 host = strchr(target, '%');
395 user = strchr(target, '!');
397 /* msgto form: nick!user@host */
401 host = server; /* not "@server" but "@host" */
406 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
407 if (Client_Type(cl) != CLIENT_USER &&
408 Client_Type(cl) != CLIENT_SERVICE)
410 if (nick != NULL && host != NULL) {
411 if (strcmp(nick, Client_ID(cl)) == 0 &&
412 strcmp(user, Client_User(cl)) == 0 &&
413 strcasecmp(host, Client_Hostname(cl)) == 0)
418 if (strcasecmp(user, Client_User(cl)) != 0)
420 if (host != NULL && strcasecmp(host,
421 Client_Hostname(cl)) != 0)
423 if (server != NULL && strcasecmp(server,
424 Client_ID(Client_Introducer(cl))) != 0)
431 /* Target is a user, enforce type */
433 if (Client_Type(cl) != ForceType &&
434 !(ForceType == CLIENT_USER &&
435 (Client_Type(cl) == CLIENT_USER ||
436 Client_Type(cl) == CLIENT_SERVICE))) {
438 if (Client_Type(cl) != ForceType) {
442 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
448 if (ForceType == CLIENT_SERVICE &&
449 (Conn_Options(Client_Conn(Client_NextHop(cl)))
451 /* SQUERY command but RFC 1459 link: convert
452 * request to PRIVMSG command */
453 Req->command = "PRIVMSG";
457 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
458 && strchr(Client_Modes(cl), 'a')) {
460 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
466 if (Client_Conn(from) > NONE) {
467 Conn_UpdateIdle(Client_Conn(from));
469 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
470 Req->command, Client_ID(cl),
473 } else if (ForceType != CLIENT_SERVICE
474 && (chan = Channel_Search(currentTarget))) {
475 if (!Channel_Write(chan, from, Client, Req->command,
476 SendErrors, Req->argv[1]))
478 } else if (ForceType != CLIENT_SERVICE
479 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
480 && strchr("$#", currentTarget[0])
481 && strchr(currentTarget, '.')) {
483 if (!Send_Message_Mask(from, Req->command, currentTarget,
484 Req->argv[1], SendErrors))
489 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
490 Client_ID(from), currentTarget))
494 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
502 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
503 char * message, bool SendErrors)
507 char *mask = targetMask + 1;
508 const char *check_wildcards;
512 if (strchr(Client_Modes(from), 'o') == NULL) {
515 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
520 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
521 * dot (".") and no wildcards ("*", "?") following the last one.
523 check_wildcards = strrchr(targetMask, '.');
524 assert(check_wildcards != NULL);
525 if (check_wildcards &&
526 check_wildcards[strcspn(check_wildcards, "*?")])
530 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
533 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
534 if (targetMask[0] == '#') {
535 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
536 if (Client_Type(cl) != CLIENT_USER)
538 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
540 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
541 command, Client_ID(cl), message))
545 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
546 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
547 if (Client_Type(cl) != CLIENT_USER)
549 client_match = MatchCaseInsensitive(mask,
550 Client_ID(Client_Introducer(cl)));
552 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
553 command, Client_ID(cl), message))
558 } /* Send_Message_Mask */