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