]> arthur.barton.de Git - ngircd-alex.git/blobdiff - src/ngircd/client.c
- New function Client_DestroyNow().
[ngircd-alex.git] / src / ngircd / client.c
index dfa7f870e9928a2bbb377c38d0f4593682c0cf19..bfc3a4f8af43aec4fe0390a9503ba34aecc0846e 100644 (file)
@@ -2,23 +2,13 @@
  * ngIRCd -- The Next Generation IRC Daemon
  * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
  *
- * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
- * der GNU General Public License (GPL), wie von der Free Software Foundation
- * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
- * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
- * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
- * der an ngIRCd beteiligten Autoren finden Sie in der Datei AUTHORS.
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * 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.
  *
- * $Id: client.c,v 1.56 2002/05/30 16:52:21 alex Exp $
- *
- * client.c: Management aller Clients
- *
- * Der Begriff "Client" ist in diesem Fall evtl. etwas verwirrend: Clients sind
- * alle Verbindungen, die im gesamten(!) IRC-Netzwerk bekannt sind. Das sind IRC-
- * Clients (User), andere Server und IRC-Services.
- * Ueber welchen IRC-Server die Verbindung nun tatsaechlich in das Netzwerk her-
- * gestellt wurde, muss der jeweiligen Struktur entnommen werden. Ist es dieser
- * Server gewesen, so existiert eine entsprechende CONNECTION-Struktur.
+ * Client management.
  */
 
 
@@ -27,6 +17,8 @@
 
 #include "portab.h"
 
+static char UNUSED id[] = "$Id: client.c,v 1.73 2003/01/15 14:28:25 alex Exp $";
+
 #include "imp.h"
 #include <assert.h>
 #include <unistd.h>
 #include <exp.h>
 
 
+#define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
+
+
 LOCAL CLIENT *This_Server, *My_Clients;
-LOCAL CHAR GetID_Buffer[CLIENT_ID_LEN];
+LOCAL CHAR GetID_Buffer[GETID_LEN];
 
 
-LOCAL INT Count PARAMS(( CLIENT_TYPE Type ));
-LOCAL INT MyCount PARAMS(( CLIENT_TYPE Type ));
+LOCAL LONG Count PARAMS(( CLIENT_TYPE Type ));
+LOCAL LONG MyCount PARAMS(( CLIENT_TYPE Type ));
 
 LOCAL CLIENT *New_Client_Struct PARAMS(( VOID ));
 LOCAL VOID Generate_MyToken PARAMS(( CLIENT *Client ));
+LOCAL VOID Adjust_Counters PARAMS(( CLIENT *Client ));
+
+#ifndef Client_DestroyNow
+GLOBAL VOID Client_DestroyNow PARAMS((CLIENT *Client ));
+#endif
+
+
+LONG Max_Users = 0, My_Max_Users = 0;
 
 
 GLOBAL VOID
@@ -87,7 +90,7 @@ Client_Init( VOID )
 
        gethostname( This_Server->host, CLIENT_HOST_LEN );
        h = gethostbyname( This_Server->host );
-       if( h ) strcpy( This_Server->host, h->h_name );
+       if( h ) strlcpy( This_Server->host, h->h_name, sizeof( This_Server->host ));
 
        Client_SetID( This_Server, Conf_ServerName );
        Client_SetInfo( This_Server, Conf_ServerInfo );
@@ -102,7 +105,8 @@ Client_Exit( VOID )
        CLIENT *c, *next;
        INT cnt;
 
-       Client_Destroy( This_Server, "Server going down.", NULL, FALSE );
+       if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, FALSE );
+       else Client_Destroy( This_Server, "Server going down.", NULL, FALSE );
        
        cnt = 0;
        c = My_Clients;
@@ -175,12 +179,15 @@ Client_New( CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer, INT Type, CHAR *
        if( Type == CLIENT_SERVER ) Generate_MyToken( client );
 
        /* ist der User away? */
-       if( strchr( client->modes, 'a' )) strcpy( client->away, DEFAULT_AWAY_MSG );
+       if( strchr( client->modes, 'a' )) strlcpy( client->away, DEFAULT_AWAY_MSG, sizeof( client->away ));
 
        /* Verketten */
        client->next = (POINTER *)My_Clients;
        My_Clients = client;
 
+       /* Adjust counters */
+       Adjust_Counters( client );
+
        return client;
 } /* Client_New */
 
@@ -200,7 +207,7 @@ Client_Destroy( CLIENT *Client, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN SendQuit )
        if( ! txt ) txt = "Reason unknown.";
 
        /* Netz-Split-Nachricht vorbereiten (noch nicht optimal) */
-       if( Client->type == CLIENT_SERVER ) sprintf( msg, "%s: lost server %s", This_Server->id, Client->id );
+       if( Client->type == CLIENT_SERVER ) snprintf( msg, sizeof( msg ), "%s: lost server %s", This_Server->id, Client->id );
 
        last = NULL;
        c = My_Clients;
@@ -249,7 +256,7 @@ Client_Destroy( CLIENT *Client, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN SendQuit )
                                                else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "QUIT :" );
                                        }
                                }
-                               Channel_RemoveClient( c, FwdMsg ? FwdMsg : c->id );
+                               Channel_Quit( c, FwdMsg ? FwdMsg : c->id );
                        }
                        else if( c->type == CLIENT_SERVER )
                        {
@@ -260,7 +267,7 @@ Client_Destroy( CLIENT *Client, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN SendQuit )
                                }
 
                                /* andere Server informieren */
-                               if( ! NGIRCd_Quit )
+                               if( ! NGIRCd_SignalQuit )
                                {
                                        if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
                                        else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
@@ -289,6 +296,35 @@ Client_Destroy( CLIENT *Client, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN SendQuit )
 } /* Client_Destroy */
 
 
+GLOBAL VOID
+Client_DestroyNow( CLIENT *Client )
+{
+       /* Destroy client structure immediately. This function is only
+        * intended for the connection layer to remove client structures
+        * of connections that can't be established! */
+
+       CLIENT *last, *c;
+
+       assert( Client != NULL );
+
+       last = NULL;
+       c = My_Clients;
+       while( c )
+       {
+               if( c == Client )
+               {
+                       /* Wir haben den Client gefunden: entfernen */
+                       if( last ) last->next = c->next;
+                       else My_Clients = (CLIENT *)c->next;
+                       free( c );
+                       break;
+               }
+               last = c;
+               c = (CLIENT *)c->next;
+       }
+} /* Client_DestroyNow */
+
+
 GLOBAL VOID
 Client_SetHostname( CLIENT *Client, CHAR *Hostname )
 {
@@ -297,8 +333,7 @@ Client_SetHostname( CLIENT *Client, CHAR *Hostname )
        assert( Client != NULL );
        assert( Hostname != NULL );
        
-       strncpy( Client->host, Hostname, CLIENT_HOST_LEN - 1 );
-       Client->host[CLIENT_HOST_LEN - 1] = '\0';
+       strlcpy( Client->host, Hostname, sizeof( Client->host ));
 } /* Client_SetHostname */
 
 
@@ -310,8 +345,7 @@ Client_SetID( CLIENT *Client, CHAR *ID )
        assert( Client != NULL );
        assert( ID != NULL );
        
-       strncpy( Client->id, ID, CLIENT_ID_LEN - 1 );
-       Client->id[CLIENT_ID_LEN - 1] = '\0';
+       strlcpy( Client->id, ID, sizeof( Client->id ));
 
        /* Hash */
        Client->hash = Hash( Client->id );
@@ -326,13 +360,12 @@ Client_SetUser( CLIENT *Client, CHAR *User, BOOLEAN Idented )
        assert( Client != NULL );
        assert( User != NULL );
        
-       if( Idented ) strncpy( Client->user, User, CLIENT_USER_LEN - 1 );
+       if( Idented ) strlcpy( Client->user, User, sizeof( Client->user ));
        else
        {
                Client->user[0] = '~';
-               strncpy( Client->user + 1, User, CLIENT_USER_LEN - 2 );
+               strlcpy( Client->user + 1, User, sizeof( Client->user ) - 1 );
        }
-       Client->user[CLIENT_USER_LEN - 1] = '\0';
 } /* Client_SetUser */
 
 
@@ -344,24 +377,34 @@ Client_SetInfo( CLIENT *Client, CHAR *Info )
        assert( Client != NULL );
        assert( Info != NULL );
        
-       strncpy( Client->info, Info, CLIENT_INFO_LEN - 1 );
-       Client->info[CLIENT_INFO_LEN - 1] = '\0';
+       strlcpy( Client->info, Info, sizeof( Client->info ));
 } /* Client_SetInfo */
 
 
 GLOBAL VOID
 Client_SetModes( CLIENT *Client, CHAR *Modes )
 {
-       /* Hostname eines Clients setzen */
+       /* Modes eines Clients setzen */
 
        assert( Client != NULL );
        assert( Modes != NULL );
 
-       strncpy( Client->modes, Modes, CLIENT_MODE_LEN - 1 );
-       Client->modes[CLIENT_MODE_LEN - 1] = '\0';
+       strlcpy( Client->modes, Modes, sizeof( Client->modes ));
 } /* Client_SetModes */
 
 
+GLOBAL VOID
+Client_SetFlags( CLIENT *Client, CHAR *Flags )
+{
+       /* Flags eines Clients setzen */
+
+       assert( Client != NULL );
+       assert( Flags != NULL );
+
+       strlcpy( Client->flags, Flags, sizeof( Client->flags ));
+} /* Client_SetFlags */
+
+
 GLOBAL VOID
 Client_SetPassword( CLIENT *Client, CHAR *Pwd )
 {
@@ -370,32 +413,20 @@ Client_SetPassword( CLIENT *Client, CHAR *Pwd )
        assert( Client != NULL );
        assert( Pwd != NULL );
        
-       strncpy( Client->pwd, Pwd, CLIENT_PASS_LEN - 1 );
-       Client->pwd[CLIENT_PASS_LEN - 1] = '\0';
+       strlcpy( Client->pwd, Pwd, sizeof( Client->pwd ));
 } /* Client_SetPassword */
 
 
 GLOBAL VOID
 Client_SetAway( CLIENT *Client, CHAR *Txt )
 {
-       /* Von einem Client gelieferte AWAY-Nachricht */
+       /* Set AWAY reason of client */
 
        assert( Client != NULL );
+       assert( Txt != NULL );
 
-       if( Txt )
-       {
-               /* Client AWAY setzen */
-               strncpy( Client->away, Txt, CLIENT_AWAY_LEN - 1 );
-               Client->away[CLIENT_AWAY_LEN - 1] = '\0';
-               Client_ModeAdd( Client, 'a' );
-               Log( LOG_DEBUG, "User \"%s\" is away: %s", Client_Mask( Client ), Txt );
-       }
-       else
-       {
-               /* AWAY loeschen */
-               Client_ModeDel( Client, 'a' );
-               Log( LOG_DEBUG, "User \"%s\" is no longer away.", Client_Mask( Client ));
-       }
+       strlcpy( Client->away, Txt, sizeof( Client->away ));
+       Log( LOG_DEBUG, "User \"%s\" is away: %s", Client_Mask( Client ), Txt );
 } /* Client_SetAway */
 
 
@@ -405,6 +436,7 @@ Client_SetType( CLIENT *Client, INT Type )
        assert( Client != NULL );
        Client->type = Type;
        if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
+       Adjust_Counters( Client );
 } /* Client_SetType */
 
 
@@ -456,7 +488,7 @@ Client_ModeAdd( CLIENT *Client, CHAR Mode )
        if( ! strchr( Client->modes, x[0] ))
        {
                /* Client hat den Mode noch nicht -> setzen */
-               strcat( Client->modes, x );
+               strlcat( Client->modes, x, sizeof( Client->modes ));
                return TRUE;
        }
        else return FALSE;
@@ -522,8 +554,7 @@ Client_Search( CHAR *Nick )
        assert( Nick != NULL );
 
        /* Nick kopieren und ggf. Host-Mask abschneiden */
-       strncpy( search_id, Nick, CLIENT_ID_LEN - 1 );
-       search_id[CLIENT_ID_LEN - 1] = '\0';
+       strlcpy( search_id, Nick, sizeof( search_id ));
        ptr = strchr( search_id, '!' );
        if( ptr ) *ptr = '\0';
 
@@ -636,6 +667,14 @@ Client_Modes( CLIENT *Client )
 } /* Client_Modes */
 
 
+GLOBAL CHAR *
+Client_Flags( CLIENT *Client )
+{
+       assert( Client != NULL );
+       return Client->flags;
+} /* Client_Flags */
+
+
 GLOBAL BOOLEAN
 Client_OperByMe( CLIENT *Client )
 {
@@ -691,7 +730,7 @@ Client_Mask( CLIENT *Client )
        
        if( Client->type == CLIENT_SERVER ) return Client->id;
 
-       sprintf( GetID_Buffer, "%s!%s@%s", Client->id, Client->user, Client->host );
+       snprintf( GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host );
        return GetID_Buffer;
 } /* Client_Mask */
 
@@ -783,8 +822,9 @@ Client_CheckID( CLIENT *Client, CHAR *ID )
                if( strcasecmp( c->id, ID ) == 0 )
                {
                        /* die Server-ID gibt es bereits */
-                       sprintf( str, "ID \"%s\" already registered!", ID );
-                       Log( LOG_ERR, "%s (on connection %d)", str, Client->conn_id );
+                       snprintf( str, sizeof( str ), "ID \"%s\" already registered", ID );
+                       if( Client->conn_id != c->conn_id ) Log( LOG_ERR, "%s (on connection %d)!", str, c->conn_id );
+                       else Log( LOG_ERR, "%s (via network)!", str );
                        Conn_Close( Client->conn_id, str, str, TRUE );
                        return FALSE;
                }
@@ -815,46 +855,46 @@ Client_Next( CLIENT *c )
 } /* Client_Next */
 
 
-GLOBAL INT
+GLOBAL LONG
 Client_UserCount( VOID )
 {
        return Count( CLIENT_USER );
 } /* Client_UserCount */
 
 
-GLOBAL INT
+GLOBAL LONG
 Client_ServiceCount( VOID )
 {
        return Count( CLIENT_SERVICE );;
 } /* Client_ServiceCount */
 
 
-GLOBAL INT
+GLOBAL LONG
 Client_ServerCount( VOID )
 {
        return Count( CLIENT_SERVER );
 } /* Client_ServerCount */
 
 
-GLOBAL INT
+GLOBAL LONG
 Client_MyUserCount( VOID )
 {
        return MyCount( CLIENT_USER );
 } /* Client_MyUserCount */
 
 
-GLOBAL INT
+GLOBAL LONG
 Client_MyServiceCount( VOID )
 {
        return MyCount( CLIENT_SERVICE );
 } /* Client_MyServiceCount */
 
 
-GLOBAL INT
+GLOBAL LONG
 Client_MyServerCount( VOID )
 {
        CLIENT *c;
-       INT cnt;
+       LONG cnt;
 
        cnt = 0;
        c = My_Clients;
@@ -867,11 +907,11 @@ Client_MyServerCount( VOID )
 } /* Client_MyServerCount */
 
 
-GLOBAL INT
+GLOBAL LONG
 Client_OperCount( VOID )
 {
        CLIENT *c;
-       INT cnt;
+       LONG cnt;
 
        cnt = 0;
        c = My_Clients;
@@ -884,11 +924,11 @@ Client_OperCount( VOID )
 } /* Client_OperCount */
 
 
-GLOBAL INT
+GLOBAL LONG
 Client_UnknownCount( VOID )
 {
        CLIENT *c;
-       INT cnt;
+       LONG cnt;
 
        cnt = 0;
        c = My_Clients;
@@ -901,6 +941,20 @@ Client_UnknownCount( VOID )
 } /* Client_UnknownCount */
 
 
+GLOBAL LONG
+Client_MaxUserCount( VOID )
+{
+       return Max_Users;
+} /* Client_MaxUserCount */
+
+
+GLOBAL LONG
+Client_MyMaxUserCount( VOID )
+{
+       return My_Max_Users;
+} /* Client_MyMaxUserCount */
+
+
 GLOBAL BOOLEAN
 Client_IsValidNick( CHAR *Nick )
 {
@@ -928,11 +982,11 @@ Client_IsValidNick( CHAR *Nick )
 } /* Client_IsValidNick */
 
 
-LOCAL INT
+LOCAL LONG
 Count( CLIENT_TYPE Type )
 {
        CLIENT *c;
-       INT cnt;
+       LONG cnt;
 
        cnt = 0;
        c = My_Clients;
@@ -945,11 +999,11 @@ Count( CLIENT_TYPE Type )
 } /* Count */
 
 
-LOCAL INT
+LOCAL LONG
 MyCount( CLIENT_TYPE Type )
 {
        CLIENT *c;
-       INT cnt;
+       LONG cnt;
 
        cnt = 0;
        c = My_Clients;
@@ -972,7 +1026,7 @@ New_Client_Struct( VOID )
        c = malloc( sizeof( CLIENT ));
        if( ! c )
        {
-               Log( LOG_EMERG, "Can't allocate memory!" );
+               Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
                return NULL;
        }
 
@@ -993,6 +1047,7 @@ New_Client_Struct( VOID )
        c->token = -1;
        c->mytoken = -1;
        strcpy( c->away, "" );
+       strcpy( c->flags, "" );
 
        return c;
 } /* New_Client */
@@ -1022,4 +1077,24 @@ Generate_MyToken( CLIENT *Client )
 } /* Generate_MyToken */
 
 
+LOCAL VOID
+Adjust_Counters( CLIENT *Client )
+{
+       LONG count;
+
+       assert( Client != NULL );
+
+       if( Client->type != CLIENT_USER ) return;
+       
+       if( Client->conn_id != NONE )
+       {
+               /* Local connection */
+               count = Client_MyUserCount( );
+               if( count > My_Max_Users ) My_Max_Users = count;
+       }
+       count = Client_UserCount( );
+       if( count > Max_Users ) Max_Users = count;
+} /* Adjust_Counters */
+
+
 /* -eof- */