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"
40 static char *Option_String PARAMS((CONN_ID Idx));
41 static bool Send_Message PARAMS((CLIENT *Client, REQUEST *Req, int ForceType));
45 IRC_ERROR( CLIENT *Client, REQUEST *Req )
47 assert( Client != NULL );
48 assert( Req != NULL );
50 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
51 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
58 * Kill client on request.
59 * This function implements the IRC command "KILL" wich is used to selectively
60 * disconnect clients. It can be used by IRC operators and servers, for example
61 * to "solve" nick collisions after netsplits.
62 * Please note that this function is also called internally, without a real
63 * KILL command beeing received over the network! Client is Client_ThisServer()
66 IRC_KILL( CLIENT *Client, REQUEST *Req )
69 char reason[COMMAND_LEN], *msg;
70 CONN_ID my_conn, conn;
72 assert( Client != NULL );
73 assert( Req != NULL );
75 if(( Client_Type( Client ) != CLIENT_SERVER ) &&
76 ( ! Client_OperByMe( Client )))
78 /* The originator of the KILL is neither an IRC operator of
79 * this server nor a server. */
80 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
86 /* This command requires exactly 2 parameters! */
87 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
88 Client_ID( Client ), Req->command );
91 if( Req->prefix ) prefix = Client_Search( Req->prefix );
95 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
97 prefix = Client_ThisServer( );
100 if( Client != Client_ThisServer( ))
102 /* This is a "real" KILL received from the network. */
103 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
104 Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
107 /* Build reason string */
108 if( Client_Type( Client ) == CLIENT_USER )
110 /* Prefix the "reason" if the originator is a regular user,
111 * so users can't spoof KILLs of servers. */
112 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
113 Client_ID( Client ), Req->argv[1] );
116 strlcpy( reason, Req->argv[1], sizeof( reason ));
118 /* Inform other servers */
119 IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
120 Req->argv[0], reason );
122 /* Save ID of this connection */
123 my_conn = Client_Conn( Client );
125 /* Do we host such a client? */
126 c = Client_Search( Req->argv[0] );
129 if(( Client_Type( c ) != CLIENT_USER ) &&
130 ( Client_Type( c ) != CLIENT_GOTNICK ))
132 /* Target of this KILL is not a regular user, this is
133 * invalid! So we ignore this case if we received a
134 * regular KILL from the network and try to kill the
135 * client/connection anyway (but log an error!) if the
136 * origin is the local server. */
138 if( Client != Client_ThisServer( ))
140 /* Invalid KILL received from remote */
141 if( Client_Type( c ) == CLIENT_SERVER )
142 msg = ERR_CANTKILLSERVER_MSG;
144 msg = ERR_NOPRIVILEGES_MSG;
145 return IRC_WriteStrClient( Client, msg,
146 Client_ID( Client ));
149 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
150 Client_Type( c ), Req->argv[0] );
153 /* Kill client NOW! */
154 conn = Client_Conn( c );
155 Client_Destroy( c, NULL, reason, false );
157 Conn_Close( conn, NULL, reason, true );
160 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
162 /* Are we still connected or were we killed, too? */
163 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
171 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
176 assert( Client != NULL );
177 assert( Req != NULL );
179 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
181 /* Falsche Anzahl Parameter? */
182 if( Req->argc != 2 ) return CONNECTED;
184 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
186 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
188 to = Client_Search( Req->argv[0] );
189 if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
191 /* Okay, Ziel ist ein User */
192 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
196 chan = Channel_Search(Req->argv[0]);
198 return Channel_Notice(chan, from, Client, Req->argv[1]);
206 * Handler for the IRC command PRIVMSG.
209 IRC_PRIVMSG(CLIENT *Client, REQUEST *Req)
211 return Send_Message(Client, Req, CLIENT_USER);
216 * Handler for the IRC command SQUERY.
219 IRC_SQUERY(CLIENT *Client, REQUEST *Req)
221 return Send_Message(Client, Req, CLIENT_SERVICE);
226 IRC_TRACE( CLIENT *Client, REQUEST *Req )
228 CLIENT *from, *target, *c;
230 char user[CLIENT_USER_LEN];
232 assert( Client != NULL );
233 assert( Req != NULL );
235 /* Bad number of arguments? */
236 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
239 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
241 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
244 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
245 else target = Client_ThisServer( );
247 /* Forward command to other server? */
248 if( target != Client_ThisServer( ))
250 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
252 /* Send RPL_TRACELINK back to initiator */
253 idx = Client_Conn( Client ); assert( idx > NONE );
254 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
255 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;
257 /* Forward command */
258 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
262 /* Infos about all connected servers */
266 if( Client_Conn( c ) > NONE )
269 if( Client_Type( c ) == CLIENT_SERVER )
272 strlcpy( user, Client_User( c ), sizeof( user ));
273 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
274 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;
276 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
279 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
282 c = Client_Next( c );
285 IRC_SetPenalty( Client, 3 );
286 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
291 IRC_HELP( CLIENT *Client, REQUEST *Req )
295 assert( Client != NULL );
296 assert( Req != NULL );
298 /* Bad number of arguments? */
299 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
301 cmd = Parse_GetCommandStruct( );
304 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
308 IRC_SetPenalty( Client, 2 );
314 Option_String( CONN_ID Idx )
316 static char option_txt[8];
319 options = Conn_Options(Idx);
321 strcpy(option_txt, "F"); /* No idea what this means, but the
322 * original ircd sends it ... */
324 if(options & CONN_ZIP) /* zlib compression supported. */
325 strcat(option_txt, "z");
329 } /* Option_String */
333 Send_Message(CLIENT * Client, REQUEST * Req, int ForceType)
338 assert(Client != NULL);
342 return IRC_WriteStrClient(Client, ERR_NORECIPIENT_MSG,
343 Client_ID(Client), Req->command);
345 return IRC_WriteStrClient(Client, ERR_NOTEXTTOSEND_MSG,
348 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
349 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 cl = Client_Search(Req->argv[0]);
361 /* Target is a user, enforce type */
362 if (Client_Type(cl) != ForceType)
363 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
367 if ((Client_Type(Client) != CLIENT_SERVER)
368 && (strchr(Client_Modes(cl), 'a'))) {
370 if (!IRC_WriteStrClient
371 (from, RPL_AWAY_MSG, Client_ID(from), Client_ID(cl),
376 if (Client_Conn(from) > NONE)
377 Conn_UpdateIdle(Client_Conn(from));
378 return IRC_WriteStrClientPrefix(cl, from, "PRIVMSG %s :%s",
379 Client_ID(cl), Req->argv[1]);
382 chan = Channel_Search(Req->argv[0]);
384 return Channel_Write(chan, from, Client, Req->argv[1]);
386 return IRC_WriteStrClient(from, ERR_NOSUCHNICK_MSG,
387 Client_ID(from), Req->argv[0]);