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