]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
dee2ada5de29ad8834d7309e88c3e7aef1dafc48
[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.67 2002/12/22 23:29:09 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 ) strcpy( This_Server->host, h->h_name );
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' )) strcpy( client->away, DEFAULT_AWAY_MSG );
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 ) sprintf( 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         strncpy( Client->host, Hostname, CLIENT_HOST_LEN - 1 );
304         Client->host[CLIENT_HOST_LEN - 1] = '\0';
305 } /* Client_SetHostname */
306
307
308 GLOBAL VOID
309 Client_SetID( CLIENT *Client, CHAR *ID )
310 {
311         /* Hostname eines Clients setzen, Hash-Wert berechnen */
312
313         assert( Client != NULL );
314         assert( ID != NULL );
315         
316         strncpy( Client->id, ID, CLIENT_ID_LEN - 1 );
317         Client->id[CLIENT_ID_LEN - 1] = '\0';
318
319         /* Hash */
320         Client->hash = Hash( Client->id );
321 } /* Client_SetID */
322
323
324 GLOBAL VOID
325 Client_SetUser( CLIENT *Client, CHAR *User, BOOLEAN Idented )
326 {
327         /* Username eines Clients setzen */
328
329         assert( Client != NULL );
330         assert( User != NULL );
331         
332         if( Idented ) strncpy( Client->user, User, CLIENT_USER_LEN - 1 );
333         else
334         {
335                 Client->user[0] = '~';
336                 strncpy( Client->user + 1, User, CLIENT_USER_LEN - 2 );
337         }
338         Client->user[CLIENT_USER_LEN - 1] = '\0';
339 } /* Client_SetUser */
340
341
342 GLOBAL VOID
343 Client_SetInfo( CLIENT *Client, CHAR *Info )
344 {
345         /* Hostname eines Clients setzen */
346
347         assert( Client != NULL );
348         assert( Info != NULL );
349         
350         strncpy( Client->info, Info, CLIENT_INFO_LEN - 1 );
351         Client->info[CLIENT_INFO_LEN - 1] = '\0';
352 } /* Client_SetInfo */
353
354
355 GLOBAL VOID
356 Client_SetModes( CLIENT *Client, CHAR *Modes )
357 {
358         /* Modes eines Clients setzen */
359
360         assert( Client != NULL );
361         assert( Modes != NULL );
362
363         strncpy( Client->modes, Modes, CLIENT_MODE_LEN - 1 );
364         Client->modes[CLIENT_MODE_LEN - 1] = '\0';
365 } /* Client_SetModes */
366
367
368 GLOBAL VOID
369 Client_SetFlags( CLIENT *Client, CHAR *Flags )
370 {
371         /* Flags eines Clients setzen */
372
373         assert( Client != NULL );
374         assert( Flags != NULL );
375
376         strncpy( Client->flags, Flags, CLIENT_FLAGS_LEN - 1 );
377         Client->flags[CLIENT_FLAGS_LEN - 1] = '\0';
378 } /* Client_SetFlags */
379
380
381 GLOBAL VOID
382 Client_SetPassword( CLIENT *Client, CHAR *Pwd )
383 {
384         /* Von einem Client geliefertes Passwort */
385
386         assert( Client != NULL );
387         assert( Pwd != NULL );
388         
389         strncpy( Client->pwd, Pwd, CLIENT_PASS_LEN - 1 );
390         Client->pwd[CLIENT_PASS_LEN - 1] = '\0';
391 } /* Client_SetPassword */
392
393
394 GLOBAL VOID
395 Client_SetAway( CLIENT *Client, CHAR *Txt )
396 {
397         /* Von einem Client gelieferte AWAY-Nachricht */
398
399         assert( Client != NULL );
400
401         if( Txt )
402         {
403                 /* Client AWAY setzen */
404                 strncpy( Client->away, Txt, CLIENT_AWAY_LEN - 1 );
405                 Client->away[CLIENT_AWAY_LEN - 1] = '\0';
406                 Client_ModeAdd( Client, 'a' );
407                 Log( LOG_DEBUG, "User \"%s\" is away: %s", Client_Mask( Client ), Txt );
408         }
409         else
410         {
411                 /* AWAY loeschen */
412                 Client_ModeDel( Client, 'a' );
413                 Log( LOG_DEBUG, "User \"%s\" is no longer away.", Client_Mask( Client ));
414         }
415 } /* Client_SetAway */
416
417
418 GLOBAL VOID
419 Client_SetType( CLIENT *Client, INT Type )
420 {
421         assert( Client != NULL );
422         Client->type = Type;
423         if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
424         Adjust_Counters( Client );
425 } /* Client_SetType */
426
427
428 GLOBAL VOID
429 Client_SetHops( CLIENT *Client, INT Hops )
430 {
431         assert( Client != NULL );
432         Client->hops = Hops;
433 } /* Client_SetHops */
434
435
436 GLOBAL VOID
437 Client_SetToken( CLIENT *Client, INT Token )
438 {
439         assert( Client != NULL );
440         Client->token = Token;
441 } /* Client_SetToken */
442
443
444 GLOBAL VOID
445 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
446 {
447         assert( Client != NULL );
448         assert( Introducer != NULL );
449         Client->introducer = Introducer;
450 } /* Client_SetIntroducer */
451
452
453 GLOBAL VOID
454 Client_SetOperByMe( CLIENT *Client, BOOLEAN OperByMe )
455 {
456         assert( Client != NULL );
457         Client->oper_by_me = OperByMe;
458 } /* Client_SetOperByMe */
459
460
461 GLOBAL BOOLEAN
462 Client_ModeAdd( CLIENT *Client, CHAR Mode )
463 {
464         /* Mode soll gesetzt werden. TRUE wird geliefert, wenn der
465          * Mode neu gesetzt wurde, FALSE, wenn der Client den Mode
466          * bereits hatte. */
467
468         CHAR x[2];
469         
470         assert( Client != NULL );
471
472         x[0] = Mode; x[1] = '\0';
473         if( ! strchr( Client->modes, x[0] ))
474         {
475                 /* Client hat den Mode noch nicht -> setzen */
476                 strcat( Client->modes, x );
477                 return TRUE;
478         }
479         else return FALSE;
480 } /* Client_ModeAdd */
481
482
483 GLOBAL BOOLEAN
484 Client_ModeDel( CLIENT *Client, CHAR Mode )
485 {
486         /* Mode soll geloescht werden. TRUE wird geliefert, wenn der
487         * Mode entfernt wurde, FALSE, wenn der Client den Mode
488         * ueberhaupt nicht hatte. */
489
490         CHAR x[2], *p;
491
492         assert( Client != NULL );
493
494         x[0] = Mode; x[1] = '\0';
495
496         p = strchr( Client->modes, x[0] );
497         if( ! p ) return FALSE;
498
499         /* Client hat den Mode -> loeschen */
500         while( *p )
501         {
502                 *p = *(p + 1);
503                 p++;
504         }
505         return TRUE;
506 } /* Client_ModeDel */
507
508
509 GLOBAL CLIENT *
510 Client_GetFromConn( CONN_ID Idx )
511 {
512         /* Client-Struktur, die zur lokalen Verbindung Idx gehoert,
513          * liefern. Wird keine gefunden, so wird NULL geliefert. */
514
515         CLIENT *c;
516
517         assert( Idx >= 0 );
518         
519         c = My_Clients;
520         while( c )
521         {
522                 if( c->conn_id == Idx ) return c;
523                 c = (CLIENT *)c->next;
524         }
525         return NULL;
526 } /* Client_GetFromConn */
527
528
529 GLOBAL CLIENT *
530 Client_Search( CHAR *Nick )
531 {
532         /* Client-Struktur, die den entsprechenden Nick hat, liefern.
533          * Wird keine gefunden, so wird NULL geliefert. */
534
535         CHAR search_id[CLIENT_ID_LEN], *ptr;
536         CLIENT *c = NULL;
537         UINT32 search_hash;
538
539         assert( Nick != NULL );
540
541         /* Nick kopieren und ggf. Host-Mask abschneiden */
542         strncpy( search_id, Nick, CLIENT_ID_LEN - 1 );
543         search_id[CLIENT_ID_LEN - 1] = '\0';
544         ptr = strchr( search_id, '!' );
545         if( ptr ) *ptr = '\0';
546
547         search_hash = Hash( search_id );
548
549         c = My_Clients;
550         while( c )
551         {
552                 if( c->hash == search_hash )
553                 {
554                         /* lt. Hash-Wert: Treffer! */
555                         if( strcasecmp( c->id, search_id ) == 0 ) return c;
556                 }
557                 c = (CLIENT *)c->next;
558         }
559         return NULL;
560 } /* Client_Search */
561
562
563 GLOBAL CLIENT *
564 Client_GetFromToken( CLIENT *Client, INT Token )
565 {
566         /* Client-Struktur, die den entsprechenden Introducer (=Client)
567          * und das gegebene Token hat, liefern. Wird keine gefunden,
568          * so wird NULL geliefert. */
569
570         CLIENT *c;
571
572         assert( Client != NULL );
573         assert( Token > 0 );
574
575         c = My_Clients;
576         while( c )
577         {
578                 if(( c->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c->token == Token )) return c;
579                 c = (CLIENT *)c->next;
580         }
581         return NULL;
582 } /* Client_GetFromToken */
583
584
585 GLOBAL INT
586 Client_Type( CLIENT *Client )
587 {
588         assert( Client != NULL );
589         return Client->type;
590 } /* Client_Type */
591
592
593 GLOBAL CONN_ID
594 Client_Conn( CLIENT *Client )
595 {
596         assert( Client != NULL );
597         return Client->conn_id;
598 } /* Client_Conn */
599
600
601 GLOBAL CHAR *
602 Client_ID( CLIENT *Client )
603 {
604         assert( Client != NULL );
605
606 #ifdef DEBUG
607         if( Client->type == CLIENT_USER ) assert( strlen( Client->id ) < CLIENT_NICK_LEN );
608 #endif
609                                                    
610         if( Client->id[0] ) return Client->id;
611         else return "*";
612 } /* Client_ID */
613
614
615 GLOBAL CHAR *
616 Client_Info( CLIENT *Client )
617 {
618         assert( Client != NULL );
619         return Client->info;
620 } /* Client_Info */
621
622
623 GLOBAL CHAR *
624 Client_User( CLIENT *Client )
625 {
626         assert( Client != NULL );
627         if( Client->user[0] ) return Client->user;
628         else return "~";
629 } /* Client_User */
630
631
632 GLOBAL CHAR *
633 Client_Hostname( CLIENT *Client )
634 {
635         assert( Client != NULL );
636         return Client->host;
637 } /* Client_Hostname */
638
639
640 GLOBAL CHAR *
641 Client_Password( CLIENT *Client )
642 {
643         assert( Client != NULL );
644         return Client->pwd;
645 } /* Client_Password */
646
647
648 GLOBAL CHAR *
649 Client_Modes( CLIENT *Client )
650 {
651         assert( Client != NULL );
652         return Client->modes;
653 } /* Client_Modes */
654
655
656 GLOBAL CHAR *
657 Client_Flags( CLIENT *Client )
658 {
659         assert( Client != NULL );
660         return Client->flags;
661 } /* Client_Flags */
662
663
664 GLOBAL BOOLEAN
665 Client_OperByMe( CLIENT *Client )
666 {
667         assert( Client != NULL );
668         return Client->oper_by_me;
669 } /* Client_OperByMe */
670
671
672 GLOBAL INT
673 Client_Hops( CLIENT *Client )
674 {
675         assert( Client != NULL );
676         return Client->hops;
677 } /* Client_Hops */
678
679
680 GLOBAL INT
681 Client_Token( CLIENT *Client )
682 {
683         assert( Client != NULL );
684         return Client->token;
685 } /* Client_Token */
686
687
688 GLOBAL INT
689 Client_MyToken( CLIENT *Client )
690 {
691         assert( Client != NULL );
692         return Client->mytoken;
693 } /* Client_MyToken */
694
695
696 GLOBAL CLIENT *
697 Client_NextHop( CLIENT *Client )
698 {
699         CLIENT *c;
700         
701         assert( Client != NULL );
702
703         c = Client;
704         while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server )) c = c->introducer;
705         return c;
706 } /* Client_NextHop */
707
708
709 GLOBAL CHAR *
710 Client_Mask( CLIENT *Client )
711 {
712         /* Client-"ID" liefern, wie sie z.B. fuer
713          * Prefixe benoetigt wird. */
714
715         assert( Client != NULL );
716         
717         if( Client->type == CLIENT_SERVER ) return Client->id;
718
719         snprintf( GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host );
720         return GetID_Buffer;
721 } /* Client_Mask */
722
723
724 GLOBAL CLIENT *
725 Client_Introducer( CLIENT *Client )
726 {
727         assert( Client != NULL );
728         return Client->introducer;
729 } /* Client_Introducer */
730
731
732 GLOBAL CLIENT *
733 Client_TopServer( CLIENT *Client )
734 {
735         assert( Client != NULL );
736         return Client->topserver;
737 } /* Client_TopServer */
738
739
740 GLOBAL BOOLEAN
741 Client_HasMode( CLIENT *Client, CHAR Mode )
742 {
743         assert( Client != NULL );
744         return strchr( Client->modes, Mode ) != NULL;
745 } /* Client_HasMode */
746
747
748 GLOBAL CHAR *
749 Client_Away( CLIENT *Client )
750 {
751         /* AWAY-Text liefern */
752
753         assert( Client != NULL );
754         return Client->away;
755 } /* Client_Away */
756
757
758 GLOBAL BOOLEAN
759 Client_CheckNick( CLIENT *Client, CHAR *Nick )
760 {
761         /* Nick ueberpruefen */
762
763         assert( Client != NULL );
764         assert( Nick != NULL );
765         
766         /* Nick ungueltig? */
767         if( ! Client_IsValidNick( Nick ))
768         {
769                 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
770                 return FALSE;
771         }
772
773         /* Nick bereits vergeben? */
774         if( Client_Search( Nick ))
775         {
776                 /* den Nick gibt es bereits */
777                 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
778                 return FALSE;
779         }
780
781         return TRUE;
782 } /* Client_CheckNick */
783
784
785 GLOBAL BOOLEAN
786 Client_CheckID( CLIENT *Client, CHAR *ID )
787 {
788         /* Nick ueberpruefen */
789
790         CHAR str[COMMAND_LEN];
791         CLIENT *c;
792
793         assert( Client != NULL );
794         assert( Client->conn_id > NONE );
795         assert( ID != NULL );
796
797         /* Nick zu lang? */
798         if( strlen( ID ) > CLIENT_ID_LEN )
799         {
800                 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), ID );
801                 return FALSE;
802         }
803
804         /* ID bereits vergeben? */
805         c = My_Clients;
806         while( c )
807         {
808                 if( strcasecmp( c->id, ID ) == 0 )
809                 {
810                         /* die Server-ID gibt es bereits */
811                         sprintf( str, "ID \"%s\" already registered", ID );
812                         if( Client->conn_id != c->conn_id ) Log( LOG_ERR, "%s (on connection %d)!", str, c->conn_id );
813                         else Log( LOG_ERR, "%s (via network)!", str );
814                         Conn_Close( Client->conn_id, str, str, TRUE );
815                         return FALSE;
816                 }
817                 c = (CLIENT *)c->next;
818         }
819
820         return TRUE;
821 } /* Client_CheckID */
822
823
824 GLOBAL CLIENT *
825 Client_First( VOID )
826 {
827         /* Ersten Client liefern. */
828
829         return My_Clients;
830 } /* Client_First */
831
832
833 GLOBAL CLIENT *
834 Client_Next( CLIENT *c )
835 {
836         /* Naechsten Client liefern. Existiert keiner,
837          * so wird NULL geliefert. */
838
839         assert( c != NULL );
840         return (CLIENT *)c->next;
841 } /* Client_Next */
842
843
844 GLOBAL LONG
845 Client_UserCount( VOID )
846 {
847         return Count( CLIENT_USER );
848 } /* Client_UserCount */
849
850
851 GLOBAL LONG
852 Client_ServiceCount( VOID )
853 {
854         return Count( CLIENT_SERVICE );;
855 } /* Client_ServiceCount */
856
857
858 GLOBAL LONG
859 Client_ServerCount( VOID )
860 {
861         return Count( CLIENT_SERVER );
862 } /* Client_ServerCount */
863
864
865 GLOBAL LONG
866 Client_MyUserCount( VOID )
867 {
868         return MyCount( CLIENT_USER );
869 } /* Client_MyUserCount */
870
871
872 GLOBAL LONG
873 Client_MyServiceCount( VOID )
874 {
875         return MyCount( CLIENT_SERVICE );
876 } /* Client_MyServiceCount */
877
878
879 GLOBAL LONG
880 Client_MyServerCount( VOID )
881 {
882         CLIENT *c;
883         LONG cnt;
884
885         cnt = 0;
886         c = My_Clients;
887         while( c )
888         {
889                 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
890                 c = (CLIENT *)c->next;
891         }
892         return cnt;
893 } /* Client_MyServerCount */
894
895
896 GLOBAL LONG
897 Client_OperCount( VOID )
898 {
899         CLIENT *c;
900         LONG cnt;
901
902         cnt = 0;
903         c = My_Clients;
904         while( c )
905         {
906                 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
907                 c = (CLIENT *)c->next;
908         }
909         return cnt;
910 } /* Client_OperCount */
911
912
913 GLOBAL LONG
914 Client_UnknownCount( VOID )
915 {
916         CLIENT *c;
917         LONG cnt;
918
919         cnt = 0;
920         c = My_Clients;
921         while( c )
922         {
923                 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
924                 c = (CLIENT *)c->next;
925         }
926         return cnt;
927 } /* Client_UnknownCount */
928
929
930 GLOBAL LONG
931 Client_MaxUserCount( VOID )
932 {
933         return Max_Users;
934 } /* Client_MaxUserCount */
935
936
937 GLOBAL LONG
938 Client_MyMaxUserCount( VOID )
939 {
940         return My_Max_Users;
941 } /* Client_MyMaxUserCount */
942
943
944 GLOBAL BOOLEAN
945 Client_IsValidNick( CHAR *Nick )
946 {
947         /* Ist der Nick gueltig? */
948
949         CHAR *ptr, goodchars[20];
950         
951         assert( Nick != NULL );
952
953         strcpy( goodchars, ";0123456789-" );
954
955         if( Nick[0] == '#' ) return FALSE;
956         if( strchr( goodchars, Nick[0] )) return FALSE;
957         if( strlen( Nick ) >= CLIENT_NICK_LEN ) return FALSE;
958
959         ptr = Nick;
960         while( *ptr )
961         {
962                 if(( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return FALSE;
963                 if(( *ptr > '}' ) && ( ! strchr( goodchars, *ptr ))) return FALSE;
964                 ptr++;
965         }
966         
967         return TRUE;
968 } /* Client_IsValidNick */
969
970
971 LOCAL LONG
972 Count( CLIENT_TYPE Type )
973 {
974         CLIENT *c;
975         LONG cnt;
976
977         cnt = 0;
978         c = My_Clients;
979         while( c )
980         {
981                 if( c->type == Type ) cnt++;
982                 c = (CLIENT *)c->next;
983         }
984         return cnt;
985 } /* Count */
986
987
988 LOCAL LONG
989 MyCount( CLIENT_TYPE Type )
990 {
991         CLIENT *c;
992         LONG cnt;
993
994         cnt = 0;
995         c = My_Clients;
996         while( c )
997         {
998                 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
999                 c = (CLIENT *)c->next;
1000         }
1001         return cnt;
1002 } /* MyCount */
1003
1004
1005 LOCAL CLIENT *
1006 New_Client_Struct( VOID )
1007 {
1008         /* Neue CLIENT-Struktur pre-initialisieren */
1009         
1010         CLIENT *c;
1011         
1012         c = malloc( sizeof( CLIENT ));
1013         if( ! c )
1014         {
1015                 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1016                 return NULL;
1017         }
1018
1019         c->next = NULL;
1020         c->hash = 0;
1021         c->type = CLIENT_UNKNOWN;
1022         c->conn_id = NONE;
1023         c->introducer = NULL;
1024         c->topserver = NULL;
1025         strcpy( c->id, "" );
1026         strcpy( c->pwd, "" );
1027         strcpy( c->host, "" );
1028         strcpy( c->user, "" );
1029         strcpy( c->info, "" );
1030         strcpy( c->modes, "" );
1031         c->oper_by_me = FALSE;
1032         c->hops = -1;
1033         c->token = -1;
1034         c->mytoken = -1;
1035         strcpy( c->away, "" );
1036         strcpy( c->flags, "" );
1037
1038         return c;
1039 } /* New_Client */
1040
1041
1042 LOCAL VOID
1043 Generate_MyToken( CLIENT *Client )
1044 {
1045         CLIENT *c;
1046         INT token;
1047
1048         c = My_Clients;
1049         token = 2;
1050         while( c )
1051         {
1052                 if( c->mytoken == token )
1053                 {
1054                         /* Das Token wurde bereits vergeben */
1055                         token++;
1056                         c = My_Clients;
1057                         continue;
1058                 }
1059                 else c = (CLIENT *)c->next;
1060         }
1061         Client->mytoken = token;
1062         Log( LOG_DEBUG, "Assigned token %d to server \"%s\".", token, Client->id );
1063 } /* Generate_MyToken */
1064
1065
1066 LOCAL VOID
1067 Adjust_Counters( CLIENT *Client )
1068 {
1069         LONG count;
1070
1071         assert( Client != NULL );
1072
1073         if( Client->type != CLIENT_USER ) return;
1074         
1075         if( Client->conn_id != NONE )
1076         {
1077                 /* Local connection */
1078                 count = Client_MyUserCount( );
1079                 if( count > My_Max_Users ) My_Max_Users = count;
1080         }
1081         count = Client_UserCount( );
1082         if( count > Max_Users ) Max_Users = count;
1083 } /* Adjust_Counters */
1084
1085
1086 /* -eof- */