]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/parse.c
- nicht unterstuetzte Channel-Modes entfernt.
[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.33 2002/05/27 13:09:27 alex Exp $
13  *
14  * parse.c: Parsen der Client-Anfragen
15  */
16
17
18 #include "portab.h"
19
20 #include "imp.h"
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <string.h>
25
26 #include "ngircd.h"
27 #include "defines.h"
28 #include "conn.h"
29 #include "client.h"
30 #include "channel.h"
31 #include "log.h"
32 #include "messages.h"
33 #include "tool.h"
34
35 #include "exp.h"
36 #include "parse.h"
37
38 #include "imp.h"
39 #include "irc.h"
40 #include "irc-channel.h"
41 #include "irc-login.h"
42 #include "irc-mode.h"
43 #include "irc-op.h"
44 #include "irc-oper.h"
45 #include "irc-server.h"
46 #include "irc-write.h"
47
48 #include "exp.h"
49
50
51 LOCAL VOID Init_Request PARAMS(( REQUEST *Req ));
52
53 LOCAL BOOLEAN Parse_Error PARAMS(( CONN_ID Idx, CHAR *Error ));
54
55 LOCAL BOOLEAN Validate_Prefix PARAMS(( REQUEST *Req ));
56 LOCAL BOOLEAN Validate_Command PARAMS(( REQUEST *Req ));
57 LOCAL BOOLEAN Validate_Args PARAMS(( REQUEST *Req ));
58
59 LOCAL BOOLEAN Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
60
61
62 GLOBAL BOOLEAN
63 Parse_Request( CONN_ID Idx, CHAR *Request )
64 {
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. */
68
69         REQUEST req;
70         CHAR *start, *ptr;
71
72         assert( Idx >= 0 );
73         assert( Request != NULL );
74
75 #ifdef SNIFFER
76         if( NGIRCd_Sniffer ) Log( LOG_DEBUG, " <- connection %d: '%s'.", Idx, Request );
77 #endif
78         
79         Init_Request( &req );
80
81         /* Fuehrendes und folgendes "Geraffel" verwerfen */
82         ngt_TrimStr( Request );
83
84         /* gibt es ein Prefix? */
85         if( Request[0] == ':' )
86         {
87                 /* Prefix vorhanden */
88                 req.prefix = Request + 1;
89                 ptr = strchr( Request, ' ' );
90                 if( ! ptr ) return Parse_Error( Idx, "Prefix without command!?" );
91                 *ptr = '\0';
92 #ifndef STRICT_RFC
93                 /* multiple Leerzeichen als Trenner zwischen
94                  * Prefix und Befehl ignorieren */
95                 while( *(ptr + 1) == ' ' ) ptr++;
96 #endif
97                 start = ptr + 1;
98         }
99         else start = Request;
100
101         if( ! Validate_Prefix( &req )) return Parse_Error( Idx, "Invalid prefix");
102
103         /* Befehl */
104         ptr = strchr( start, ' ' );
105         if( ptr )
106         {
107                 *ptr = '\0';
108 #ifndef STRICT_RFC
109                 /* multiple Leerzeichen als Trenner vor
110                 *Parametertrennern ignorieren */
111                 while( *(ptr + 1) == ' ' ) ptr++;
112 #endif
113         }
114         req.command = start;
115
116         if( ! Validate_Command( &req )) return Parse_Error( Idx, "Invalid command" );
117
118         /* Argumente, Parameter */
119         if( ptr )
120         {
121                 /* Prinzipiell gibt es welche :-) */
122                 start = ptr + 1;
123                 while( start )
124                 {
125                         /* Parameter-String "zerlegen" */
126                         if( start[0] == ':' )
127                         {
128                                 req.argv[req.argc] = start + 1;
129                                 ptr = NULL;
130                         }
131                         else
132                         {
133                                 req.argv[req.argc] = start;
134                                 ptr = strchr( start, ' ' );
135                                 if( ptr )
136                                 {
137                                         *ptr = '\0';
138 #ifndef STRICT_RFC
139                                         /* multiple Leerzeichen als
140                                          * Parametertrenner ignorieren */
141                                         while( *(ptr + 1) == ' ' ) ptr++;
142 #endif
143                                 }
144                         }
145                         
146                         req.argc++;
147
148                         if( start[0] == ':' ) break;
149                         if( req.argc > 14 ) break;
150                         
151                         if( ptr ) start = ptr + 1;
152                         else start = NULL;
153                 }
154         }
155         
156         if( ! Validate_Args( &req )) return Parse_Error( Idx, "Invalid argument(s)" );
157
158         return Handle_Request( Idx, &req );
159 } /* Parse_Request */
160
161
162 LOCAL VOID
163 Init_Request( REQUEST *Req )
164 {
165         /* Neue Request-Struktur initialisieren */
166
167         INT i;
168         
169         assert( Req != NULL );
170
171         Req->prefix = NULL;
172         Req->command = NULL;
173         for( i = 0; i < 15; Req->argv[i++] = NULL );
174         Req->argc = 0;
175 } /* Init_Request */
176
177
178 LOCAL BOOLEAN
179 Parse_Error( CONN_ID Idx, CHAR *Error )
180 {
181         /* Fehler beim Parsen. Fehlermeldung an den Client schicken.
182          * TRUE: Connection wurde durch diese Funktion nicht geschlossen,
183          * FALSE: Connection wurde terminiert. */
184         
185         assert( Idx >= 0 );
186         assert( Error != NULL );
187
188         Log( LOG_DEBUG, "Connection %d: Parse error: %s", Idx, Error );
189         return Conn_WriteStr( Idx, "ERROR :Parse error: %s", Error );
190 } /* Parse_Error */
191
192
193 LOCAL BOOLEAN
194 Validate_Prefix( REQUEST *Req )
195 {
196         assert( Req != NULL );
197         return TRUE;
198 } /* Validate_Prefix */
199
200
201 LOCAL BOOLEAN
202 Validate_Command( REQUEST *Req )
203 {
204         assert( Req != NULL );
205         return TRUE;
206 } /* Validate_Comman */
207
208
209 LOCAL BOOLEAN
210 Validate_Args( REQUEST *Req )
211 {
212         assert( Req != NULL );
213         return TRUE;
214 } /* Validate_Args */
215
216
217 LOCAL BOOLEAN
218 Handle_Request( CONN_ID Idx, REQUEST *Req )
219 {
220         /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
221          * wird die Verbindung geschlossen und FALSE geliefert. */
222
223         CLIENT *client, *target, *prefix;
224         CHAR str[LINE_LEN];
225         INT i;
226
227         assert( Idx >= 0 );
228         assert( Req != NULL );
229         assert( Req->command != NULL );
230
231         client = Client_GetFromConn( Idx );
232         assert( client != NULL );
233
234         /* Statuscode, der geforwarded werden muss? */
235         if(( strlen( Req->command ) == 3 ) && ( atoi( Req->command ) > 100 ))
236         {
237                 /* Befehl ist ein Statuscode */
238
239                 /* Zielserver ermitteln */
240                 if(( Client_Type( client ) == CLIENT_SERVER ) && ( Req->argc > 0 )) target = Client_Search( Req->argv[0] );
241                 else target = NULL;
242                 if( ! target )
243                 {
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!" );
246                         return TRUE;
247                 }
248                 if( target == Client_ThisServer( ))
249                 {
250                         Log( LOG_DEBUG, "Ignored status code %s from \"%s\".", Req->command, Client_ID( client ));
251                         return TRUE;
252                 }
253
254                 /* Quell-Client ermitteln */
255                 if( ! Req->prefix[0] )
256                 {
257                         Log( LOG_WARNING, "Got status code without prefix!?" );
258                         return TRUE;
259                 }
260                 else prefix = Client_Search( Req->prefix );
261                 if( ! prefix )
262                 {
263                         Log( LOG_WARNING, "Got status code from unknown source: \"%s\"", Req->prefix );
264                         return TRUE;
265                 }
266
267                 /* Statuscode weiterleiten */
268                 strcpy( str, Req->command );
269                 for( i = 0; i < Req->argc; i++ )
270                 {
271                         if( i < Req->argc - 1 ) strcat( str, " " );
272                         else strcat( str, " :" );
273                         strcat( str, Req->argv[i] );
274                 }
275                 return IRC_WriteStrClientPrefix( target, prefix, str );
276         }
277
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 );
312         
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" );
316
317         return TRUE;
318 } /* Handle_Request */
319
320
321 /* -eof- */