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 being 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 the client NOW:
164 * - Close the local connection (if there is one),
165 * - Destroy the CLIENT structure for remote clients.
166 * Note: Conn_Close() removes the CLIENT structure as well. */
167 conn = Client_Conn( c );
169 Conn_Close(conn, NULL, reason, true);
171 Client_Destroy(c, NULL, reason, false);
174 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
176 /* Are we still connected or were we killed, too? */
177 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
185 * Handler for the IRC command NOTICE.
188 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
190 return Send_Message(Client, Req, CLIENT_USER, false);
195 * Handler for the IRC command PRIVMSG.
198 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
200 return Send_Message(Client, Req, CLIENT_USER, true);
205 * Handler for the IRC command SQUERY.
208 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
210 return Send_Message(Client, Req, CLIENT_SERVICE, true);
215 IRC_TRACE( CLIENT *Client, REQUEST *Req )
217 CLIENT *from, *target, *c;
219 char user[CLIENT_USER_LEN];
221 assert( Client != NULL );
222 assert( Req != NULL );
224 /* Bad number of arguments? */
225 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
228 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
230 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
233 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
234 else target = Client_ThisServer( );
236 /* Forward command to other server? */
237 if( target != Client_ThisServer( ))
239 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
241 /* Send RPL_TRACELINK back to initiator */
242 idx = Client_Conn( Client ); assert( idx > NONE );
243 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
244 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;
246 /* Forward command */
247 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
251 /* Infos about all connected servers */
255 if( Client_Conn( c ) > NONE )
258 if( Client_Type( c ) == CLIENT_SERVER )
261 strlcpy( user, Client_User( c ), sizeof( user ));
262 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
263 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;
265 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
268 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
271 c = Client_Next( c );
274 IRC_SetPenalty( Client, 3 );
275 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
280 IRC_HELP( CLIENT *Client, REQUEST *Req )
284 assert( Client != NULL );
285 assert( Req != NULL );
287 /* Bad number of arguments? */
288 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
290 cmd = Parse_GetCommandStruct( );
293 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
297 IRC_SetPenalty( Client, 2 );
303 Option_String( CONN_ID Idx )
305 static char option_txt[8];
308 options = Conn_Options(Idx);
310 strcpy(option_txt, "F"); /* No idea what this means, but the
311 * original ircd sends it ... */
313 if(options & CONN_ZIP) /* zlib compression supported. */
314 strcat(option_txt, "z");
318 } /* Option_String */
322 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
326 char *currentTarget = Req->argv[0];
327 char *lastCurrentTarget = NULL;
329 assert(Client != NULL);
332 if (Req->argc == 0) {
335 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
336 Client_ID(Client), Req->command);
338 if (Req->argc == 1) {
341 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
347 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
348 Client_ID(Client), Req->command);
351 if (Client_Type(Client) == CLIENT_SERVER)
352 from = Client_Search(Req->prefix);
356 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
357 Client_ID(Client), Req->prefix);
359 /* handle msgtarget = msgto *("," msgto) */
360 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
361 ngt_UpperStr(Req->command);
363 while (currentTarget) {
364 /* Check for and handle valid <msgto> of form:
366 * msgto = channel / ( user [ "%" host ] "@" servername )
367 * msgto =/ ( user "%" host ) / targetmask
368 * msgto =/ nickname / ( nickname "!" user "@" host )
370 if (strchr(currentTarget, '!') == NULL)
372 cl = Client_Search(currentTarget);
377 /* If currentTarget isn't a nickname check for:
378 * user ["%" host] "@" servername
380 * nickname "!" user "@" host
382 char target[COMMAND_LEN];
386 char * server = NULL;
388 strlcpy(target, currentTarget, COMMAND_LEN);
389 server = strchr(target, '@');
394 host = strchr(target, '%');
399 user = strchr(target, '!');
401 /* msgto form: nick!user@host */
405 host = server; /* not "@server" but "@host" */
410 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
411 if (Client_Type(cl) != CLIENT_USER &&
412 Client_Type(cl) != CLIENT_SERVICE)
414 if (nick != NULL && host != NULL) {
415 if (strcmp(nick, Client_ID(cl)) == 0 &&
416 strcmp(user, Client_User(cl)) == 0 &&
417 strcasecmp(host, Client_Hostname(cl)) == 0)
422 if (strcasecmp(user, Client_User(cl)) != 0)
424 if (host != NULL && strcasecmp(host,
425 Client_Hostname(cl)) != 0)
427 if (server != NULL && strcasecmp(server,
428 Client_ID(Client_Introducer(cl))) != 0)
435 /* Target is a user, enforce type */
437 if (Client_Type(cl) != ForceType &&
438 !(ForceType == CLIENT_USER &&
439 (Client_Type(cl) == CLIENT_USER ||
440 Client_Type(cl) == CLIENT_SERVICE))) {
442 if (Client_Type(cl) != ForceType) {
446 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
452 if (ForceType == CLIENT_SERVICE &&
453 (Conn_Options(Client_Conn(Client_NextHop(cl)))
455 /* SQUERY command but RFC 1459 link: convert
456 * request to PRIVMSG command */
457 Req->command = "PRIVMSG";
461 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
462 && strchr(Client_Modes(cl), 'a')) {
464 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
470 if (Client_Conn(from) > NONE) {
471 Conn_UpdateIdle(Client_Conn(from));
473 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
474 Req->command, Client_ID(cl),
477 } else if (ForceType != CLIENT_SERVICE
478 && (chan = Channel_Search(currentTarget))) {
479 if (!Channel_Write(chan, from, Client, Req->command,
480 SendErrors, Req->argv[1]))
482 } else if (ForceType != CLIENT_SERVICE
483 /* $#: server/target mask, RFC 2812, sec. 3.3.1 */
484 && strchr("$#", currentTarget[0])
485 && strchr(currentTarget, '.')) {
487 if (!Send_Message_Mask(from, Req->command, currentTarget,
488 Req->argv[1], SendErrors))
493 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
494 Client_ID(from), currentTarget))
498 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
506 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
507 char * message, bool SendErrors)
511 char *mask = targetMask + 1;
512 const char *check_wildcards;
516 if (strchr(Client_Modes(from), 'o') == NULL) {
519 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
524 * RFC 2812, sec. 3.3.1 requires that targetMask have at least one
525 * dot (".") and no wildcards ("*", "?") following the last one.
527 check_wildcards = strrchr(targetMask, '.');
528 assert(check_wildcards != NULL);
529 if (check_wildcards &&
530 check_wildcards[strcspn(check_wildcards, "*?")])
534 return IRC_WriteStrClient(from, ERR_WILDTOPLEVEL, targetMask);
537 /* #: hostmask, see RFC 2812, sec. 3.3.1 */
538 if (targetMask[0] == '#') {
539 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
540 if (Client_Type(cl) != CLIENT_USER)
542 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
544 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
545 command, Client_ID(cl), message))
549 assert(targetMask[0] == '$'); /* $: server mask, see RFC 2812, sec. 3.3.1 */
550 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
551 if (Client_Type(cl) != CLIENT_USER)
553 client_match = MatchCaseInsensitive(mask,
554 Client_ID(Client_Introducer(cl)));
556 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
557 command, Client_ID(cl), message))
562 } /* Send_Message_Mask */