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