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 ));
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 )
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] );
195 chan = Channel_Search(Req->argv[0]);
197 return Channel_Notice(chan, from, Client, Req->argv[1]);
205 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
210 assert( Client != NULL );
211 assert( Req != NULL );
213 /* Falsche Anzahl Parameter? */
214 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
215 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
216 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
218 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
220 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
222 cl = Client_Search( Req->argv[0] );
225 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
226 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
228 /* Okay, Ziel ist ein User */
229 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
231 /* Ziel-User ist AWAY: Meldung verschicken */
232 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
236 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
237 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
240 chan = Channel_Search( Req->argv[0] );
241 if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
243 return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
248 IRC_TRACE( CLIENT *Client, REQUEST *Req )
250 CLIENT *from, *target, *c;
252 char user[CLIENT_USER_LEN];
254 assert( Client != NULL );
255 assert( Req != NULL );
257 /* Bad number of arguments? */
258 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
261 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
263 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
266 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
267 else target = Client_ThisServer( );
269 /* Forward command to other server? */
270 if( target != Client_ThisServer( ))
272 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
274 /* Send RPL_TRACELINK back to initiator */
275 idx = Client_Conn( Client ); assert( idx > NONE );
276 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
277 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;
279 /* Forward command */
280 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
284 /* Infos about all connected servers */
288 if( Client_Conn( c ) > NONE )
291 if( Client_Type( c ) == CLIENT_SERVER )
294 strlcpy( user, Client_User( c ), sizeof( user ));
295 if( user[0] == '~' ) strlcpy( user, "unknown", sizeof( user ));
296 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;
298 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
301 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
304 c = Client_Next( c );
307 IRC_SetPenalty( Client, 3 );
308 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE_NAME, PACKAGE_VERSION, NGIRCd_DebugLevel );
313 IRC_HELP( CLIENT *Client, REQUEST *Req )
317 assert( Client != NULL );
318 assert( Req != NULL );
320 /* Bad number of arguments? */
321 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
323 cmd = Parse_GetCommandStruct( );
326 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
330 IRC_SetPenalty( Client, 2 );
336 Option_String( CONN_ID Idx )
338 static char option_txt[8];
341 options = Conn_Options(Idx);
343 strcpy(option_txt, "F"); /* No idea what this means, but the
344 * original ircd sends it ... */
346 if(options & CONN_ZIP) /* zlib compression supported. */
347 strcat(option_txt, "z");
351 } /* Option_String */