Blob


1 /*
2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 *
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.
11 *
12 * $Id: parse.c,v 1.31 2002/03/25 17:13:46 alex Exp $
13 *
14 * parse.c: Parsen der Client-Anfragen
15 */
18 #include "portab.h"
20 #include "imp.h"
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
26 #include "ngircd.h"
27 #include "client.h"
28 #include "conn.h"
29 #include "defines.h"
30 #include "irc.h"
31 #include "irc-channel.h"
32 #include "irc-login.h"
33 #include "irc-mode.h"
34 #include "irc-oper.h"
35 #include "irc-server.h"
36 #include "irc-write.h"
37 #include "log.h"
38 #include "messages.h"
39 #include "tool.h"
41 #include "exp.h"
42 #include "parse.h"
45 LOCAL VOID Init_Request( REQUEST *Req );
47 LOCAL BOOLEAN Parse_Error( CONN_ID Idx, CHAR *Error );
49 LOCAL BOOLEAN Validate_Prefix( REQUEST *Req );
50 LOCAL BOOLEAN Validate_Command( REQUEST *Req );
51 LOCAL BOOLEAN Validate_Args( REQUEST *Req );
53 LOCAL BOOLEAN Handle_Request( CONN_ID Idx, REQUEST *Req );
56 GLOBAL BOOLEAN Parse_Request( CONN_ID Idx, CHAR *Request )
57 {
58 /* Client-Request parsen. Bei einem schwerwiegenden Fehler wird
59 * die Verbindung geschlossen und FALSE geliefert.
60 * Der Aufbau gueltiger Requests ist in RFC 2812, 2.3 definiert. */
62 REQUEST req;
63 CHAR *start, *ptr;
65 assert( Idx >= 0 );
66 assert( Request != NULL );
68 #ifdef SNIFFER
69 if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
70 #endif
72 Init_Request( &req );
74 /* Fuehrendes und folgendes "Geraffel" verwerfen */
75 ngt_TrimStr( Request );
77 /* gibt es ein Prefix? */
78 if( Request[0] == ':' )
79 {
80 /* Prefix vorhanden */
81 req.prefix = Request + 1;
82 ptr = strchr( Request, ' ' );
83 if( ! ptr ) return Parse_Error( Idx, "Prefix without command!?" );
84 *ptr = '\0';
85 #ifndef STRICT_RFC
86 /* multiple Leerzeichen als Trenner zwischen
87 * Prefix und Befehl ignorieren */
88 while( *(ptr + 1) == ' ' ) ptr++;
89 #endif
90 start = ptr + 1;
91 }
92 else start = Request;
94 if( ! Validate_Prefix( &req )) return Parse_Error( Idx, "Invalid prefix");
96 /* Befehl */
97 ptr = strchr( start, ' ' );
98 if( ptr )
99 {
100 *ptr = '\0';
101 #ifndef STRICT_RFC
102 /* multiple Leerzeichen als Trenner vor
103 *Parametertrennern ignorieren */
104 while( *(ptr + 1) == ' ' ) ptr++;
105 #endif
107 req.command = start;
109 if( ! Validate_Command( &req )) return Parse_Error( Idx, "Invalid command" );
111 /* Argumente, Parameter */
112 if( ptr )
114 /* Prinzipiell gibt es welche :-) */
115 start = ptr + 1;
116 while( start )
118 /* Parameter-String "zerlegen" */
119 if( start[0] == ':' )
121 req.argv[req.argc] = start + 1;
122 ptr = NULL;
124 else
126 req.argv[req.argc] = start;
127 ptr = strchr( start, ' ' );
128 if( ptr )
130 *ptr = '\0';
131 #ifndef STRICT_RFC
132 /* multiple Leerzeichen als
133 * Parametertrenner ignorieren */
134 while( *(ptr + 1) == ' ' ) ptr++;
135 #endif
139 req.argc++;
141 if( start[0] == ':' ) break;
142 if( req.argc > 14 ) break;
144 if( ptr ) start = ptr + 1;
145 else start = NULL;
149 if( ! Validate_Args( &req )) return Parse_Error( Idx, "Invalid argument(s)" );
151 return Handle_Request( Idx, &req );
152 } /* Parse_Request */
155 LOCAL VOID Init_Request( REQUEST *Req )
157 /* Neue Request-Struktur initialisieren */
159 INT i;
161 assert( Req != NULL );
163 Req->prefix = NULL;
164 Req->command = NULL;
165 for( i = 0; i < 15; Req->argv[i++] = NULL );
166 Req->argc = 0;
167 } /* Init_Request */
170 LOCAL BOOLEAN Parse_Error( CONN_ID Idx, CHAR *Error )
172 /* Fehler beim Parsen. Fehlermeldung an den Client schicken.
173 * TRUE: Connection wurde durch diese Funktion nicht geschlossen,
174 * FALSE: Connection wurde terminiert. */
176 assert( Idx >= 0 );
177 assert( Error != NULL );
179 Log( LOG_DEBUG, "Connection %d: Parse error: %s", Idx, Error );
180 return Conn_WriteStr( Idx, "ERROR :Parse error: %s", Error );
181 } /* Parse_Error */
184 LOCAL BOOLEAN Validate_Prefix( REQUEST *Req )
186 assert( Req != NULL );
187 return TRUE;
188 } /* Validate_Prefix */
191 LOCAL BOOLEAN Validate_Command( REQUEST *Req )
193 assert( Req != NULL );
194 return TRUE;
195 } /* Validate_Comman */
198 LOCAL BOOLEAN Validate_Args( REQUEST *Req )
200 assert( Req != NULL );
201 return TRUE;
202 } /* Validate_Args */
205 LOCAL BOOLEAN Handle_Request( CONN_ID Idx, REQUEST *Req )
207 /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
208 * wird die Verbindung geschlossen und FALSE geliefert. */
210 CLIENT *client, *target, *prefix;
211 CHAR str[LINE_LEN];
212 INT i;
214 assert( Idx >= 0 );
215 assert( Req != NULL );
216 assert( Req->command != NULL );
218 client = Client_GetFromConn( Idx );
219 assert( client != NULL );
221 /* Statuscode, der geforwarded werden muss? */
222 if(( strlen( Req->command ) == 3 ) && ( atoi( Req->command ) > 100 ))
224 /* Befehl ist ein Statuscode */
226 /* Zielserver ermitteln */
227 if(( Client_Type( client ) == CLIENT_SERVER ) && ( Req->argc > 0 )) target = Client_Search( Req->argv[0] );
228 else target = NULL;
229 if( ! target )
231 if( Req->argc > 0 ) Log( LOG_WARNING, "Unknown target for status code: \"%s\"", Req->argv[0] );
232 else Log( LOG_WARNING, "Unknown target for status code!" );
233 return TRUE;
235 if( target == Client_ThisServer( ))
237 Log( LOG_DEBUG, "Ignored status code %s from \"%s\".", Req->command, Client_ID( client ));
238 return TRUE;
241 /* Quell-Client ermitteln */
242 if( ! Req->prefix[0] )
244 Log( LOG_WARNING, "Got status code without prefix!?" );
245 return TRUE;
247 else prefix = Client_Search( Req->prefix );
248 if( ! prefix )
250 Log( LOG_WARNING, "Got status code from unknown source: \"%s\"", Req->prefix );
251 return TRUE;
254 /* Statuscode weiterleiten */
255 strcpy( str, Req->command );
256 for( i = 0; i < Req->argc; i++ )
258 if( i < Req->argc - 1 ) strcat( str, " " );
259 else strcat( str, " :" );
260 strcat( str, Req->argv[i] );
262 return IRC_WriteStrClientPrefix( target, prefix, str );
265 if( strcasecmp( Req->command, "PASS" ) == 0 ) return IRC_PASS( client, Req );
266 else if( strcasecmp( Req->command, "NICK" ) == 0 ) return IRC_NICK( client, Req );
267 else if( strcasecmp( Req->command, "USER" ) == 0 ) return IRC_USER( client, Req );
268 else if( strcasecmp( Req->command, "SERVER" ) == 0 ) return IRC_SERVER( client, Req );
269 else if( strcasecmp( Req->command, "NJOIN" ) == 0 ) return IRC_NJOIN( client, Req );
270 else if( strcasecmp( Req->command, "QUIT" ) == 0 ) return IRC_QUIT( client, Req );
271 else if( strcasecmp( Req->command, "SQUIT" ) == 0 ) return IRC_SQUIT( client, Req );
272 else if( strcasecmp( Req->command, "PING" ) == 0 ) return IRC_PING( client, Req );
273 else if( strcasecmp( Req->command, "PONG" ) == 0 ) return IRC_PONG( client, Req );
274 else if( strcasecmp( Req->command, "MOTD" ) == 0 ) return IRC_MOTD( client, Req );
275 else if( strcasecmp( Req->command, "PRIVMSG" ) == 0 ) return IRC_PRIVMSG( client, Req );
276 else if( strcasecmp( Req->command, "NOTICE" ) == 0 ) return IRC_NOTICE( client, Req );
277 else if( strcasecmp( Req->command, "MODE" ) == 0 ) return IRC_MODE( client, Req );
278 else if( strcasecmp( Req->command, "NAMES" ) == 0 ) return IRC_NAMES( client, Req );
279 else if( strcasecmp( Req->command, "ISON" ) == 0 ) return IRC_ISON( client, Req );
280 else if( strcasecmp( Req->command, "WHOIS" ) == 0 ) return IRC_WHOIS( client, Req );
281 else if( strcasecmp( Req->command, "USERHOST" ) == 0 ) return IRC_USERHOST( client, Req );
282 else if( strcasecmp( Req->command, "OPER" ) == 0 ) return IRC_OPER( client, Req );
283 else if( strcasecmp( Req->command, "DIE" ) == 0 ) return IRC_DIE( client, Req );
284 else if( strcasecmp( Req->command, "RESTART" ) == 0 ) return IRC_RESTART( client, Req );
285 else if( strcasecmp( Req->command, "ERROR" ) == 0 ) return IRC_ERROR( client, Req );
286 else if( strcasecmp( Req->command, "LUSERS" ) == 0 ) return IRC_LUSERS( client, Req );
287 else if( strcasecmp( Req->command, "LINKS" ) == 0 ) return IRC_LINKS( client, Req );
288 else if( strcasecmp( Req->command, "JOIN" ) == 0 ) return IRC_JOIN( client, Req );
289 else if( strcasecmp( Req->command, "PART" ) == 0 ) return IRC_PART( client, Req );
290 else if( strcasecmp( Req->command, "VERSION" ) == 0 ) return IRC_VERSION( client, Req );
291 else if( strcasecmp( Req->command, "KILL" ) == 0 ) return IRC_KILL( client, Req );
292 else if( strcasecmp( Req->command, "AWAY" ) == 0 ) return IRC_AWAY( client, Req );
293 else if( strcasecmp( Req->command, "TOPIC" ) == 0 ) return IRC_TOPIC( client, Req );
294 else if( strcasecmp( Req->command, "WHO" ) == 0 ) return IRC_WHO( client, Req );
296 /* Unbekannter Befehl */
297 if( Client_Type( client ) != CLIENT_SERVER ) IRC_WriteStrClient( client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( client ), Req->command );
298 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" );
300 return TRUE;
301 } /* Handle_Request */
304 /* -eof- */