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