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.126 2005/04/18 15:44:39 alex Exp $";
28 #include "conn-func.h"
32 #include "irc-write.h"
41 LOCAL char *Option_String PARAMS(( CONN_ID Idx ));
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( Client_NextHop( 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 ) && ( Client_GetFromConn( my_conn )))
171 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
175 assert( Client != NULL );
176 assert( Req != NULL );
178 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
180 /* Falsche Anzahl Parameter? */
181 if( Req->argc != 2 ) return CONNECTED;
183 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
185 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
187 to = Client_Search( Req->argv[0] );
188 if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
190 /* Okay, Ziel ist ein User */
191 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
193 else return CONNECTED;
198 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
203 assert( Client != NULL );
204 assert( Req != NULL );
206 /* Falsche Anzahl Parameter? */
207 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
208 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
209 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
211 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
213 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
215 cl = Client_Search( Req->argv[0] );
218 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
219 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
221 /* Okay, Ziel ist ein User */
222 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
224 /* Ziel-User ist AWAY: Meldung verschicken */
225 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
229 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
230 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
233 chan = Channel_Search( Req->argv[0] );
234 if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
236 return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
241 IRC_TRACE( CLIENT *Client, REQUEST *Req )
243 CLIENT *from, *target, *c;
245 char user[CLIENT_USER_LEN];
247 assert( Client != NULL );
248 assert( Req != NULL );
250 /* Bad number of arguments? */
251 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
254 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
256 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
259 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
260 else target = Client_ThisServer( );
262 /* Forward command to other server? */
263 if( target != Client_ThisServer( ))
265 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
267 /* Send RPL_TRACELINK back to initiator */
268 idx = Client_Conn( Client ); assert( idx > NONE );
269 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
270 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;
272 /* Forward command */
273 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
277 /* Infos about all connected servers */
281 if( Client_Conn( c ) > NONE )
284 if( Client_Type( c ) == CLIENT_SERVER )
287 strlcpy( user, Client_User( c ), sizeof( user ));
288 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
289 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;
291 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
294 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
297 c = Client_Next( c );
300 IRC_SetPenalty( Client, 3 );
301 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
306 IRC_HELP( CLIENT *Client, REQUEST *Req )
310 assert( Client != NULL );
311 assert( Req != NULL );
313 /* Bad number of arguments? */
314 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
316 cmd = Parse_GetCommandStruct( );
319 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
323 IRC_SetPenalty( Client, 2 );
329 Option_String( CONN_ID Idx )
331 static char option_txt[8];
334 options = Conn_Options( Idx );
336 strcpy( option_txt, "F" ); /* No idea what this means but the original ircd sends it ... */
338 if( options & CONN_ZIP ) strcat( option_txt, "z" );
342 } /* Option_String */