2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
5 * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6 * der GNU General Public License (GPL), wie von der Free Software Foundation
7 * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8 * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9 * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10 * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
12 * $Id: parse.c,v 1.33 2002/05/27 13:09:27 alex Exp $
14 * parse.c: Parsen der Client-Anfragen
40 #include "irc-channel.h"
41 #include "irc-login.h"
45 #include "irc-server.h"
46 #include "irc-write.h"
51 LOCAL VOID Init_Request PARAMS(( REQUEST *Req ));
53 LOCAL BOOLEAN Parse_Error PARAMS(( CONN_ID Idx, CHAR *Error ));
55 LOCAL BOOLEAN Validate_Prefix PARAMS(( REQUEST *Req ));
56 LOCAL BOOLEAN Validate_Command PARAMS(( REQUEST *Req ));
57 LOCAL BOOLEAN Validate_Args PARAMS(( REQUEST *Req ));
59 LOCAL BOOLEAN Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
63 Parse_Request( CONN_ID Idx, CHAR *Request )
65 /* Client-Request parsen. Bei einem schwerwiegenden Fehler wird
66 * die Verbindung geschlossen und FALSE geliefert.
67 * Der Aufbau gueltiger Requests ist in RFC 2812, 2.3 definiert. */
73 assert( Request != NULL );
76 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
81 /* Fuehrendes und folgendes "Geraffel" verwerfen */
82 ngt_TrimStr( Request );
84 /* gibt es ein Prefix? */
85 if( Request[0] == ':' )
87 /* Prefix vorhanden */
88 req.prefix = Request + 1;
89 ptr = strchr( Request, ' ' );
90 if( ! ptr ) return Parse_Error( Idx, "Prefix without command!?" );
93 /* multiple Leerzeichen als Trenner zwischen
94 * Prefix und Befehl ignorieren */
95 while( *(ptr + 1) == ' ' ) ptr++;
101 if( ! Validate_Prefix( &req )) return Parse_Error( Idx, "Invalid prefix");
104 ptr = strchr( start, ' ' );
109 /* multiple Leerzeichen als Trenner vor
110 *Parametertrennern ignorieren */
111 while( *(ptr + 1) == ' ' ) ptr++;
116 if( ! Validate_Command( &req )) return Parse_Error( Idx, "Invalid command" );
118 /* Argumente, Parameter */
121 /* Prinzipiell gibt es welche :-) */
125 /* Parameter-String "zerlegen" */
126 if( start[0] == ':' )
128 req.argv[req.argc] = start + 1;
133 req.argv[req.argc] = start;
134 ptr = strchr( start, ' ' );
139 /* multiple Leerzeichen als
140 * Parametertrenner ignorieren */
141 while( *(ptr + 1) == ' ' ) ptr++;
148 if( start[0] == ':' ) break;
149 if( req.argc > 14 ) break;
151 if( ptr ) start = ptr + 1;
156 if( ! Validate_Args( &req )) return Parse_Error( Idx, "Invalid argument(s)" );
158 return Handle_Request( Idx, &req );
159 } /* Parse_Request */
163 Init_Request( REQUEST *Req )
165 /* Neue Request-Struktur initialisieren */
169 assert( Req != NULL );
173 for( i = 0; i < 15; Req->argv[i++] = NULL );
179 Parse_Error( CONN_ID Idx, CHAR *Error )
181 /* Fehler beim Parsen. Fehlermeldung an den Client schicken.
182 * TRUE: Connection wurde durch diese Funktion nicht geschlossen,
183 * FALSE: Connection wurde terminiert. */
186 assert( Error != NULL );
188 Log( LOG_DEBUG, "Connection %d: Parse error: %s", Idx, Error );
189 return Conn_WriteStr( Idx, "ERROR :Parse error: %s", Error );
194 Validate_Prefix( REQUEST *Req )
196 assert( Req != NULL );
198 } /* Validate_Prefix */
202 Validate_Command( REQUEST *Req )
204 assert( Req != NULL );
206 } /* Validate_Comman */
210 Validate_Args( REQUEST *Req )
212 assert( Req != NULL );
214 } /* Validate_Args */
218 Handle_Request( CONN_ID Idx, REQUEST *Req )
220 /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
221 * wird die Verbindung geschlossen und FALSE geliefert. */
223 CLIENT *client, *target, *prefix;
228 assert( Req != NULL );
229 assert( Req->command != NULL );
231 client = Client_GetFromConn( Idx );
232 assert( client != NULL );
234 /* Statuscode, der geforwarded werden muss? */
235 if(( strlen( Req->command ) == 3 ) && ( atoi( Req->command ) > 100 ))
237 /* Befehl ist ein Statuscode */
239 /* Zielserver ermitteln */
240 if(( Client_Type( client ) == CLIENT_SERVER ) && ( Req->argc > 0 )) target = Client_Search( Req->argv[0] );
244 if( Req->argc > 0 ) Log( LOG_WARNING, "Unknown target for status code: \"%s\"", Req->argv[0] );
245 else Log( LOG_WARNING, "Unknown target for status code!" );
248 if( target == Client_ThisServer( ))
250 Log( LOG_DEBUG, "Ignored status code %s from \"%s\".", Req->command, Client_ID( client ));
254 /* Quell-Client ermitteln */
255 if( ! Req->prefix[0] )
257 Log( LOG_WARNING, "Got status code without prefix!?" );
260 else prefix = Client_Search( Req->prefix );
263 Log( LOG_WARNING, "Got status code from unknown source: \"%s\"", Req->prefix );
267 /* Statuscode weiterleiten */
268 strcpy( str, Req->command );
269 for( i = 0; i < Req->argc; i++ )
271 if( i < Req->argc - 1 ) strcat( str, " " );
272 else strcat( str, " :" );
273 strcat( str, Req->argv[i] );
275 return IRC_WriteStrClientPrefix( target, prefix, str );
278 if( strcasecmp( Req->command, "PASS" ) == 0 ) return IRC_PASS( client, Req );
279 else if( strcasecmp( Req->command, "NICK" ) == 0 ) return IRC_NICK( client, Req );
280 else if( strcasecmp( Req->command, "USER" ) == 0 ) return IRC_USER( client, Req );
281 else if( strcasecmp( Req->command, "SERVER" ) == 0 ) return IRC_SERVER( client, Req );
282 else if( strcasecmp( Req->command, "NJOIN" ) == 0 ) return IRC_NJOIN( client, Req );
283 else if( strcasecmp( Req->command, "QUIT" ) == 0 ) return IRC_QUIT( client, Req );
284 else if( strcasecmp( Req->command, "SQUIT" ) == 0 ) return IRC_SQUIT( client, Req );
285 else if( strcasecmp( Req->command, "PING" ) == 0 ) return IRC_PING( client, Req );
286 else if( strcasecmp( Req->command, "PONG" ) == 0 ) return IRC_PONG( client, Req );
287 else if( strcasecmp( Req->command, "MOTD" ) == 0 ) return IRC_MOTD( client, Req );
288 else if( strcasecmp( Req->command, "PRIVMSG" ) == 0 ) return IRC_PRIVMSG( client, Req );
289 else if( strcasecmp( Req->command, "NOTICE" ) == 0 ) return IRC_NOTICE( client, Req );
290 else if( strcasecmp( Req->command, "MODE" ) == 0 ) return IRC_MODE( client, Req );
291 else if( strcasecmp( Req->command, "NAMES" ) == 0 ) return IRC_NAMES( client, Req );
292 else if( strcasecmp( Req->command, "ISON" ) == 0 ) return IRC_ISON( client, Req );
293 else if( strcasecmp( Req->command, "WHOIS" ) == 0 ) return IRC_WHOIS( client, Req );
294 else if( strcasecmp( Req->command, "USERHOST" ) == 0 ) return IRC_USERHOST( client, Req );
295 else if( strcasecmp( Req->command, "OPER" ) == 0 ) return IRC_OPER( client, Req );
296 else if( strcasecmp( Req->command, "DIE" ) == 0 ) return IRC_DIE( client, Req );
297 else if( strcasecmp( Req->command, "RESTART" ) == 0 ) return IRC_RESTART( client, Req );
298 else if( strcasecmp( Req->command, "ERROR" ) == 0 ) return IRC_ERROR( client, Req );
299 else if( strcasecmp( Req->command, "LUSERS" ) == 0 ) return IRC_LUSERS( client, Req );
300 else if( strcasecmp( Req->command, "LINKS" ) == 0 ) return IRC_LINKS( client, Req );
301 else if( strcasecmp( Req->command, "JOIN" ) == 0 ) return IRC_JOIN( client, Req );
302 else if( strcasecmp( Req->command, "PART" ) == 0 ) return IRC_PART( client, Req );
303 else if( strcasecmp( Req->command, "VERSION" ) == 0 ) return IRC_VERSION( client, Req );
304 else if( strcasecmp( Req->command, "KILL" ) == 0 ) return IRC_KILL( client, Req );
305 else if( strcasecmp( Req->command, "AWAY" ) == 0 ) return IRC_AWAY( client, Req );
306 else if( strcasecmp( Req->command, "TOPIC" ) == 0 ) return IRC_TOPIC( client, Req );
307 else if( strcasecmp( Req->command, "WHO" ) == 0 ) return IRC_WHO( client, Req );
308 else if( strcasecmp( Req->command, "LIST" ) == 0 ) return IRC_LIST( client, Req );
309 else if( strcasecmp( Req->command, "INVITE" ) == 0 ) return IRC_INVITE( client, Req );
310 else if( strcasecmp( Req->command, "KICK" ) == 0 ) return IRC_KICK( client, Req );
311 else if( strcasecmp( Req->command, "BAN" ) == 0 ) return IRC_BAN( client, Req );
313 /* Unbekannter Befehl */
314 if( Client_Type( client ) != CLIENT_SERVER ) IRC_WriteStrClient( client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( client ), Req->command );
315 Log( LOG_DEBUG, "Connection %d: Unknown command \"%s\", %d %s,%s prefix.", Client_Conn( client ), Req->command, Req->argc, Req->argc == 1 ? "parameter" : "parameters", Req->prefix ? "" : " no" );
318 } /* Handle_Request */