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