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