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.76 2004/03/11 22:16:31 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 <strings.h>
29 #include <netdb.h>
31 #include "conn.h"
33 #include "exp.h"
34 #include "client.h"
36 #include <imp.h>
37 #include "ngircd.h"
38 #include "channel.h"
39 #include "resolve.h"
40 #include "conf.h"
41 #include "hash.h"
42 #include "irc-write.h"
43 #include "log.h"
44 #include "messages.h"
46 #include <exp.h>
49 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
52 LOCAL CLIENT *This_Server, *My_Clients;
53 LOCAL CHAR GetID_Buffer[GETID_LEN];
56 LOCAL LONG Count PARAMS(( CLIENT_TYPE Type ));
57 LOCAL LONG MyCount PARAMS(( CLIENT_TYPE Type ));
59 LOCAL CLIENT *New_Client_Struct PARAMS(( VOID ));
60 LOCAL VOID Generate_MyToken PARAMS(( CLIENT *Client ));
61 LOCAL VOID Adjust_Counters PARAMS(( CLIENT *Client ));
63 #ifndef Client_DestroyNow
64 GLOBAL VOID Client_DestroyNow PARAMS((CLIENT *Client ));
65 #endif
68 LONG Max_Users = 0, My_Max_Users = 0;
71 GLOBAL VOID
72 Client_Init( VOID )
73 {
74 struct hostent *h;
76 This_Server = New_Client_Struct( );
77 if( ! This_Server )
78 {
79 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
80 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
81 exit( 1 );
82 }
84 /* Client-Struktur dieses Servers */
85 This_Server->next = NULL;
86 This_Server->type = CLIENT_SERVER;
87 This_Server->conn_id = NONE;
88 This_Server->introducer = This_Server;
89 This_Server->mytoken = 1;
90 This_Server->hops = 0;
92 gethostname( This_Server->host, CLIENT_HOST_LEN );
93 h = gethostbyname( This_Server->host );
94 if( h ) strlcpy( This_Server->host, h->h_name, sizeof( This_Server->host ));
96 Client_SetID( This_Server, Conf_ServerName );
97 Client_SetInfo( This_Server, Conf_ServerInfo );
99 My_Clients = This_Server;
100 } /* Client_Init */
103 GLOBAL VOID
104 Client_Exit( VOID )
106 CLIENT *c, *next;
107 INT cnt;
109 if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, FALSE );
110 else Client_Destroy( This_Server, "Server going down.", NULL, FALSE );
112 cnt = 0;
113 c = My_Clients;
114 while( c )
116 cnt++;
117 next = (CLIENT *)c->next;
118 free( c );
119 c = next;
121 if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
122 } /* Client_Exit */
125 GLOBAL CLIENT *
126 Client_ThisServer( VOID )
128 return This_Server;
129 } /* Client_ThisServer */
132 GLOBAL CLIENT *
133 Client_NewLocal( CONN_ID Idx, CHAR *Hostname, INT Type, BOOLEAN Idented )
135 /* Neuen lokalen Client erzeugen: Wrapper-Funktion fuer Client_New(). */
136 return Client_New( Idx, This_Server, NULL, Type, NULL, NULL, Hostname, NULL, 0, 0, NULL, Idented );
137 } /* Client_NewLocal */
140 GLOBAL CLIENT *
141 Client_NewRemoteServer( CLIENT *Introducer, CHAR *Hostname, CLIENT *TopServer, INT Hops, INT Token, CHAR *Info, BOOLEAN Idented )
143 /* Neuen Remote-Client erzeugen: Wrapper-Funktion fuer Client_New (). */
144 return Client_New( NONE, Introducer, TopServer, CLIENT_SERVER, Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented );
145 } /* Client_NewRemoteServer */
148 GLOBAL CLIENT *
149 Client_NewRemoteUser( CLIENT *Introducer, CHAR *Nick, INT Hops, CHAR *User, CHAR *Hostname, INT Token, CHAR *Modes, CHAR *Info, BOOLEAN Idented )
151 /* Neuen Remote-Client erzeugen: Wrapper-Funktion fuer Client_New (). */
152 return Client_New( NONE, Introducer, NULL, CLIENT_USER, Nick, User, Hostname, Info, Hops, Token, Modes, Idented );
153 } /* Client_NewRemoteUser */
156 GLOBAL CLIENT *
157 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 )
159 CLIENT *client;
161 assert( Idx >= NONE );
162 assert( Introducer != NULL );
163 assert( Hostname != NULL );
165 client = New_Client_Struct( );
166 if( ! client ) return NULL;
168 /* Initialisieren */
169 client->conn_id = Idx;
170 client->introducer = Introducer;
171 client->topserver = TopServer;
172 client->type = Type;
173 if( ID ) Client_SetID( client, ID );
174 if( User ) Client_SetUser( client, User, Idented );
175 if( Hostname ) Client_SetHostname( client, Hostname );
176 if( Info ) Client_SetInfo( client, Info );
177 client->hops = Hops;
178 client->token = Token;
179 if( Modes ) Client_SetModes( client, Modes );
180 if( Type == CLIENT_SERVER ) Generate_MyToken( client );
182 /* ist der User away? */
183 if( strchr( client->modes, 'a' )) strlcpy( client->away, DEFAULT_AWAY_MSG, sizeof( client->away ));
185 /* Verketten */
186 client->next = (POINTER *)My_Clients;
187 My_Clients = client;
189 /* Adjust counters */
190 Adjust_Counters( client );
192 return client;
193 } /* Client_New */
196 GLOBAL VOID
197 Client_Destroy( CLIENT *Client, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN SendQuit )
199 /* Client entfernen. */
201 CLIENT *last, *c;
202 CHAR msg[LINE_LEN], *txt;
204 assert( Client != NULL );
206 if( LogMsg ) txt = LogMsg;
207 else txt = FwdMsg;
208 if( ! txt ) txt = "Reason unknown.";
210 /* Netz-Split-Nachricht vorbereiten (noch nicht optimal) */
211 if( Client->type == CLIENT_SERVER ) snprintf( msg, sizeof( msg ), "%s: lost server %s", This_Server->id, Client->id );
213 last = NULL;
214 c = My_Clients;
215 while( c )
217 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
219 /* der Client, der geloescht wird ist ein Server. Der Client, den wir gerade
220 * pruefen, ist ein Child von diesem und muss daher auch entfernt werden */
221 Client_Destroy( c, NULL, msg, FALSE );
222 last = NULL;
223 c = My_Clients;
224 continue;
226 if( c == Client )
228 /* Wir haben den Client gefunden: entfernen */
229 if( last ) last->next = c->next;
230 else My_Clients = (CLIENT *)c->next;
232 if( c->type == CLIENT_USER )
234 if( c->conn_id != NONE )
236 /* Ein lokaler User */
237 Log( LOG_NOTICE, "User \"%s\" unregistered (connection %d): %s", Client_Mask( c ), c->conn_id, txt );
239 if( SendQuit )
241 /* Alle andere Server informieren! */
242 if( FwdMsg ) IRC_WriteStrServersPrefix( NULL, c, "QUIT :%s", FwdMsg );
243 else IRC_WriteStrServersPrefix( NULL, c, "QUIT :" );
246 else
248 /* Remote User */
249 Log( LOG_DEBUG, "User \"%s\" unregistered: %s", Client_Mask( c ), txt );
251 if( SendQuit )
253 /* Andere Server informieren, ausser denen, die "in
254 * Richtung dem liegen", auf dem der User registriert
255 * ist. Von denen haben wir das QUIT ja wohl bekommen. */
256 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "QUIT :%s", FwdMsg );
257 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "QUIT :" );
260 Channel_Quit( c, FwdMsg ? FwdMsg : c->id );
262 else if( c->type == CLIENT_SERVER )
264 if( c != This_Server )
266 if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
267 else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
270 /* andere Server informieren */
271 if( ! NGIRCd_SignalQuit )
273 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
274 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
277 else
279 if( c->conn_id != NONE )
281 if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
282 else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
284 else
286 if( c->id[0] ) Log( LOG_WARNING, "Unregistered unknown client \"%s\": %s", c->id, txt );
287 else Log( LOG_WARNING, "Unregistered unknown client: %s", c->id, txt );
291 free( c );
292 break;
294 last = c;
295 c = (CLIENT *)c->next;
297 } /* Client_Destroy */
300 GLOBAL VOID
301 Client_DestroyNow( CLIENT *Client )
303 /* Destroy client structure immediately. This function is only
304 * intended for the connection layer to remove client structures
305 * of connections that can't be established! */
307 CLIENT *last, *c;
309 assert( Client != NULL );
311 last = NULL;
312 c = My_Clients;
313 while( c )
315 if( c == Client )
317 /* Wir haben den Client gefunden: entfernen */
318 if( last ) last->next = c->next;
319 else My_Clients = (CLIENT *)c->next;
320 free( c );
321 break;
323 last = c;
324 c = (CLIENT *)c->next;
326 } /* Client_DestroyNow */
329 GLOBAL VOID
330 Client_SetHostname( CLIENT *Client, CHAR *Hostname )
332 /* Hostname eines Clients setzen */
334 assert( Client != NULL );
335 assert( Hostname != NULL );
337 strlcpy( Client->host, Hostname, sizeof( Client->host ));
338 } /* Client_SetHostname */
341 GLOBAL VOID
342 Client_SetID( CLIENT *Client, CHAR *ID )
344 /* Hostname eines Clients setzen, Hash-Wert berechnen */
346 assert( Client != NULL );
347 assert( ID != NULL );
349 strlcpy( Client->id, ID, sizeof( Client->id ));
351 /* Hash */
352 Client->hash = Hash( Client->id );
353 } /* Client_SetID */
356 GLOBAL VOID
357 Client_SetUser( CLIENT *Client, CHAR *User, BOOLEAN Idented )
359 /* Username eines Clients setzen */
361 assert( Client != NULL );
362 assert( User != NULL );
364 if( Idented ) strlcpy( Client->user, User, sizeof( Client->user ));
365 else
367 Client->user[0] = '~';
368 strlcpy( Client->user + 1, User, sizeof( Client->user ) - 1 );
370 } /* Client_SetUser */
373 GLOBAL VOID
374 Client_SetInfo( CLIENT *Client, CHAR *Info )
376 /* Hostname eines Clients setzen */
378 assert( Client != NULL );
379 assert( Info != NULL );
381 strlcpy( Client->info, Info, sizeof( Client->info ));
382 } /* Client_SetInfo */
385 GLOBAL VOID
386 Client_SetModes( CLIENT *Client, CHAR *Modes )
388 /* Modes eines Clients setzen */
390 assert( Client != NULL );
391 assert( Modes != NULL );
393 strlcpy( Client->modes, Modes, sizeof( Client->modes ));
394 } /* Client_SetModes */
397 GLOBAL VOID
398 Client_SetFlags( CLIENT *Client, CHAR *Flags )
400 /* Flags eines Clients setzen */
402 assert( Client != NULL );
403 assert( Flags != NULL );
405 strlcpy( Client->flags, Flags, sizeof( Client->flags ));
406 } /* Client_SetFlags */
409 GLOBAL VOID
410 Client_SetPassword( CLIENT *Client, CHAR *Pwd )
412 /* Von einem Client geliefertes Passwort */
414 assert( Client != NULL );
415 assert( Pwd != NULL );
417 strlcpy( Client->pwd, Pwd, sizeof( Client->pwd ));
418 } /* Client_SetPassword */
421 GLOBAL VOID
422 Client_SetAway( CLIENT *Client, CHAR *Txt )
424 /* Set AWAY reason of client */
426 assert( Client != NULL );
427 assert( Txt != NULL );
429 strlcpy( Client->away, Txt, sizeof( Client->away ));
430 Log( LOG_DEBUG, "User \"%s\" is away: %s", Client_Mask( Client ), Txt );
431 } /* Client_SetAway */
434 GLOBAL VOID
435 Client_SetType( CLIENT *Client, INT Type )
437 assert( Client != NULL );
438 Client->type = Type;
439 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
440 Adjust_Counters( Client );
441 } /* Client_SetType */
444 GLOBAL VOID
445 Client_SetHops( CLIENT *Client, INT Hops )
447 assert( Client != NULL );
448 Client->hops = Hops;
449 } /* Client_SetHops */
452 GLOBAL VOID
453 Client_SetToken( CLIENT *Client, INT Token )
455 assert( Client != NULL );
456 Client->token = Token;
457 } /* Client_SetToken */
460 GLOBAL VOID
461 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
463 assert( Client != NULL );
464 assert( Introducer != NULL );
465 Client->introducer = Introducer;
466 } /* Client_SetIntroducer */
469 GLOBAL VOID
470 Client_SetOperByMe( CLIENT *Client, BOOLEAN OperByMe )
472 assert( Client != NULL );
473 Client->oper_by_me = OperByMe;
474 } /* Client_SetOperByMe */
477 GLOBAL BOOLEAN
478 Client_ModeAdd( CLIENT *Client, CHAR Mode )
480 /* Mode soll gesetzt werden. TRUE wird geliefert, wenn der
481 * Mode neu gesetzt wurde, FALSE, wenn der Client den Mode
482 * bereits hatte. */
484 CHAR x[2];
486 assert( Client != NULL );
488 x[0] = Mode; x[1] = '\0';
489 if( ! strchr( Client->modes, x[0] ))
491 /* Client hat den Mode noch nicht -> setzen */
492 strlcat( Client->modes, x, sizeof( Client->modes ));
493 return TRUE;
495 else return FALSE;
496 } /* Client_ModeAdd */
499 GLOBAL BOOLEAN
500 Client_ModeDel( CLIENT *Client, CHAR Mode )
502 /* Mode soll geloescht werden. TRUE wird geliefert, wenn der
503 * Mode entfernt wurde, FALSE, wenn der Client den Mode
504 * ueberhaupt nicht hatte. */
506 CHAR x[2], *p;
508 assert( Client != NULL );
510 x[0] = Mode; x[1] = '\0';
512 p = strchr( Client->modes, x[0] );
513 if( ! p ) return FALSE;
515 /* Client hat den Mode -> loeschen */
516 while( *p )
518 *p = *(p + 1);
519 p++;
521 return TRUE;
522 } /* Client_ModeDel */
525 GLOBAL CLIENT *
526 Client_GetFromConn( CONN_ID Idx )
528 /* Client-Struktur, die zur lokalen Verbindung Idx gehoert,
529 * liefern. Wird keine gefunden, so wird NULL geliefert. */
531 CLIENT *c;
533 assert( Idx >= 0 );
535 c = My_Clients;
536 while( c )
538 if( c->conn_id == Idx ) return c;
539 c = (CLIENT *)c->next;
541 return NULL;
542 } /* Client_GetFromConn */
545 GLOBAL CLIENT *
546 Client_Search( CHAR *Nick )
548 /* Client-Struktur, die den entsprechenden Nick hat, liefern.
549 * Wird keine gefunden, so wird NULL geliefert. */
551 CHAR search_id[CLIENT_ID_LEN], *ptr;
552 CLIENT *c = NULL;
553 UINT32 search_hash;
555 assert( Nick != NULL );
557 /* Nick kopieren und ggf. Host-Mask abschneiden */
558 strlcpy( search_id, Nick, sizeof( search_id ));
559 ptr = strchr( search_id, '!' );
560 if( ptr ) *ptr = '\0';
562 search_hash = Hash( search_id );
564 c = My_Clients;
565 while( c )
567 if( c->hash == search_hash )
569 /* lt. Hash-Wert: Treffer! */
570 if( strcasecmp( c->id, search_id ) == 0 ) return c;
572 c = (CLIENT *)c->next;
574 return NULL;
575 } /* Client_Search */
578 GLOBAL CLIENT *
579 Client_GetFromToken( CLIENT *Client, INT Token )
581 /* Client-Struktur, die den entsprechenden Introducer (=Client)
582 * und das gegebene Token hat, liefern. Wird keine gefunden,
583 * so wird NULL geliefert. */
585 CLIENT *c;
587 assert( Client != NULL );
588 assert( Token > 0 );
590 c = My_Clients;
591 while( c )
593 if(( c->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c->token == Token )) return c;
594 c = (CLIENT *)c->next;
596 return NULL;
597 } /* Client_GetFromToken */
600 GLOBAL INT
601 Client_Type( CLIENT *Client )
603 assert( Client != NULL );
604 return Client->type;
605 } /* Client_Type */
608 GLOBAL CONN_ID
609 Client_Conn( CLIENT *Client )
611 assert( Client != NULL );
612 return Client->conn_id;
613 } /* Client_Conn */
616 GLOBAL CHAR *
617 Client_ID( CLIENT *Client )
619 assert( Client != NULL );
621 #ifdef DEBUG
622 if( Client->type == CLIENT_USER ) assert( strlen( Client->id ) < CLIENT_NICK_LEN );
623 #endif
625 if( Client->id[0] ) return Client->id;
626 else return "*";
627 } /* Client_ID */
630 GLOBAL CHAR *
631 Client_Info( CLIENT *Client )
633 assert( Client != NULL );
634 return Client->info;
635 } /* Client_Info */
638 GLOBAL CHAR *
639 Client_User( CLIENT *Client )
641 assert( Client != NULL );
642 if( Client->user[0] ) return Client->user;
643 else return "~";
644 } /* Client_User */
647 GLOBAL CHAR *
648 Client_Hostname( CLIENT *Client )
650 assert( Client != NULL );
651 return Client->host;
652 } /* Client_Hostname */
655 GLOBAL CHAR *
656 Client_Password( CLIENT *Client )
658 assert( Client != NULL );
659 return Client->pwd;
660 } /* Client_Password */
663 GLOBAL CHAR *
664 Client_Modes( CLIENT *Client )
666 assert( Client != NULL );
667 return Client->modes;
668 } /* Client_Modes */
671 GLOBAL CHAR *
672 Client_Flags( CLIENT *Client )
674 assert( Client != NULL );
675 return Client->flags;
676 } /* Client_Flags */
679 GLOBAL BOOLEAN
680 Client_OperByMe( CLIENT *Client )
682 assert( Client != NULL );
683 return Client->oper_by_me;
684 } /* Client_OperByMe */
687 GLOBAL INT
688 Client_Hops( CLIENT *Client )
690 assert( Client != NULL );
691 return Client->hops;
692 } /* Client_Hops */
695 GLOBAL INT
696 Client_Token( CLIENT *Client )
698 assert( Client != NULL );
699 return Client->token;
700 } /* Client_Token */
703 GLOBAL INT
704 Client_MyToken( CLIENT *Client )
706 assert( Client != NULL );
707 return Client->mytoken;
708 } /* Client_MyToken */
711 GLOBAL CLIENT *
712 Client_NextHop( CLIENT *Client )
714 CLIENT *c;
716 assert( Client != NULL );
718 c = Client;
719 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server )) c = c->introducer;
720 return c;
721 } /* Client_NextHop */
724 GLOBAL CHAR *
725 Client_Mask( CLIENT *Client )
727 /* Client-"ID" liefern, wie sie z.B. fuer
728 * Prefixe benoetigt wird. */
730 assert( Client != NULL );
732 if( Client->type == CLIENT_SERVER ) return Client->id;
734 snprintf( GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host );
735 return GetID_Buffer;
736 } /* Client_Mask */
739 GLOBAL CLIENT *
740 Client_Introducer( CLIENT *Client )
742 assert( Client != NULL );
743 return Client->introducer;
744 } /* Client_Introducer */
747 GLOBAL CLIENT *
748 Client_TopServer( CLIENT *Client )
750 assert( Client != NULL );
751 return Client->topserver;
752 } /* Client_TopServer */
755 GLOBAL BOOLEAN
756 Client_HasMode( CLIENT *Client, CHAR Mode )
758 assert( Client != NULL );
759 return strchr( Client->modes, Mode ) != NULL;
760 } /* Client_HasMode */
763 GLOBAL CHAR *
764 Client_Away( CLIENT *Client )
766 /* AWAY-Text liefern */
768 assert( Client != NULL );
769 return Client->away;
770 } /* Client_Away */
773 GLOBAL BOOLEAN
774 Client_CheckNick( CLIENT *Client, CHAR *Nick )
776 /* Nick ueberpruefen */
778 assert( Client != NULL );
779 assert( Nick != NULL );
781 /* Nick ungueltig? */
782 if( ! Client_IsValidNick( Nick ))
784 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
785 return FALSE;
788 /* Nick bereits vergeben? */
789 if( Client_Search( Nick ))
791 /* den Nick gibt es bereits */
792 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
793 return FALSE;
796 return TRUE;
797 } /* Client_CheckNick */
800 GLOBAL BOOLEAN
801 Client_CheckID( CLIENT *Client, CHAR *ID )
803 /* Nick ueberpruefen */
805 CHAR str[COMMAND_LEN];
806 CLIENT *c;
808 assert( Client != NULL );
809 assert( Client->conn_id > NONE );
810 assert( ID != NULL );
812 /* Nick zu lang? */
813 if( strlen( ID ) > CLIENT_ID_LEN )
815 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), ID );
816 return FALSE;
819 /* ID bereits vergeben? */
820 c = My_Clients;
821 while( c )
823 if( strcasecmp( c->id, ID ) == 0 )
825 /* die Server-ID gibt es bereits */
826 snprintf( str, sizeof( str ), "ID \"%s\" already registered", ID );
827 if( Client->conn_id != c->conn_id ) Log( LOG_ERR, "%s (on connection %d)!", str, c->conn_id );
828 else Log( LOG_ERR, "%s (via network)!", str );
829 Conn_Close( Client->conn_id, str, str, TRUE );
830 return FALSE;
832 c = (CLIENT *)c->next;
835 return TRUE;
836 } /* Client_CheckID */
839 GLOBAL CLIENT *
840 Client_First( VOID )
842 /* Ersten Client liefern. */
844 return My_Clients;
845 } /* Client_First */
848 GLOBAL CLIENT *
849 Client_Next( CLIENT *c )
851 /* Naechsten Client liefern. Existiert keiner,
852 * so wird NULL geliefert. */
854 assert( c != NULL );
855 return (CLIENT *)c->next;
856 } /* Client_Next */
859 GLOBAL LONG
860 Client_UserCount( VOID )
862 return Count( CLIENT_USER );
863 } /* Client_UserCount */
866 GLOBAL LONG
867 Client_ServiceCount( VOID )
869 return Count( CLIENT_SERVICE );;
870 } /* Client_ServiceCount */
873 GLOBAL LONG
874 Client_ServerCount( VOID )
876 return Count( CLIENT_SERVER );
877 } /* Client_ServerCount */
880 GLOBAL LONG
881 Client_MyUserCount( VOID )
883 return MyCount( CLIENT_USER );
884 } /* Client_MyUserCount */
887 GLOBAL LONG
888 Client_MyServiceCount( VOID )
890 return MyCount( CLIENT_SERVICE );
891 } /* Client_MyServiceCount */
894 GLOBAL LONG
895 Client_MyServerCount( VOID )
897 CLIENT *c;
898 LONG cnt;
900 cnt = 0;
901 c = My_Clients;
902 while( c )
904 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
905 c = (CLIENT *)c->next;
907 return cnt;
908 } /* Client_MyServerCount */
911 GLOBAL LONG
912 Client_OperCount( VOID )
914 CLIENT *c;
915 LONG cnt;
917 cnt = 0;
918 c = My_Clients;
919 while( c )
921 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
922 c = (CLIENT *)c->next;
924 return cnt;
925 } /* Client_OperCount */
928 GLOBAL LONG
929 Client_UnknownCount( VOID )
931 CLIENT *c;
932 LONG cnt;
934 cnt = 0;
935 c = My_Clients;
936 while( c )
938 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
939 c = (CLIENT *)c->next;
941 return cnt;
942 } /* Client_UnknownCount */
945 GLOBAL LONG
946 Client_MaxUserCount( VOID )
948 return Max_Users;
949 } /* Client_MaxUserCount */
952 GLOBAL LONG
953 Client_MyMaxUserCount( VOID )
955 return My_Max_Users;
956 } /* Client_MyMaxUserCount */
959 GLOBAL BOOLEAN
960 Client_IsValidNick( CHAR *Nick )
962 /* Ist der Nick gueltig? */
964 CHAR *ptr, goodchars[20];
966 assert( Nick != NULL );
968 strcpy( goodchars, ";0123456789-" );
970 if( Nick[0] == '#' ) return FALSE;
971 if( strchr( goodchars, Nick[0] )) return FALSE;
972 if( strlen( Nick ) >= CLIENT_NICK_LEN ) return FALSE;
974 ptr = Nick;
975 while( *ptr )
977 if(( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return FALSE;
978 if(( *ptr > '}' ) && ( ! strchr( goodchars, *ptr ))) return FALSE;
979 ptr++;
982 return TRUE;
983 } /* Client_IsValidNick */
986 LOCAL LONG
987 Count( CLIENT_TYPE Type )
989 CLIENT *c;
990 LONG cnt;
992 cnt = 0;
993 c = My_Clients;
994 while( c )
996 if( c->type == Type ) cnt++;
997 c = (CLIENT *)c->next;
999 return cnt;
1000 } /* Count */
1003 LOCAL LONG
1004 MyCount( CLIENT_TYPE Type )
1006 CLIENT *c;
1007 LONG cnt;
1009 cnt = 0;
1010 c = My_Clients;
1011 while( c )
1013 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1014 c = (CLIENT *)c->next;
1016 return cnt;
1017 } /* MyCount */
1020 LOCAL CLIENT *
1021 New_Client_Struct( VOID )
1023 /* Neue CLIENT-Struktur pre-initialisieren */
1025 CLIENT *c;
1027 c = (CLIENT *)malloc( sizeof( CLIENT ));
1028 if( ! c )
1030 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1031 return NULL;
1034 c->next = NULL;
1035 c->hash = 0;
1036 c->type = CLIENT_UNKNOWN;
1037 c->conn_id = NONE;
1038 c->introducer = NULL;
1039 c->topserver = NULL;
1040 strcpy( c->id, "" );
1041 strcpy( c->pwd, "" );
1042 strcpy( c->host, "" );
1043 strcpy( c->user, "" );
1044 strcpy( c->info, "" );
1045 strcpy( c->modes, "" );
1046 c->oper_by_me = FALSE;
1047 c->hops = -1;
1048 c->token = -1;
1049 c->mytoken = -1;
1050 strcpy( c->away, "" );
1051 strcpy( c->flags, "" );
1053 return c;
1054 } /* New_Client */
1057 LOCAL VOID
1058 Generate_MyToken( CLIENT *Client )
1060 CLIENT *c;
1061 INT token;
1063 c = My_Clients;
1064 token = 2;
1065 while( c )
1067 if( c->mytoken == token )
1069 /* Das Token wurde bereits vergeben */
1070 token++;
1071 c = My_Clients;
1072 continue;
1074 else c = (CLIENT *)c->next;
1076 Client->mytoken = token;
1077 Log( LOG_DEBUG, "Assigned token %d to server \"%s\".", token, Client->id );
1078 } /* Generate_MyToken */
1081 LOCAL VOID
1082 Adjust_Counters( CLIENT *Client )
1084 LONG count;
1086 assert( Client != NULL );
1088 if( Client->type != CLIENT_USER ) return;
1090 if( Client->conn_id != NONE )
1092 /* Local connection */
1093 count = Client_MyUserCount( );
1094 if( count > My_Max_Users ) My_Max_Users = count;
1096 count = Client_UserCount( );
1097 if( count > Max_Users ) Max_Users = count;
1098 } /* Adjust_Counters */
1101 /* -eof- */