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