2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 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.
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 static CLIENT *This_Server, *My_Clients;
53 static WHOWAS My_Whowas[MAX_WHOWAS];
54 static int Last_Whowas = -1;
55 static long Max_Users, My_Max_Users;
58 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
59 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
61 static CLIENT *New_Client_Struct PARAMS(( void ));
62 static void Generate_MyToken PARAMS(( CLIENT *Client ));
63 static void Adjust_Counters PARAMS(( CLIENT *Client ));
65 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
66 CLIENT *TopServer, int Type, const char *ID,
67 const char *User, const char *Hostname, const char *Info,
68 int Hops, int Token, const char *Modes,
71 static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
80 This_Server = New_Client_Struct( );
83 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
84 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
88 /* Client-Struktur dieses Servers */
89 This_Server->next = NULL;
90 This_Server->type = CLIENT_SERVER;
91 This_Server->conn_id = NONE;
92 This_Server->introducer = This_Server;
93 This_Server->mytoken = 1;
94 This_Server->hops = 0;
96 gethostname( This_Server->host, CLIENT_HOST_LEN );
98 h = gethostbyname( This_Server->host );
99 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
101 Client_SetID( This_Server, Conf_ServerName );
102 Client_SetInfo( This_Server, Conf_ServerInfo );
104 My_Clients = This_Server;
106 memset( &My_Whowas, 0, sizeof( My_Whowas ));
116 if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
117 else Client_Destroy( This_Server, "Server going down.", NULL, false );
124 next = (CLIENT *)c->next;
128 if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
133 Client_ThisServer( void )
136 } /* Client_ThisServer */
140 * Initialize new local client; wrapper function for Init_New_Client().
141 * @return New CLIENT structure.
144 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
146 return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
147 Hostname, NULL, 0, 0, NULL, Idented);
148 } /* Client_NewLocal */
152 * Initialize new remote server; wrapper function for Init_New_Client().
153 * @return New CLIENT structure.
156 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
157 int Hops, int Token, const char *Info, bool Idented)
159 return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
160 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
161 } /* Client_NewRemoteServer */
165 * Initialize new remote client; wrapper function for Init_New_Client().
166 * @return New CLIENT structure.
169 Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
170 const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
172 return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
173 User, Hostname, Info, Hops, Token, Modes, Idented);
174 } /* Client_NewRemoteUser */
178 * Initialize new client and set up the given parameters like client type,
179 * user name, host name, introducing server etc. ...
180 * @return New CLIENT structure.
183 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
184 int Type, const char *ID, const char *User, const char *Hostname, const char *Info, int Hops,
185 int Token, const char *Modes, bool Idented)
189 assert( Idx >= NONE );
190 assert( Introducer != NULL );
191 assert( Hostname != NULL );
193 client = New_Client_Struct( );
194 if( ! client ) return NULL;
197 client->starttime = time(NULL);
198 client->conn_id = Idx;
199 client->introducer = Introducer;
200 client->topserver = TopServer;
202 if( ID ) Client_SetID( client, ID );
203 if( User ) Client_SetUser( client, User, Idented );
204 if( Hostname ) Client_SetHostname( client, Hostname );
205 if( Info ) Client_SetInfo( client, Info );
207 client->token = Token;
208 if( Modes ) Client_SetModes( client, Modes );
209 if( Type == CLIENT_SERVER ) Generate_MyToken( client );
211 if( strchr( client->modes, 'a' ))
212 strlcpy( client->away, DEFAULT_AWAY_MSG, sizeof( client->away ));
215 client->next = (POINTER *)My_Clients;
218 /* Adjust counters */
219 Adjust_Counters( client );
222 } /* Init_New_Client */
226 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
228 /* Client entfernen. */
234 assert( Client != NULL );
236 if( LogMsg ) txt = LogMsg;
238 if( ! txt ) txt = "Reason unknown.";
240 /* Netz-Split-Nachricht vorbereiten (noch nicht optimal) */
241 if( Client->type == CLIENT_SERVER ) {
242 strlcpy(msg, This_Server->id, sizeof (msg));
243 strlcat(msg, " ", sizeof (msg));
244 strlcat(msg, Client->id, sizeof (msg));
251 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
253 /* der Client, der geloescht wird ist ein Server. Der Client, den wir gerade
254 * pruefen, ist ein Child von diesem und muss daher auch entfernt werden */
255 Client_Destroy( c, NULL, msg, false );
262 /* Wir haben den Client gefunden: entfernen */
263 if( last ) last->next = c->next;
264 else My_Clients = (CLIENT *)c->next;
266 if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
267 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
268 else if( c->type == CLIENT_SERVER )
270 if( c != This_Server )
272 if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
273 else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
276 /* andere Server informieren */
277 if( ! NGIRCd_SignalQuit )
279 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
280 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
285 if( c->conn_id != NONE )
287 if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
288 else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
290 Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
291 c->id[0] ? c->id : "(No Nick)", txt );
299 c = (CLIENT *)c->next;
301 } /* Client_Destroy */
305 Client_SetHostname( CLIENT *Client, const char *Hostname )
307 assert( Client != NULL );
308 assert( Hostname != NULL );
310 strlcpy( Client->host, Hostname, sizeof( Client->host ));
311 } /* Client_SetHostname */
315 Client_SetID( CLIENT *Client, const char *ID )
317 assert( Client != NULL );
318 assert( ID != NULL );
320 strlcpy( Client->id, ID, sizeof( Client->id ));
323 Client->hash = Hash( Client->id );
328 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
330 /* set clients username */
332 assert( Client != NULL );
333 assert( User != NULL );
336 strlcpy(Client->user, User, sizeof(Client->user));
338 Client->user[0] = '~';
339 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
341 } /* Client_SetUser */
345 Client_SetInfo( CLIENT *Client, const char *Info )
347 /* set client hostname */
349 assert( Client != NULL );
350 assert( Info != NULL );
352 strlcpy(Client->info, Info, sizeof(Client->info));
353 } /* Client_SetInfo */
357 Client_SetModes( CLIENT *Client, const char *Modes )
359 assert( Client != NULL );
360 assert( Modes != NULL );
362 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
363 } /* Client_SetModes */
367 Client_SetFlags( CLIENT *Client, const char *Flags )
369 assert( Client != NULL );
370 assert( Flags != NULL );
372 strlcpy(Client->flags, Flags, sizeof(Client->flags));
373 } /* Client_SetFlags */
377 Client_SetPassword( CLIENT *Client, const char *Pwd )
379 /* set password sent by client */
381 assert( Client != NULL );
382 assert( Pwd != NULL );
384 strlcpy(Client->pwd, Pwd, sizeof(Client->pwd));
385 } /* Client_SetPassword */
389 Client_SetAway( CLIENT *Client, const char *Txt )
391 /* Set AWAY reason of client */
393 assert( Client != NULL );
394 assert( Txt != NULL );
396 strlcpy( Client->away, Txt, sizeof( Client->away ));
397 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
398 Client_Mask(Client), Txt);
399 } /* Client_SetAway */
403 Client_SetType( CLIENT *Client, int Type )
405 assert( Client != NULL );
407 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
408 Adjust_Counters( Client );
409 } /* Client_SetType */
413 Client_SetHops( CLIENT *Client, int Hops )
415 assert( Client != NULL );
417 } /* Client_SetHops */
421 Client_SetToken( CLIENT *Client, int Token )
423 assert( Client != NULL );
424 Client->token = Token;
425 } /* Client_SetToken */
429 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
431 assert( Client != NULL );
432 assert( Introducer != NULL );
433 Client->introducer = Introducer;
434 } /* Client_SetIntroducer */
438 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
440 assert( Client != NULL );
441 Client->oper_by_me = OperByMe;
442 } /* Client_SetOperByMe */
446 Client_ModeAdd( CLIENT *Client, char Mode )
449 * If Client already alread had Mode, return false.
450 * If the Mode was newly set, return true.
455 assert( Client != NULL );
457 x[0] = Mode; x[1] = '\0';
458 if (!strchr( Client->modes, x[0])) {
459 strlcat( Client->modes, x, sizeof( Client->modes ));
463 } /* Client_ModeAdd */
467 Client_ModeDel( CLIENT *Client, char Mode )
470 * If Mode was removed, return true.
471 * If Client did not have Mode, return false.
476 assert( Client != NULL );
478 x[0] = Mode; x[1] = '\0';
480 p = strchr( Client->modes, x[0] );
481 if( ! p ) return false;
483 /* Client has Mode -> delete */
490 } /* Client_ModeDel */
494 Client_Search( const char *Nick )
496 /* return Client-Structure that has the corresponding Nick.
497 * If none is found, return NULL.
500 char search_id[CLIENT_ID_LEN], *ptr;
504 assert( Nick != NULL );
506 /* copy Nick and truncate hostmask if necessary */
507 strlcpy( search_id, Nick, sizeof( search_id ));
508 ptr = strchr( search_id, '!' );
509 if( ptr ) *ptr = '\0';
511 search_hash = Hash(search_id);
515 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
517 c = (CLIENT *)c->next;
520 } /* Client_Search */
524 Client_GetFromToken( CLIENT *Client, int Token )
526 /* Client-Struktur, die den entsprechenden Introducer (=Client)
527 * und das gegebene Token hat, liefern. Wird keine gefunden,
528 * so wird NULL geliefert. */
532 assert( Client != NULL );
537 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
540 c = (CLIENT *)c->next;
543 } /* Client_GetFromToken */
547 Client_Type( CLIENT *Client )
549 assert( Client != NULL );
555 Client_Conn( CLIENT *Client )
557 assert( Client != NULL );
558 return Client->conn_id;
563 Client_ID( CLIENT *Client )
565 assert( Client != NULL );
568 if(Client->type == CLIENT_USER)
569 assert(strlen(Client->id) < Conf_MaxNickLength);
572 if( Client->id[0] ) return Client->id;
578 Client_Info( CLIENT *Client )
580 assert( Client != NULL );
586 Client_User( CLIENT *Client )
588 assert( Client != NULL );
589 return Client->user[0] ? Client->user : "~";
594 Client_Hostname( CLIENT *Client )
596 assert( Client != NULL );
598 } /* Client_Hostname */
602 Client_Password( CLIENT *Client )
604 assert( Client != NULL );
606 } /* Client_Password */
610 Client_Modes( CLIENT *Client )
612 assert( Client != NULL );
613 return Client->modes;
618 Client_Flags( CLIENT *Client )
620 assert( Client != NULL );
621 return Client->flags;
626 Client_OperByMe( CLIENT *Client )
628 assert( Client != NULL );
629 return Client->oper_by_me;
630 } /* Client_OperByMe */
634 Client_Hops( CLIENT *Client )
636 assert( Client != NULL );
642 Client_Token( CLIENT *Client )
644 assert( Client != NULL );
645 return Client->token;
650 Client_MyToken( CLIENT *Client )
652 assert( Client != NULL );
653 return Client->mytoken;
654 } /* Client_MyToken */
658 Client_NextHop( CLIENT *Client )
662 assert( Client != NULL );
665 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
669 } /* Client_NextHop */
673 * return Client-ID ("client!user@host"), this ID is needed for e.g.
674 * prefixes. Returnes pointer to static buffer.
677 Client_Mask( CLIENT *Client )
679 static char GetID_Buffer[GETID_LEN];
681 assert( Client != NULL );
683 if( Client->type == CLIENT_SERVER ) return Client->id;
685 snprintf(GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host);
691 Client_Introducer( CLIENT *Client )
693 assert( Client != NULL );
694 return Client->introducer;
695 } /* Client_Introducer */
699 Client_TopServer( CLIENT *Client )
701 assert( Client != NULL );
702 return Client->topserver;
703 } /* Client_TopServer */
707 Client_HasMode( CLIENT *Client, char Mode )
709 assert( Client != NULL );
710 return strchr( Client->modes, Mode ) != NULL;
711 } /* Client_HasMode */
715 Client_Away( CLIENT *Client )
717 assert( Client != NULL );
723 Client_CheckNick( CLIENT *Client, char *Nick )
725 assert( Client != NULL );
726 assert( Nick != NULL );
728 if (! Client_IsValidNick( Nick ))
730 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
734 /* Nick bereits vergeben? */
735 if( Client_Search( Nick ))
737 /* den Nick gibt es bereits */
738 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
743 } /* Client_CheckNick */
747 Client_CheckID( CLIENT *Client, char *ID )
749 char str[COMMAND_LEN];
752 assert( Client != NULL );
753 assert( Client->conn_id > NONE );
754 assert( ID != NULL );
757 if (strlen(ID) > CLIENT_ID_LEN) {
758 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
762 /* does ID already exist? */
765 if (strcasecmp(c->id, ID) == 0) {
766 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
767 if (Client->conn_id != c->conn_id)
768 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
770 Log(LOG_ERR, "%s (via network)!", str);
771 Conn_Close(Client->conn_id, str, str, true);
774 c = (CLIENT *)c->next;
778 } /* Client_CheckID */
789 Client_Next( CLIENT *c )
792 return (CLIENT *)c->next;
797 Client_UserCount( void )
799 return Count( CLIENT_USER );
800 } /* Client_UserCount */
804 Client_ServiceCount( void )
806 return Count( CLIENT_SERVICE );;
807 } /* Client_ServiceCount */
811 Client_ServerCount( void )
813 return Count( CLIENT_SERVER );
814 } /* Client_ServerCount */
818 Client_MyUserCount( void )
820 return MyCount( CLIENT_USER );
821 } /* Client_MyUserCount */
825 Client_MyServiceCount( void )
827 return MyCount( CLIENT_SERVICE );
828 } /* Client_MyServiceCount */
832 Client_MyServerCount( void )
835 unsigned long cnt = 0;
840 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
841 c = (CLIENT *)c->next;
844 } /* Client_MyServerCount */
848 Client_OperCount( void )
851 unsigned long cnt = 0;
856 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
857 c = (CLIENT *)c->next;
860 } /* Client_OperCount */
864 Client_UnknownCount( void )
867 unsigned long cnt = 0;
872 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
873 c = (CLIENT *)c->next;
877 } /* Client_UnknownCount */
881 Client_MaxUserCount( void )
884 } /* Client_MaxUserCount */
888 Client_MyMaxUserCount( void )
891 } /* Client_MyMaxUserCount */
895 Client_IsValidNick( const char *Nick )
898 static const char goodchars[] = ";0123456789-";
900 assert( Nick != NULL );
902 if( Nick[0] == '#' ) return false;
903 if( strchr( goodchars, Nick[0] )) return false;
904 if( strlen( Nick ) >= Conf_MaxNickLength) return false;
909 if (( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return false;
910 if ( *ptr > '}' ) return false;
915 } /* Client_IsValidNick */
919 * Return pointer to "My_Whowas" structure.
922 Client_GetWhowas( void )
925 } /* Client_GetWhowas */
928 * Return the index of the last used WHOWAS entry.
931 Client_GetLastWhowasIndex( void )
934 } /* Client_GetLastWhowasIndex */
938 * Get the start time of this client.
939 * The result is the start time in seconds since 1970-01-01, as reported
940 * by the C function time(NULL).
943 Client_StartTime(CLIENT *Client)
945 assert( Client != NULL );
946 return Client->starttime;
947 } /* Client_Uptime */
951 Count( CLIENT_TYPE Type )
954 unsigned long cnt = 0;
959 if( c->type == Type ) cnt++;
960 c = (CLIENT *)c->next;
967 MyCount( CLIENT_TYPE Type )
970 unsigned long cnt = 0;
975 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
976 c = (CLIENT *)c->next;
983 New_Client_Struct( void )
985 /* Neue CLIENT-Struktur pre-initialisieren */
989 c = (CLIENT *)malloc( sizeof( CLIENT ));
992 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
996 memset( c, 0, sizeof ( CLIENT ));
998 c->type = CLIENT_UNKNOWN;
1000 c->oper_by_me = false;
1010 Generate_MyToken( CLIENT *Client )
1019 if( c->mytoken == token )
1021 /* Das Token wurde bereits vergeben */
1026 else c = (CLIENT *)c->next;
1028 Client->mytoken = token;
1029 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1030 } /* Generate_MyToken */
1034 Adjust_Counters( CLIENT *Client )
1038 assert( Client != NULL );
1040 if( Client->type != CLIENT_USER ) return;
1042 if( Client->conn_id != NONE )
1044 /* Local connection */
1045 count = Client_MyUserCount( );
1046 if( count > My_Max_Users ) My_Max_Users = count;
1048 count = Client_UserCount( );
1049 if( count > Max_Users ) Max_Users = count;
1050 } /* Adjust_Counters */
1054 * Register client in My_Whowas structure for further recall by WHOWAS.
1055 * Note: Only clients that have been connected at least 30 seconds will be
1056 * registered to prevent automated IRC bots to "destroy" a nice server
1060 Client_RegisterWhowas( CLIENT *Client )
1065 assert( Client != NULL );
1068 /* Don't register clients that were connected less than 30 seconds. */
1069 if( now - Client->starttime < 30 )
1072 slot = Last_Whowas + 1;
1073 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1076 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1079 My_Whowas[slot].time = now;
1080 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1081 sizeof( My_Whowas[slot].id ));
1082 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1083 sizeof( My_Whowas[slot].user ));
1084 strlcpy( My_Whowas[slot].host, Client_Hostname( Client ),
1085 sizeof( My_Whowas[slot].host ));
1086 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1087 sizeof( My_Whowas[slot].info ));
1088 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1089 sizeof( My_Whowas[slot].server ));
1092 } /* Client_RegisterWhowas */
1096 Client_TypeText(CLIENT *Client)
1098 assert(Client != NULL);
1099 switch (Client_Type(Client)) {
1103 case CLIENT_SERVICE:
1112 } /* Client_TypeText */
1116 * Destroy user or service client.
1119 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1121 if(Client->conn_id != NONE) {
1122 /* Local (directly connected) client */
1124 "%s \"%s\" unregistered (connection %d): %s",
1125 Client_TypeText(Client), Client_Mask(Client),
1126 Client->conn_id, Txt);
1129 /* Inforam all the other servers */
1131 IRC_WriteStrServersPrefix(NULL,
1132 Client, "QUIT :%s", FwdMsg );
1134 IRC_WriteStrServersPrefix(NULL,
1139 LogDebug("%s \"%s\" unregistered: %s",
1140 Client_TypeText(Client), Client_Mask(Client), Txt);
1143 /* Inform all the other servers, but the ones in the
1144 * direction we got the QUIT from */
1146 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1147 Client, "QUIT :%s", FwdMsg );
1149 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1154 /* Unregister client from channels */
1155 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1157 /* Register client in My_Whowas structure */
1158 Client_RegisterWhowas(Client);
1159 } /* Destroy_UserOrService */