Blob


1 /*
2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
10 *
11 * Client management.
12 */
15 #define __client_c__
18 #include "portab.h"
20 static char UNUSED id[] = "$Id: client.c,v 1.72 2003/01/08 22:03:21 alex Exp $";
22 #include "imp.h"
23 #include <assert.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <netdb.h>
30 #include "conn.h"
32 #include "exp.h"
33 #include "client.h"
35 #include <imp.h>
36 #include "ngircd.h"
37 #include "channel.h"
38 #include "resolve.h"
39 #include "conf.h"
40 #include "hash.h"
41 #include "irc-write.h"
42 #include "log.h"
43 #include "messages.h"
45 #include <exp.h>
48 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
51 LOCAL CLIENT *This_Server, *My_Clients;
52 LOCAL CHAR GetID_Buffer[GETID_LEN];
55 LOCAL LONG Count PARAMS(( CLIENT_TYPE Type ));
56 LOCAL LONG MyCount PARAMS(( CLIENT_TYPE Type ));
58 LOCAL CLIENT *New_Client_Struct PARAMS(( VOID ));
59 LOCAL VOID Generate_MyToken PARAMS(( CLIENT *Client ));
60 LOCAL VOID Adjust_Counters PARAMS(( CLIENT *Client ));
63 LONG Max_Users = 0, My_Max_Users = 0;
66 GLOBAL VOID
67 Client_Init( VOID )
68 {
69 struct hostent *h;
71 This_Server = New_Client_Struct( );
72 if( ! This_Server )
73 {
74 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
75 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
76 exit( 1 );
77 }
79 /* Client-Struktur dieses Servers */
80 This_Server->next = NULL;
81 This_Server->type = CLIENT_SERVER;
82 This_Server->conn_id = NONE;
83 This_Server->introducer = This_Server;
84 This_Server->mytoken = 1;
85 This_Server->hops = 0;
87 gethostname( This_Server->host, CLIENT_HOST_LEN );
88 h = gethostbyname( This_Server->host );
89 if( h ) strlcpy( This_Server->host, h->h_name, sizeof( This_Server->host ));
91 Client_SetID( This_Server, Conf_ServerName );
92 Client_SetInfo( This_Server, Conf_ServerInfo );
94 My_Clients = This_Server;
95 } /* Client_Init */
98 GLOBAL VOID
99 Client_Exit( VOID )
101 CLIENT *c, *next;
102 INT cnt;
104 if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, FALSE );
105 else Client_Destroy( This_Server, "Server going down.", NULL, FALSE );
107 cnt = 0;
108 c = My_Clients;
109 while( c )
111 cnt++;
112 next = (CLIENT *)c->next;
113 free( c );
114 c = next;
116 if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
117 } /* Client_Exit */
120 GLOBAL CLIENT *
121 Client_ThisServer( VOID )
123 return This_Server;
124 } /* Client_ThisServer */
127 GLOBAL CLIENT *
128 Client_NewLocal( CONN_ID Idx, CHAR *Hostname, INT Type, BOOLEAN Idented )
130 /* Neuen lokalen Client erzeugen: Wrapper-Funktion fuer Client_New(). */
131 return Client_New( Idx, This_Server, NULL, Type, NULL, NULL, Hostname, NULL, 0, 0, NULL, Idented );
132 } /* Client_NewLocal */
135 GLOBAL CLIENT *
136 Client_NewRemoteServer( CLIENT *Introducer, CHAR *Hostname, CLIENT *TopServer, INT Hops, INT Token, CHAR *Info, BOOLEAN Idented )
138 /* Neuen Remote-Client erzeugen: Wrapper-Funktion fuer Client_New (). */
139 return Client_New( NONE, Introducer, TopServer, CLIENT_SERVER, Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented );
140 } /* Client_NewRemoteServer */
143 GLOBAL CLIENT *
144 Client_NewRemoteUser( CLIENT *Introducer, CHAR *Nick, INT Hops, CHAR *User, CHAR *Hostname, INT Token, CHAR *Modes, CHAR *Info, BOOLEAN Idented )
146 /* Neuen Remote-Client erzeugen: Wrapper-Funktion fuer Client_New (). */
147 return Client_New( NONE, Introducer, NULL, CLIENT_USER, Nick, User, Hostname, Info, Hops, Token, Modes, Idented );
148 } /* Client_NewRemoteUser */
151 GLOBAL CLIENT *
152 Client_New( CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer, INT Type, CHAR *ID, CHAR *User, CHAR *Hostname, CHAR *Info, INT Hops, INT Token, CHAR *Modes, BOOLEAN Idented )
154 CLIENT *client;
156 assert( Idx >= NONE );
157 assert( Introducer != NULL );
158 assert( Hostname != NULL );
160 client = New_Client_Struct( );
161 if( ! client ) return NULL;
163 /* Initialisieren */
164 client->conn_id = Idx;
165 client->introducer = Introducer;
166 client->topserver = TopServer;
167 client->type = Type;
168 if( ID ) Client_SetID( client, ID );
169 if( User ) Client_SetUser( client, User, Idented );
170 if( Hostname ) Client_SetHostname( client, Hostname );
171 if( Info ) Client_SetInfo( client, Info );
172 client->hops = Hops;
173 client->token = Token;
174 if( Modes ) Client_SetModes( client, Modes );
175 if( Type == CLIENT_SERVER ) Generate_MyToken( client );
177 /* ist der User away? */
178 if( strchr( client->modes, 'a' )) strlcpy( client->away, DEFAULT_AWAY_MSG, sizeof( client->away ));
180 /* Verketten */
181 client->next = (POINTER *)My_Clients;
182 My_Clients = client;
184 /* Adjust counters */
185 Adjust_Counters( client );
187 return client;
188 } /* Client_New */
191 GLOBAL VOID
192 Client_Destroy( CLIENT *Client, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN SendQuit )
194 /* Client entfernen. */
196 CLIENT *last, *c;
197 CHAR msg[LINE_LEN], *txt;
199 assert( Client != NULL );
201 if( LogMsg ) txt = LogMsg;
202 else txt = FwdMsg;
203 if( ! txt ) txt = "Reason unknown.";
205 /* Netz-Split-Nachricht vorbereiten (noch nicht optimal) */
206 if( Client->type == CLIENT_SERVER ) snprintf( msg, sizeof( msg ), "%s: lost server %s", This_Server->id, Client->id );
208 last = NULL;
209 c = My_Clients;
210 while( c )
212 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
214 /* der Client, der geloescht wird ist ein Server. Der Client, den wir gerade
215 * pruefen, ist ein Child von diesem und muss daher auch entfernt werden */
216 Client_Destroy( c, NULL, msg, FALSE );
217 last = NULL;
218 c = My_Clients;
219 continue;
221 if( c == Client )
223 /* Wir haben den Client gefunden: entfernen */
224 if( last ) last->next = c->next;
225 else My_Clients = (CLIENT *)c->next;
227 if( c->type == CLIENT_USER )
229 if( c->conn_id != NONE )
231 /* Ein lokaler User */
232 Log( LOG_NOTICE, "User \"%s\" unregistered (connection %d): %s", Client_Mask( c ), c->conn_id, txt );
234 if( SendQuit )
236 /* Alle andere Server informieren! */
237 if( FwdMsg ) IRC_WriteStrServersPrefix( NULL, c, "QUIT :%s", FwdMsg );
238 else IRC_WriteStrServersPrefix( NULL, c, "QUIT :" );
241 else
243 /* Remote User */
244 Log( LOG_DEBUG, "User \"%s\" unregistered: %s", Client_Mask( c ), txt );
246 if( SendQuit )
248 /* Andere Server informieren, ausser denen, die "in
249 * Richtung dem liegen", auf dem der User registriert
250 * ist. Von denen haben wir das QUIT ja wohl bekommen. */
251 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "QUIT :%s", FwdMsg );
252 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "QUIT :" );
255 Channel_Quit( c, FwdMsg ? FwdMsg : c->id );
257 else if( c->type == CLIENT_SERVER )
259 if( c != This_Server )
261 if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
262 else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
265 /* andere Server informieren */
266 if( ! NGIRCd_SignalQuit )
268 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
269 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
272 else
274 if( c->conn_id != NONE )
276 if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
277 else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
279 else
281 if( c->id[0] ) Log( LOG_WARNING, "Unregistered unknown client \"%s\": %s", c->id, txt );
282 else Log( LOG_WARNING, "Unregistered unknown client: %s", c->id, txt );
286 free( c );
287 break;
289 last = c;
290 c = (CLIENT *)c->next;
292 } /* Client_Destroy */
295 GLOBAL VOID
296 Client_SetHostname( CLIENT *Client, CHAR *Hostname )
298 /* Hostname eines Clients setzen */
300 assert( Client != NULL );
301 assert( Hostname != NULL );
303 strlcpy( Client->host, Hostname, sizeof( Client->host ));
304 } /* Client_SetHostname */
307 GLOBAL VOID
308 Client_SetID( CLIENT *Client, CHAR *ID )
310 /* Hostname eines Clients setzen, Hash-Wert berechnen */
312 assert( Client != NULL );
313 assert( ID != NULL );
315 strlcpy( Client->id, ID, sizeof( Client->id ));
317 /* Hash */
318 Client->hash = Hash( Client->id );
319 } /* Client_SetID */
322 GLOBAL VOID
323 Client_SetUser( CLIENT *Client, CHAR *User, BOOLEAN Idented )
325 /* Username eines Clients setzen */
327 assert( Client != NULL );
328 assert( User != NULL );
330 if( Idented ) strlcpy( Client->user, User, sizeof( Client->user ));
331 else
333 Client->user[0] = '~';
334 strlcpy( Client->user + 1, User, sizeof( Client->user ) - 1 );
336 } /* Client_SetUser */
339 GLOBAL VOID
340 Client_SetInfo( CLIENT *Client, CHAR *Info )
342 /* Hostname eines Clients setzen */
344 assert( Client != NULL );
345 assert( Info != NULL );
347 strlcpy( Client->info, Info, sizeof( Client->info ));
348 } /* Client_SetInfo */
351 GLOBAL VOID
352 Client_SetModes( CLIENT *Client, CHAR *Modes )
354 /* Modes eines Clients setzen */
356 assert( Client != NULL );
357 assert( Modes != NULL );
359 strlcpy( Client->modes, Modes, sizeof( Client->modes ));
360 } /* Client_SetModes */
363 GLOBAL VOID
364 Client_SetFlags( CLIENT *Client, CHAR *Flags )
366 /* Flags eines Clients setzen */
368 assert( Client != NULL );
369 assert( Flags != NULL );
371 strlcpy( Client->flags, Flags, sizeof( Client->flags ));
372 } /* Client_SetFlags */
375 GLOBAL VOID
376 Client_SetPassword( CLIENT *Client, CHAR *Pwd )
378 /* Von einem Client geliefertes Passwort */
380 assert( Client != NULL );
381 assert( Pwd != NULL );
383 strlcpy( Client->pwd, Pwd, sizeof( Client->pwd ));
384 } /* Client_SetPassword */
387 GLOBAL VOID
388 Client_SetAway( CLIENT *Client, CHAR *Txt )
390 /* Set AWAY reason of client */
392 assert( Client != NULL );
393 assert( Txt != NULL );
395 strlcpy( Client->away, Txt, sizeof( Client->away ));
396 Log( LOG_DEBUG, "User \"%s\" is away: %s", Client_Mask( Client ), Txt );
397 } /* Client_SetAway */
400 GLOBAL VOID
401 Client_SetType( CLIENT *Client, INT Type )
403 assert( Client != NULL );
404 Client->type = Type;
405 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
406 Adjust_Counters( Client );
407 } /* Client_SetType */
410 GLOBAL VOID
411 Client_SetHops( CLIENT *Client, INT Hops )
413 assert( Client != NULL );
414 Client->hops = Hops;
415 } /* Client_SetHops */
418 GLOBAL VOID
419 Client_SetToken( CLIENT *Client, INT Token )
421 assert( Client != NULL );
422 Client->token = Token;
423 } /* Client_SetToken */
426 GLOBAL VOID
427 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
429 assert( Client != NULL );
430 assert( Introducer != NULL );
431 Client->introducer = Introducer;
432 } /* Client_SetIntroducer */
435 GLOBAL VOID
436 Client_SetOperByMe( CLIENT *Client, BOOLEAN OperByMe )
438 assert( Client != NULL );
439 Client->oper_by_me = OperByMe;
440 } /* Client_SetOperByMe */
443 GLOBAL BOOLEAN
444 Client_ModeAdd( CLIENT *Client, CHAR Mode )
446 /* Mode soll gesetzt werden. TRUE wird geliefert, wenn der
447 * Mode neu gesetzt wurde, FALSE, wenn der Client den Mode
448 * bereits hatte. */
450 CHAR x[2];
452 assert( Client != NULL );
454 x[0] = Mode; x[1] = '\0';
455 if( ! strchr( Client->modes, x[0] ))
457 /* Client hat den Mode noch nicht -> setzen */
458 strlcat( Client->modes, x, sizeof( Client->modes ));
459 return TRUE;
461 else return FALSE;
462 } /* Client_ModeAdd */
465 GLOBAL BOOLEAN
466 Client_ModeDel( CLIENT *Client, CHAR Mode )
468 /* Mode soll geloescht werden. TRUE wird geliefert, wenn der
469 * Mode entfernt wurde, FALSE, wenn der Client den Mode
470 * ueberhaupt nicht hatte. */
472 CHAR x[2], *p;
474 assert( Client != NULL );
476 x[0] = Mode; x[1] = '\0';
478 p = strchr( Client->modes, x[0] );
479 if( ! p ) return FALSE;
481 /* Client hat den Mode -> loeschen */
482 while( *p )
484 *p = *(p + 1);
485 p++;
487 return TRUE;
488 } /* Client_ModeDel */
491 GLOBAL CLIENT *
492 Client_GetFromConn( CONN_ID Idx )
494 /* Client-Struktur, die zur lokalen Verbindung Idx gehoert,
495 * liefern. Wird keine gefunden, so wird NULL geliefert. */
497 CLIENT *c;
499 assert( Idx >= 0 );
501 c = My_Clients;
502 while( c )
504 if( c->conn_id == Idx ) return c;
505 c = (CLIENT *)c->next;
507 return NULL;
508 } /* Client_GetFromConn */
511 GLOBAL CLIENT *
512 Client_Search( CHAR *Nick )
514 /* Client-Struktur, die den entsprechenden Nick hat, liefern.
515 * Wird keine gefunden, so wird NULL geliefert. */
517 CHAR search_id[CLIENT_ID_LEN], *ptr;
518 CLIENT *c = NULL;
519 UINT32 search_hash;
521 assert( Nick != NULL );
523 /* Nick kopieren und ggf. Host-Mask abschneiden */
524 strlcpy( search_id, Nick, sizeof( search_id ));
525 ptr = strchr( search_id, '!' );
526 if( ptr ) *ptr = '\0';
528 search_hash = Hash( search_id );
530 c = My_Clients;
531 while( c )
533 if( c->hash == search_hash )
535 /* lt. Hash-Wert: Treffer! */
536 if( strcasecmp( c->id, search_id ) == 0 ) return c;
538 c = (CLIENT *)c->next;
540 return NULL;
541 } /* Client_Search */
544 GLOBAL CLIENT *
545 Client_GetFromToken( CLIENT *Client, INT Token )
547 /* Client-Struktur, die den entsprechenden Introducer (=Client)
548 * und das gegebene Token hat, liefern. Wird keine gefunden,
549 * so wird NULL geliefert. */
551 CLIENT *c;
553 assert( Client != NULL );
554 assert( Token > 0 );
556 c = My_Clients;
557 while( c )
559 if(( c->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c->token == Token )) return c;
560 c = (CLIENT *)c->next;
562 return NULL;
563 } /* Client_GetFromToken */
566 GLOBAL INT
567 Client_Type( CLIENT *Client )
569 assert( Client != NULL );
570 return Client->type;
571 } /* Client_Type */
574 GLOBAL CONN_ID
575 Client_Conn( CLIENT *Client )
577 assert( Client != NULL );
578 return Client->conn_id;
579 } /* Client_Conn */
582 GLOBAL CHAR *
583 Client_ID( CLIENT *Client )
585 assert( Client != NULL );
587 #ifdef DEBUG
588 if( Client->type == CLIENT_USER ) assert( strlen( Client->id ) < CLIENT_NICK_LEN );
589 #endif
591 if( Client->id[0] ) return Client->id;
592 else return "*";
593 } /* Client_ID */
596 GLOBAL CHAR *
597 Client_Info( CLIENT *Client )
599 assert( Client != NULL );
600 return Client->info;
601 } /* Client_Info */
604 GLOBAL CHAR *
605 Client_User( CLIENT *Client )
607 assert( Client != NULL );
608 if( Client->user[0] ) return Client->user;
609 else return "~";
610 } /* Client_User */
613 GLOBAL CHAR *
614 Client_Hostname( CLIENT *Client )
616 assert( Client != NULL );
617 return Client->host;
618 } /* Client_Hostname */
621 GLOBAL CHAR *
622 Client_Password( CLIENT *Client )
624 assert( Client != NULL );
625 return Client->pwd;
626 } /* Client_Password */
629 GLOBAL CHAR *
630 Client_Modes( CLIENT *Client )
632 assert( Client != NULL );
633 return Client->modes;
634 } /* Client_Modes */
637 GLOBAL CHAR *
638 Client_Flags( CLIENT *Client )
640 assert( Client != NULL );
641 return Client->flags;
642 } /* Client_Flags */
645 GLOBAL BOOLEAN
646 Client_OperByMe( CLIENT *Client )
648 assert( Client != NULL );
649 return Client->oper_by_me;
650 } /* Client_OperByMe */
653 GLOBAL INT
654 Client_Hops( CLIENT *Client )
656 assert( Client != NULL );
657 return Client->hops;
658 } /* Client_Hops */
661 GLOBAL INT
662 Client_Token( CLIENT *Client )
664 assert( Client != NULL );
665 return Client->token;
666 } /* Client_Token */
669 GLOBAL INT
670 Client_MyToken( CLIENT *Client )
672 assert( Client != NULL );
673 return Client->mytoken;
674 } /* Client_MyToken */
677 GLOBAL CLIENT *
678 Client_NextHop( CLIENT *Client )
680 CLIENT *c;
682 assert( Client != NULL );
684 c = Client;
685 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server )) c = c->introducer;
686 return c;
687 } /* Client_NextHop */
690 GLOBAL CHAR *
691 Client_Mask( CLIENT *Client )
693 /* Client-"ID" liefern, wie sie z.B. fuer
694 * Prefixe benoetigt wird. */
696 assert( Client != NULL );
698 if( Client->type == CLIENT_SERVER ) return Client->id;
700 snprintf( GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host );
701 return GetID_Buffer;
702 } /* Client_Mask */
705 GLOBAL CLIENT *
706 Client_Introducer( CLIENT *Client )
708 assert( Client != NULL );
709 return Client->introducer;
710 } /* Client_Introducer */
713 GLOBAL CLIENT *
714 Client_TopServer( CLIENT *Client )
716 assert( Client != NULL );
717 return Client->topserver;
718 } /* Client_TopServer */
721 GLOBAL BOOLEAN
722 Client_HasMode( CLIENT *Client, CHAR Mode )
724 assert( Client != NULL );
725 return strchr( Client->modes, Mode ) != NULL;
726 } /* Client_HasMode */
729 GLOBAL CHAR *
730 Client_Away( CLIENT *Client )
732 /* AWAY-Text liefern */
734 assert( Client != NULL );
735 return Client->away;
736 } /* Client_Away */
739 GLOBAL BOOLEAN
740 Client_CheckNick( CLIENT *Client, CHAR *Nick )
742 /* Nick ueberpruefen */
744 assert( Client != NULL );
745 assert( Nick != NULL );
747 /* Nick ungueltig? */
748 if( ! Client_IsValidNick( Nick ))
750 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
751 return FALSE;
754 /* Nick bereits vergeben? */
755 if( Client_Search( Nick ))
757 /* den Nick gibt es bereits */
758 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
759 return FALSE;
762 return TRUE;
763 } /* Client_CheckNick */
766 GLOBAL BOOLEAN
767 Client_CheckID( CLIENT *Client, CHAR *ID )
769 /* Nick ueberpruefen */
771 CHAR str[COMMAND_LEN];
772 CLIENT *c;
774 assert( Client != NULL );
775 assert( Client->conn_id > NONE );
776 assert( ID != NULL );
778 /* Nick zu lang? */
779 if( strlen( ID ) > CLIENT_ID_LEN )
781 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), ID );
782 return FALSE;
785 /* ID bereits vergeben? */
786 c = My_Clients;
787 while( c )
789 if( strcasecmp( c->id, ID ) == 0 )
791 /* die Server-ID gibt es bereits */
792 snprintf( str, sizeof( str ), "ID \"%s\" already registered", ID );
793 if( Client->conn_id != c->conn_id ) Log( LOG_ERR, "%s (on connection %d)!", str, c->conn_id );
794 else Log( LOG_ERR, "%s (via network)!", str );
795 Conn_Close( Client->conn_id, str, str, TRUE );
796 return FALSE;
798 c = (CLIENT *)c->next;
801 return TRUE;
802 } /* Client_CheckID */
805 GLOBAL CLIENT *
806 Client_First( VOID )
808 /* Ersten Client liefern. */
810 return My_Clients;
811 } /* Client_First */
814 GLOBAL CLIENT *
815 Client_Next( CLIENT *c )
817 /* Naechsten Client liefern. Existiert keiner,
818 * so wird NULL geliefert. */
820 assert( c != NULL );
821 return (CLIENT *)c->next;
822 } /* Client_Next */
825 GLOBAL LONG
826 Client_UserCount( VOID )
828 return Count( CLIENT_USER );
829 } /* Client_UserCount */
832 GLOBAL LONG
833 Client_ServiceCount( VOID )
835 return Count( CLIENT_SERVICE );;
836 } /* Client_ServiceCount */
839 GLOBAL LONG
840 Client_ServerCount( VOID )
842 return Count( CLIENT_SERVER );
843 } /* Client_ServerCount */
846 GLOBAL LONG
847 Client_MyUserCount( VOID )
849 return MyCount( CLIENT_USER );
850 } /* Client_MyUserCount */
853 GLOBAL LONG
854 Client_MyServiceCount( VOID )
856 return MyCount( CLIENT_SERVICE );
857 } /* Client_MyServiceCount */
860 GLOBAL LONG
861 Client_MyServerCount( VOID )
863 CLIENT *c;
864 LONG cnt;
866 cnt = 0;
867 c = My_Clients;
868 while( c )
870 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
871 c = (CLIENT *)c->next;
873 return cnt;
874 } /* Client_MyServerCount */
877 GLOBAL LONG
878 Client_OperCount( VOID )
880 CLIENT *c;
881 LONG cnt;
883 cnt = 0;
884 c = My_Clients;
885 while( c )
887 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
888 c = (CLIENT *)c->next;
890 return cnt;
891 } /* Client_OperCount */
894 GLOBAL LONG
895 Client_UnknownCount( VOID )
897 CLIENT *c;
898 LONG cnt;
900 cnt = 0;
901 c = My_Clients;
902 while( c )
904 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
905 c = (CLIENT *)c->next;
907 return cnt;
908 } /* Client_UnknownCount */
911 GLOBAL LONG
912 Client_MaxUserCount( VOID )
914 return Max_Users;
915 } /* Client_MaxUserCount */
918 GLOBAL LONG
919 Client_MyMaxUserCount( VOID )
921 return My_Max_Users;
922 } /* Client_MyMaxUserCount */
925 GLOBAL BOOLEAN
926 Client_IsValidNick( CHAR *Nick )
928 /* Ist der Nick gueltig? */
930 CHAR *ptr, goodchars[20];
932 assert( Nick != NULL );
934 strcpy( goodchars, ";0123456789-" );
936 if( Nick[0] == '#' ) return FALSE;
937 if( strchr( goodchars, Nick[0] )) return FALSE;
938 if( strlen( Nick ) >= CLIENT_NICK_LEN ) return FALSE;
940 ptr = Nick;
941 while( *ptr )
943 if(( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return FALSE;
944 if(( *ptr > '}' ) && ( ! strchr( goodchars, *ptr ))) return FALSE;
945 ptr++;
948 return TRUE;
949 } /* Client_IsValidNick */
952 LOCAL LONG
953 Count( CLIENT_TYPE Type )
955 CLIENT *c;
956 LONG cnt;
958 cnt = 0;
959 c = My_Clients;
960 while( c )
962 if( c->type == Type ) cnt++;
963 c = (CLIENT *)c->next;
965 return cnt;
966 } /* Count */
969 LOCAL LONG
970 MyCount( CLIENT_TYPE Type )
972 CLIENT *c;
973 LONG cnt;
975 cnt = 0;
976 c = My_Clients;
977 while( c )
979 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
980 c = (CLIENT *)c->next;
982 return cnt;
983 } /* MyCount */
986 LOCAL CLIENT *
987 New_Client_Struct( VOID )
989 /* Neue CLIENT-Struktur pre-initialisieren */
991 CLIENT *c;
993 c = malloc( sizeof( CLIENT ));
994 if( ! c )
996 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
997 return NULL;
1000 c->next = NULL;
1001 c->hash = 0;
1002 c->type = CLIENT_UNKNOWN;
1003 c->conn_id = NONE;
1004 c->introducer = NULL;
1005 c->topserver = NULL;
1006 strcpy( c->id, "" );
1007 strcpy( c->pwd, "" );
1008 strcpy( c->host, "" );
1009 strcpy( c->user, "" );
1010 strcpy( c->info, "" );
1011 strcpy( c->modes, "" );
1012 c->oper_by_me = FALSE;
1013 c->hops = -1;
1014 c->token = -1;
1015 c->mytoken = -1;
1016 strcpy( c->away, "" );
1017 strcpy( c->flags, "" );
1019 return c;
1020 } /* New_Client */
1023 LOCAL VOID
1024 Generate_MyToken( CLIENT *Client )
1026 CLIENT *c;
1027 INT token;
1029 c = My_Clients;
1030 token = 2;
1031 while( c )
1033 if( c->mytoken == token )
1035 /* Das Token wurde bereits vergeben */
1036 token++;
1037 c = My_Clients;
1038 continue;
1040 else c = (CLIENT *)c->next;
1042 Client->mytoken = token;
1043 Log( LOG_DEBUG, "Assigned token %d to server \"%s\".", token, Client->id );
1044 } /* Generate_MyToken */
1047 LOCAL VOID
1048 Adjust_Counters( CLIENT *Client )
1050 LONG count;
1052 assert( Client != NULL );
1054 if( Client->type != CLIENT_USER ) return;
1056 if( Client->conn_id != NONE )
1058 /* Local connection */
1059 count = Client_MyUserCount( );
1060 if( count > My_Max_Users ) My_Max_Users = count;
1062 count = Client_UserCount( );
1063 if( count > Max_Users ) Max_Users = count;
1064 } /* Adjust_Counters */
1067 /* -eof- */