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