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