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