]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
ClientUserNick setting
[ngircd-alex.git] / src / ngircd / client.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2010 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
12 #define __client_c__
13
14 #include "portab.h"
15
16 /**
17  * @file
18  * Client management.
19  */
20
21 #include "imp.h"
22 #include <assert.h>
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <strings.h>
28 #include <netdb.h>
29
30 #include "defines.h"
31 #include "conn.h"
32
33 #include "exp.h"
34 #include "client.h"
35
36 #include <imp.h>
37 #include "ngircd.h"
38 #include "channel.h"
39 #include "conf.h"
40 #include "hash.h"
41 #include "irc-write.h"
42 #include "log.h"
43 #include "messages.h"
44
45 #include <exp.h>
46
47 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
48
49 static CLIENT *This_Server, *My_Clients;
50
51 static WHOWAS My_Whowas[MAX_WHOWAS];
52 static int Last_Whowas = -1;
53 static long Max_Users, My_Max_Users;
54
55
56 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
57 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
58
59 static CLIENT *New_Client_Struct PARAMS(( void ));
60 static void Generate_MyToken PARAMS(( CLIENT *Client ));
61 static void Adjust_Counters PARAMS(( CLIENT *Client ));
62
63 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
64                                        CLIENT *TopServer, int Type, const char *ID,
65                                        const char *User, const char *Hostname, const char *Info,
66                                        int Hops, int Token, const char *Modes,
67                                        bool Idented));
68
69 static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
70                                         bool SendQuit));
71
72
73 GLOBAL void
74 Client_Init( void )
75 {
76         struct hostent *h;
77         
78         This_Server = New_Client_Struct( );
79         if( ! This_Server )
80         {
81                 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
82                 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
83                 exit( 1 );
84         }
85
86         /* Client-Struktur dieses Servers */
87         This_Server->next = NULL;
88         This_Server->type = CLIENT_SERVER;
89         This_Server->conn_id = NONE;
90         This_Server->introducer = This_Server;
91         This_Server->mytoken = 1;
92         This_Server->hops = 0;
93
94         gethostname( This_Server->host, CLIENT_HOST_LEN );
95         if (Conf_DNS) {
96                 h = gethostbyname( This_Server->host );
97                 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
98         }
99         Client_SetID( This_Server, Conf_ServerName );
100         Client_SetInfo( This_Server, Conf_ServerInfo );
101
102         My_Clients = This_Server;
103         
104         memset( &My_Whowas, 0, sizeof( My_Whowas ));
105 } /* Client_Init */
106
107
108 GLOBAL void
109 Client_Exit( void )
110 {
111         CLIENT *c, *next;
112         int cnt;
113
114         if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
115         else Client_Destroy( This_Server, "Server going down.", NULL, false );
116         
117         cnt = 0;
118         c = My_Clients;
119         while( c )
120         {
121                 cnt++;
122                 next = (CLIENT *)c->next;
123                 free( c );
124                 c = next;
125         }
126         if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
127 } /* Client_Exit */
128
129
130 GLOBAL CLIENT *
131 Client_ThisServer( void )
132 {
133         return This_Server;
134 } /* Client_ThisServer */
135
136
137 /**
138  * Initialize new local client; wrapper function for Init_New_Client().
139  * @return New CLIENT structure.
140  */
141 GLOBAL CLIENT *
142 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
143 {
144         return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
145                 Hostname, NULL, 0, 0, NULL, Idented);
146 } /* Client_NewLocal */
147
148
149 /**
150  * Initialize new remote server; wrapper function for Init_New_Client().
151  * @return New CLIENT structure.
152  */
153 GLOBAL CLIENT *
154 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
155  int Hops, int Token, const char *Info, bool Idented)
156 {
157         return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
158                 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
159 } /* Client_NewRemoteServer */
160
161
162 /**
163  * Initialize new remote client; wrapper function for Init_New_Client().
164  * @return New CLIENT structure.
165  */
166 GLOBAL CLIENT *
167 Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
168  const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
169 {
170         return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
171                 User, Hostname, Info, Hops, Token, Modes, Idented);
172 } /* Client_NewRemoteUser */
173
174
175 /**
176  * Initialize new client and set up the given parameters like client type,
177  * user name, host name, introducing server etc. ...
178  * @return New CLIENT structure.
179  */
180 static CLIENT *
181 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
182   int Type, const char *ID, const char *User, const char *Hostname,
183   const char *Info, int Hops, int Token, const char *Modes, bool Idented)
184 {
185         CLIENT *client;
186
187         assert(Idx >= NONE);
188         assert(Introducer != NULL);
189         assert(Hostname != NULL);
190
191         client = New_Client_Struct();
192         if (!client)
193                 return NULL;
194
195         client->starttime = time(NULL);
196         client->conn_id = Idx;
197         client->introducer = Introducer;
198         client->topserver = TopServer;
199         client->type = Type;
200         if (ID)
201                 Client_SetID(client, ID);
202         if (User) {
203                 Client_SetUser(client, User, Idented);
204                 Client_SetOrigUser(client, User);
205         }
206         if (Hostname)
207                 Client_SetHostname(client, Hostname);
208         if (Info)
209                 Client_SetInfo(client, Info);
210         client->hops = Hops;
211         client->token = Token;
212         if (Modes)
213                 Client_SetModes(client, Modes);
214         if (Type == CLIENT_SERVER)
215                 Generate_MyToken(client);
216
217         if (strchr(client->modes, 'a'))
218                 strlcpy(client->away, DEFAULT_AWAY_MSG, sizeof(client->away));
219
220         client->next = (POINTER *)My_Clients;
221         My_Clients = client;
222
223         Adjust_Counters(client);
224
225         return client;
226 } /* Init_New_Client */
227
228
229 GLOBAL void
230 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
231 {
232         /* remove a client */
233         
234         CLIENT *last, *c;
235         char msg[LINE_LEN];
236         const char *txt;
237
238         assert( Client != NULL );
239
240         if( LogMsg ) txt = LogMsg;
241         else txt = FwdMsg;
242         if( ! txt ) txt = "Reason unknown.";
243
244         /* netsplit message */
245         if( Client->type == CLIENT_SERVER ) {
246                 strlcpy(msg, This_Server->id, sizeof (msg));
247                 strlcat(msg, " ", sizeof (msg));
248                 strlcat(msg, Client->id, sizeof (msg));
249         }
250
251         last = NULL;
252         c = My_Clients;
253         while( c )
254         {
255                 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
256                 {
257                         /*
258                          * The client that is about to be removed is a server,
259                          * the client we are checking right now is a child of that
260                          * server and thus has to be removed, too.
261                          *
262                          * Call Client_Destroy() recursively with the server as the
263                          * new "object to be removed". This starts the cycle again, until
264                          * all servers that are linked via the original server have been
265                          * removed.
266                          */
267                         Client_Destroy( c, NULL, msg, false );
268                         last = NULL;
269                         c = My_Clients;
270                         continue;
271                 }
272                 if( c == Client )
273                 {
274                         /* found  the client: remove it */
275                         if( last ) last->next = c->next;
276                         else My_Clients = (CLIENT *)c->next;
277
278                         if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
279                                 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
280                         else if( c->type == CLIENT_SERVER )
281                         {
282                                 if( c != This_Server )
283                                 {
284                                         if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
285                                         else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
286                                 }
287
288                                 /* inform other servers */
289                                 if( ! NGIRCd_SignalQuit )
290                                 {
291                                         if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
292                                         else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
293                                 }
294                         }
295                         else
296                         {
297                                 if( c->conn_id != NONE )
298                                 {
299                                         if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
300                                         else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
301                                 } else {
302                                         Log(LOG_WARNING, "Unregistered unknown client \"%s\": %s",
303                                                                 c->id[0] ? c->id : "(No Nick)", txt );
304                                 }
305                         }
306
307                         free( c );
308                         break;
309                 }
310                 last = c;
311                 c = (CLIENT *)c->next;
312         }
313 } /* Client_Destroy */
314
315
316 GLOBAL void
317 Client_SetHostname( CLIENT *Client, const char *Hostname )
318 {
319         assert( Client != NULL );
320         assert( Hostname != NULL );
321
322         if (strlen(Conf_ClientHost)) {
323                 strlcpy( Client->host, Conf_ClientHost, sizeof( Client->host ));
324         } else {
325                 strlcpy( Client->host, Hostname, sizeof( Client->host ));
326         }
327 } /* Client_SetHostname */
328
329
330 GLOBAL void
331 Client_SetID( CLIENT *Client, const char *ID )
332 {
333         assert( Client != NULL );
334         assert( ID != NULL );
335         
336         strlcpy( Client->id, ID, sizeof( Client->id ));
337
338         if (Conf_ClientUserNick)
339                 strlcpy( Client->user, ID, sizeof( Client->user ));
340
341         /* Hash */
342         Client->hash = Hash( Client->id );
343 } /* Client_SetID */
344
345
346 GLOBAL void
347 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
348 {
349         /* set clients username */
350
351         assert( Client != NULL );
352         assert( User != NULL );
353
354         if (Conf_ClientUserNick) return;
355
356         if (Idented) {
357                 strlcpy(Client->user, User, sizeof(Client->user));
358         } else {
359                 Client->user[0] = '~';
360                 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
361         }
362 } /* Client_SetUser */
363
364
365 /**
366  * Set "original" user name of a client.
367  * This function saves the "original" user name, the user name specified by
368  * the peer using the USER command, into the CLIENT structure. This user
369  * name may be used for authentication, for example.
370  * @param Client The client.
371  * @param User User name to set.
372  */
373 GLOBAL void
374 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
375 {
376         assert(Client != NULL);
377         assert(User != NULL);
378
379 #if defined(PAM) && defined(IDENTAUTH)
380         strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
381 #endif
382 } /* Client_SetOrigUser */
383
384
385 GLOBAL void
386 Client_SetInfo( CLIENT *Client, const char *Info )
387 {
388         /* set client hostname */
389
390         assert( Client != NULL );
391         assert( Info != NULL );
392
393         strlcpy(Client->info, Info, sizeof(Client->info));
394 } /* Client_SetInfo */
395
396
397 GLOBAL void
398 Client_SetModes( CLIENT *Client, const char *Modes )
399 {
400         assert( Client != NULL );
401         assert( Modes != NULL );
402
403         strlcpy(Client->modes, Modes, sizeof( Client->modes ));
404 } /* Client_SetModes */
405
406
407 GLOBAL void
408 Client_SetFlags( CLIENT *Client, const char *Flags )
409 {
410         assert( Client != NULL );
411         assert( Flags != NULL );
412
413         strlcpy(Client->flags, Flags, sizeof(Client->flags));
414 } /* Client_SetFlags */
415
416
417 GLOBAL void
418 Client_SetPassword( CLIENT *Client, const char *Pwd )
419 {
420         /* set password sent by client */
421
422         assert( Client != NULL );
423         assert( Pwd != NULL );
424
425         strlcpy(Client->pwd, Pwd, sizeof(Client->pwd));
426 } /* Client_SetPassword */
427
428
429 GLOBAL void
430 Client_SetAway( CLIENT *Client, const char *Txt )
431 {
432         /* Set AWAY reason of client */
433
434         assert( Client != NULL );
435         assert( Txt != NULL );
436
437         strlcpy( Client->away, Txt, sizeof( Client->away ));
438         LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
439                  Client_Mask(Client), Txt);
440 } /* Client_SetAway */
441
442
443 GLOBAL void
444 Client_SetType( CLIENT *Client, int Type )
445 {
446         assert( Client != NULL );
447         Client->type = Type;
448         if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
449         Adjust_Counters( Client );
450 } /* Client_SetType */
451
452
453 GLOBAL void
454 Client_SetHops( CLIENT *Client, int Hops )
455 {
456         assert( Client != NULL );
457         Client->hops = Hops;
458 } /* Client_SetHops */
459
460
461 GLOBAL void
462 Client_SetToken( CLIENT *Client, int Token )
463 {
464         assert( Client != NULL );
465         Client->token = Token;
466 } /* Client_SetToken */
467
468
469 GLOBAL void
470 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
471 {
472         assert( Client != NULL );
473         assert( Introducer != NULL );
474         Client->introducer = Introducer;
475 } /* Client_SetIntroducer */
476
477
478 GLOBAL void
479 Client_SetOperByMe( CLIENT *Client, bool OperByMe )
480 {
481         assert( Client != NULL );
482         Client->oper_by_me = OperByMe;
483 } /* Client_SetOperByMe */
484
485
486 GLOBAL bool
487 Client_ModeAdd( CLIENT *Client, char Mode )
488 {
489         /* Set Mode.
490          * If Client already alread had Mode, return false.
491          * If the Mode was newly set, return true.
492          */
493
494         char x[2];
495
496         assert( Client != NULL );
497
498         x[0] = Mode; x[1] = '\0';
499         if (!strchr( Client->modes, x[0])) {
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( const 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                 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
557                         return c;
558                 c = (CLIENT *)c->next;
559         }
560         return NULL;
561 } /* Client_Search */
562
563
564 /**
565  * Get client structure ("introducer") identfied by a server token.
566  * @return CLIENT structure or NULL if none could be found.
567  */
568 GLOBAL CLIENT *
569 Client_GetFromToken( CLIENT *Client, int Token )
570 {
571         CLIENT *c;
572
573         assert( Client != NULL );
574
575         if (!Token)
576                 return NULL;
577
578         c = My_Clients;
579         while (c) {
580                 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
581                         (c->token == Token))
582                                 return c;
583                 c = (CLIENT *)c->next;
584         }
585         return NULL;
586 } /* Client_GetFromToken */
587
588
589 GLOBAL int
590 Client_Type( CLIENT *Client )
591 {
592         assert( Client != NULL );
593         return Client->type;
594 } /* Client_Type */
595
596
597 GLOBAL CONN_ID
598 Client_Conn( CLIENT *Client )
599 {
600         assert( Client != NULL );
601         return Client->conn_id;
602 } /* Client_Conn */
603
604
605 GLOBAL char *
606 Client_ID( CLIENT *Client )
607 {
608         assert( Client != NULL );
609
610 #ifdef DEBUG
611         if(Client->type == CLIENT_USER)
612                 assert(strlen(Client->id) < Conf_MaxNickLength);
613 #endif
614                                                    
615         if( Client->id[0] ) return Client->id;
616         else return "*";
617 } /* Client_ID */
618
619
620 GLOBAL char *
621 Client_Info( CLIENT *Client )
622 {
623         assert( Client != NULL );
624         return Client->info;
625 } /* Client_Info */
626
627
628 GLOBAL char *
629 Client_User( CLIENT *Client )
630 {
631         assert( Client != NULL );
632         return Client->user[0] ? Client->user : "~";
633 } /* Client_User */
634
635
636 #ifdef PAM
637
638 /**
639  * Get the "original" user name as supplied by the USER command.
640  * The user name as given by the client is used for authentication instead
641  * of the one detected using IDENT requests.
642  * @param Client The client.
643  * @return Original user name.
644  */
645 GLOBAL char *
646 Client_OrigUser(CLIENT *Client) {
647 #ifndef IDENTAUTH
648         char *user = Client->user;
649
650         if (user[0] == '~')
651                 user++;
652         return user;
653 #else
654         return Client->orig_user;
655 #endif
656 } /* Client_OrigUser */
657
658 #endif
659
660
661 /**
662  * Return the hostname of a client.
663  * @param Client Pointer to client structure
664  * @return Pointer to client hostname
665  */
666 GLOBAL char *
667 Client_Hostname(CLIENT *Client)
668 {
669         assert (Client != NULL);
670         return Client->host;
671 } /* Client_Hostname */
672
673
674 /**
675  * Get potentially cloaked hostname of a client.
676  * If the client has not enabled cloaking, the real hostname is used.
677  * @param Client Pointer to client structure
678  * @return Pointer to client hostname
679  */
680 GLOBAL char *
681 Client_HostnameCloaked(CLIENT *Client)
682 {
683         assert(Client != NULL);
684         if (Client_HasMode(Client, 'x'))
685                 return Client_ID(Client->introducer);
686         else
687                 return Client_Hostname(Client);
688 } /* Client_HostnameCloaked */
689
690
691 GLOBAL char *
692 Client_Password( CLIENT *Client )
693 {
694         assert( Client != NULL );
695         return Client->pwd;
696 } /* Client_Password */
697
698
699 GLOBAL char *
700 Client_Modes( CLIENT *Client )
701 {
702         assert( Client != NULL );
703         return Client->modes;
704 } /* Client_Modes */
705
706
707 GLOBAL char *
708 Client_Flags( CLIENT *Client )
709 {
710         assert( Client != NULL );
711         return Client->flags;
712 } /* Client_Flags */
713
714
715 GLOBAL bool
716 Client_OperByMe( CLIENT *Client )
717 {
718         assert( Client != NULL );
719         return Client->oper_by_me;
720 } /* Client_OperByMe */
721
722
723 GLOBAL int
724 Client_Hops( CLIENT *Client )
725 {
726         assert( Client != NULL );
727         return Client->hops;
728 } /* Client_Hops */
729
730
731 GLOBAL int
732 Client_Token( CLIENT *Client )
733 {
734         assert( Client != NULL );
735         return Client->token;
736 } /* Client_Token */
737
738
739 GLOBAL int
740 Client_MyToken( CLIENT *Client )
741 {
742         assert( Client != NULL );
743         return Client->mytoken;
744 } /* Client_MyToken */
745
746
747 GLOBAL CLIENT *
748 Client_NextHop( CLIENT *Client )
749 {
750         CLIENT *c;
751
752         assert( Client != NULL );
753
754         c = Client;
755         while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
756                 c = c->introducer;
757
758         return c;
759 } /* Client_NextHop */
760
761
762 /**
763  * Return ID of a client: "client!user@host"
764  * This client ID is used for IRC prefixes, for example.
765  * Please note that this function uses a global static buffer, so you can't
766  * nest invocations without overwriting erlier results!
767  * @param Client Pointer to client structure
768  * @return Pointer to global buffer containing the client ID
769  */
770 GLOBAL char *
771 Client_Mask( CLIENT *Client )
772 {
773         static char Mask_Buffer[GETID_LEN];
774
775         assert (Client != NULL);
776
777         /* Servers: return name only, there is no "mask" */
778         if (Client->type == CLIENT_SERVER)
779                 return Client->id;
780
781         snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
782                  Client->id, Client->user, Client->host);
783         return Mask_Buffer;
784 } /* Client_Mask */
785
786
787 /**
788  * Return ID of a client with cloaked hostname: "client!user@server-name"
789  * This client ID is used for IRC prefixes, for example.
790  * Please note that this function uses a global static buffer, so you can't
791  * nest invocations without overwriting erlier results!
792  * If the client has not enabled cloaking, the real hostname is used.
793  * @param Client Pointer to client structure
794  * @return Pointer to global buffer containing the client ID
795  */
796 GLOBAL char *
797 Client_MaskCloaked(CLIENT *Client)
798 {
799         static char Mask_Buffer[GETID_LEN];
800
801         assert (Client != NULL);
802
803         /* Is the client using cloaking at all? */
804         if (!Client_HasMode(Client, 'x'))
805             return Client_Mask(Client);
806
807         snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
808                  Client->id, Client->user, Client_ID(Client->introducer));
809         return Mask_Buffer;
810 } /* Client_MaskCloaked */
811
812
813 GLOBAL CLIENT *
814 Client_Introducer( CLIENT *Client )
815 {
816         assert( Client != NULL );
817         return Client->introducer;
818 } /* Client_Introducer */
819
820
821 GLOBAL CLIENT *
822 Client_TopServer( CLIENT *Client )
823 {
824         assert( Client != NULL );
825         return Client->topserver;
826 } /* Client_TopServer */
827
828
829 GLOBAL bool
830 Client_HasMode( CLIENT *Client, char Mode )
831 {
832         assert( Client != NULL );
833         return strchr( Client->modes, Mode ) != NULL;
834 } /* Client_HasMode */
835
836
837 GLOBAL char *
838 Client_Away( CLIENT *Client )
839 {
840         assert( Client != NULL );
841         return Client->away;
842 } /* Client_Away */
843
844
845 GLOBAL bool
846 Client_CheckNick( CLIENT *Client, char *Nick )
847 {
848         assert( Client != NULL );
849         assert( Nick != NULL );
850
851         if (! Client_IsValidNick( Nick ))
852         {
853                 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
854                 return false;
855         }
856
857         /* Nick bereits vergeben? */
858         if( Client_Search( Nick ))
859         {
860                 /* den Nick gibt es bereits */
861                 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
862                 return false;
863         }
864
865         return true;
866 } /* Client_CheckNick */
867
868
869 GLOBAL bool
870 Client_CheckID( CLIENT *Client, char *ID )
871 {
872         char str[COMMAND_LEN];
873         CLIENT *c;
874
875         assert( Client != NULL );
876         assert( Client->conn_id > NONE );
877         assert( ID != NULL );
878
879         /* ID too long? */
880         if (strlen(ID) > CLIENT_ID_LEN) {
881                 IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID(Client), ID);
882                 return false;
883         }
884
885         /* ID already in use? */
886         c = My_Clients;
887         while (c) {
888                 if (strcasecmp(c->id, ID) == 0) {
889                         snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
890                         if (c->conn_id != NONE)
891                                 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
892                         else
893                                 Log(LOG_ERR, "%s (via network)!", str);
894                         Conn_Close(Client->conn_id, str, str, true);
895                         return false;
896                 }
897                 c = (CLIENT *)c->next;
898         }
899
900         return true;
901 } /* Client_CheckID */
902
903
904 GLOBAL CLIENT *
905 Client_First( void )
906 {
907         return My_Clients;
908 } /* Client_First */
909
910
911 GLOBAL CLIENT *
912 Client_Next( CLIENT *c )
913 {
914         assert( c != NULL );
915         return (CLIENT *)c->next;
916 } /* Client_Next */
917
918
919 GLOBAL long
920 Client_UserCount( void )
921 {
922         return Count( CLIENT_USER );
923 } /* Client_UserCount */
924
925
926 GLOBAL long
927 Client_ServiceCount( void )
928 {
929         return Count( CLIENT_SERVICE );;
930 } /* Client_ServiceCount */
931
932
933 GLOBAL long
934 Client_ServerCount( void )
935 {
936         return Count( CLIENT_SERVER );
937 } /* Client_ServerCount */
938
939
940 GLOBAL long
941 Client_MyUserCount( void )
942 {
943         return MyCount( CLIENT_USER );
944 } /* Client_MyUserCount */
945
946
947 GLOBAL long
948 Client_MyServiceCount( void )
949 {
950         return MyCount( CLIENT_SERVICE );
951 } /* Client_MyServiceCount */
952
953
954 GLOBAL unsigned long
955 Client_MyServerCount( void )
956 {
957         CLIENT *c;
958         unsigned long cnt = 0;
959
960         c = My_Clients;
961         while( c )
962         {
963                 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
964                 c = (CLIENT *)c->next;
965         }
966         return cnt;
967 } /* Client_MyServerCount */
968
969
970 GLOBAL unsigned long
971 Client_OperCount( void )
972 {
973         CLIENT *c;
974         unsigned long cnt = 0;
975
976         c = My_Clients;
977         while( c )
978         {
979                 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
980                 c = (CLIENT *)c->next;
981         }
982         return cnt;
983 } /* Client_OperCount */
984
985
986 GLOBAL unsigned long
987 Client_UnknownCount( void )
988 {
989         CLIENT *c;
990         unsigned long cnt = 0;
991
992         c = My_Clients;
993         while( c )
994         {
995                 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
996                 c = (CLIENT *)c->next;
997         }
998
999         return cnt;
1000 } /* Client_UnknownCount */
1001
1002
1003 GLOBAL long
1004 Client_MaxUserCount( void )
1005 {
1006         return Max_Users;
1007 } /* Client_MaxUserCount */
1008
1009
1010 GLOBAL long
1011 Client_MyMaxUserCount( void )
1012 {
1013         return My_Max_Users;
1014 } /* Client_MyMaxUserCount */
1015
1016
1017 GLOBAL bool
1018 Client_IsValidNick( const char *Nick )
1019 {
1020         const char *ptr;
1021         static const char goodchars[] = ";0123456789-";
1022
1023         assert( Nick != NULL );
1024
1025         if( Nick[0] == '#' ) return false;
1026         if( strchr( goodchars, Nick[0] )) return false;
1027         if( strlen( Nick ) >= Conf_MaxNickLength) return false;
1028
1029         ptr = Nick;
1030         while( *ptr )
1031         {
1032                 if (( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return false;
1033                 if ( *ptr > '}' ) return false;
1034                 ptr++;
1035         }
1036
1037         return true;
1038 } /* Client_IsValidNick */
1039
1040
1041 /**
1042  * Return pointer to "My_Whowas" structure.
1043  */
1044 GLOBAL WHOWAS *
1045 Client_GetWhowas( void )
1046 {
1047         return My_Whowas;
1048 } /* Client_GetWhowas */
1049
1050 /**
1051  * Return the index of the last used WHOWAS entry.
1052  */
1053 GLOBAL int
1054 Client_GetLastWhowasIndex( void )
1055 {
1056         return Last_Whowas;
1057 } /* Client_GetLastWhowasIndex */
1058
1059
1060 /**
1061  * Get the start time of this client.
1062  * The result is the start time in seconds since 1970-01-01, as reported
1063  * by the C function time(NULL).
1064  */
1065 GLOBAL time_t
1066 Client_StartTime(CLIENT *Client)
1067 {
1068         assert( Client != NULL );
1069         return Client->starttime;
1070 } /* Client_Uptime */
1071
1072
1073 static unsigned long
1074 Count( CLIENT_TYPE Type )
1075 {
1076         CLIENT *c;
1077         unsigned long cnt = 0;
1078
1079         c = My_Clients;
1080         while( c )
1081         {
1082                 if( c->type == Type ) cnt++;
1083                 c = (CLIENT *)c->next;
1084         }
1085         return cnt;
1086 } /* Count */
1087
1088
1089 static unsigned long
1090 MyCount( CLIENT_TYPE Type )
1091 {
1092         CLIENT *c;
1093         unsigned long cnt = 0;
1094
1095         c = My_Clients;
1096         while( c )
1097         {
1098                 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1099                 c = (CLIENT *)c->next;
1100         }
1101         return cnt;
1102 } /* MyCount */
1103
1104
1105 static CLIENT *
1106 New_Client_Struct( void )
1107 {
1108         /* Neue CLIENT-Struktur pre-initialisieren */
1109
1110         CLIENT *c;
1111
1112         c = (CLIENT *)malloc( sizeof( CLIENT ));
1113         if( ! c )
1114         {
1115                 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1116                 return NULL;
1117         }
1118
1119         memset( c, 0, sizeof ( CLIENT ));
1120
1121         c->type = CLIENT_UNKNOWN;
1122         c->conn_id = NONE;
1123         c->oper_by_me = false;
1124         c->hops = -1;
1125         c->token = -1;
1126         c->mytoken = -1;
1127
1128         return c;
1129 } /* New_Client */
1130
1131
1132 static void
1133 Generate_MyToken( CLIENT *Client )
1134 {
1135         CLIENT *c;
1136         int token;
1137
1138         c = My_Clients;
1139         token = 2;
1140         while( c )
1141         {
1142                 if( c->mytoken == token )
1143                 {
1144                         /* Das Token wurde bereits vergeben */
1145                         token++;
1146                         c = My_Clients;
1147                         continue;
1148                 }
1149                 else c = (CLIENT *)c->next;
1150         }
1151         Client->mytoken = token;
1152         LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1153 } /* Generate_MyToken */
1154
1155
1156 static void
1157 Adjust_Counters( CLIENT *Client )
1158 {
1159         long count;
1160
1161         assert( Client != NULL );
1162
1163         if( Client->type != CLIENT_USER ) return;
1164
1165         if( Client->conn_id != NONE )
1166         {
1167                 /* Local connection */
1168                 count = Client_MyUserCount( );
1169                 if( count > My_Max_Users ) My_Max_Users = count;
1170         }
1171         count = Client_UserCount( );
1172         if( count > Max_Users ) Max_Users = count;
1173 } /* Adjust_Counters */
1174
1175
1176 /**
1177  * Register client in My_Whowas structure for further recall by WHOWAS.
1178  * Note: Only clients that have been connected at least 30 seconds will be
1179  * registered to prevent automated IRC bots to "destroy" a nice server
1180  * history database.
1181  */
1182 GLOBAL void
1183 Client_RegisterWhowas( CLIENT *Client )
1184 {
1185         int slot;
1186         time_t now;
1187
1188         assert( Client != NULL );
1189
1190         now = time(NULL);
1191         /* Don't register clients that were connected less than 30 seconds. */
1192         if( now - Client->starttime < 30 )
1193                 return;
1194
1195         slot = Last_Whowas + 1;
1196         if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1197
1198 #ifdef DEBUG
1199         Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1200 #endif
1201
1202         My_Whowas[slot].time = now;
1203         strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1204                  sizeof( My_Whowas[slot].id ));
1205         strlcpy( My_Whowas[slot].user, Client_User( Client ),
1206                  sizeof( My_Whowas[slot].user ));
1207         strlcpy( My_Whowas[slot].host, Client_HostnameCloaked( Client ),
1208                  sizeof( My_Whowas[slot].host ));
1209         strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1210                  sizeof( My_Whowas[slot].info ));
1211         strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1212                  sizeof( My_Whowas[slot].server ));
1213
1214         Last_Whowas = slot;
1215 } /* Client_RegisterWhowas */
1216
1217
1218 GLOBAL const char *
1219 Client_TypeText(CLIENT *Client)
1220 {
1221         assert(Client != NULL);
1222         switch (Client_Type(Client)) {
1223                 case CLIENT_USER:
1224                         return "User";
1225                         break;
1226                 case CLIENT_SERVICE:
1227                         return "Service";
1228                         break;
1229                 case CLIENT_SERVER:
1230                         return "Server";
1231                         break;
1232                 default:
1233                         return "Client";
1234         }
1235 } /* Client_TypeText */
1236
1237
1238 /**
1239  * Destroy user or service client.
1240  */
1241 static void
1242 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1243 {
1244         if(Client->conn_id != NONE) {
1245                 /* Local (directly connected) client */
1246                 Log(LOG_NOTICE,
1247                     "%s \"%s\" unregistered (connection %d): %s",
1248                     Client_TypeText(Client), Client_Mask(Client),
1249                     Client->conn_id, Txt);
1250                 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1251                                  Client_ID(Client), Client_User(Client),
1252                                  Client_Hostname(Client), Txt);
1253
1254                 if (SendQuit) {
1255                         /* Inforam all the other servers */
1256                         if (FwdMsg)
1257                                 IRC_WriteStrServersPrefix(NULL,
1258                                                 Client, "QUIT :%s", FwdMsg );
1259                         else
1260                                 IRC_WriteStrServersPrefix(NULL,
1261                                                 Client, "QUIT :");
1262                 }
1263         } else {
1264                 /* Remote client */
1265                 LogDebug("%s \"%s\" unregistered: %s",
1266                          Client_TypeText(Client), Client_Mask(Client), Txt);
1267
1268                 if(SendQuit) {
1269                         /* Inform all the other servers, but the ones in the
1270                          * direction we got the QUIT from */
1271                         if(FwdMsg)
1272                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1273                                                 Client, "QUIT :%s", FwdMsg );
1274                         else
1275                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1276                                                 Client, "QUIT :" );
1277                 }
1278         }
1279
1280         /* Unregister client from channels */
1281         Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1282
1283         /* Register client in My_Whowas structure */
1284         Client_RegisterWhowas(Client);
1285 } /* Destroy_UserOrService */
1286
1287
1288 #ifdef DEBUG
1289
1290 GLOBAL void
1291 Client_DebugDump(void)
1292 {
1293         CLIENT *c;
1294
1295         Log(LOG_DEBUG, "Client status:");
1296         c = My_Clients;
1297         while (c) {
1298                 Log(LOG_DEBUG,
1299                     " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1300                    Client_ID(c), Client_Type(c), Client_Hostname(c),
1301                    Client_User(c), Client_Conn(c), Client_StartTime(c),
1302                    Client_Flags(c));
1303                 c = (CLIENT *)c->next;
1304         }
1305 } /* Client_DumpClients */
1306
1307 #endif
1308
1309
1310 /* -eof- */