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