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.131 2006/07/23 14:55:40 alex Exp $";
26 #include "conn-func.h"
31 #include "irc-write.h"
40 static char *Option_String PARAMS(( CONN_ID Idx ));
44 IRC_ERROR( CLIENT *Client, REQUEST *Req )
46 assert( Client != NULL );
47 assert( Req != NULL );
49 if( Req->argc < 1 ) Log( LOG_NOTICE, "Got ERROR from \"%s\"!", Client_Mask( Client ));
50 else Log( LOG_NOTICE, "Got ERROR from \"%s\": %s!", Client_Mask( Client ), Req->argv[0] );
57 * Kill client on request.
58 * This function implements the IRC command "KILL" wich is used to selectively
59 * disconnect clients. It can be used by IRC operators and servers, for example
60 * to "solve" nick collisions after netsplits.
61 * Please note that this function is also called internally, without a real
62 * KILL command beeing received over the network! Client is Client_ThisServer()
65 IRC_KILL( CLIENT *Client, REQUEST *Req )
68 char reason[COMMAND_LEN], *msg;
69 CONN_ID my_conn, conn;
71 assert( Client != NULL );
72 assert( Req != NULL );
74 if(( Client_Type( Client ) != CLIENT_SERVER ) &&
75 ( ! Client_OperByMe( Client )))
77 /* The originator of the KILL is neither an IRC operator of
78 * this server nor a server. */
79 return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG,
85 /* This command requires exactly 2 parameters! */
86 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
87 Client_ID( Client ), Req->command );
90 if( Req->prefix ) prefix = Client_Search( Req->prefix );
94 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!",
96 prefix = Client_ThisServer( );
99 if( Client != Client_ThisServer( ))
101 /* This is a "real" KILL received from the network. */
102 Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s",
103 Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
106 /* Build reason string */
107 if( Client_Type( Client ) == CLIENT_USER )
109 /* Prefix the "reason" if the originator is a regular user,
110 * so users can't spoof KILLs of servers. */
111 snprintf( reason, sizeof( reason ), "KILLed by %s: %s",
112 Client_ID( Client ), Req->argv[1] );
115 strlcpy( reason, Req->argv[1], sizeof( reason ));
117 /* Inform other servers */
118 IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s",
119 Req->argv[0], reason );
121 /* Save ID of this connection */
122 my_conn = Client_Conn( Client );
124 /* Do we host such a client? */
125 c = Client_Search( Req->argv[0] );
128 if(( Client_Type( c ) != CLIENT_USER ) &&
129 ( Client_Type( c ) != CLIENT_GOTNICK ))
131 /* Target of this KILL is not a regular user, this is
132 * invalid! So we ignore this case if we received a
133 * regular KILL from the network and try to kill the
134 * client/connection anyway (but log an error!) if the
135 * origin is the local server. */
137 if( Client != Client_ThisServer( ))
139 /* Invalid KILL received from remote */
140 if( Client_Type( c ) == CLIENT_SERVER )
141 msg = ERR_CANTKILLSERVER_MSG;
143 msg = ERR_NOPRIVILEGES_MSG;
144 return IRC_WriteStrClient( Client, msg,
145 Client_ID( Client ));
148 Log( LOG_ERR, "Got KILL for invalid client type: %d, \"%s\"!",
149 Client_Type( c ), Req->argv[0] );
152 /* Kill client NOW! */
153 conn = Client_Conn( c );
154 Client_Destroy( c, NULL, reason, false );
156 Conn_Close( conn, NULL, reason, true );
159 Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
161 /* Are we still connected or were we killed, too? */
162 if(( my_conn > NONE ) && ( Conn_GetClient( my_conn )))
170 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
174 assert( Client != NULL );
175 assert( Req != NULL );
177 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
179 /* Falsche Anzahl Parameter? */
180 if( Req->argc != 2 ) return CONNECTED;
182 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
184 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
186 to = Client_Search( Req->argv[0] );
187 if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
189 /* Okay, Ziel ist ein User */
190 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
192 else return CONNECTED;
197 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
202 assert( Client != NULL );
203 assert( Req != NULL );
205 /* Falsche Anzahl Parameter? */
206 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
207 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
208 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
210 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
212 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
214 cl = Client_Search( Req->argv[0] );
217 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
218 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
220 /* Okay, Ziel ist ein User */
221 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
223 /* Ziel-User ist AWAY: Meldung verschicken */
224 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
228 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
229 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
232 chan = Channel_Search( Req->argv[0] );
233 if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
235 return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
240 IRC_TRACE( CLIENT *Client, REQUEST *Req )
242 CLIENT *from, *target, *c;
244 char user[CLIENT_USER_LEN];
246 assert( Client != NULL );
247 assert( Req != NULL );
249 /* Bad number of arguments? */
250 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
253 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
255 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
258 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
259 else target = Client_ThisServer( );
261 /* Forward command to other server? */
262 if( target != Client_ThisServer( ))
264 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
266 /* Send RPL_TRACELINK back to initiator */
267 idx = Client_Conn( Client ); assert( idx > NONE );
268 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
269 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;
271 /* Forward command */
272 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
276 /* Infos about all connected servers */
280 if( Client_Conn( c ) > NONE )
283 if( Client_Type( c ) == CLIENT_SERVER )
286 strlcpy( user, Client_User( c ), sizeof( user ));
287 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
288 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;
290 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
293 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
296 c = Client_Next( c );
299 IRC_SetPenalty( Client, 3 );
300 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
305 IRC_HELP( CLIENT *Client, REQUEST *Req )
309 assert( Client != NULL );
310 assert( Req != NULL );
312 /* Bad number of arguments? */
313 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
315 cmd = Parse_GetCommandStruct( );
318 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
322 IRC_SetPenalty( Client, 2 );
328 Option_String( CONN_ID Idx )
330 static char option_txt[8];
333 options = Conn_Options(Idx);
335 strcpy(option_txt, "F"); /* No idea what this means, but the
336 * original ircd sends it ... */
338 if(options & CONN_ZIP) /* zlib compression supported. */
339 strcat(option_txt, "z");
343 } /* Option_String */