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;
196 client->starttime = time(NULL);
197 client->conn_id = Idx;
198 client->introducer = Introducer;
199 client->topserver = TopServer;
201 if( ID ) Client_SetID( client, ID );
202 if( User ) Client_SetUser( client, User, Idented );
203 if( Hostname ) Client_SetHostname( client, Hostname );
204 if( Info ) Client_SetInfo( client, Info );
206 client->token = Token;
207 if( Modes ) Client_SetModes( client, Modes );
208 if( Type == CLIENT_SERVER ) Generate_MyToken( client );
210 if( strchr( client->modes, 'a' ))
211 strlcpy( client->away, DEFAULT_AWAY_MSG, sizeof( client->away ));
213 client->next = (POINTER *)My_Clients;
216 Adjust_Counters( client );
219 } /* Init_New_Client */
223 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
225 /* remove a client */
231 assert( Client != NULL );
233 if( LogMsg ) txt = LogMsg;
235 if( ! txt ) txt = "Reason unknown.";
237 /* netsplit message */
238 if( Client->type == CLIENT_SERVER ) {
239 strlcpy(msg, This_Server->id, sizeof (msg));
240 strlcat(msg, " ", sizeof (msg));
241 strlcat(msg, Client->id, sizeof (msg));
248 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
251 * The client that is about to be removed is a server,
252 * the client we are checking right now is a child of that
253 * server and thus has to be removed, too.
255 * Call Client_Destroy() recursively with the server as the
256 * new "object to be removed". This starts the cycle again, until
257 * all servers that are linked via the original server have been
260 Client_Destroy( c, NULL, msg, false );
267 /* found the client: remove it */
268 if( last ) last->next = c->next;
269 else My_Clients = (CLIENT *)c->next;
271 if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
272 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
273 else if( c->type == CLIENT_SERVER )
275 if( c != This_Server )
277 if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
278 else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
281 /* inform other servers */
282 if( ! NGIRCd_SignalQuit )
284 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
285 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
290 if( c->conn_id != NONE )
292 if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
293 else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
295 Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
296 c->id[0] ? c->id : "(No Nick)", txt );
304 c = (CLIENT *)c->next;
306 } /* Client_Destroy */
310 Client_SetHostname( CLIENT *Client, const char *Hostname )
312 assert( Client != NULL );
313 assert( Hostname != NULL );
315 strlcpy( Client->host, Hostname, sizeof( Client->host ));
316 } /* Client_SetHostname */
320 Client_SetID( CLIENT *Client, const char *ID )
322 assert( Client != NULL );
323 assert( ID != NULL );
325 strlcpy( Client->id, ID, sizeof( Client->id ));
328 Client->hash = Hash( Client->id );
333 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
335 /* set clients username */
337 assert( Client != NULL );
338 assert( User != NULL );
341 strlcpy(Client->user, User, sizeof(Client->user));
343 Client->user[0] = '~';
344 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
346 } /* Client_SetUser */
350 Client_SetInfo( CLIENT *Client, const char *Info )
352 /* set client hostname */
354 assert( Client != NULL );
355 assert( Info != NULL );
357 strlcpy(Client->info, Info, sizeof(Client->info));
358 } /* Client_SetInfo */
362 Client_SetModes( CLIENT *Client, const char *Modes )
364 assert( Client != NULL );
365 assert( Modes != NULL );
367 strlcpy(Client->modes, Modes, sizeof( Client->modes ));
368 } /* Client_SetModes */
372 Client_SetFlags( CLIENT *Client, const char *Flags )
374 assert( Client != NULL );
375 assert( Flags != NULL );
377 strlcpy(Client->flags, Flags, sizeof(Client->flags));
378 } /* Client_SetFlags */
382 Client_SetPassword( CLIENT *Client, const char *Pwd )
384 /* set password sent by client */
386 assert( Client != NULL );
387 assert( Pwd != NULL );
389 strlcpy(Client->pwd, Pwd, sizeof(Client->pwd));
390 } /* Client_SetPassword */
394 Client_SetAway( CLIENT *Client, const char *Txt )
396 /* Set AWAY reason of client */
398 assert( Client != NULL );
399 assert( Txt != NULL );
401 strlcpy( Client->away, Txt, sizeof( Client->away ));
402 LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
403 Client_Mask(Client), Txt);
404 } /* Client_SetAway */
408 Client_SetType( CLIENT *Client, int Type )
410 assert( Client != NULL );
412 if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
413 Adjust_Counters( Client );
414 } /* Client_SetType */
418 Client_SetHops( CLIENT *Client, int Hops )
420 assert( Client != NULL );
422 } /* Client_SetHops */
426 Client_SetToken( CLIENT *Client, int Token )
428 assert( Client != NULL );
429 Client->token = Token;
430 } /* Client_SetToken */
434 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
436 assert( Client != NULL );
437 assert( Introducer != NULL );
438 Client->introducer = Introducer;
439 } /* Client_SetIntroducer */
443 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
445 assert( Client != NULL );
446 Client->oper_by_me = OperByMe;
447 } /* Client_SetOperByMe */
451 Client_ModeAdd( CLIENT *Client, char Mode )
454 * If Client already alread had Mode, return false.
455 * If the Mode was newly set, return true.
460 assert( Client != NULL );
462 x[0] = Mode; x[1] = '\0';
463 if (!strchr( Client->modes, x[0])) {
464 strlcat( Client->modes, x, sizeof( Client->modes ));
468 } /* Client_ModeAdd */
472 Client_ModeDel( CLIENT *Client, char Mode )
475 * If Mode was removed, return true.
476 * If Client did not have Mode, return false.
481 assert( Client != NULL );
483 x[0] = Mode; x[1] = '\0';
485 p = strchr( Client->modes, x[0] );
486 if( ! p ) return false;
488 /* Client has Mode -> delete */
495 } /* Client_ModeDel */
499 Client_Search( const char *Nick )
501 /* return Client-Structure that has the corresponding Nick.
502 * If none is found, return NULL.
505 char search_id[CLIENT_ID_LEN], *ptr;
509 assert( Nick != NULL );
511 /* copy Nick and truncate hostmask if necessary */
512 strlcpy( search_id, Nick, sizeof( search_id ));
513 ptr = strchr( search_id, '!' );
514 if( ptr ) *ptr = '\0';
516 search_hash = Hash(search_id);
520 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
522 c = (CLIENT *)c->next;
525 } /* Client_Search */
529 Client_GetFromToken( CLIENT *Client, int Token )
531 /* Client-Struktur, die den entsprechenden Introducer (=Client)
532 * und das gegebene Token hat, liefern. Wird keine gefunden,
533 * so wird NULL geliefert. */
537 assert( Client != NULL );
542 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
545 c = (CLIENT *)c->next;
548 } /* Client_GetFromToken */
552 Client_Type( CLIENT *Client )
554 assert( Client != NULL );
560 Client_Conn( CLIENT *Client )
562 assert( Client != NULL );
563 return Client->conn_id;
568 Client_ID( CLIENT *Client )
570 assert( Client != NULL );
573 if(Client->type == CLIENT_USER)
574 assert(strlen(Client->id) < Conf_MaxNickLength);
577 if( Client->id[0] ) return Client->id;
583 Client_Info( CLIENT *Client )
585 assert( Client != NULL );
591 Client_User( CLIENT *Client )
593 assert( Client != NULL );
594 return Client->user[0] ? Client->user : "~";
599 Client_Hostname( CLIENT *Client )
601 assert( Client != NULL );
603 } /* Client_Hostname */
607 Client_Password( CLIENT *Client )
609 assert( Client != NULL );
611 } /* Client_Password */
615 Client_Modes( CLIENT *Client )
617 assert( Client != NULL );
618 return Client->modes;
623 Client_Flags( CLIENT *Client )
625 assert( Client != NULL );
626 return Client->flags;
631 Client_OperByMe( CLIENT *Client )
633 assert( Client != NULL );
634 return Client->oper_by_me;
635 } /* Client_OperByMe */
639 Client_Hops( CLIENT *Client )
641 assert( Client != NULL );
647 Client_Token( CLIENT *Client )
649 assert( Client != NULL );
650 return Client->token;
655 Client_MyToken( CLIENT *Client )
657 assert( Client != NULL );
658 return Client->mytoken;
659 } /* Client_MyToken */
663 Client_NextHop( CLIENT *Client )
667 assert( Client != NULL );
670 while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
674 } /* Client_NextHop */
678 * return Client-ID ("client!user@host"), this ID is needed for e.g.
679 * prefixes. Returnes pointer to static buffer.
682 Client_Mask( CLIENT *Client )
684 static char GetID_Buffer[GETID_LEN];
686 assert( Client != NULL );
688 if( Client->type == CLIENT_SERVER ) return Client->id;
690 snprintf(GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host);
696 Client_Introducer( CLIENT *Client )
698 assert( Client != NULL );
699 return Client->introducer;
700 } /* Client_Introducer */
704 Client_TopServer( CLIENT *Client )
706 assert( Client != NULL );
707 return Client->topserver;
708 } /* Client_TopServer */
712 Client_HasMode( CLIENT *Client, char Mode )
714 assert( Client != NULL );
715 return strchr( Client->modes, Mode ) != NULL;
716 } /* Client_HasMode */
720 Client_Away( CLIENT *Client )
722 assert( Client != NULL );
728 Client_CheckNick( CLIENT *Client, char *Nick )
730 assert( Client != NULL );
731 assert( Nick != NULL );
733 if (! Client_IsValidNick( Nick ))
735 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
739 /* Nick bereits vergeben? */
740 if( Client_Search( Nick ))
742 /* den Nick gibt es bereits */
743 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
748 } /* Client_CheckNick */
752 Client_CheckID( CLIENT *Client, char *ID )
754 char str[COMMAND_LEN];
757 assert( Client != NULL );
758 assert( Client->conn_id > NONE );
759 assert( ID != NULL );
762 if (strlen(ID) > CLIENT_ID_LEN) {
763 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
767 /* ID already in use? */
770 if (strcasecmp(c->id, ID) == 0) {
771 snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
772 if (c->conn_id != NONE)
773 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
775 Log(LOG_ERR, "%s (via network)!", str);
776 Conn_Close(Client->conn_id, str, str, true);
779 c = (CLIENT *)c->next;
783 } /* Client_CheckID */
794 Client_Next( CLIENT *c )
797 return (CLIENT *)c->next;
802 Client_UserCount( void )
804 return Count( CLIENT_USER );
805 } /* Client_UserCount */
809 Client_ServiceCount( void )
811 return Count( CLIENT_SERVICE );;
812 } /* Client_ServiceCount */
816 Client_ServerCount( void )
818 return Count( CLIENT_SERVER );
819 } /* Client_ServerCount */
823 Client_MyUserCount( void )
825 return MyCount( CLIENT_USER );
826 } /* Client_MyUserCount */
830 Client_MyServiceCount( void )
832 return MyCount( CLIENT_SERVICE );
833 } /* Client_MyServiceCount */
837 Client_MyServerCount( void )
840 unsigned long cnt = 0;
845 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
846 c = (CLIENT *)c->next;
849 } /* Client_MyServerCount */
853 Client_OperCount( void )
856 unsigned long cnt = 0;
861 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
862 c = (CLIENT *)c->next;
865 } /* Client_OperCount */
869 Client_UnknownCount( void )
872 unsigned long cnt = 0;
877 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
878 c = (CLIENT *)c->next;
882 } /* Client_UnknownCount */
886 Client_MaxUserCount( void )
889 } /* Client_MaxUserCount */
893 Client_MyMaxUserCount( void )
896 } /* Client_MyMaxUserCount */
900 Client_IsValidNick( const char *Nick )
903 static const char goodchars[] = ";0123456789-";
905 assert( Nick != NULL );
907 if( Nick[0] == '#' ) return false;
908 if( strchr( goodchars, Nick[0] )) return false;
909 if( strlen( Nick ) >= Conf_MaxNickLength) return false;
914 if (( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return false;
915 if ( *ptr > '}' ) return false;
920 } /* Client_IsValidNick */
924 * Return pointer to "My_Whowas" structure.
927 Client_GetWhowas( void )
930 } /* Client_GetWhowas */
933 * Return the index of the last used WHOWAS entry.
936 Client_GetLastWhowasIndex( void )
939 } /* Client_GetLastWhowasIndex */
943 * Get the start time of this client.
944 * The result is the start time in seconds since 1970-01-01, as reported
945 * by the C function time(NULL).
948 Client_StartTime(CLIENT *Client)
950 assert( Client != NULL );
951 return Client->starttime;
952 } /* Client_Uptime */
956 Count( CLIENT_TYPE Type )
959 unsigned long cnt = 0;
964 if( c->type == Type ) cnt++;
965 c = (CLIENT *)c->next;
972 MyCount( CLIENT_TYPE Type )
975 unsigned long cnt = 0;
980 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
981 c = (CLIENT *)c->next;
988 New_Client_Struct( void )
990 /* Neue CLIENT-Struktur pre-initialisieren */
994 c = (CLIENT *)malloc( sizeof( CLIENT ));
997 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1001 memset( c, 0, sizeof ( CLIENT ));
1003 c->type = CLIENT_UNKNOWN;
1005 c->oper_by_me = false;
1015 Generate_MyToken( CLIENT *Client )
1024 if( c->mytoken == token )
1026 /* Das Token wurde bereits vergeben */
1031 else c = (CLIENT *)c->next;
1033 Client->mytoken = token;
1034 LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1035 } /* Generate_MyToken */
1039 Adjust_Counters( CLIENT *Client )
1043 assert( Client != NULL );
1045 if( Client->type != CLIENT_USER ) return;
1047 if( Client->conn_id != NONE )
1049 /* Local connection */
1050 count = Client_MyUserCount( );
1051 if( count > My_Max_Users ) My_Max_Users = count;
1053 count = Client_UserCount( );
1054 if( count > Max_Users ) Max_Users = count;
1055 } /* Adjust_Counters */
1059 * Register client in My_Whowas structure for further recall by WHOWAS.
1060 * Note: Only clients that have been connected at least 30 seconds will be
1061 * registered to prevent automated IRC bots to "destroy" a nice server
1065 Client_RegisterWhowas( CLIENT *Client )
1070 assert( Client != NULL );
1073 /* Don't register clients that were connected less than 30 seconds. */
1074 if( now - Client->starttime < 30 )
1077 slot = Last_Whowas + 1;
1078 if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1081 Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1084 My_Whowas[slot].time = now;
1085 strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1086 sizeof( My_Whowas[slot].id ));
1087 strlcpy( My_Whowas[slot].user, Client_User( Client ),
1088 sizeof( My_Whowas[slot].user ));
1089 strlcpy( My_Whowas[slot].host, Client_Hostname( Client ),
1090 sizeof( My_Whowas[slot].host ));
1091 strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1092 sizeof( My_Whowas[slot].info ));
1093 strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1094 sizeof( My_Whowas[slot].server ));
1097 } /* Client_RegisterWhowas */
1101 Client_TypeText(CLIENT *Client)
1103 assert(Client != NULL);
1104 switch (Client_Type(Client)) {
1108 case CLIENT_SERVICE:
1117 } /* Client_TypeText */
1121 * Destroy user or service client.
1124 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1126 if(Client->conn_id != NONE) {
1127 /* Local (directly connected) client */
1129 "%s \"%s\" unregistered (connection %d): %s",
1130 Client_TypeText(Client), Client_Mask(Client),
1131 Client->conn_id, Txt);
1134 /* Inforam all the other servers */
1136 IRC_WriteStrServersPrefix(NULL,
1137 Client, "QUIT :%s", FwdMsg );
1139 IRC_WriteStrServersPrefix(NULL,
1144 LogDebug("%s \"%s\" unregistered: %s",
1145 Client_TypeText(Client), Client_Mask(Client), Txt);
1148 /* Inform all the other servers, but the ones in the
1149 * direction we got the QUIT from */
1151 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1152 Client, "QUIT :%s", FwdMsg );
1154 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1159 /* Unregister client from channels */
1160 Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1162 /* Register client in My_Whowas structure */
1163 Client_RegisterWhowas(Client);
1164 } /* Destroy_UserOrService */