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