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.65.2.3 2003/01/08 23:13:45 alex Exp $";
41 #include "irc-write.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;
71 This_Server = New_Client_Struct( );
74 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
75 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
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 ) strcpy( This_Server->host, h->h_name );
91 Client_SetID( This_Server, Conf_ServerName );
92 Client_SetInfo( This_Server, Conf_ServerInfo );
94 My_Clients = This_Server;
104 if( NGIRCd_Restart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, FALSE );
105 else Client_Destroy( This_Server, "Server going down.", NULL, FALSE );
112 next = (CLIENT *)c->next;
116 if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
121 Client_ThisServer( VOID )
124 } /* Client_ThisServer */
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 */
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 */
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 */
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 )
156 assert( Idx >= NONE );
157 assert( Introducer != NULL );
158 assert( Hostname != NULL );
160 client = New_Client_Struct( );
161 if( ! client ) return NULL;
164 client->conn_id = Idx;
165 client->introducer = Introducer;
166 client->topserver = TopServer;
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 );
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' )) strcpy( client->away, DEFAULT_AWAY_MSG );
181 client->next = (POINTER *)My_Clients;
184 /* Adjust counters */
185 Adjust_Counters( client );
192 Client_Destroy( CLIENT *Client, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN SendQuit )
194 /* Client entfernen. */
197 CHAR msg[LINE_LEN], *txt;
199 assert( Client != NULL );
201 if( LogMsg ) txt = LogMsg;
203 if( ! txt ) txt = "Reason unknown.";
205 /* Netz-Split-Nachricht vorbereiten (noch nicht optimal) */
206 if( Client->type == CLIENT_SERVER ) sprintf( msg, "%s: lost server %s", This_Server->id, Client->id );
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 );
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 );
236 /* Alle andere Server informieren! */
237 if( FwdMsg ) IRC_WriteStrServersPrefix( NULL, c, "QUIT :%s", FwdMsg );
238 else IRC_WriteStrServersPrefix( NULL, c, "QUIT :" );
244 Log( LOG_DEBUG, "User \"%s\" unregistered: %s", Client_Mask( c ), txt );
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 */
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 );
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 );
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 );
290 c = (CLIENT *)c->next;
292 } /* Client_Destroy */
296 Client_SetHostname( CLIENT *Client, CHAR *Hostname )
298 /* Hostname eines Clients setzen */
300 assert( Client != NULL );
301 assert( Hostname != NULL );
303 strncpy( Client->host, Hostname, CLIENT_HOST_LEN - 1 );
304 Client->host[CLIENT_HOST_LEN - 1] = '\0';
305 } /* Client_SetHostname */
309 Client_SetID( CLIENT *Client, CHAR *ID )
311 /* Hostname eines Clients setzen, Hash-Wert berechnen */
313 assert( Client != NULL );
314 assert( ID != NULL );
316 strncpy( Client->id, ID, CLIENT_ID_LEN - 1 );
317 Client->id[CLIENT_ID_LEN - 1] = '\0';
320 Client->hash = Hash( Client->id );
325 Client_SetUser( CLIENT *Client, CHAR *User, BOOLEAN Idented )
327 /* Username eines Clients setzen */
329 assert( Client != NULL );
330 assert( User != NULL );
332 if( Idented ) strncpy( Client->user, User, CLIENT_USER_LEN - 1 );
335 Client->user[0] = '~';
336 strncpy( Client->user + 1, User, CLIENT_USER_LEN - 2 );
338 Client->user[CLIENT_USER_LEN - 1] = '\0';
339 } /* Client_SetUser */
343 Client_SetInfo( CLIENT *Client, CHAR *Info )
345 /* Hostname eines Clients setzen */
347 assert( Client != NULL );
348 assert( Info != NULL );
350 strncpy( Client->info, Info, CLIENT_INFO_LEN - 1 );
351 Client->info[CLIENT_INFO_LEN - 1] = '\0';
352 } /* Client_SetInfo */
356 Client_SetModes( CLIENT *Client, CHAR *Modes )
358 /* Modes eines Clients setzen */
360 assert( Client != NULL );
361 assert( Modes != NULL );
363 strncpy( Client->modes, Modes, CLIENT_MODE_LEN - 1 );
364 Client->modes[CLIENT_MODE_LEN - 1] = '\0';
365 } /* Client_SetModes */
369 Client_SetFlags( CLIENT *Client, CHAR *Flags )
371 /* Flags eines Clients setzen */
373 assert( Client != NULL );
374 assert( Flags != NULL );
376 strncpy( Client->flags, Flags, CLIENT_FLAGS_LEN - 1 );
377 Client->flags[CLIENT_FLAGS_LEN - 1] = '\0';
378 } /* Client_SetFlags */
382 Client_SetPassword( CLIENT *Client, CHAR *Pwd )
384 /* Von einem Client geliefertes Passwort */
386 assert( Client != NULL );
387 assert( Pwd != NULL );
389 strncpy( Client->pwd, Pwd, CLIENT_PASS_LEN - 1 );
390 Client->pwd[CLIENT_PASS_LEN - 1] = '\0';
391 } /* Client_SetPassword */
395 Client_SetAway( CLIENT *Client, CHAR *Txt )
397 /* Set AWAY reason of client */
399 assert( Client != NULL );
400 assert( Txt != NULL );
402 strncpy( Client->away, Txt, CLIENT_AWAY_LEN - 1 );
403 Client->away[CLIENT_AWAY_LEN - 1] = '\0';
404 Log( LOG_DEBUG, "User \"%s\" is away: %s", Client_Mask( Client ), Txt );
405 } /* Client_SetAway */
409 Client_SetType( CLIENT *Client, INT Type )
411 assert( Client != NULL );
413 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
414 Adjust_Counters( Client );
415 } /* Client_SetType */
419 Client_SetHops( CLIENT *Client, INT Hops )
421 assert( Client != NULL );
423 } /* Client_SetHops */
427 Client_SetToken( CLIENT *Client, INT Token )
429 assert( Client != NULL );
430 Client->token = Token;
431 } /* Client_SetToken */
435 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
437 assert( Client != NULL );
438 assert( Introducer != NULL );
439 Client->introducer = Introducer;
440 } /* Client_SetIntroducer */
444 Client_SetOperByMe( CLIENT *Client, BOOLEAN OperByMe )
446 assert( Client != NULL );
447 Client->oper_by_me = OperByMe;
448 } /* Client_SetOperByMe */
452 Client_ModeAdd( CLIENT *Client, CHAR Mode )
454 /* Mode soll gesetzt werden. TRUE wird geliefert, wenn der
455 * Mode neu gesetzt wurde, FALSE, wenn der Client den Mode
460 assert( Client != NULL );
462 x[0] = Mode; x[1] = '\0';
463 if( ! strchr( Client->modes, x[0] ))
465 /* Client hat den Mode noch nicht -> setzen */
466 strcat( Client->modes, x );
470 } /* Client_ModeAdd */
474 Client_ModeDel( CLIENT *Client, CHAR Mode )
476 /* Mode soll geloescht werden. TRUE wird geliefert, wenn der
477 * Mode entfernt wurde, FALSE, wenn der Client den Mode
478 * ueberhaupt nicht hatte. */
482 assert( Client != NULL );
484 x[0] = Mode; x[1] = '\0';
486 p = strchr( Client->modes, x[0] );
487 if( ! p ) return FALSE;
489 /* Client hat den Mode -> loeschen */
496 } /* Client_ModeDel */
500 Client_GetFromConn( CONN_ID Idx )
502 /* Client-Struktur, die zur lokalen Verbindung Idx gehoert,
503 * liefern. Wird keine gefunden, so wird NULL geliefert. */
512 if( c->conn_id == Idx ) return c;
513 c = (CLIENT *)c->next;
516 } /* Client_GetFromConn */
520 Client_Search( CHAR *Nick )
522 /* Client-Struktur, die den entsprechenden Nick hat, liefern.
523 * Wird keine gefunden, so wird NULL geliefert. */
525 CHAR search_id[CLIENT_ID_LEN], *ptr;
529 assert( Nick != NULL );
531 /* Nick kopieren und ggf. Host-Mask abschneiden */
532 strncpy( search_id, Nick, CLIENT_ID_LEN - 1 );
533 search_id[CLIENT_ID_LEN - 1] = '\0';
534 ptr = strchr( search_id, '!' );
535 if( ptr ) *ptr = '\0';
537 search_hash = Hash( search_id );
542 if( c->hash == search_hash )
544 /* lt. Hash-Wert: Treffer! */
545 if( strcasecmp( c->id, search_id ) == 0 ) return c;
547 c = (CLIENT *)c->next;
550 } /* Client_Search */
554 Client_GetFromToken( CLIENT *Client, INT Token )
556 /* Client-Struktur, die den entsprechenden Introducer (=Client)
557 * und das gegebene Token hat, liefern. Wird keine gefunden,
558 * so wird NULL geliefert. */
562 assert( Client != NULL );
568 if(( c->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c->token == Token )) return c;
569 c = (CLIENT *)c->next;
572 } /* Client_GetFromToken */
576 Client_Type( CLIENT *Client )
578 assert( Client != NULL );
584 Client_Conn( CLIENT *Client )
586 assert( Client != NULL );
587 return Client->conn_id;
592 Client_ID( CLIENT *Client )
594 assert( Client != NULL );
597 if( Client->type == CLIENT_USER ) assert( strlen( Client->id ) < CLIENT_NICK_LEN );
600 if( Client->id[0] ) return Client->id;
606 Client_Info( CLIENT *Client )
608 assert( Client != NULL );
614 Client_User( CLIENT *Client )
616 assert( Client != NULL );
617 if( Client->user[0] ) return Client->user;
623 Client_Hostname( CLIENT *Client )
625 assert( Client != NULL );
627 } /* Client_Hostname */
631 Client_Password( CLIENT *Client )
633 assert( Client != NULL );
635 } /* Client_Password */
639 Client_Modes( CLIENT *Client )
641 assert( Client != NULL );
642 return Client->modes;
647 Client_Flags( CLIENT *Client )
649 assert( Client != NULL );
650 return Client->flags;
655 Client_OperByMe( CLIENT *Client )
657 assert( Client != NULL );
658 return Client->oper_by_me;
659 } /* Client_OperByMe */
663 Client_Hops( CLIENT *Client )
665 assert( Client != NULL );
671 Client_Token( CLIENT *Client )
673 assert( Client != NULL );
674 return Client->token;
679 Client_MyToken( CLIENT *Client )
681 assert( Client != NULL );
682 return Client->mytoken;
683 } /* Client_MyToken */
687 Client_NextHop( CLIENT *Client )
691 assert( Client != NULL );
694 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server )) c = c->introducer;
696 } /* Client_NextHop */
700 Client_Mask( CLIENT *Client )
702 /* Client-"ID" liefern, wie sie z.B. fuer
703 * Prefixe benoetigt wird. */
705 assert( Client != NULL );
707 if( Client->type == CLIENT_SERVER ) return Client->id;
709 snprintf( GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host );
715 Client_Introducer( CLIENT *Client )
717 assert( Client != NULL );
718 return Client->introducer;
719 } /* Client_Introducer */
723 Client_TopServer( CLIENT *Client )
725 assert( Client != NULL );
726 return Client->topserver;
727 } /* Client_TopServer */
731 Client_HasMode( CLIENT *Client, CHAR Mode )
733 assert( Client != NULL );
734 return strchr( Client->modes, Mode ) != NULL;
735 } /* Client_HasMode */
739 Client_Away( CLIENT *Client )
741 /* AWAY-Text liefern */
743 assert( Client != NULL );
749 Client_CheckNick( CLIENT *Client, CHAR *Nick )
751 /* Nick ueberpruefen */
753 assert( Client != NULL );
754 assert( Nick != NULL );
756 /* Nick ungueltig? */
757 if( ! Client_IsValidNick( Nick ))
759 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
763 /* Nick bereits vergeben? */
764 if( Client_Search( Nick ))
766 /* den Nick gibt es bereits */
767 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
772 } /* Client_CheckNick */
776 Client_CheckID( CLIENT *Client, CHAR *ID )
778 /* Nick ueberpruefen */
780 CHAR str[COMMAND_LEN];
783 assert( Client != NULL );
784 assert( Client->conn_id > NONE );
785 assert( ID != NULL );
788 if( strlen( ID ) > CLIENT_ID_LEN )
790 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), ID );
794 /* ID bereits vergeben? */
798 if( strcasecmp( c->id, ID ) == 0 )
800 /* die Server-ID gibt es bereits */
801 sprintf( str, "ID \"%s\" already registered", ID );
802 if( Client->conn_id != c->conn_id ) Log( LOG_ERR, "%s (on connection %d)!", str, c->conn_id );
803 else Log( LOG_ERR, "%s (via network)!", str );
804 Conn_Close( Client->conn_id, str, str, TRUE );
807 c = (CLIENT *)c->next;
811 } /* Client_CheckID */
817 /* Ersten Client liefern. */
824 Client_Next( CLIENT *c )
826 /* Naechsten Client liefern. Existiert keiner,
827 * so wird NULL geliefert. */
830 return (CLIENT *)c->next;
835 Client_UserCount( VOID )
837 return Count( CLIENT_USER );
838 } /* Client_UserCount */
842 Client_ServiceCount( VOID )
844 return Count( CLIENT_SERVICE );;
845 } /* Client_ServiceCount */
849 Client_ServerCount( VOID )
851 return Count( CLIENT_SERVER );
852 } /* Client_ServerCount */
856 Client_MyUserCount( VOID )
858 return MyCount( CLIENT_USER );
859 } /* Client_MyUserCount */
863 Client_MyServiceCount( VOID )
865 return MyCount( CLIENT_SERVICE );
866 } /* Client_MyServiceCount */
870 Client_MyServerCount( VOID )
879 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
880 c = (CLIENT *)c->next;
883 } /* Client_MyServerCount */
887 Client_OperCount( VOID )
896 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
897 c = (CLIENT *)c->next;
900 } /* Client_OperCount */
904 Client_UnknownCount( VOID )
913 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
914 c = (CLIENT *)c->next;
917 } /* Client_UnknownCount */
921 Client_MaxUserCount( VOID )
924 } /* Client_MaxUserCount */
928 Client_MyMaxUserCount( VOID )
931 } /* Client_MyMaxUserCount */
935 Client_IsValidNick( CHAR *Nick )
937 /* Ist der Nick gueltig? */
939 CHAR *ptr, goodchars[20];
941 assert( Nick != NULL );
943 strcpy( goodchars, ";0123456789-" );
945 if( Nick[0] == '#' ) return FALSE;
946 if( strchr( goodchars, Nick[0] )) return FALSE;
947 if( strlen( Nick ) >= CLIENT_NICK_LEN ) return FALSE;
952 if(( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return FALSE;
953 if(( *ptr > '}' ) && ( ! strchr( goodchars, *ptr ))) return FALSE;
958 } /* Client_IsValidNick */
962 Count( CLIENT_TYPE Type )
971 if( c->type == Type ) cnt++;
972 c = (CLIENT *)c->next;
979 MyCount( CLIENT_TYPE Type )
988 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
989 c = (CLIENT *)c->next;
996 New_Client_Struct( VOID )
998 /* Neue CLIENT-Struktur pre-initialisieren */
1002 c = malloc( sizeof( CLIENT ));
1005 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1011 c->type = CLIENT_UNKNOWN;
1013 c->introducer = NULL;
1014 c->topserver = NULL;
1015 strcpy( c->id, "" );
1016 strcpy( c->pwd, "" );
1017 strcpy( c->host, "" );
1018 strcpy( c->user, "" );
1019 strcpy( c->info, "" );
1020 strcpy( c->modes, "" );
1021 c->oper_by_me = FALSE;
1025 strcpy( c->away, "" );
1026 strcpy( c->flags, "" );
1033 Generate_MyToken( CLIENT *Client )
1042 if( c->mytoken == token )
1044 /* Das Token wurde bereits vergeben */
1049 else c = (CLIENT *)c->next;
1051 Client->mytoken = token;
1052 Log( LOG_DEBUG, "Assigned token %d to server \"%s\".", token, Client->id );
1053 } /* Generate_MyToken */
1057 Adjust_Counters( CLIENT *Client )
1061 assert( Client != NULL );
1063 if( Client->type != CLIENT_USER ) return;
1065 if( Client->conn_id != NONE )
1067 /* Local connection */
1068 count = Client_MyUserCount( );
1069 if( count > My_Max_Users ) My_Max_Users = count;
1071 count = Client_UserCount( );
1072 if( count > Max_Users ) Max_Users = count;
1073 } /* Adjust_Counters */