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 );
56 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
57 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
64 * Kill client on request.
65 * This function implements the IRC command "KILL" wich is used to selectively
66 * disconnect clients. It can be used by IRC operators and servers, for example
67 * to "solve" nick collisions after netsplits.
68 * Please note that this function is also called internally, without a real
69 * KILL command beeing received over the network! Client is Client_ThisServer()
72 IRC_KILL( CLIENT *Client, REQUEST *Req )
75 char reason[COMMAND_LEN], *msg;
76 CONN_ID my_conn, conn;
78 assert( Client != NULL );
79 assert( Req != NULL );
81 if(( Client_Type( Client ) != CLIENT_SERVER ) &&
82 ( ! Client_OperByMe( Client )))
84 /* The originator of the KILL is neither an IRC operator of
85 * this server nor a server. */
86 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
92 /* This command requires exactly 2 parameters! */
93 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
94 Client_ID( Client ), Req->command );
97 if( Req->prefix ) prefix = Client_Search( Req->prefix );
101 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
103 prefix = Client_ThisServer( );
106 if( Client != Client_ThisServer( ))
108 /* This is a "real" KILL received from the network. */
109 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
110 Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
113 /* Build reason string */
114 if( Client_Type( Client ) == CLIENT_USER )
116 /* Prefix the "reason" if the originator is a regular user,
117 * so users can't spoof KILLs of servers. */
118 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
119 Client_ID( Client ), Req->argv[1] );
122 strlcpy( reason, Req->argv[1], sizeof( reason ));
124 /* Inform other servers */
125 IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
126 Req->argv[0], reason );
128 /* Save ID of this connection */
129 my_conn = Client_Conn( Client );
131 /* Do we host such a client? */
132 c = Client_Search( Req->argv[0] );
135 if(( Client_Type( c ) != CLIENT_USER ) &&
136 ( Client_Type( c ) != CLIENT_GOTNICK ))
138 /* Target of this KILL is not a regular user, this is
139 * invalid! So we ignore this case if we received a
140 * regular KILL from the network and try to kill the
141 * client/connection anyway (but log an error!) if the
142 * origin is the local server. */
144 if( Client != Client_ThisServer( ))
146 /* Invalid KILL received from remote */
147 if( Client_Type( c ) == CLIENT_SERVER )
148 msg = ERR_CANTKILLSERVER_MSG;
150 msg = ERR_NOPRIVILEGES_MSG;
151 return IRC_WriteStrClient( Client, msg,
152 Client_ID( Client ));
155 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
156 Client_Type( c ), Req->argv[0] );
159 /* Kill client NOW! */
160 conn = Client_Conn( c );
161 Client_Destroy( c, NULL, reason, false );
163 Conn_Close( conn, NULL, reason, true );
166 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
168 /* Are we still connected or were we killed, too? */
169 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
177 * Handler for the IRC command NOTICE.
180 IRC_NOTICE(CLIENT *Client, REQUEST *Req)
182 return Send_Message(Client, Req, CLIENT_USER, false);
187 * Handler for the IRC command PRIVMSG.
190 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
192 return Send_Message(Client, Req, CLIENT_USER, true);
197 * Handler for the IRC command SQUERY.
200 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
202 return Send_Message(Client, Req, CLIENT_SERVICE, true);
207 IRC_TRACE( CLIENT *Client, REQUEST *Req )
209 CLIENT *from, *target, *c;
211 char user[CLIENT_USER_LEN];
213 assert( Client != NULL );
214 assert( Req != NULL );
216 /* Bad number of arguments? */
217 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
220 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
222 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
225 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
226 else target = Client_ThisServer( );
228 /* Forward command to other server? */
229 if( target != Client_ThisServer( ))
231 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
233 /* Send RPL_TRACELINK back to initiator */
234 idx = Client_Conn( Client ); assert( idx > NONE );
235 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
236 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;
238 /* Forward command */
239 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
243 /* Infos about all connected servers */
247 if( Client_Conn( c ) > NONE )
250 if( Client_Type( c ) == CLIENT_SERVER )
253 strlcpy( user, Client_User( c ), sizeof( user ));
254 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
255 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;
257 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
260 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
263 c = Client_Next( c );
266 IRC_SetPenalty( Client, 3 );
267 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
272 IRC_HELP( CLIENT *Client, REQUEST *Req )
276 assert( Client != NULL );
277 assert( Req != NULL );
279 /* Bad number of arguments? */
280 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
282 cmd = Parse_GetCommandStruct( );
285 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
289 IRC_SetPenalty( Client, 2 );
295 Option_String( CONN_ID Idx )
297 static char option_txt[8];
300 options = Conn_Options(Idx);
302 strcpy(option_txt, "F"); /* No idea what this means, but the
303 * original ircd sends it ... */
305 if(options & CONN_ZIP) /* zlib compression supported. */
306 strcat(option_txt, "z");
310 } /* Option_String */
314 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType, bool SendErrors)
318 char *currentTarget = Req->argv[0];
319 char *lastCurrentTarget = NULL;
321 assert(Client != NULL);
324 if (Req->argc == 0) {
327 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
328 Client_ID(Client), Req->command);
330 if (Req->argc == 1) {
333 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
339 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
340 Client_ID(Client), Req->command);
343 if (Client_Type(Client) == CLIENT_SERVER)
344 from = Client_Search(Req->prefix);
348 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
349 Client_ID(Client), Req->prefix);
351 /* handle msgtarget = msgto *("," msgto) */
352 currentTarget = strtok_r(currentTarget, ",", &lastCurrentTarget);
353 ngt_UpperStr(Req->command);
355 while (currentTarget) {
356 /* Check for and handle valid <msgto> of form:
358 * msgto = channel / ( user [ "%" host ] "@" servername )
359 * msgto =/ ( user "%" host ) / targetmask
360 * msgto =/ nickname / ( nickname "!" user "@" host )
362 if (strchr(currentTarget, '!') == NULL)
364 cl = Client_Search(currentTarget);
369 /* If currentTarget isn't a nickname check for:
370 * user ["%" host] "@" servername
372 * nickname "!" user "@" host
374 char target[COMMAND_LEN];
378 char * server = NULL;
380 strlcpy(target, currentTarget, COMMAND_LEN);
381 server = strchr(target, '@');
386 host = strchr(target, '%');
391 user = strchr(target, '!');
393 /* msgto form: nick!user@host */
397 host = server; /* not "@server" but "@host" */
402 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
403 if (Client_Type(cl) != CLIENT_USER &&
404 Client_Type(cl) != CLIENT_SERVICE)
406 if (nick != NULL && host != NULL) {
407 if (strcmp(nick, Client_ID(cl)) == 0 &&
408 strcmp(user, Client_User(cl)) == 0 &&
409 strcasecmp(host, Client_Hostname(cl)) == 0)
414 if (strcasecmp(user, Client_User(cl)) != 0)
416 if (host != NULL && strcasecmp(host,
417 Client_Hostname(cl)) != 0)
419 if (server != NULL && strcasecmp(server,
420 Client_ID(Client_Introducer(cl))) != 0)
427 /* Target is a user, enforce type */
429 if (Client_Type(cl) != ForceType &&
430 !(ForceType == CLIENT_USER &&
431 (Client_Type(cl) == CLIENT_USER ||
432 Client_Type(cl) == CLIENT_SERVICE))) {
434 if (Client_Type(cl) != ForceType) {
438 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
444 if (ForceType == CLIENT_SERVICE &&
445 (Conn_Options(Client_Conn(Client_NextHop(cl)))
447 /* SQUERY command but RFC 1459 link: convert
448 * request to PRIVMSG command */
449 Req->command = "PRIVMSG";
453 if (SendErrors && (Client_Type(Client) != CLIENT_SERVER)
454 && strchr(Client_Modes(cl), 'a')) {
456 if (!IRC_WriteStrClient(from, RPL_AWAY_MSG,
462 if (Client_Conn(from) > NONE) {
463 Conn_UpdateIdle(Client_Conn(from));
465 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
466 Req->command, Client_ID(cl),
469 } else if (ForceType != CLIENT_SERVICE
470 && strchr("$#", currentTarget[0])
471 && strchr(currentTarget, '.')) {
473 if (!Send_Message_Mask(from, Req->command, currentTarget,
474 Req->argv[1], SendErrors))
476 } else if (ForceType != CLIENT_SERVICE
477 && (chan = Channel_Search(currentTarget))) {
479 if (!Channel_Write(chan, from, Client, Req->command,
480 SendErrors, Req->argv[1]))
485 if (!IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
486 Client_ID(from), currentTarget))
490 currentTarget = strtok_r(NULL, ",", &lastCurrentTarget);
498 Send_Message_Mask(CLIENT * from, char * command, char * targetMask,
499 char * message, bool SendErrors)
503 char *mask = targetMask + 1;
507 if (strchr(Client_Modes(from), 'o') == NULL) {
510 return IRC_WriteStrClient(from, ERR_NOPRIVILEGES_MSG,
514 if (targetMask[0] == '#') {
515 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
516 if (Client_Type(cl) != CLIENT_USER)
518 client_match = MatchCaseInsensitive(mask, Client_Hostname(cl));
520 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
521 command, Client_ID(cl), message))
525 for (cl = Client_First(); cl != NULL; cl = Client_Next(cl)) {
526 if (Client_Type(cl) != CLIENT_USER)
528 client_match = MatchCaseInsensitive(mask,
529 Client_ID(Client_Introducer(cl)));
531 if (!IRC_WriteStrClientPrefix(cl, from, "%s %s :%s",
532 command, Client_ID(cl), message))
537 } /* Send_Message_Mask */