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