]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
New function Client_TypeText() and Destroy_UserOrService().
[ngircd-alex.git] / src / ngircd / client.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2008 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  */
13
14
15 #define __client_c__
16
17
18 #include "portab.h"
19
20 #include "imp.h"
21 #include <assert.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <netdb.h>
28
29 #include "defines.h"
30 #include "conn.h"
31
32 #include "exp.h"
33 #include "client.h"
34
35 #include <imp.h>
36 #include "ngircd.h"
37 #include "channel.h"
38 #include "resolve.h"
39 #include "conf.h"
40 #include "hash.h"
41 #include "irc-write.h"
42 #include "log.h"
43 #include "messages.h"
44
45 #include <exp.h>
46
47
48 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
49
50
51 static CLIENT *This_Server, *My_Clients;
52 static char GetID_Buffer[GETID_LEN];
53
54 static WHOWAS My_Whowas[MAX_WHOWAS];
55 static int Last_Whowas = -1;
56 static long Max_Users, My_Max_Users;
57
58
59 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
60 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
61
62 static CLIENT *New_Client_Struct PARAMS(( void ));
63 static void Generate_MyToken PARAMS(( CLIENT *Client ));
64 static void Adjust_Counters PARAMS(( CLIENT *Client ));
65
66 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
67                                        CLIENT *TopServer, int Type, char *ID,
68                                        char *User, char *Hostname, char *Info,
69                                        int Hops, int Token, char *Modes,
70                                        bool Idented));
71
72 static void Destroy_UserOrService PARAMS((CLIENT *Client, char *Txt, char *FwdMsg,
73                                         bool SendQuit));
74
75
76 GLOBAL void
77 Client_Init( void )
78 {
79         struct hostent *h;
80         
81         This_Server = New_Client_Struct( );
82         if( ! This_Server )
83         {
84                 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
85                 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
86                 exit( 1 );
87         }
88
89         /* Client-Struktur dieses Servers */
90         This_Server->next = NULL;
91         This_Server->type = CLIENT_SERVER;
92         This_Server->conn_id = NONE;
93         This_Server->introducer = This_Server;
94         This_Server->mytoken = 1;
95         This_Server->hops = 0;
96
97         gethostname( This_Server->host, CLIENT_HOST_LEN );
98         if (!Conf_NoDNS) {
99                 h = gethostbyname( This_Server->host );
100                 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
101         }
102         Client_SetID( This_Server, Conf_ServerName );
103         Client_SetInfo( This_Server, Conf_ServerInfo );
104
105         My_Clients = This_Server;
106         
107         memset( &My_Whowas, 0, sizeof( My_Whowas ));
108 } /* Client_Init */
109
110
111 GLOBAL void
112 Client_Exit( void )
113 {
114         CLIENT *c, *next;
115         int cnt;
116
117         if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
118         else Client_Destroy( This_Server, "Server going down.", NULL, false );
119         
120         cnt = 0;
121         c = My_Clients;
122         while( c )
123         {
124                 cnt++;
125                 next = (CLIENT *)c->next;
126                 free( c );
127                 c = next;
128         }
129         if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
130 } /* Client_Exit */
131
132
133 GLOBAL CLIENT *
134 Client_ThisServer( void )
135 {
136         return This_Server;
137 } /* Client_ThisServer */
138
139
140 /**
141  * Initialize new local client; wrapper function for Init_New_Client().
142  * @return New CLIENT structure.
143  */
144 GLOBAL CLIENT *
145 Client_NewLocal(CONN_ID Idx, char *Hostname, int Type, bool Idented)
146 {
147         return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
148                 Hostname, NULL, 0, 0, NULL, Idented);
149 } /* Client_NewLocal */
150
151
152 /**
153  * Initialize new remote server; wrapper function for Init_New_Client().
154  * @return New CLIENT structure.
155  */
156 GLOBAL CLIENT *
157 Client_NewRemoteServer(CLIENT *Introducer, char *Hostname, CLIENT *TopServer,
158  int Hops, int Token, char *Info, bool Idented)
159 {
160         return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
161                 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
162 } /* Client_NewRemoteServer */
163
164
165 /**
166  * Initialize new remote client; wrapper function for Init_New_Client().
167  * @return New CLIENT structure.
168  */
169 GLOBAL CLIENT *
170 Client_NewRemoteUser(CLIENT *Introducer, char *Nick, int Hops, char *User,
171  char *Hostname, int Token, char *Modes, char *Info, bool Idented)
172 {
173         return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
174                 User, Hostname, Info, Hops, Token, Modes, Idented);
175 } /* Client_NewRemoteUser */
176
177
178 /**
179  * Initialize new client and set up the given parameters like client type,
180  * user name, host name, introducing server etc. ...
181  * @return New CLIENT structure.
182  */
183 static CLIENT *
184 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
185  int Type, char *ID, char *User, char *Hostname, char *Info, int Hops,
186  int Token, char *Modes, bool Idented)
187 {
188         CLIENT *client;
189
190         assert( Idx >= NONE );
191         assert( Introducer != NULL );
192         assert( Hostname != NULL );
193
194         client = New_Client_Struct( );
195         if( ! client ) return NULL;
196
197         /* Initialisieren */
198         client->starttime = time(NULL);
199         client->conn_id = Idx;
200         client->introducer = Introducer;
201         client->topserver = TopServer;
202         client->type = Type;
203         if( ID ) Client_SetID( client, ID );
204         if( User ) Client_SetUser( client, User, Idented );
205         if( Hostname ) Client_SetHostname( client, Hostname );
206         if( Info ) Client_SetInfo( client, Info );
207         client->hops = Hops;
208         client->token = Token;
209         if( Modes ) Client_SetModes( client, Modes );
210         if( Type == CLIENT_SERVER ) Generate_MyToken( client );
211
212         if( strchr( client->modes, 'a' ))
213                 strlcpy( client->away, DEFAULT_AWAY_MSG, sizeof( client->away ));
214
215         /* Verketten */
216         client->next = (POINTER *)My_Clients;
217         My_Clients = client;
218
219         /* Adjust counters */
220         Adjust_Counters( client );
221
222         return client;
223 } /* Init_New_Client */
224
225
226 GLOBAL void
227 Client_Destroy( CLIENT *Client, char *LogMsg, char *FwdMsg, bool SendQuit )
228 {
229         /* Client entfernen. */
230         
231         CLIENT *last, *c;
232         char msg[LINE_LEN], *txt;
233
234         assert( Client != NULL );
235
236         if( LogMsg ) txt = LogMsg;
237         else txt = FwdMsg;
238         if( ! txt ) txt = "Reason unknown.";
239
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));
245         }
246
247         last = NULL;
248         c = My_Clients;
249         while( c )
250         {
251                 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
252                 {
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 );
256                         last = NULL;
257                         c = My_Clients;
258                         continue;
259                 }
260                 if( c == Client )
261                 {
262                         /* Wir haben den Client gefunden: entfernen */
263                         if( last ) last->next = c->next;
264                         else My_Clients = (CLIENT *)c->next;
265
266                         if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
267                                 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
268                         else if( c->type == CLIENT_SERVER )
269                         {
270                                 if( c != This_Server )
271                                 {
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 );
274                                 }
275
276                                 /* andere Server informieren */
277                                 if( ! NGIRCd_SignalQuit )
278                                 {
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 );
281                                 }
282                         }
283                         else
284                         {
285                                 if( c->conn_id != NONE )
286                                 {
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 );
289                                 } else {
290                                         Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
291                                                                 c->id[0] ? c->id : "(No Nick)", txt );
292                                 }
293                         }
294
295                         free( c );
296                         break;
297                 }
298                 last = c;
299                 c = (CLIENT *)c->next;
300         }
301 } /* Client_Destroy */
302
303
304 GLOBAL void
305 Client_SetHostname( CLIENT *Client, char *Hostname )
306 {
307         /* Hostname eines Clients setzen */
308
309         assert( Client != NULL );
310         assert( Hostname != NULL );
311
312         strlcpy( Client->host, Hostname, sizeof( Client->host ));
313 } /* Client_SetHostname */
314
315
316 GLOBAL void
317 Client_SetID( CLIENT *Client, char *ID )
318 {
319         /* Hostname eines Clients setzen, Hash-Wert berechnen */
320
321         assert( Client != NULL );
322         assert( ID != NULL );
323         
324         strlcpy( Client->id, ID, sizeof( Client->id ));
325
326         /* Hash */
327         Client->hash = Hash( Client->id );
328 } /* Client_SetID */
329
330
331 GLOBAL void
332 Client_SetUser( CLIENT *Client, char *User, bool Idented )
333 {
334         /* Username eines Clients setzen */
335
336         assert( Client != NULL );
337         assert( User != NULL );
338
339         if( Idented ) strlcpy( Client->user, User, sizeof( Client->user ));
340         else
341         {
342                 Client->user[0] = '~';
343                 strlcpy( Client->user + 1, User, sizeof( Client->user ) - 1 );
344         }
345 } /* Client_SetUser */
346
347
348 GLOBAL void
349 Client_SetInfo( CLIENT *Client, char *Info )
350 {
351         /* Hostname eines Clients setzen */
352
353         assert( Client != NULL );
354         assert( Info != NULL );
355
356         strlcpy( Client->info, Info, sizeof( Client->info ));
357 } /* Client_SetInfo */
358
359
360 GLOBAL void
361 Client_SetModes( CLIENT *Client, char *Modes )
362 {
363         /* Modes eines Clients setzen */
364
365         assert( Client != NULL );
366         assert( Modes != NULL );
367
368         strlcpy( Client->modes, Modes, sizeof( Client->modes ));
369 } /* Client_SetModes */
370
371
372 GLOBAL void
373 Client_SetFlags( CLIENT *Client, char *Flags )
374 {
375         /* Flags eines Clients setzen */
376
377         assert( Client != NULL );
378         assert( Flags != NULL );
379
380         strlcpy( Client->flags, Flags, sizeof( Client->flags ));
381 } /* Client_SetFlags */
382
383
384 GLOBAL void
385 Client_SetPassword( CLIENT *Client, char *Pwd )
386 {
387         /* Von einem Client geliefertes Passwort */
388
389         assert( Client != NULL );
390         assert( Pwd != NULL );
391
392         strlcpy( Client->pwd, Pwd, sizeof( Client->pwd ));
393 } /* Client_SetPassword */
394
395
396 GLOBAL void
397 Client_SetAway( CLIENT *Client, char *Txt )
398 {
399         /* Set AWAY reason of client */
400
401         assert( Client != NULL );
402         assert( Txt != NULL );
403
404         strlcpy( Client->away, Txt, sizeof( Client->away ));
405         Log( LOG_DEBUG, "User \"%s\" is away: %s", Client_Mask( Client ), Txt );
406 } /* Client_SetAway */
407
408
409 GLOBAL void
410 Client_SetType( CLIENT *Client, int Type )
411 {
412         assert( Client != NULL );
413         Client->type = Type;
414         if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
415         Adjust_Counters( Client );
416 } /* Client_SetType */
417
418
419 GLOBAL void
420 Client_SetHops( CLIENT *Client, int Hops )
421 {
422         assert( Client != NULL );
423         Client->hops = Hops;
424 } /* Client_SetHops */
425
426
427 GLOBAL void
428 Client_SetToken( CLIENT *Client, int Token )
429 {
430         assert( Client != NULL );
431         Client->token = Token;
432 } /* Client_SetToken */
433
434
435 GLOBAL void
436 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
437 {
438         assert( Client != NULL );
439         assert( Introducer != NULL );
440         Client->introducer = Introducer;
441 } /* Client_SetIntroducer */
442
443
444 GLOBAL void
445 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
446 {
447         assert( Client != NULL );
448         Client->oper_by_me = OperByMe;
449 } /* Client_SetOperByMe */
450
451
452 GLOBAL bool
453 Client_ModeAdd( CLIENT *Client, char Mode )
454 {
455         /* Set Mode.
456          * If Client already alread had Mode, return false.
457          * If the Mode was newly set, return true.
458          */
459
460         char x[2];
461
462         assert( Client != NULL );
463
464         x[0] = Mode; x[1] = '\0';
465         if( ! strchr( Client->modes, x[0] ))
466         {
467                 /* Client hat den Mode noch nicht -> setzen */
468                 strlcat( Client->modes, x, sizeof( Client->modes ));
469                 return true;
470         }
471         else return false;
472 } /* Client_ModeAdd */
473
474
475 GLOBAL bool
476 Client_ModeDel( CLIENT *Client, char Mode )
477 {
478         /* Delete Mode.
479          * If Mode was removed, return true.
480          * If Client did not have Mode, return false.
481          */
482
483         char x[2], *p;
484
485         assert( Client != NULL );
486
487         x[0] = Mode; x[1] = '\0';
488
489         p = strchr( Client->modes, x[0] );
490         if( ! p ) return false;
491
492         /* Client has Mode -> delete */
493         while( *p )
494         {
495                 *p = *(p + 1);
496                 p++;
497         }
498         return true;
499 } /* Client_ModeDel */
500
501
502 GLOBAL CLIENT *
503 Client_Search( const char *Nick )
504 {
505         /* return Client-Structure that has the corresponding Nick.
506          * If none is found, return NULL.
507          */
508
509         char search_id[CLIENT_ID_LEN], *ptr;
510         CLIENT *c = NULL;
511         UINT32 search_hash;
512
513         assert( Nick != NULL );
514
515         /* copy Nick and truncate hostmask if necessary */
516         strlcpy( search_id, Nick, sizeof( search_id ));
517         ptr = strchr( search_id, '!' );
518         if( ptr ) *ptr = '\0';
519
520         search_hash = Hash( search_id );
521
522         c = My_Clients;
523         while( c )
524         {
525                 if( c->hash == search_hash )
526                 {
527                         /* lt. Hash-Wert: Treffer! */
528                         if( strcasecmp( c->id, search_id ) == 0 ) return c;
529                 }
530                 c = (CLIENT *)c->next;
531         }
532         return NULL;
533 } /* Client_Search */
534
535
536 GLOBAL CLIENT *
537 Client_GetFromToken( CLIENT *Client, int Token )
538 {
539         /* Client-Struktur, die den entsprechenden Introducer (=Client)
540          * und das gegebene Token hat, liefern. Wird keine gefunden,
541          * so wird NULL geliefert. */
542
543         CLIENT *c;
544
545         assert( Client != NULL );
546         assert( Token > 0 );
547
548         c = My_Clients;
549         while( c )
550         {
551                 if(( c->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c->token == Token )) return c;
552                 c = (CLIENT *)c->next;
553         }
554         return NULL;
555 } /* Client_GetFromToken */
556
557
558 GLOBAL int
559 Client_Type( CLIENT *Client )
560 {
561         assert( Client != NULL );
562         return Client->type;
563 } /* Client_Type */
564
565
566 GLOBAL CONN_ID
567 Client_Conn( CLIENT *Client )
568 {
569         assert( Client != NULL );
570         return Client->conn_id;
571 } /* Client_Conn */
572
573
574 GLOBAL char *
575 Client_ID( CLIENT *Client )
576 {
577         assert( Client != NULL );
578
579 #ifdef DEBUG
580         if(Client->type == CLIENT_USER)
581                 assert(strlen(Client->id) < Conf_MaxNickLength);
582 #endif
583                                                    
584         if( Client->id[0] ) return Client->id;
585         else return "*";
586 } /* Client_ID */
587
588
589 GLOBAL char *
590 Client_Info( CLIENT *Client )
591 {
592         assert( Client != NULL );
593         return Client->info;
594 } /* Client_Info */
595
596
597 GLOBAL char *
598 Client_User( CLIENT *Client )
599 {
600         assert( Client != NULL );
601         return Client->user[0] ? Client->user : "~";
602 } /* Client_User */
603
604
605 GLOBAL char *
606 Client_Hostname( CLIENT *Client )
607 {
608         assert( Client != NULL );
609         return Client->host;
610 } /* Client_Hostname */
611
612
613 GLOBAL char *
614 Client_Password( CLIENT *Client )
615 {
616         assert( Client != NULL );
617         return Client->pwd;
618 } /* Client_Password */
619
620
621 GLOBAL char *
622 Client_Modes( CLIENT *Client )
623 {
624         assert( Client != NULL );
625         return Client->modes;
626 } /* Client_Modes */
627
628
629 GLOBAL char *
630 Client_Flags( CLIENT *Client )
631 {
632         assert( Client != NULL );
633         return Client->flags;
634 } /* Client_Flags */
635
636
637 GLOBAL bool
638 Client_OperByMe( CLIENT *Client )
639 {
640         assert( Client != NULL );
641         return Client->oper_by_me;
642 } /* Client_OperByMe */
643
644
645 GLOBAL int
646 Client_Hops( CLIENT *Client )
647 {
648         assert( Client != NULL );
649         return Client->hops;
650 } /* Client_Hops */
651
652
653 GLOBAL int
654 Client_Token( CLIENT *Client )
655 {
656         assert( Client != NULL );
657         return Client->token;
658 } /* Client_Token */
659
660
661 GLOBAL int
662 Client_MyToken( CLIENT *Client )
663 {
664         assert( Client != NULL );
665         return Client->mytoken;
666 } /* Client_MyToken */
667
668
669 GLOBAL CLIENT *
670 Client_NextHop( CLIENT *Client )
671 {
672         CLIENT *c;
673
674         assert( Client != NULL );
675
676         c = Client;
677         while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
678                 c = c->introducer;
679
680         return c;
681 } /* Client_NextHop */
682
683
684 GLOBAL char *
685 Client_Mask( CLIENT *Client )
686 {
687         /* Client-"ID" liefern, wie sie z.B. fuer
688          * Prefixe benoetigt wird. */
689
690         assert( Client != NULL );
691
692         if( Client->type == CLIENT_SERVER ) return Client->id;
693
694         snprintf( GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host );
695         return GetID_Buffer;
696 } /* Client_Mask */
697
698
699 GLOBAL CLIENT *
700 Client_Introducer( CLIENT *Client )
701 {
702         assert( Client != NULL );
703         return Client->introducer;
704 } /* Client_Introducer */
705
706
707 GLOBAL CLIENT *
708 Client_TopServer( CLIENT *Client )
709 {
710         assert( Client != NULL );
711         return Client->topserver;
712 } /* Client_TopServer */
713
714
715 GLOBAL bool
716 Client_HasMode( CLIENT *Client, char Mode )
717 {
718         assert( Client != NULL );
719         return strchr( Client->modes, Mode ) != NULL;
720 } /* Client_HasMode */
721
722
723 GLOBAL char *
724 Client_Away( CLIENT *Client )
725 {
726         /* AWAY-Text liefern */
727
728         assert( Client != NULL );
729         return Client->away;
730 } /* Client_Away */
731
732
733 GLOBAL bool
734 Client_CheckNick( CLIENT *Client, char *Nick )
735 {
736         assert( Client != NULL );
737         assert( Nick != NULL );
738
739         if( ! Client_IsValidNick( Nick ))
740         {
741                 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
742                 return false;
743         }
744
745         /* Nick bereits vergeben? */
746         if( Client_Search( Nick ))
747         {
748                 /* den Nick gibt es bereits */
749                 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
750                 return false;
751         }
752
753         return true;
754 } /* Client_CheckNick */
755
756
757 GLOBAL bool
758 Client_CheckID( CLIENT *Client, char *ID )
759 {
760         /* Nick ueberpruefen */
761
762         char str[COMMAND_LEN];
763         CLIENT *c;
764
765         assert( Client != NULL );
766         assert( Client->conn_id > NONE );
767         assert( ID != NULL );
768
769         /* Nick zu lang? */
770         if( strlen( ID ) > CLIENT_ID_LEN )
771         {
772                 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), ID );
773                 return false;
774         }
775
776         /* ID bereits vergeben? */
777         c = My_Clients;
778         while( c )
779         {
780                 if( strcasecmp( c->id, ID ) == 0 )
781                 {
782                         /* die Server-ID gibt es bereits */
783                         snprintf( str, sizeof( str ), "ID \"%s\" already registered", ID );
784                         if( Client->conn_id != c->conn_id ) Log( LOG_ERR, "%s (on connection %d)!", str, c->conn_id );
785                         else Log( LOG_ERR, "%s (via network)!", str );
786                         Conn_Close( Client->conn_id, str, str, true);
787                         return false;
788                 }
789                 c = (CLIENT *)c->next;
790         }
791
792         return true;
793 } /* Client_CheckID */
794
795
796 GLOBAL CLIENT *
797 Client_First( void )
798 {
799         /* Ersten Client liefern. */
800
801         return My_Clients;
802 } /* Client_First */
803
804
805 GLOBAL CLIENT *
806 Client_Next( CLIENT *c )
807 {
808         /* Naechsten Client liefern. Existiert keiner,
809          * so wird NULL geliefert. */
810
811         assert( c != NULL );
812         return (CLIENT *)c->next;
813 } /* Client_Next */
814
815
816 GLOBAL long
817 Client_UserCount( void )
818 {
819         return Count( CLIENT_USER );
820 } /* Client_UserCount */
821
822
823 GLOBAL long
824 Client_ServiceCount( void )
825 {
826         return Count( CLIENT_SERVICE );;
827 } /* Client_ServiceCount */
828
829
830 GLOBAL long
831 Client_ServerCount( void )
832 {
833         return Count( CLIENT_SERVER );
834 } /* Client_ServerCount */
835
836
837 GLOBAL long
838 Client_MyUserCount( void )
839 {
840         return MyCount( CLIENT_USER );
841 } /* Client_MyUserCount */
842
843
844 GLOBAL long
845 Client_MyServiceCount( void )
846 {
847         return MyCount( CLIENT_SERVICE );
848 } /* Client_MyServiceCount */
849
850
851 GLOBAL unsigned long
852 Client_MyServerCount( void )
853 {
854         CLIENT *c;
855         unsigned long cnt = 0;
856
857         c = My_Clients;
858         while( c )
859         {
860                 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
861                 c = (CLIENT *)c->next;
862         }
863         return cnt;
864 } /* Client_MyServerCount */
865
866
867 GLOBAL unsigned long
868 Client_OperCount( void )
869 {
870         CLIENT *c;
871         unsigned long cnt = 0;
872
873         c = My_Clients;
874         while( c )
875         {
876                 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
877                 c = (CLIENT *)c->next;
878         }
879         return cnt;
880 } /* Client_OperCount */
881
882
883 GLOBAL unsigned long
884 Client_UnknownCount( void )
885 {
886         CLIENT *c;
887         unsigned long cnt = 0;
888
889         c = My_Clients;
890         while( c )
891         {
892                 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
893                 c = (CLIENT *)c->next;
894         }
895
896         return cnt;
897 } /* Client_UnknownCount */
898
899
900 GLOBAL long
901 Client_MaxUserCount( void )
902 {
903         return Max_Users;
904 } /* Client_MaxUserCount */
905
906
907 GLOBAL long
908 Client_MyMaxUserCount( void )
909 {
910         return My_Max_Users;
911 } /* Client_MyMaxUserCount */
912
913
914 GLOBAL bool
915 Client_IsValidNick( const char *Nick )
916 {
917         const char *ptr;
918         static const char goodchars[] = ";0123456789-";
919
920         assert( Nick != NULL );
921
922         if( Nick[0] == '#' ) return false;
923         if( strchr( goodchars, Nick[0] )) return false;
924         if( strlen( Nick ) >= Conf_MaxNickLength) return false;
925
926         ptr = Nick;
927         while( *ptr )
928         {
929                 if (( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return false;
930                 if ( *ptr > '}' ) return false;
931                 ptr++;
932         }
933
934         return true;
935 } /* Client_IsValidNick */
936
937
938 /**
939  * Return pointer to "My_Whowas" structure.
940  */
941 GLOBAL WHOWAS *
942 Client_GetWhowas( void )
943 {
944         return My_Whowas;
945 } /* Client_GetWhowas */
946
947 /**
948  * Return the index of the last used WHOWAS entry.
949  */
950 GLOBAL int
951 Client_GetLastWhowasIndex( void )
952 {
953         return Last_Whowas;
954 } /* Client_GetLastWhowasIndex */
955
956
957 /**
958  * Get the start time of this client.
959  * The result is the start time in seconds since 1970-01-01, as reported
960  * by the C function time(NULL).
961  */
962 GLOBAL time_t
963 Client_StartTime(CLIENT *Client)
964 {
965         assert( Client != NULL );
966         return Client->starttime;
967 } /* Client_Uptime */
968
969
970 static unsigned long
971 Count( CLIENT_TYPE Type )
972 {
973         CLIENT *c;
974         unsigned long cnt = 0;
975
976         c = My_Clients;
977         while( c )
978         {
979                 if( c->type == Type ) cnt++;
980                 c = (CLIENT *)c->next;
981         }
982         return cnt;
983 } /* Count */
984
985
986 static unsigned long
987 MyCount( CLIENT_TYPE Type )
988 {
989         CLIENT *c;
990         unsigned long cnt = 0;
991
992         c = My_Clients;
993         while( c )
994         {
995                 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
996                 c = (CLIENT *)c->next;
997         }
998         return cnt;
999 } /* MyCount */
1000
1001
1002 static CLIENT *
1003 New_Client_Struct( void )
1004 {
1005         /* Neue CLIENT-Struktur pre-initialisieren */
1006
1007         CLIENT *c;
1008
1009         c = (CLIENT *)malloc( sizeof( CLIENT ));
1010         if( ! c )
1011         {
1012                 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1013                 return NULL;
1014         }
1015
1016         memset( c, 0, sizeof ( CLIENT ));
1017
1018         c->type = CLIENT_UNKNOWN;
1019         c->conn_id = NONE;
1020         c->oper_by_me = false;
1021         c->hops = -1;
1022         c->token = -1;
1023         c->mytoken = -1;
1024
1025         return c;
1026 } /* New_Client */
1027
1028
1029 static void
1030 Generate_MyToken( CLIENT *Client )
1031 {
1032         CLIENT *c;
1033         int token;
1034
1035         c = My_Clients;
1036         token = 2;
1037         while( c )
1038         {
1039                 if( c->mytoken == token )
1040                 {
1041                         /* Das Token wurde bereits vergeben */
1042                         token++;
1043                         c = My_Clients;
1044                         continue;
1045                 }
1046                 else c = (CLIENT *)c->next;
1047         }
1048         Client->mytoken = token;
1049         Log( LOG_DEBUG, "Assigned token %d to server \"%s\".", token, Client->id );
1050 } /* Generate_MyToken */
1051
1052
1053 static void
1054 Adjust_Counters( CLIENT *Client )
1055 {
1056         long count;
1057
1058         assert( Client != NULL );
1059
1060         if( Client->type != CLIENT_USER ) return;
1061
1062         if( Client->conn_id != NONE )
1063         {
1064                 /* Local connection */
1065                 count = Client_MyUserCount( );
1066                 if( count > My_Max_Users ) My_Max_Users = count;
1067         }
1068         count = Client_UserCount( );
1069         if( count > Max_Users ) Max_Users = count;
1070 } /* Adjust_Counters */
1071
1072
1073 /**
1074  * Register client in My_Whowas structure for further recall by WHOWAS.
1075  * Note: Only clients that have been connected at least 30 seconds will be
1076  * registered to prevent automated IRC bots to "destroy" a nice server
1077  * history database.
1078  */
1079 GLOBAL void
1080 Client_RegisterWhowas( CLIENT *Client )
1081 {
1082         int slot;
1083         time_t now;
1084
1085         assert( Client != NULL );
1086
1087         now = time(NULL);
1088         /* Don't register clients that were connected less than 30 seconds. */
1089         if( now - Client->starttime < 30 )
1090                 return;
1091
1092         slot = Last_Whowas + 1;
1093         if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1094
1095 #ifdef DEBUG
1096         Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1097 #endif
1098
1099         My_Whowas[slot].time = now;
1100         strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1101                  sizeof( My_Whowas[slot].id ));
1102         strlcpy( My_Whowas[slot].user, Client_User( Client ),
1103                  sizeof( My_Whowas[slot].user ));
1104         strlcpy( My_Whowas[slot].host, Client_Hostname( Client ),
1105                  sizeof( My_Whowas[slot].host ));
1106         strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1107                  sizeof( My_Whowas[slot].info ));
1108         strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1109                  sizeof( My_Whowas[slot].server ));
1110
1111         Last_Whowas = slot;
1112 } /* Client_RegisterWhowas */
1113
1114
1115 GLOBAL char *
1116 Client_TypeText(CLIENT *Client)
1117 {
1118         assert(Client != NULL);
1119         switch (Client_Type(Client)) {
1120                 case CLIENT_USER:
1121                         return "User";
1122                         break;
1123                 case CLIENT_SERVICE:
1124                         return "Service";
1125                         break;
1126                 case CLIENT_SERVER:
1127                         return "Server";
1128                         break;
1129                 default:
1130                         return "Client";
1131         }
1132 } /* Client_TypeText */
1133
1134
1135 /**
1136  * Destroy user or service client.
1137  */
1138 static void
1139 Destroy_UserOrService(CLIENT *Client, char *Txt, char *FwdMsg, bool SendQuit)
1140 {
1141         if(Client->conn_id != NONE) {
1142                 /* Local (directly connected) client */
1143                 Log(LOG_NOTICE,
1144                     "%s \"%s\" unregistered (connection %d): %s",
1145                     Client_TypeText(Client), Client_Mask(Client),
1146                     Client->conn_id, Txt);
1147
1148                 if (SendQuit) {
1149                         /* Inforam all the other servers */
1150                         if (FwdMsg)
1151                                 IRC_WriteStrServersPrefix(NULL,
1152                                                 Client, "QUIT :%s", FwdMsg );
1153                         else
1154                                 IRC_WriteStrServersPrefix(NULL,
1155                                                 Client, "QUIT :");
1156                 }
1157         } else {
1158                 /* Remote client */
1159                 LogDebug("%s \"%s\" unregistered: %s",
1160                          Client_TypeText(Client), Client_Mask(Client), Txt);
1161
1162                 if(SendQuit) {
1163                         /* Inform all the other servers, but the ones in the
1164                          * direction we got the QUIT from */
1165                         if(FwdMsg)
1166                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1167                                                 Client, "QUIT :%s", FwdMsg );
1168                         else
1169                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1170                                                 Client, "QUIT :" );
1171                 }
1172         }
1173
1174         /* Unregister client from channels */
1175         Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1176
1177         /* Register client in My_Whowas structure */
1178         Client_RegisterWhowas(Client);
1179 } /* Destroy_UserOrService */
1180
1181
1182 /* -eof- */