]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/parse.c
Client_GetFromConn() removed and replaced with new function Conn_GetClient()
[ngircd-alex.git] / src / ngircd / parse.c
index de6e696056830120a53539b11fa3e2999078b6bb..548c3729543aa9838ffccc2e85c9500e88436735 100644 (file)
@@ -7,14 +7,17 @@
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  * Please read the file COPYING, README and AUTHORS for more information.
- *
- * IRC command parser and validator
  */
 
 
 #include "portab.h"
 
-static char UNUSED id[] = "$Id: parse.c,v 1.61 2005/03/19 18:43:49 fw Exp $";
+static char UNUSED id[] = "$Id: parse.c,v 1.67 2006/04/23 10:37:27 fw Exp $";
+
+/**
+ * @file
+ * IRC command parser and validator.
+ */
 
 #include "imp.h"
 #include <assert.h>
@@ -100,15 +103,21 @@ COMMAND My_Commands[] =
 };
 
 
-LOCAL void Init_Request PARAMS(( REQUEST *Req ));
+static void Init_Request PARAMS(( REQUEST *Req ));
 
-LOCAL bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
-LOCAL bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
-LOCAL bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
+static bool Validate_Prefix PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
+static bool Validate_Command PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
+static bool Validate_Args PARAMS(( CONN_ID Idx, REQUEST *Req, bool *Closed ));
 
-LOCAL bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
+static bool Handle_Request PARAMS(( CONN_ID Idx, REQUEST *Req ));
 
 
+/**
+ * Return the pointer to the global "IRC command structure".
+ * This structure, an array of type "COMMAND" describes all the IRC commands
+ * implemented by ngIRCd and how to handle them.
+ * @return Pointer to the global command structure.
+ */
 GLOBAL COMMAND *
 Parse_GetCommandStruct( void )
 {
@@ -116,13 +125,27 @@ Parse_GetCommandStruct( void )
 } /* Parse_GetCommandStruct */
 
 
+/**
+ * Parse a command ("request") received from a client.
+ * 
+ * This function is called after the connection layer received a valid CR+LF
+ * terminated line of text: we asume that this is a valid IRC command and
+ * try to do something useful with it :-)
+ *
+ * All errors are reported to the client from which the command has been
+ * received, and if the error is fatal this connection is closed down.
+ *
+ * This function is able to parse the syntax as described in RFC 2812,
+ * section 2.3.
+ *
+ * @param Idx Index of the connection from which the command has been received.
+ * @param Request NULL terminated line of text (the "command").
+ * @return true on success (valid command or "regular" error), false if a
+ *     fatal error occured and the connection has been shut down.
+ */
 GLOBAL bool
 Parse_Request( CONN_ID Idx, char *Request )
 {
-       /* Client-Request parsen. Bei einem schwerwiegenden Fehler wird
-        * die Verbindung geschlossen und false geliefert.
-        * Der Aufbau gueltiger Requests ist in RFC 2812, 2.3 definiert. */
-
        REQUEST req;
        char *start, *ptr;
        bool closed;
@@ -220,7 +243,11 @@ Parse_Request( CONN_ID Idx, char *Request )
 } /* Parse_Request */
 
 
-LOCAL void
+/**
+ * Initialize request structure.
+ * @param Req Request structure to be initialized.
+ */
+static void
 Init_Request( REQUEST *Req )
 {
        /* Neue Request-Struktur initialisieren */
@@ -236,7 +263,7 @@ Init_Request( REQUEST *Req )
 } /* Init_Request */
 
 
-LOCAL bool
+static bool
 Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
 {
        CLIENT *client, *c;
@@ -250,7 +277,7 @@ Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
        if( ! Req->prefix ) return true;
 
        /* Client-Struktur der Connection ermitteln */
-       client = Client_GetFromConn( Idx );
+       client = Conn_GetClient( Idx );
        assert( client != NULL );
 
        /* nur validieren, wenn bereits registrierte Verbindung */
@@ -279,7 +306,7 @@ Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
        {
                /* das angegebene Prefix ist aus dieser Richtung, also
                 * aus der gegebenen Connection, ungueltig! */
-               Log( LOG_ERR, "Spoofed prefix \"%s\" from \"%s\" (connection %d, command %s)!", Req->prefix, Client_Mask( Client_GetFromConn( Idx )), Idx, Req->command );
+               Log( LOG_ERR, "Spoofed prefix \"%s\" from \"%s\" (connection %d, command %s)!", Req->prefix, Client_Mask( Conn_GetClient( Idx )), Idx, Req->command );
                Conn_Close( Idx, NULL, "Spoofed prefix", true);
                *Closed = true;
                return false;
@@ -289,8 +316,8 @@ Validate_Prefix( CONN_ID Idx, REQUEST *Req, bool *Closed )
 } /* Validate_Prefix */
 
 
-LOCAL bool
-Validate_Command( CONN_ID Idx, REQUEST *Req, bool *Closed )
+static bool
+Validate_Command( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
 {
        assert( Idx >= 0 );
        assert( Req != NULL );
@@ -300,8 +327,8 @@ Validate_Command( CONN_ID Idx, REQUEST *Req, bool *Closed )
 } /* Validate_Comman */
 
 
-LOCAL bool
-Validate_Args( CONN_ID Idx, REQUEST *Req, bool *Closed )
+static bool
+Validate_Args( UNUSED CONN_ID Idx, UNUSED REQUEST *Req, bool *Closed )
 {
        assert( Idx >= 0 );
        assert( Req != NULL );
@@ -311,7 +338,7 @@ Validate_Args( CONN_ID Idx, REQUEST *Req, bool *Closed )
 } /* Validate_Args */
 
 
-LOCAL bool
+static bool
 Handle_Request( CONN_ID Idx, REQUEST *Req )
 {
        /* Client-Request verarbeiten. Bei einem schwerwiegenden Fehler
@@ -327,7 +354,7 @@ Handle_Request( CONN_ID Idx, REQUEST *Req )
        assert( Req != NULL );
        assert( Req->command != NULL );
 
-       client = Client_GetFromConn( Idx );
+       client = Conn_GetClient( Idx );
        assert( client != NULL );
 
        /* Statuscode? */
@@ -406,11 +433,26 @@ Handle_Request( CONN_ID Idx, REQUEST *Req )
                        return IRC_WriteStrClient( client, ERR_NOTREGISTERED_MSG, Client_ID( client ));
                }
        }
+
+       if( Client_Type( client ) != CLIENT_USER &&
+           Client_Type( client ) != CLIENT_SERVER &&
+           Client_Type( client ) != CLIENT_SERVICE )
+               return true;
        
-       /* Unbekannter Befehl */
-       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" );
-       if( Client_Type( client ) != CLIENT_SERVER ) return IRC_WriteStrClient( client, ERR_UNKNOWNCOMMAND_MSG, Client_ID( client ), Req->command );
-       else return true;
+       /* Unknown command and registered connection: generate error: */
+       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" );
+
+       if (Client_Type(client) != CLIENT_SERVER) {
+               result = IRC_WriteStrClient(client, ERR_UNKNOWNCOMMAND_MSG,
+                               Client_ID(client), Req->command);
+               Conn_SetPenalty(Idx, 1);
+               return result;
+       }
+
+       return true;
 } /* Handle_Request */