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