]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
Fixed some warnings of non-gcc-compilers (e. g. original Apple compiler on
[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.76 2004/03/11 22:16:31 alex Exp $";
21
22 #include "imp.h"
23 #include <assert.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <strings.h>
29 #include <netdb.h>
30
31 #include "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, BOOLEAN 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, BOOLEAN 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, BOOLEAN 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, BOOLEAN 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, BOOLEAN 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, BOOLEAN 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, BOOLEAN OperByMe )
471 {
472         assert( Client != NULL );
473         Client->oper_by_me = OperByMe;
474 } /* Client_SetOperByMe */
475
476
477 GLOBAL BOOLEAN
478 Client_ModeAdd( CLIENT *Client, CHAR Mode )
479 {
480         /* Mode soll gesetzt werden. TRUE wird geliefert, wenn der
481          * Mode neu gesetzt wurde, FALSE, wenn der Client den Mode
482          * bereits hatte. */
483
484         CHAR x[2];
485         
486         assert( Client != NULL );
487
488         x[0] = Mode; x[1] = '\0';
489         if( ! strchr( Client->modes, x[0] ))
490         {
491                 /* Client hat den Mode noch nicht -> setzen */
492                 strlcat( Client->modes, x, sizeof( Client->modes ));
493                 return TRUE;
494         }
495         else return FALSE;
496 } /* Client_ModeAdd */
497
498
499 GLOBAL BOOLEAN
500 Client_ModeDel( CLIENT *Client, CHAR Mode )
501 {
502         /* Mode soll geloescht werden. TRUE wird geliefert, wenn der
503         * Mode entfernt wurde, FALSE, wenn der Client den Mode
504         * ueberhaupt nicht hatte. */
505
506         CHAR x[2], *p;
507
508         assert( Client != NULL );
509
510         x[0] = Mode; x[1] = '\0';
511
512         p = strchr( Client->modes, x[0] );
513         if( ! p ) return FALSE;
514
515         /* Client hat den Mode -> loeschen */
516         while( *p )
517         {
518                 *p = *(p + 1);
519                 p++;
520         }
521         return TRUE;
522 } /* Client_ModeDel */
523
524
525 GLOBAL CLIENT *
526 Client_GetFromConn( CONN_ID Idx )
527 {
528         /* Client-Struktur, die zur lokalen Verbindung Idx gehoert,
529          * liefern. Wird keine gefunden, so wird NULL geliefert. */
530
531         CLIENT *c;
532
533         assert( Idx >= 0 );
534         
535         c = My_Clients;
536         while( c )
537         {
538                 if( c->conn_id == Idx ) return c;
539                 c = (CLIENT *)c->next;
540         }
541         return NULL;
542 } /* Client_GetFromConn */
543
544
545 GLOBAL CLIENT *
546 Client_Search( CHAR *Nick )
547 {
548         /* Client-Struktur, die den entsprechenden Nick hat, liefern.
549          * Wird keine gefunden, so wird NULL geliefert. */
550
551         CHAR search_id[CLIENT_ID_LEN], *ptr;
552         CLIENT *c = NULL;
553         UINT32 search_hash;
554
555         assert( Nick != NULL );
556
557         /* Nick kopieren und ggf. Host-Mask abschneiden */
558         strlcpy( search_id, Nick, sizeof( search_id ));
559         ptr = strchr( search_id, '!' );
560         if( ptr ) *ptr = '\0';
561
562         search_hash = Hash( search_id );
563
564         c = My_Clients;
565         while( c )
566         {
567                 if( c->hash == search_hash )
568                 {
569                         /* lt. Hash-Wert: Treffer! */
570                         if( strcasecmp( c->id, search_id ) == 0 ) return c;
571                 }
572                 c = (CLIENT *)c->next;
573         }
574         return NULL;
575 } /* Client_Search */
576
577
578 GLOBAL CLIENT *
579 Client_GetFromToken( CLIENT *Client, INT Token )
580 {
581         /* Client-Struktur, die den entsprechenden Introducer (=Client)
582          * und das gegebene Token hat, liefern. Wird keine gefunden,
583          * so wird NULL geliefert. */
584
585         CLIENT *c;
586
587         assert( Client != NULL );
588         assert( Token > 0 );
589
590         c = My_Clients;
591         while( c )
592         {
593                 if(( c->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c->token == Token )) return c;
594                 c = (CLIENT *)c->next;
595         }
596         return NULL;
597 } /* Client_GetFromToken */
598
599
600 GLOBAL INT
601 Client_Type( CLIENT *Client )
602 {
603         assert( Client != NULL );
604         return Client->type;
605 } /* Client_Type */
606
607
608 GLOBAL CONN_ID
609 Client_Conn( CLIENT *Client )
610 {
611         assert( Client != NULL );
612         return Client->conn_id;
613 } /* Client_Conn */
614
615
616 GLOBAL CHAR *
617 Client_ID( CLIENT *Client )
618 {
619         assert( Client != NULL );
620
621 #ifdef DEBUG
622         if( Client->type == CLIENT_USER ) assert( strlen( Client->id ) < CLIENT_NICK_LEN );
623 #endif
624                                                    
625         if( Client->id[0] ) return Client->id;
626         else return "*";
627 } /* Client_ID */
628
629
630 GLOBAL CHAR *
631 Client_Info( CLIENT *Client )
632 {
633         assert( Client != NULL );
634         return Client->info;
635 } /* Client_Info */
636
637
638 GLOBAL CHAR *
639 Client_User( CLIENT *Client )
640 {
641         assert( Client != NULL );
642         if( Client->user[0] ) return Client->user;
643         else return "~";
644 } /* Client_User */
645
646
647 GLOBAL CHAR *
648 Client_Hostname( CLIENT *Client )
649 {
650         assert( Client != NULL );
651         return Client->host;
652 } /* Client_Hostname */
653
654
655 GLOBAL CHAR *
656 Client_Password( CLIENT *Client )
657 {
658         assert( Client != NULL );
659         return Client->pwd;
660 } /* Client_Password */
661
662
663 GLOBAL CHAR *
664 Client_Modes( CLIENT *Client )
665 {
666         assert( Client != NULL );
667         return Client->modes;
668 } /* Client_Modes */
669
670
671 GLOBAL CHAR *
672 Client_Flags( CLIENT *Client )
673 {
674         assert( Client != NULL );
675         return Client->flags;
676 } /* Client_Flags */
677
678
679 GLOBAL BOOLEAN
680 Client_OperByMe( CLIENT *Client )
681 {
682         assert( Client != NULL );
683         return Client->oper_by_me;
684 } /* Client_OperByMe */
685
686
687 GLOBAL INT
688 Client_Hops( CLIENT *Client )
689 {
690         assert( Client != NULL );
691         return Client->hops;
692 } /* Client_Hops */
693
694
695 GLOBAL INT
696 Client_Token( CLIENT *Client )
697 {
698         assert( Client != NULL );
699         return Client->token;
700 } /* Client_Token */
701
702
703 GLOBAL INT
704 Client_MyToken( CLIENT *Client )
705 {
706         assert( Client != NULL );
707         return Client->mytoken;
708 } /* Client_MyToken */
709
710
711 GLOBAL CLIENT *
712 Client_NextHop( CLIENT *Client )
713 {
714         CLIENT *c;
715         
716         assert( Client != NULL );
717
718         c = Client;
719         while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server )) c = c->introducer;
720         return c;
721 } /* Client_NextHop */
722
723
724 GLOBAL CHAR *
725 Client_Mask( CLIENT *Client )
726 {
727         /* Client-"ID" liefern, wie sie z.B. fuer
728          * Prefixe benoetigt wird. */
729
730         assert( Client != NULL );
731         
732         if( Client->type == CLIENT_SERVER ) return Client->id;
733
734         snprintf( GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host );
735         return GetID_Buffer;
736 } /* Client_Mask */
737
738
739 GLOBAL CLIENT *
740 Client_Introducer( CLIENT *Client )
741 {
742         assert( Client != NULL );
743         return Client->introducer;
744 } /* Client_Introducer */
745
746
747 GLOBAL CLIENT *
748 Client_TopServer( CLIENT *Client )
749 {
750         assert( Client != NULL );
751         return Client->topserver;
752 } /* Client_TopServer */
753
754
755 GLOBAL BOOLEAN
756 Client_HasMode( CLIENT *Client, CHAR Mode )
757 {
758         assert( Client != NULL );
759         return strchr( Client->modes, Mode ) != NULL;
760 } /* Client_HasMode */
761
762
763 GLOBAL CHAR *
764 Client_Away( CLIENT *Client )
765 {
766         /* AWAY-Text liefern */
767
768         assert( Client != NULL );
769         return Client->away;
770 } /* Client_Away */
771
772
773 GLOBAL BOOLEAN
774 Client_CheckNick( CLIENT *Client, CHAR *Nick )
775 {
776         /* Nick ueberpruefen */
777
778         assert( Client != NULL );
779         assert( Nick != NULL );
780         
781         /* Nick ungueltig? */
782         if( ! Client_IsValidNick( Nick ))
783         {
784                 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
785                 return FALSE;
786         }
787
788         /* Nick bereits vergeben? */
789         if( Client_Search( Nick ))
790         {
791                 /* den Nick gibt es bereits */
792                 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
793                 return FALSE;
794         }
795
796         return TRUE;
797 } /* Client_CheckNick */
798
799
800 GLOBAL BOOLEAN
801 Client_CheckID( CLIENT *Client, CHAR *ID )
802 {
803         /* Nick ueberpruefen */
804
805         CHAR str[COMMAND_LEN];
806         CLIENT *c;
807
808         assert( Client != NULL );
809         assert( Client->conn_id > NONE );
810         assert( ID != NULL );
811
812         /* Nick zu lang? */
813         if( strlen( ID ) > CLIENT_ID_LEN )
814         {
815                 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), ID );
816                 return FALSE;
817         }
818
819         /* ID bereits vergeben? */
820         c = My_Clients;
821         while( c )
822         {
823                 if( strcasecmp( c->id, ID ) == 0 )
824                 {
825                         /* die Server-ID gibt es bereits */
826                         snprintf( str, sizeof( str ), "ID \"%s\" already registered", ID );
827                         if( Client->conn_id != c->conn_id ) Log( LOG_ERR, "%s (on connection %d)!", str, c->conn_id );
828                         else Log( LOG_ERR, "%s (via network)!", str );
829                         Conn_Close( Client->conn_id, str, str, TRUE );
830                         return FALSE;
831                 }
832                 c = (CLIENT *)c->next;
833         }
834
835         return TRUE;
836 } /* Client_CheckID */
837
838
839 GLOBAL CLIENT *
840 Client_First( VOID )
841 {
842         /* Ersten Client liefern. */
843
844         return My_Clients;
845 } /* Client_First */
846
847
848 GLOBAL CLIENT *
849 Client_Next( CLIENT *c )
850 {
851         /* Naechsten Client liefern. Existiert keiner,
852          * so wird NULL geliefert. */
853
854         assert( c != NULL );
855         return (CLIENT *)c->next;
856 } /* Client_Next */
857
858
859 GLOBAL LONG
860 Client_UserCount( VOID )
861 {
862         return Count( CLIENT_USER );
863 } /* Client_UserCount */
864
865
866 GLOBAL LONG
867 Client_ServiceCount( VOID )
868 {
869         return Count( CLIENT_SERVICE );;
870 } /* Client_ServiceCount */
871
872
873 GLOBAL LONG
874 Client_ServerCount( VOID )
875 {
876         return Count( CLIENT_SERVER );
877 } /* Client_ServerCount */
878
879
880 GLOBAL LONG
881 Client_MyUserCount( VOID )
882 {
883         return MyCount( CLIENT_USER );
884 } /* Client_MyUserCount */
885
886
887 GLOBAL LONG
888 Client_MyServiceCount( VOID )
889 {
890         return MyCount( CLIENT_SERVICE );
891 } /* Client_MyServiceCount */
892
893
894 GLOBAL LONG
895 Client_MyServerCount( VOID )
896 {
897         CLIENT *c;
898         LONG cnt;
899
900         cnt = 0;
901         c = My_Clients;
902         while( c )
903         {
904                 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
905                 c = (CLIENT *)c->next;
906         }
907         return cnt;
908 } /* Client_MyServerCount */
909
910
911 GLOBAL LONG
912 Client_OperCount( VOID )
913 {
914         CLIENT *c;
915         LONG cnt;
916
917         cnt = 0;
918         c = My_Clients;
919         while( c )
920         {
921                 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
922                 c = (CLIENT *)c->next;
923         }
924         return cnt;
925 } /* Client_OperCount */
926
927
928 GLOBAL LONG
929 Client_UnknownCount( VOID )
930 {
931         CLIENT *c;
932         LONG cnt;
933
934         cnt = 0;
935         c = My_Clients;
936         while( c )
937         {
938                 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
939                 c = (CLIENT *)c->next;
940         }
941         return cnt;
942 } /* Client_UnknownCount */
943
944
945 GLOBAL LONG
946 Client_MaxUserCount( VOID )
947 {
948         return Max_Users;
949 } /* Client_MaxUserCount */
950
951
952 GLOBAL LONG
953 Client_MyMaxUserCount( VOID )
954 {
955         return My_Max_Users;
956 } /* Client_MyMaxUserCount */
957
958
959 GLOBAL BOOLEAN
960 Client_IsValidNick( CHAR *Nick )
961 {
962         /* Ist der Nick gueltig? */
963
964         CHAR *ptr, goodchars[20];
965         
966         assert( Nick != NULL );
967
968         strcpy( goodchars, ";0123456789-" );
969
970         if( Nick[0] == '#' ) return FALSE;
971         if( strchr( goodchars, Nick[0] )) return FALSE;
972         if( strlen( Nick ) >= CLIENT_NICK_LEN ) return FALSE;
973
974         ptr = Nick;
975         while( *ptr )
976         {
977                 if(( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return FALSE;
978                 if(( *ptr > '}' ) && ( ! strchr( goodchars, *ptr ))) return FALSE;
979                 ptr++;
980         }
981         
982         return TRUE;
983 } /* Client_IsValidNick */
984
985
986 LOCAL LONG
987 Count( CLIENT_TYPE Type )
988 {
989         CLIENT *c;
990         LONG cnt;
991
992         cnt = 0;
993         c = My_Clients;
994         while( c )
995         {
996                 if( c->type == Type ) cnt++;
997                 c = (CLIENT *)c->next;
998         }
999         return cnt;
1000 } /* Count */
1001
1002
1003 LOCAL LONG
1004 MyCount( CLIENT_TYPE Type )
1005 {
1006         CLIENT *c;
1007         LONG cnt;
1008
1009         cnt = 0;
1010         c = My_Clients;
1011         while( c )
1012         {
1013                 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1014                 c = (CLIENT *)c->next;
1015         }
1016         return cnt;
1017 } /* MyCount */
1018
1019
1020 LOCAL CLIENT *
1021 New_Client_Struct( VOID )
1022 {
1023         /* Neue CLIENT-Struktur pre-initialisieren */
1024         
1025         CLIENT *c;
1026         
1027         c = (CLIENT *)malloc( sizeof( CLIENT ));
1028         if( ! c )
1029         {
1030                 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1031                 return NULL;
1032         }
1033
1034         c->next = NULL;
1035         c->hash = 0;
1036         c->type = CLIENT_UNKNOWN;
1037         c->conn_id = NONE;
1038         c->introducer = NULL;
1039         c->topserver = NULL;
1040         strcpy( c->id, "" );
1041         strcpy( c->pwd, "" );
1042         strcpy( c->host, "" );
1043         strcpy( c->user, "" );
1044         strcpy( c->info, "" );
1045         strcpy( c->modes, "" );
1046         c->oper_by_me = FALSE;
1047         c->hops = -1;
1048         c->token = -1;
1049         c->mytoken = -1;
1050         strcpy( c->away, "" );
1051         strcpy( c->flags, "" );
1052
1053         return c;
1054 } /* New_Client */
1055
1056
1057 LOCAL VOID
1058 Generate_MyToken( CLIENT *Client )
1059 {
1060         CLIENT *c;
1061         INT token;
1062
1063         c = My_Clients;
1064         token = 2;
1065         while( c )
1066         {
1067                 if( c->mytoken == token )
1068                 {
1069                         /* Das Token wurde bereits vergeben */
1070                         token++;
1071                         c = My_Clients;
1072                         continue;
1073                 }
1074                 else c = (CLIENT *)c->next;
1075         }
1076         Client->mytoken = token;
1077         Log( LOG_DEBUG, "Assigned token %d to server \"%s\".", token, Client->id );
1078 } /* Generate_MyToken */
1079
1080
1081 LOCAL VOID
1082 Adjust_Counters( CLIENT *Client )
1083 {
1084         LONG count;
1085
1086         assert( Client != NULL );
1087
1088         if( Client->type != CLIENT_USER ) return;
1089         
1090         if( Client->conn_id != NONE )
1091         {
1092                 /* Local connection */
1093                 count = Client_MyUserCount( );
1094                 if( count > My_Max_Users ) My_Max_Users = count;
1095         }
1096         count = Client_UserCount( );
1097         if( count > Max_Users ) Max_Users = count;
1098 } /* Adjust_Counters */
1099
1100
1101 /* -eof- */