2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2003 by 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.119 2003/03/19 21:16:53 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 IRC_KILL( CLIENT *Client, REQUEST *Req )
61 CHAR reason[COMMAND_LEN];
62 CONN_ID my_conn, conn;
64 assert( Client != NULL );
65 assert( Req != NULL );
67 /* Is the user an IRC operator? */
68 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( ! Client_OperByMe( Client ))) return IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
70 /* Bad number of parameters? */
71 if(( Req->argc != 2 )) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
73 if( Req->prefix ) prefix = Client_Search( Req->prefix );
77 Log( LOG_WARNING, "Got KILL with invalid prefix: \"%s\"!", Req->prefix );
78 prefix = Client_ThisServer( );
81 if( Client != Client_ThisServer( )) Log( LOG_NOTICE|LOG_snotice, "Got KILL command from \"%s\" for \"%s\": %s", Client_Mask( prefix ), Req->argv[0], Req->argv[1] );
83 /* Build reason string */
84 if( Client_Type( Client ) == CLIENT_USER ) snprintf( reason, sizeof( reason ), "KILLed by %s: %s", Client_ID( Client ), Req->argv[1] );
85 else strlcpy( reason, Req->argv[1], sizeof( reason ));
87 /* Inform other servers */
88 IRC_WriteStrServersPrefix( Client, prefix, "KILL %s :%s", Req->argv[0], reason );
90 /* Save ID of this connection */
91 my_conn = Client_Conn( Client );
93 /* Do we host such a client? */
94 c = Client_Search( Req->argv[0] );
97 /* Yes, there is such a client -- but is it a valid user? */
98 if( Client_Type( c ) == CLIENT_SERVER ) IRC_WriteStrClient( Client, ERR_CANTKILLSERVER_MSG, Client_ID( Client ));
99 else if( Client_Type( c ) != CLIENT_USER )IRC_WriteStrClient( Client, ERR_NOPRIVILEGES_MSG, Client_ID( Client ));
103 conn = Client_Conn( c );
104 Client_Destroy( c, NULL, reason, FALSE );
105 if( conn != NONE ) Conn_Close( conn, NULL, reason, TRUE );
108 else Log( LOG_NOTICE, "Client with nick \"%s\" is unknown here.", Req->argv[0] );
110 /* Are we still connected or were we killed, too? */
111 if(( my_conn > NONE ) && ( Client_GetFromConn( my_conn ))) return CONNECTED;
112 else return DISCONNECTED;
117 IRC_NOTICE( CLIENT *Client, REQUEST *Req )
121 assert( Client != NULL );
122 assert( Req != NULL );
124 if(( Client_Type( Client ) != CLIENT_USER ) && ( Client_Type( Client ) != CLIENT_SERVER )) return CONNECTED;
126 /* Falsche Anzahl Parameter? */
127 if( Req->argc != 2 ) return CONNECTED;
129 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
131 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
133 to = Client_Search( Req->argv[0] );
134 if(( to ) && ( Client_Type( to ) == CLIENT_USER ))
136 /* Okay, Ziel ist ein User */
137 return IRC_WriteStrClientPrefix( to, from, "NOTICE %s :%s", Client_ID( to ), Req->argv[1] );
139 else return CONNECTED;
144 IRC_PRIVMSG( CLIENT *Client, REQUEST *Req )
149 assert( Client != NULL );
150 assert( Req != NULL );
152 /* Falsche Anzahl Parameter? */
153 if( Req->argc == 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
154 if( Req->argc == 1 ) return IRC_WriteStrClient( Client, ERR_NOTEXTTOSEND_MSG, Client_ID( Client ));
155 if( Req->argc > 2 ) return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
157 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
159 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
161 cl = Client_Search( Req->argv[0] );
164 /* Okay, Ziel ist ein Client. Aber ist es auch ein User? */
165 if( Client_Type( cl ) != CLIENT_USER ) return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
167 /* Okay, Ziel ist ein User */
168 if(( Client_Type( Client ) != CLIENT_SERVER ) && ( strchr( Client_Modes( cl ), 'a' )))
170 /* Ziel-User ist AWAY: Meldung verschicken */
171 if( ! IRC_WriteStrClient( from, RPL_AWAY_MSG, Client_ID( from ), Client_ID( cl ), Client_Away( cl ))) return DISCONNECTED;
175 if( Client_Conn( from ) > NONE ) Conn_UpdateIdle( Client_Conn( from ));
176 return IRC_WriteStrClientPrefix( cl, from, "PRIVMSG %s :%s", Client_ID( cl ), Req->argv[1] );
179 chan = Channel_Search( Req->argv[0] );
180 if( chan ) return Channel_Write( chan, from, Client, Req->argv[1] );
182 return IRC_WriteStrClient( from, ERR_NOSUCHNICK_MSG, Client_ID( from ), Req->argv[0] );
187 IRC_TRACE( CLIENT *Client, REQUEST *Req )
189 CLIENT *from, *target, *c;
192 assert( Client != NULL );
193 assert( Req != NULL );
195 /* Bad number of arguments? */
196 if( Req->argc > 1 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
199 if( Client_Type( Client ) == CLIENT_SERVER ) from = Client_Search( Req->prefix );
201 if( ! from ) return IRC_WriteStrClient( Client, ERR_NOSUCHNICK_MSG, Client_ID( Client ), Req->prefix );
204 if( Req->argc == 1 ) target = Client_Search( Req->argv[0] );
205 else target = Client_ThisServer( );
207 /* Forward command to other server? */
208 if( target != Client_ThisServer( ))
210 if(( ! target ) || ( Client_Type( target ) != CLIENT_SERVER )) return IRC_WriteStrClient( from, ERR_NOSUCHSERVER_MSG, Client_ID( from ), Req->argv[0] );
212 /* Send RPL_TRACELINK back to initiator */
213 idx = Client_Conn( Client ); assert( idx > NONE );
214 idx2 = Client_Conn( Client_NextHop( target )); assert( idx2 > NONE );
215 if( ! IRC_WriteStrClient( from, RPL_TRACELINK_MSG, Client_ID( from ), 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;
217 /* Forward command */
218 IRC_WriteStrClientPrefix( target, from, "TRACE %s", Req->argv[0] );
222 /* Infos about all connected servers */
226 if( Client_Conn( c ) > NONE )
229 if( Client_Type( c ) == CLIENT_SERVER )
232 if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Client_ID( c ), Client_Mask( c ), Option_String( Client_Conn( c )))) return DISCONNECTED;
234 if(( Client_Type( c ) == CLIENT_USER ) && ( strchr( Client_Modes( c ), 'o' )))
237 if( ! IRC_WriteStrClient( from, RPL_TRACEOPERATOR_MSG, Client_ID( from ), Client_ID( c ))) return DISCONNECTED;
240 c = Client_Next( c );
243 /* Some information about us */
244 if( ! IRC_WriteStrClient( from, RPL_TRACESERVER_MSG, Client_ID( from ), Conf_ServerName, Client_Mask( Client_ThisServer( )), Option_String( Client_Conn( Client )))) return DISCONNECTED;
246 return IRC_WriteStrClient( from, RPL_TRACEEND_MSG, Client_ID( from ), Conf_ServerName, PACKAGE, VERSION, NGIRCd_DebugLevel );
251 IRC_HELP( CLIENT *Client, REQUEST *Req )
255 assert( Client != NULL );
256 assert( Req != NULL );
258 /* Bad number of arguments? */
259 if( Req->argc > 0 ) return IRC_WriteStrClient( Client, ERR_NORECIPIENT_MSG, Client_ID( Client ), Req->command );
261 cmd = Parse_GetCommandStruct( );
264 if( ! IRC_WriteStrClient( Client, "NOTICE %s :%s", Client_ID( Client ), cmd->name )) return DISCONNECTED;
272 Option_String( CONN_ID Idx )
274 STATIC CHAR option_txt[8];
277 options = Conn_Options( Idx );
279 strcpy( option_txt, "F" ); /* No idea what this means but the original ircd sends it ... */
281 if( options & CONN_ZIP ) strcat( option_txt, "z" );
285 } /* Option_String */