2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
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.
20 static char UNUSED id[] = "$Id: client.c,v 1.79 2005/04/27 07:46:50 alex Exp $";
43 #include "irc-write.h"
50 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
53 LOCAL CLIENT *This_Server, *My_Clients;
54 LOCAL char GetID_Buffer[GETID_LEN];
57 LOCAL long Count PARAMS(( CLIENT_TYPE Type ));
58 LOCAL long MyCount PARAMS(( CLIENT_TYPE Type ));
60 LOCAL CLIENT *New_Client_Struct PARAMS(( void ));
61 LOCAL void Generate_MyToken PARAMS(( CLIENT *Client ));
62 LOCAL void Adjust_Counters PARAMS(( CLIENT *Client ));
64 #ifndef Client_DestroyNow
65 GLOBAL void Client_DestroyNow PARAMS((CLIENT *Client ));
69 long Max_Users = 0, My_Max_Users = 0;
77 This_Server = New_Client_Struct( );
80 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
81 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
85 /* Client-Struktur dieses Servers */
86 This_Server->next = NULL;
87 This_Server->type = CLIENT_SERVER;
88 This_Server->conn_id = NONE;
89 This_Server->introducer = This_Server;
90 This_Server->mytoken = 1;
91 This_Server->hops = 0;
93 gethostname( This_Server->host, CLIENT_HOST_LEN );
94 h = gethostbyname( This_Server->host );
95 if( h ) strlcpy( This_Server->host, h->h_name, sizeof( This_Server->host ));
97 Client_SetID( This_Server, Conf_ServerName );
98 Client_SetInfo( This_Server, Conf_ServerInfo );
100 My_Clients = This_Server;
110 if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
111 else Client_Destroy( This_Server, "Server going down.", NULL, false );
118 next = (CLIENT *)c->next;
122 if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
127 Client_ThisServer( void )
130 } /* Client_ThisServer */
134 Client_NewLocal( CONN_ID Idx, char *Hostname, int Type, bool Idented )
136 /* Neuen lokalen Client erzeugen: Wrapper-Funktion fuer Client_New(). */
137 return Client_New( Idx, This_Server, NULL, Type, NULL, NULL, Hostname, NULL, 0, 0, NULL, Idented );
138 } /* Client_NewLocal */
142 Client_NewRemoteServer( CLIENT *Introducer, char *Hostname, CLIENT *TopServer, int Hops, int Token, char *Info, bool Idented )
144 /* Neuen Remote-Client erzeugen: Wrapper-Funktion fuer Client_New (). */
145 return Client_New( NONE, Introducer, TopServer, CLIENT_SERVER, Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented );
146 } /* Client_NewRemoteServer */
150 Client_NewRemoteUser( CLIENT *Introducer, char *Nick, int Hops, char *User, char *Hostname, int Token, char *Modes, char *Info, bool Idented )
152 /* Neuen Remote-Client erzeugen: Wrapper-Funktion fuer Client_New (). */
153 return Client_New( NONE, Introducer, NULL, CLIENT_USER, Nick, User, Hostname, Info, Hops, Token, Modes, Idented );
154 } /* Client_NewRemoteUser */
158 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, bool Idented )
162 assert( Idx >= NONE );
163 assert( Introducer != NULL );
164 assert( Hostname != NULL );
166 client = New_Client_Struct( );
167 if( ! client ) return NULL;
170 client->conn_id = Idx;
171 client->introducer = Introducer;
172 client->topserver = TopServer;
174 if( ID ) Client_SetID( client, ID );
175 if( User ) Client_SetUser( client, User, Idented );
176 if( Hostname ) Client_SetHostname( client, Hostname );
177 if( Info ) Client_SetInfo( client, Info );
179 client->token = Token;
180 if( Modes ) Client_SetModes( client, Modes );
181 if( Type == CLIENT_SERVER ) Generate_MyToken( client );
183 /* ist der User away? */
184 if( strchr( client->modes, 'a' )) strlcpy( client->away, DEFAULT_AWAY_MSG, sizeof( client->away ));
187 client->next = (POINTER *)My_Clients;
190 /* Adjust counters */
191 Adjust_Counters( client );
198 Client_Destroy( CLIENT *Client, char *LogMsg, char *FwdMsg, bool SendQuit )
200 /* Client entfernen. */
203 char msg[LINE_LEN], *txt;
205 assert( Client != NULL );
207 if( LogMsg ) txt = LogMsg;
209 if( ! txt ) txt = "Reason unknown.";
211 /* Netz-Split-Nachricht vorbereiten (noch nicht optimal) */
212 if( Client->type == CLIENT_SERVER ) snprintf( msg, sizeof( msg ), "%s: lost server %s", This_Server->id, Client->id );
218 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
220 /* der Client, der geloescht wird ist ein Server. Der Client, den wir gerade
221 * pruefen, ist ein Child von diesem und muss daher auch entfernt werden */
222 Client_Destroy( c, NULL, msg, false );
229 /* Wir haben den Client gefunden: entfernen */
230 if( last ) last->next = c->next;
231 else My_Clients = (CLIENT *)c->next;
233 if( c->type == CLIENT_USER )
235 if( c->conn_id != NONE )
237 /* Ein lokaler User */
238 Log( LOG_NOTICE, "User \"%s\" unregistered (connection %d): %s", Client_Mask( c ), c->conn_id, txt );
242 /* Alle andere Server informieren! */
243 if( FwdMsg ) IRC_WriteStrServersPrefix( NULL, c, "QUIT :%s", FwdMsg );
244 else IRC_WriteStrServersPrefix( NULL, c, "QUIT :" );
250 Log( LOG_DEBUG, "User \"%s\" unregistered: %s", Client_Mask( c ), txt );
254 /* Andere Server informieren, ausser denen, die "in
255 * Richtung dem liegen", auf dem der User registriert
256 * ist. Von denen haben wir das QUIT ja wohl bekommen. */
257 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "QUIT :%s", FwdMsg );
258 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "QUIT :" );
261 Channel_Quit( c, FwdMsg ? FwdMsg : c->id );
263 else if( c->type == CLIENT_SERVER )
265 if( c != This_Server )
267 if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
268 else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
271 /* andere Server informieren */
272 if( ! NGIRCd_SignalQuit )
274 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
275 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
280 if( c->conn_id != NONE )
282 if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
283 else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
287 if( c->id[0] ) Log( LOG_WARNING, "Unregistered unknown client \"%s\": %s", c->id, txt );
288 else Log( LOG_WARNING, "Unregistered unknown client: %s", c->id, txt );
296 c = (CLIENT *)c->next;
298 } /* Client_Destroy */
302 Client_DestroyNow( CLIENT *Client )
304 /* Destroy client structure immediately. This function is only
305 * intended for the connection layer to remove client structures
306 * of connections that can't be established! */
310 assert( Client != NULL );
318 /* Wir haben den Client gefunden: entfernen */
319 if( last ) last->next = c->next;
320 else My_Clients = (CLIENT *)c->next;
325 c = (CLIENT *)c->next;
327 } /* Client_DestroyNow */
331 Client_SetHostname( CLIENT *Client, char *Hostname )
333 /* Hostname eines Clients setzen */
335 assert( Client != NULL );
336 assert( Hostname != NULL );
338 strlcpy( Client->host, Hostname, sizeof( Client->host ));
339 } /* Client_SetHostname */
343 Client_SetID( CLIENT *Client, char *ID )
345 /* Hostname eines Clients setzen, Hash-Wert berechnen */
347 assert( Client != NULL );
348 assert( ID != NULL );
350 strlcpy( Client->id, ID, sizeof( Client->id ));
353 Client->hash = Hash( Client->id );
358 Client_SetUser( CLIENT *Client, char *User, bool Idented )
360 /* Username eines Clients setzen */
362 assert( Client != NULL );
363 assert( User != NULL );
365 if( Idented ) strlcpy( Client->user, User, sizeof( Client->user ));
368 Client->user[0] = '~';
369 strlcpy( Client->user + 1, User, sizeof( Client->user ) - 1 );
371 } /* Client_SetUser */
375 Client_SetInfo( CLIENT *Client, char *Info )
377 /* Hostname eines Clients setzen */
379 assert( Client != NULL );
380 assert( Info != NULL );
382 strlcpy( Client->info, Info, sizeof( Client->info ));
383 } /* Client_SetInfo */
387 Client_SetModes( CLIENT *Client, char *Modes )
389 /* Modes eines Clients setzen */
391 assert( Client != NULL );
392 assert( Modes != NULL );
394 strlcpy( Client->modes, Modes, sizeof( Client->modes ));
395 } /* Client_SetModes */
399 Client_SetFlags( CLIENT *Client, char *Flags )
401 /* Flags eines Clients setzen */
403 assert( Client != NULL );
404 assert( Flags != NULL );
406 strlcpy( Client->flags, Flags, sizeof( Client->flags ));
407 } /* Client_SetFlags */
411 Client_SetPassword( CLIENT *Client, char *Pwd )
413 /* Von einem Client geliefertes Passwort */
415 assert( Client != NULL );
416 assert( Pwd != NULL );
418 strlcpy( Client->pwd, Pwd, sizeof( Client->pwd ));
419 } /* Client_SetPassword */
423 Client_SetAway( CLIENT *Client, char *Txt )
425 /* Set AWAY reason of client */
427 assert( Client != NULL );
428 assert( Txt != NULL );
430 strlcpy( Client->away, Txt, sizeof( Client->away ));
431 Log( LOG_DEBUG, "User \"%s\" is away: %s", Client_Mask( Client ), Txt );
432 } /* Client_SetAway */
436 Client_SetType( CLIENT *Client, int Type )
438 assert( Client != NULL );
440 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
441 Adjust_Counters( Client );
442 } /* Client_SetType */
446 Client_SetHops( CLIENT *Client, int Hops )
448 assert( Client != NULL );
450 } /* Client_SetHops */
454 Client_SetToken( CLIENT *Client, int Token )
456 assert( Client != NULL );
457 Client->token = Token;
458 } /* Client_SetToken */
462 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
464 assert( Client != NULL );
465 assert( Introducer != NULL );
466 Client->introducer = Introducer;
467 } /* Client_SetIntroducer */
471 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
473 assert( Client != NULL );
474 Client->oper_by_me = OperByMe;
475 } /* Client_SetOperByMe */
479 Client_ModeAdd( CLIENT *Client, char Mode )
482 * If Client already alread had Mode, return false.
483 * If the Mode was newly set, return true.
488 assert( Client != NULL );
490 x[0] = Mode; x[1] = '\0';
491 if( ! strchr( Client->modes, x[0] ))
493 /* Client hat den Mode noch nicht -> setzen */
494 strlcat( Client->modes, x, sizeof( Client->modes ));
498 } /* Client_ModeAdd */
502 Client_ModeDel( CLIENT *Client, char Mode )
505 * If Mode was removed, return true.
506 * If Client did not have Mode, return false.
511 assert( Client != NULL );
513 x[0] = Mode; x[1] = '\0';
515 p = strchr( Client->modes, x[0] );
516 if( ! p ) return false;
518 /* Client has Mode -> delete */
525 } /* Client_ModeDel */
529 Client_GetFromConn( CONN_ID Idx )
531 /* return Client-Structure that belongs to the local Connection Idx gehoert.
532 * If none is found, return NULL.
542 if( c->conn_id == Idx ) return c;
543 c = (CLIENT *)c->next;
546 } /* Client_GetFromConn */
550 Client_Search( char *Nick )
552 /* return Client-Structure that has the corresponding Nick.
553 * If none is found, return NULL.
556 char search_id[CLIENT_ID_LEN], *ptr;
560 assert( Nick != NULL );
562 /* copy Nick and truncate hostmask if necessary */
563 strlcpy( search_id, Nick, sizeof( search_id ));
564 ptr = strchr( search_id, '!' );
565 if( ptr ) *ptr = '\0';
567 search_hash = Hash( search_id );
572 if( c->hash == search_hash )
574 /* lt. Hash-Wert: Treffer! */
575 if( strcasecmp( c->id, search_id ) == 0 ) return c;
577 c = (CLIENT *)c->next;
580 } /* Client_Search */
584 Client_GetFromToken( CLIENT *Client, int Token )
586 /* Client-Struktur, die den entsprechenden Introducer (=Client)
587 * und das gegebene Token hat, liefern. Wird keine gefunden,
588 * so wird NULL geliefert. */
592 assert( Client != NULL );
598 if(( c->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c->token == Token )) return c;
599 c = (CLIENT *)c->next;
602 } /* Client_GetFromToken */
606 Client_Type( CLIENT *Client )
608 assert( Client != NULL );
614 Client_Conn( CLIENT *Client )
616 assert( Client != NULL );
617 return Client->conn_id;
622 Client_ID( CLIENT *Client )
624 assert( Client != NULL );
627 if( Client->type == CLIENT_USER ) assert( strlen( Client->id ) < CLIENT_NICK_LEN );
630 if( Client->id[0] ) return Client->id;
636 Client_Info( CLIENT *Client )
638 assert( Client != NULL );
644 Client_User( CLIENT *Client )
646 assert( Client != NULL );
647 if( Client->user[0] ) return Client->user;
653 Client_Hostname( CLIENT *Client )
655 assert( Client != NULL );
657 } /* Client_Hostname */
661 Client_Password( CLIENT *Client )
663 assert( Client != NULL );
665 } /* Client_Password */
669 Client_Modes( CLIENT *Client )
671 assert( Client != NULL );
672 return Client->modes;
677 Client_Flags( CLIENT *Client )
679 assert( Client != NULL );
680 return Client->flags;
685 Client_OperByMe( CLIENT *Client )
687 assert( Client != NULL );
688 return Client->oper_by_me;
689 } /* Client_OperByMe */
693 Client_Hops( CLIENT *Client )
695 assert( Client != NULL );
701 Client_Token( CLIENT *Client )
703 assert( Client != NULL );
704 return Client->token;
709 Client_MyToken( CLIENT *Client )
711 assert( Client != NULL );
712 return Client->mytoken;
713 } /* Client_MyToken */
717 Client_NextHop( CLIENT *Client )
721 assert( Client != NULL );
724 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server )) c = c->introducer;
726 } /* Client_NextHop */
730 Client_Mask( CLIENT *Client )
732 /* Client-"ID" liefern, wie sie z.B. fuer
733 * Prefixe benoetigt wird. */
735 assert( Client != NULL );
737 if( Client->type == CLIENT_SERVER ) return Client->id;
739 snprintf( GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host );
745 Client_Introducer( CLIENT *Client )
747 assert( Client != NULL );
748 return Client->introducer;
749 } /* Client_Introducer */
753 Client_TopServer( CLIENT *Client )
755 assert( Client != NULL );
756 return Client->topserver;
757 } /* Client_TopServer */
761 Client_HasMode( CLIENT *Client, char Mode )
763 assert( Client != NULL );
764 return strchr( Client->modes, Mode ) != NULL;
765 } /* Client_HasMode */
769 Client_Away( CLIENT *Client )
771 /* AWAY-Text liefern */
773 assert( Client != NULL );
779 Client_CheckNick( CLIENT *Client, char *Nick )
781 /* Nick ueberpruefen */
783 assert( Client != NULL );
784 assert( Nick != NULL );
786 /* Nick ungueltig? */
787 if( ! Client_IsValidNick( Nick ))
789 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
793 /* Nick bereits vergeben? */
794 if( Client_Search( Nick ))
796 /* den Nick gibt es bereits */
797 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
802 } /* Client_CheckNick */
806 Client_CheckID( CLIENT *Client, char *ID )
808 /* Nick ueberpruefen */
810 char str[COMMAND_LEN];
813 assert( Client != NULL );
814 assert( Client->conn_id > NONE );
815 assert( ID != NULL );
818 if( strlen( ID ) > CLIENT_ID_LEN )
820 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), ID );
824 /* ID bereits vergeben? */
828 if( strcasecmp( c->id, ID ) == 0 )
830 /* die Server-ID gibt es bereits */
831 snprintf( str, sizeof( str ), "ID \"%s\" already registered", ID );
832 if( Client->conn_id != c->conn_id ) Log( LOG_ERR, "%s (on connection %d)!", str, c->conn_id );
833 else Log( LOG_ERR, "%s (via network)!", str );
834 Conn_Close( Client->conn_id, str, str, true);
837 c = (CLIENT *)c->next;
841 } /* Client_CheckID */
847 /* Ersten Client liefern. */
854 Client_Next( CLIENT *c )
856 /* Naechsten Client liefern. Existiert keiner,
857 * so wird NULL geliefert. */
860 return (CLIENT *)c->next;
865 Client_UserCount( void )
867 return Count( CLIENT_USER );
868 } /* Client_UserCount */
872 Client_ServiceCount( void )
874 return Count( CLIENT_SERVICE );;
875 } /* Client_ServiceCount */
879 Client_ServerCount( void )
881 return Count( CLIENT_SERVER );
882 } /* Client_ServerCount */
886 Client_MyUserCount( void )
888 return MyCount( CLIENT_USER );
889 } /* Client_MyUserCount */
893 Client_MyServiceCount( void )
895 return MyCount( CLIENT_SERVICE );
896 } /* Client_MyServiceCount */
900 Client_MyServerCount( void )
909 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
910 c = (CLIENT *)c->next;
913 } /* Client_MyServerCount */
917 Client_OperCount( void )
926 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
927 c = (CLIENT *)c->next;
930 } /* Client_OperCount */
934 Client_UnknownCount( void )
943 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
944 c = (CLIENT *)c->next;
947 } /* Client_UnknownCount */
951 Client_MaxUserCount( void )
954 } /* Client_MaxUserCount */
958 Client_MyMaxUserCount( void )
961 } /* Client_MyMaxUserCount */
965 Client_IsValidNick( char *Nick )
967 /* Ist der Nick gueltig? */
969 char *ptr, goodchars[20];
971 assert( Nick != NULL );
973 strcpy( goodchars, ";0123456789-" );
975 if( Nick[0] == '#' ) return false;
976 if( strchr( goodchars, Nick[0] )) return false;
977 if( strlen( Nick ) >= CLIENT_NICK_LEN ) return false;
982 if(( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return false;
983 if(( *ptr > '}' ) && ( ! strchr( goodchars, *ptr ))) return false;
988 } /* Client_IsValidNick */
992 Count( CLIENT_TYPE Type )
1001 if( c->type == Type ) cnt++;
1002 c = (CLIENT *)c->next;
1009 MyCount( CLIENT_TYPE Type )
1018 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1019 c = (CLIENT *)c->next;
1026 New_Client_Struct( void )
1028 /* Neue CLIENT-Struktur pre-initialisieren */
1032 c = (CLIENT *)malloc( sizeof( CLIENT ));
1035 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1039 memset( c, 0, sizeof ( CLIENT ));
1041 c->type = CLIENT_UNKNOWN;
1043 c->oper_by_me = false;
1053 Generate_MyToken( CLIENT *Client )
1062 if( c->mytoken == token )
1064 /* Das Token wurde bereits vergeben */
1069 else c = (CLIENT *)c->next;
1071 Client->mytoken = token;
1072 Log( LOG_DEBUG, "Assigned token %d to server \"%s\".", token, Client->id );
1073 } /* Generate_MyToken */
1077 Adjust_Counters( CLIENT *Client )
1081 assert( Client != NULL );
1083 if( Client->type != CLIENT_USER ) return;
1085 if( Client->conn_id != NONE )
1087 /* Local connection */
1088 count = Client_MyUserCount( );
1089 if( count > My_Max_Users ) My_Max_Users = count;
1091 count = Client_UserCount( );
1092 if( count > Max_Users ) Max_Users = count;
1093 } /* Adjust_Counters */