]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
constify Client_Search() argument.
[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.98 2008/04/04 19:30:01 fw 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 static long Max_Users, My_Max_Users;
59
60
61 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
62 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
63
64 static CLIENT *New_Client_Struct PARAMS(( void ));
65 static void Generate_MyToken PARAMS(( CLIENT *Client ));
66 static void Adjust_Counters PARAMS(( CLIENT *Client ));
67
68 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
69  CLIENT *TopServer, int Type, char *ID, char *User, char *Hostname,
70  char *Info, int Hops, int Token, char *Modes, bool Idented));
71
72
73 GLOBAL void
74 Client_Init( void )
75 {
76         struct hostent *h;
77         
78         This_Server = New_Client_Struct( );
79         if( ! This_Server )
80         {
81                 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
82                 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
83                 exit( 1 );
84         }
85
86         /* Client-Struktur dieses Servers */
87         This_Server->next = NULL;
88         This_Server->type = CLIENT_SERVER;
89         This_Server->conn_id = NONE;
90         This_Server->introducer = This_Server;
91         This_Server->mytoken = 1;
92         This_Server->hops = 0;
93
94         gethostname( This_Server->host, CLIENT_HOST_LEN );
95         if (!Conf_NoDNS) {
96                 h = gethostbyname( This_Server->host );
97                 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
98         }
99         Client_SetID( This_Server, Conf_ServerName );
100         Client_SetInfo( This_Server, Conf_ServerInfo );
101
102         My_Clients = This_Server;
103         
104         memset( &My_Whowas, 0, sizeof( My_Whowas ));
105 } /* Client_Init */
106
107
108 GLOBAL void
109 Client_Exit( void )
110 {
111         CLIENT *c, *next;
112         int cnt;
113
114         if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
115         else Client_Destroy( This_Server, "Server going down.", NULL, false );
116         
117         cnt = 0;
118         c = My_Clients;
119         while( c )
120         {
121                 cnt++;
122                 next = (CLIENT *)c->next;
123                 free( c );
124                 c = next;
125         }
126         if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
127 } /* Client_Exit */
128
129
130 GLOBAL CLIENT *
131 Client_ThisServer( void )
132 {
133         return This_Server;
134 } /* Client_ThisServer */
135
136
137 /**
138  * Initialize new local client; wrapper function for Init_New_Client().
139  * @return New CLIENT structure.
140  */
141 GLOBAL CLIENT *
142 Client_NewLocal(CONN_ID Idx, char *Hostname, int Type, bool Idented)
143 {
144         return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
145                 Hostname, NULL, 0, 0, NULL, Idented);
146 } /* Client_NewLocal */
147
148
149 /**
150  * Initialize new remote server; wrapper function for Init_New_Client().
151  * @return New CLIENT structure.
152  */
153 GLOBAL CLIENT *
154 Client_NewRemoteServer(CLIENT *Introducer, char *Hostname, CLIENT *TopServer,
155  int Hops, int Token, char *Info, bool Idented)
156 {
157         return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
158                 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
159 } /* Client_NewRemoteServer */
160
161
162 /**
163  * Initialize new remote client; wrapper function for Init_New_Client().
164  * @return New CLIENT structure.
165  */
166 GLOBAL CLIENT *
167 Client_NewRemoteUser(CLIENT *Introducer, char *Nick, int Hops, char *User,
168  char *Hostname, int Token, char *Modes, char *Info, bool Idented)
169 {
170         return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
171                 User, Hostname, Info, Hops, Token, Modes, Idented);
172 } /* Client_NewRemoteUser */
173
174
175 /**
176  * Initialize new client and set up the given parameters like client type,
177  * user name, host name, introducing server etc. ...
178  * @return New CLIENT structure.
179  */
180 static CLIENT *
181 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
182  int Type, char *ID, char *User, char *Hostname, char *Info, int Hops,
183  int Token, char *Modes, bool Idented)
184 {
185         CLIENT *client;
186
187         assert( Idx >= NONE );
188         assert( Introducer != NULL );
189         assert( Hostname != NULL );
190
191         client = New_Client_Struct( );
192         if( ! client ) return NULL;
193
194         /* Initialisieren */
195         client->starttime = time(NULL);
196         client->conn_id = Idx;
197         client->introducer = Introducer;
198         client->topserver = TopServer;
199         client->type = Type;
200         if( ID ) Client_SetID( client, ID );
201         if( User ) Client_SetUser( client, User, Idented );
202         if( Hostname ) Client_SetHostname( client, Hostname );
203         if( Info ) Client_SetInfo( client, Info );
204         client->hops = Hops;
205         client->token = Token;
206         if( Modes ) Client_SetModes( client, Modes );
207         if( Type == CLIENT_SERVER ) Generate_MyToken( client );
208
209         if( strchr( client->modes, 'a' ))
210                 strlcpy( client->away, DEFAULT_AWAY_MSG, sizeof( client->away ));
211
212         /* Verketten */
213         client->next = (POINTER *)My_Clients;
214         My_Clients = client;
215
216         /* Adjust counters */
217         Adjust_Counters( client );
218
219         return client;
220 } /* Init_New_Client */
221
222
223 GLOBAL void
224 Client_Destroy( CLIENT *Client, char *LogMsg, char *FwdMsg, bool SendQuit )
225 {
226         /* Client entfernen. */
227         
228         CLIENT *last, *c;
229         char msg[LINE_LEN], *txt;
230
231         assert( Client != NULL );
232
233         if( LogMsg ) txt = LogMsg;
234         else txt = FwdMsg;
235         if( ! txt ) txt = "Reason unknown.";
236
237         /* Netz-Split-Nachricht vorbereiten (noch nicht optimal) */
238         if( Client->type == CLIENT_SERVER ) {
239                 strlcpy(msg, This_Server->id, sizeof (msg));
240                 strlcat(msg, " ", sizeof (msg));
241                 strlcat(msg, Client->id, sizeof (msg));
242         }
243
244         last = NULL;
245         c = My_Clients;
246         while( c )
247         {
248                 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
249                 {
250                         /* der Client, der geloescht wird ist ein Server. Der Client, den wir gerade
251                          * pruefen, ist ein Child von diesem und muss daher auch entfernt werden */
252                         Client_Destroy( c, NULL, msg, false );
253                         last = NULL;
254                         c = My_Clients;
255                         continue;
256                 }
257                 if( c == Client )
258                 {
259                         /* Wir haben den Client gefunden: entfernen */
260                         if( last ) last->next = c->next;
261                         else My_Clients = (CLIENT *)c->next;
262
263                         if( c->type == CLIENT_USER )
264                         {
265                                 if( c->conn_id != NONE )
266                                 {
267                                         /* Ein lokaler User */
268                                         Log( LOG_NOTICE, "User \"%s\" unregistered (connection %d): %s", Client_Mask( c ), c->conn_id, txt );
269
270                                         if( SendQuit )
271                                         {
272                                                 /* Alle andere Server informieren! */
273                                                 if( FwdMsg ) IRC_WriteStrServersPrefix( NULL, c, "QUIT :%s", FwdMsg );
274                                                 else IRC_WriteStrServersPrefix( NULL, c, "QUIT :" );
275                                         }
276                                 }
277                                 else
278                                 {
279                                         /* Remote User */
280                                         Log( LOG_DEBUG, "User \"%s\" unregistered: %s", Client_Mask( c ), txt );
281
282                                         if( SendQuit )
283                                         {
284                                                 /* Andere Server informieren, ausser denen, die "in
285                                                  * Richtung dem liegen", auf dem der User registriert
286                                                  * ist. Von denen haben wir das QUIT ja wohl bekommen. */
287                                                 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "QUIT :%s", FwdMsg );
288                                                 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "QUIT :" );
289                                         }
290                                 }
291
292                                 /* Unregister client from channels */
293                                 Channel_Quit( c, FwdMsg ? FwdMsg : c->id );
294                                 
295                                 /* Register client in My_Whowas structure */
296                                 Client_RegisterWhowas( c );
297                         }
298                         else if( c->type == CLIENT_SERVER )
299                         {
300                                 if( c != This_Server )
301                                 {
302                                         if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
303                                         else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
304                                 }
305
306                                 /* andere Server informieren */
307                                 if( ! NGIRCd_SignalQuit )
308                                 {
309                                         if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
310                                         else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
311                                 }
312                         }
313                         else
314                         {
315                                 if( c->conn_id != NONE )
316                                 {
317                                         if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
318                                         else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
319                                 } else {
320                                         Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
321                                                                 c->id[0] ? c->id : "(No Nick)", txt );
322                                 }
323                         }
324
325                         free( c );
326                         break;
327                 }
328                 last = c;
329                 c = (CLIENT *)c->next;
330         }
331 } /* Client_Destroy */
332
333
334 GLOBAL void
335 Client_SetHostname( CLIENT *Client, char *Hostname )
336 {
337         /* Hostname eines Clients setzen */
338
339         assert( Client != NULL );
340         assert( Hostname != NULL );
341
342         strlcpy( Client->host, Hostname, sizeof( Client->host ));
343 } /* Client_SetHostname */
344
345
346 GLOBAL void
347 Client_SetID( CLIENT *Client, char *ID )
348 {
349         /* Hostname eines Clients setzen, Hash-Wert berechnen */
350
351         assert( Client != NULL );
352         assert( ID != NULL );
353         
354         strlcpy( Client->id, ID, sizeof( Client->id ));
355
356         /* Hash */
357         Client->hash = Hash( Client->id );
358 } /* Client_SetID */
359
360
361 GLOBAL void
362 Client_SetUser( CLIENT *Client, char *User, bool Idented )
363 {
364         /* Username eines Clients setzen */
365
366         assert( Client != NULL );
367         assert( User != NULL );
368
369         if( Idented ) strlcpy( Client->user, User, sizeof( Client->user ));
370         else
371         {
372                 Client->user[0] = '~';
373                 strlcpy( Client->user + 1, User, sizeof( Client->user ) - 1 );
374         }
375 } /* Client_SetUser */
376
377
378 GLOBAL void
379 Client_SetInfo( CLIENT *Client, char *Info )
380 {
381         /* Hostname eines Clients setzen */
382
383         assert( Client != NULL );
384         assert( Info != NULL );
385
386         strlcpy( Client->info, Info, sizeof( Client->info ));
387 } /* Client_SetInfo */
388
389
390 GLOBAL void
391 Client_SetModes( CLIENT *Client, char *Modes )
392 {
393         /* Modes eines Clients setzen */
394
395         assert( Client != NULL );
396         assert( Modes != NULL );
397
398         strlcpy( Client->modes, Modes, sizeof( Client->modes ));
399 } /* Client_SetModes */
400
401
402 GLOBAL void
403 Client_SetFlags( CLIENT *Client, char *Flags )
404 {
405         /* Flags eines Clients setzen */
406
407         assert( Client != NULL );
408         assert( Flags != NULL );
409
410         strlcpy( Client->flags, Flags, sizeof( Client->flags ));
411 } /* Client_SetFlags */
412
413
414 GLOBAL void
415 Client_SetPassword( CLIENT *Client, char *Pwd )
416 {
417         /* Von einem Client geliefertes Passwort */
418
419         assert( Client != NULL );
420         assert( Pwd != NULL );
421
422         strlcpy( Client->pwd, Pwd, sizeof( Client->pwd ));
423 } /* Client_SetPassword */
424
425
426 GLOBAL void
427 Client_SetAway( CLIENT *Client, char *Txt )
428 {
429         /* Set AWAY reason of client */
430
431         assert( Client != NULL );
432         assert( Txt != NULL );
433
434         strlcpy( Client->away, Txt, sizeof( Client->away ));
435         Log( LOG_DEBUG, "User \"%s\" is away: %s", Client_Mask( Client ), Txt );
436 } /* Client_SetAway */
437
438
439 GLOBAL void
440 Client_SetType( CLIENT *Client, int Type )
441 {
442         assert( Client != NULL );
443         Client->type = Type;
444         if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
445         Adjust_Counters( Client );
446 } /* Client_SetType */
447
448
449 GLOBAL void
450 Client_SetHops( CLIENT *Client, int Hops )
451 {
452         assert( Client != NULL );
453         Client->hops = Hops;
454 } /* Client_SetHops */
455
456
457 GLOBAL void
458 Client_SetToken( CLIENT *Client, int Token )
459 {
460         assert( Client != NULL );
461         Client->token = Token;
462 } /* Client_SetToken */
463
464
465 GLOBAL void
466 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
467 {
468         assert( Client != NULL );
469         assert( Introducer != NULL );
470         Client->introducer = Introducer;
471 } /* Client_SetIntroducer */
472
473
474 GLOBAL void
475 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
476 {
477         assert( Client != NULL );
478         Client->oper_by_me = OperByMe;
479 } /* Client_SetOperByMe */
480
481
482 GLOBAL bool
483 Client_ModeAdd( CLIENT *Client, char Mode )
484 {
485         /* Set Mode.
486          * If Client already alread had Mode, return false.
487          * If the Mode was newly set, return true.
488          */
489
490         char x[2];
491
492         assert( Client != NULL );
493
494         x[0] = Mode; x[1] = '\0';
495         if( ! strchr( Client->modes, x[0] ))
496         {
497                 /* Client hat den Mode noch nicht -> setzen */
498                 strlcat( Client->modes, x, sizeof( Client->modes ));
499                 return true;
500         }
501         else return false;
502 } /* Client_ModeAdd */
503
504
505 GLOBAL bool
506 Client_ModeDel( CLIENT *Client, char Mode )
507 {
508         /* Delete Mode.
509          * If Mode was removed, return true.
510          * If Client did not have Mode, return false.
511          */
512
513         char x[2], *p;
514
515         assert( Client != NULL );
516
517         x[0] = Mode; x[1] = '\0';
518
519         p = strchr( Client->modes, x[0] );
520         if( ! p ) return false;
521
522         /* Client has Mode -> delete */
523         while( *p )
524         {
525                 *p = *(p + 1);
526                 p++;
527         }
528         return true;
529 } /* Client_ModeDel */
530
531
532 GLOBAL CLIENT *
533 Client_Search( const char *Nick )
534 {
535         /* return Client-Structure that has the corresponding Nick.
536          * If none is found, return NULL.
537          */
538
539         char search_id[CLIENT_ID_LEN], *ptr;
540         CLIENT *c = NULL;
541         UINT32 search_hash;
542
543         assert( Nick != NULL );
544
545         /* copy Nick and truncate hostmask if necessary */
546         strlcpy( search_id, Nick, sizeof( search_id ));
547         ptr = strchr( search_id, '!' );
548         if( ptr ) *ptr = '\0';
549
550         search_hash = Hash( search_id );
551
552         c = My_Clients;
553         while( c )
554         {
555                 if( c->hash == search_hash )
556                 {
557                         /* lt. Hash-Wert: Treffer! */
558                         if( strcasecmp( c->id, search_id ) == 0 ) return c;
559                 }
560                 c = (CLIENT *)c->next;
561         }
562         return NULL;
563 } /* Client_Search */
564
565
566 GLOBAL CLIENT *
567 Client_GetFromToken( CLIENT *Client, int Token )
568 {
569         /* Client-Struktur, die den entsprechenden Introducer (=Client)
570          * und das gegebene Token hat, liefern. Wird keine gefunden,
571          * so wird NULL geliefert. */
572
573         CLIENT *c;
574
575         assert( Client != NULL );
576         assert( Token > 0 );
577
578         c = My_Clients;
579         while( c )
580         {
581                 if(( c->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c->token == Token )) return c;
582                 c = (CLIENT *)c->next;
583         }
584         return NULL;
585 } /* Client_GetFromToken */
586
587
588 GLOBAL int
589 Client_Type( CLIENT *Client )
590 {
591         assert( Client != NULL );
592         return Client->type;
593 } /* Client_Type */
594
595
596 GLOBAL CONN_ID
597 Client_Conn( CLIENT *Client )
598 {
599         assert( Client != NULL );
600         return Client->conn_id;
601 } /* Client_Conn */
602
603
604 GLOBAL char *
605 Client_ID( CLIENT *Client )
606 {
607         assert( Client != NULL );
608
609 #ifdef DEBUG
610         if(Client->type == CLIENT_USER)
611                 assert(strlen(Client->id) < Conf_MaxNickLength);
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 ) >= Conf_MaxNickLength) 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- */