]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/parse.c
- Status-Codes an den Server selber werden ignoriert, besseres Logging.
[ngircd-alex.git] / src / ngircd / parse.c
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.18 2002/01/07 15:29:11 alex Exp $
13  *
14  * parse.c: Parsen der Client-Anfragen
15  *
16  * $Log: parse.c,v $
17  * Revision 1.18  2002/01/07 15:29:11  alex
18  * - Status-Codes an den Server selber werden ignoriert, besseres Logging.
19  *
20  * Revision 1.17  2002/01/06 17:41:44  alex
21  * - die Fehlermeldung "unbekannter Befehl" hatte ein falsches Format.
22  *
23  * Revision 1.16  2002/01/05 23:23:20  alex
24  * - generisches Forwarding von Zahlen-Statuscodes implementiert.
25  *
26  * Revision 1.15  2002/01/05 01:42:08  alex
27  * - an Server werden keine ERRORS mehr wegen unbekannter Befehle geschickt.
28  *
29  * Revision 1.14  2002/01/04 17:56:45  alex
30  * - neuer Befehl SQUIT.
31  *
32  * Revision 1.13  2002/01/04 01:20:02  alex
33  * - Client-Strukruren werden nur noch ueber Funktionen angesprochen.
34  *
35  * Revision 1.12  2002/01/03 02:24:49  alex
36  * - neue Befehle NJOIN und SERVER begonnen.
37  *
38  * Revision 1.11  2002/01/02 02:43:22  alex
39  * - Copyright-Texte aktualisiert.
40  * - neuer Befehl ERROR.
41  *
42  * Revision 1.10  2001/12/31 15:33:13  alex
43  * - neuer Befehl NAMES, kleinere Bugfixes.
44  * - Bug bei PING behoben: war zu restriktiv implementiert :-)
45  *
46  * Revision 1.9  2001/12/31 02:18:51  alex
47  * - viele neue Befehle (WHOIS, ISON, OPER, DIE, RESTART),
48  * - neuen Header "defines.h" mit (fast) allen Konstanten.
49  * - Code Cleanups und viele "kleine" Aenderungen & Bugfixes.
50  *
51  * Revision 1.8  2001/12/29 03:08:19  alex
52  * - Fuehrende und folgende Leerzeichen etc. in Requests werden geloescht.
53  * - Logmeldungen (mal wieder) ein wenig angepasst.
54  *
55  * Revision 1.7  2001/12/27 19:13:21  alex
56  * - neue Befehle NOTICE und PRIVMSG.
57  * - Debug-Logging ein wenig reduziert.
58  *
59  * Revision 1.6  2001/12/26 14:45:37  alex
60  * - "Code Cleanups".
61  *
62  * Revision 1.5  2001/12/26 03:23:03  alex
63  * - PING/PONG-Befehle implementiert.
64  *
65  * Revision 1.4  2001/12/25 22:04:26  alex
66  * - Aenderungen an den Debug- und Logging-Funktionen.
67  *
68  * Revision 1.3  2001/12/25 19:18:36  alex
69  * - Gross- und Kleinschreibung der IRC-Befehle wird ignoriert.
70  * - bessere Debug-Ausgaben.
71  *
72  * Revision 1.2  2001/12/23 21:56:47  alex
73  * - bessere Debug-Ausgaben,
74  * - Bug im Parameter-Parser behoben (bei "langem" Parameter)
75  * - erste IRC-Befehle werden erkannt :-)
76  *
77  * Revision 1.1  2001/12/21 23:53:16  alex
78  * - Modul zum Parsen von Client-Requests begonnen.
79  */
80
81
82 #include <portab.h>
83 #include "global.h"
84
85 #include <imp.h>
86 #include <assert.h>
87 #include <stdlib.h>
88 #include <stdio.h>
89 #include <string.h>
90
91 #include "client.h"
92 #include "conn.h"
93 #include "irc.h"
94 #include "log.h"
95 #include "messages.h"
96 #include "tool.h"
97
98 #include <exp.h>
99 #include "parse.h"
100
101
102 LOCAL VOID Init_Request( REQUEST *Req );
103
104 LOCAL BOOLEAN Parse_Error( CONN_ID Idx, CHAR *Error );
105
106 LOCAL BOOLEAN Validate_Prefix( REQUEST *Req );
107 LOCAL BOOLEAN Validate_Command( REQUEST *Req );
108 LOCAL BOOLEAN Validate_Args( REQUEST *Req );
109
110 LOCAL BOOLEAN Handle_Request( CONN_ID Idx, REQUEST *Req );
111
112
113 GLOBAL VOID Parse_Init( VOID )
114 {
115 } /* Parse_Init */
116
117
118 GLOBAL VOID Parse_Exit( VOID )
119 {
120 } /* Parse_Exit */
121
122
123 GLOBAL BOOLEAN Parse_Request( CONN_ID Idx, CHAR *Request )
124 {
125         /* Client-Request parsen. Bei einem schwerwiegenden Fehler wird
126          * die Verbindung geschlossen und FALSE geliefert.
127          * Der Aufbau gueltiger Requests ist in RFC 2812, 2.3 definiert. */
128
129         REQUEST req;
130         CHAR *start, *ptr;
131
132         assert( Idx >= 0 );
133         assert( Request != NULL );
134
135 #ifdef SNIFFER
136         Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
137 #endif
138         
139         Init_Request( &req );
140
141         /* Fuehrendes und folgendes "Geraffel" verwerfen */
142         ngt_TrimStr( Request );
143
144         /* gibt es ein Prefix? */
145         if( Request[0] == ':' )
146         {
147                 /* Prefix vorhanden */
148                 req.prefix = Request + 1;
149                 ptr = strchr( Request, ' ' );
150                 if( ! ptr ) return Parse_Error( Idx, "Prefix without command!?" );
151                 *ptr = '\0';
152                 start = ptr + 1;
153         }
154         else start = Request;
155
156         if( ! Validate_Prefix( &req )) return Parse_Error( Idx, "Invalid prefix");
157
158         /* Befehl */
159         ptr = strchr( start, ' ' );
160         if( ptr ) *ptr = '\0';
161         req.command = start;
162
163         if( ! Validate_Command( &req )) return Parse_Error( Idx, "Invalid command" );
164
165         /* Argumente, Parameter */
166         if( ptr )
167         {
168                 /* Prinzipiell gibt es welche :-) */
169                 start = ptr + 1;
170                 while( start )
171                 {
172                         /* Parameter-String "zerlegen" */
173                         if( start[0] == ':' )
174                         {
175                                 req.argv[req.argc] = start + 1;
176                                 ptr = NULL;
177                         }
178                         else
179                         {
180                                 req.argv[req.argc] = start;
181                                 ptr = strchr( start, ' ' );
182                                 if( ptr ) *ptr = '\0';
183                         }
184                         
185                         req.argc++;
186
187                         if( start[0] == ':' ) break;
188                         if( req.argc > 14 ) break;
189                         
190                         if( ptr ) start = ptr + 1;
191                         else start = NULL;
192                 }
193         }
194         
195         if( ! Validate_Args( &req )) return Parse_Error( Idx, "Invalid argument(s)" );
196
197         return Handle_Request( Idx, &req );
198 } /* Parse_Request */
199
200
201 LOCAL VOID Init_Request( REQUEST *Req )
202 {
203         /* Neue Request-Struktur initialisieren */
204
205         INT i;
206         
207         assert( Req != NULL );
208
209         Req->prefix = NULL;
210         Req->command = NULL;
211         for( i = 0; i < 15; Req->argv[i++] = NULL );
212         Req->argc = 0;
213 } /* Init_Request */
214
215
216 LOCAL BOOLEAN Parse_Error( CONN_ID Idx, CHAR *Error )
217 {
218         /* Fehler beim Parsen. Fehlermeldung an den Client schicken.
219          * TRUE: Connection wurde durch diese Funktion nicht geschlossen,
220          * FALSE: Connection wurde terminiert. */
221         
222         assert( Idx >= 0 );
223         assert( Error != NULL );
224
225         Log( LOG_DEBUG, "Connection %d: Parse error: %s", Idx, Error );
226         return Conn_WriteStr( Idx, "ERROR :Parse error: %s", Error );
227 } /* Parse_Error */
228
229
230 LOCAL BOOLEAN Validate_Prefix( REQUEST *Req )
231 {
232         assert( Req != NULL );
233         return TRUE;
234 } /* Validate_Prefix */
235
236
237 LOCAL BOOLEAN Validate_Command( REQUEST *Req )
238 {
239         assert( Req != NULL );
240         return TRUE;
241 } /* Validate_Comman */
242
243
244 LOCAL BOOLEAN Validate_Args( REQUEST *Req )
245 {
246         assert( Req != NULL );
247         return TRUE;
248 } /* Validate_Args */
249
250
251 LOCAL BOOLEAN Handle_Request( CONN_ID Idx, REQUEST *Req )
252 {
253         /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
254          * wird die Verbindung geschlossen und FALSE geliefert. */
255
256         CLIENT *client, *target, *prefix;
257         CHAR str[LINE_LEN];
258         INT i;
259
260         assert( Idx >= 0 );
261         assert( Req != NULL );
262         assert( Req->command != NULL );
263
264         client = Client_GetFromConn( Idx );
265         assert( client != NULL );
266
267         /* Statuscode, der geforwarded werden muss? */
268         if(( strlen( Req->command ) == 3 ) && ( atoi( Req->command ) > 100 ))
269         {
270                 /* Befehl ist ein Statuscode */
271
272                 /* Zielserver ermitteln */
273                 if(( Client_Type( client ) == CLIENT_SERVER ) && ( Req->argc > 0 )) target = Client_GetFromID( Req->argv[0] );
274                 else target = NULL;
275                 if( ! target )
276                 {
277                         if( target ) Log( LOG_WARNING, "Unknown target for status code: \"%s\"", Req->argv[0] );
278                         else Log( LOG_WARNING, "Unknown target for status code!" );
279                         return TRUE;
280                 }
281                 if( target == Client_ThisServer( ))
282                 {
283                         Log( LOG_DEBUG, "Ignored status code %s from \"%s\".", Req->command, Client_ID( client ));
284                         return TRUE;
285                 }
286
287                 /* Quell-Client ermitteln */
288                 if( ! Req->prefix[0] )
289                 {
290                         Log( LOG_WARNING, "Got status code without prefix!?" );
291                         return TRUE;
292                 }
293                 else prefix = Client_GetFromID( Req->prefix );
294                 if( ! prefix )
295                 {
296                         Log( LOG_WARNING, "Got status code from unknown source: \"%s\"", Req->prefix );
297                         return TRUE;
298                 }
299
300                 /* Statuscode weiterleiten */
301                 strcpy( str, Req->command );
302                 for( i = 0; i < Req->argc; i++ )
303                 {
304                         if( i < Req->argc - 1 ) strcat( str, " " );
305                         else strcat( str, " :" );
306                         strcat( str, Req->argv[i] );
307                 }
308                 return IRC_WriteStrClientPrefix( target, prefix, str );
309         }
310
311         if( strcasecmp( Req->command, "PASS" ) == 0 ) return IRC_PASS( client, Req );
312         else if( strcasecmp( Req->command, "NICK" ) == 0 ) return IRC_NICK( client, Req );
313         else if( strcasecmp( Req->command, "USER" ) == 0 ) return IRC_USER( client, Req );
314         else if( strcasecmp( Req->command, "SERVER" ) == 0 ) return IRC_SERVER( client, Req );
315         else if( strcasecmp( Req->command, "NJOIN" ) == 0 ) return IRC_NJOIN( client, Req );
316         else if( strcasecmp( Req->command, "QUIT" ) == 0 ) return IRC_QUIT( client, Req );
317         else if( strcasecmp( Req->command, "SQUIT" ) == 0 ) return IRC_SQUIT( client, Req );
318         else if( strcasecmp( Req->command, "PING" ) == 0 ) return IRC_PING( client, Req );
319         else if( strcasecmp( Req->command, "PONG" ) == 0 ) return IRC_PONG( client, Req );
320         else if( strcasecmp( Req->command, "MOTD" ) == 0 ) return IRC_MOTD( client, Req );
321         else if( strcasecmp( Req->command, "PRIVMSG" ) == 0 ) return IRC_PRIVMSG( client, Req );
322         else if( strcasecmp( Req->command, "NOTICE" ) == 0 ) return IRC_NOTICE( client, Req );
323         else if( strcasecmp( Req->command, "MODE" ) == 0 ) return IRC_MODE( client, Req );
324         else if( strcasecmp( Req->command, "NAMES" ) == 0 ) return IRC_NAMES( client, Req );
325         else if( strcasecmp( Req->command, "ISON" ) == 0 ) return IRC_ISON( client, Req );
326         else if( strcasecmp( Req->command, "WHOIS" ) == 0 ) return IRC_WHOIS( client, Req );
327         else if( strcasecmp( Req->command, "USERHOST" ) == 0 ) return IRC_USERHOST( client, Req );
328         else if( strcasecmp( Req->command, "OPER" ) == 0 ) return IRC_OPER( client, Req );
329         else if( strcasecmp( Req->command, "DIE" ) == 0 ) return IRC_DIE( client, Req );
330         else if( strcasecmp( Req->command, "RESTART" ) == 0 ) return IRC_RESTART( client, Req );
331         else if( strcasecmp( Req->command, "ERROR" ) == 0 ) return IRC_ERROR( client, Req );
332         
333         /* Unbekannter Befehl */
334         if( Client_Type( client ) != CLIENT_SERVER ) IRC_WriteStrClient( client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( client ), Req->command );
335         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" );
336
337         return TRUE;
338 } /* Handle_Request */
339
340
341 /* -eof- */