]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
Streamline DEBUG_ARRAY, DEBUG_BUFFER, DEBUG_IO, DEBUG_ZIP
[ngircd-alex.git] / src / ngircd / client.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
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 <assert.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <strings.h>
27 #include <time.h>
28 #include <netdb.h>
29
30 #include "conn.h"
31 #include "ngircd.h"
32 #include "channel.h"
33 #include "conf.h"
34 #include "conn-func.h"
35 #include "hash.h"
36 #include "irc-write.h"
37 #include "log.h"
38 #include "match.h"
39 #include "messages.h"
40
41 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
42
43 static CLIENT *This_Server, *My_Clients;
44
45 static WHOWAS My_Whowas[MAX_WHOWAS];
46 static int Last_Whowas = -1;
47 static long Max_Users, My_Max_Users;
48
49
50 static unsigned long Count PARAMS(( CLIENT_TYPE Type ));
51 static unsigned long MyCount PARAMS(( CLIENT_TYPE Type ));
52
53 static CLIENT *New_Client_Struct PARAMS(( void ));
54 static void Generate_MyToken PARAMS(( CLIENT *Client ));
55 static void Adjust_Counters PARAMS(( CLIENT *Client ));
56
57 static void Free_Client PARAMS(( CLIENT **Client ));
58
59 static CLIENT *Init_New_Client PARAMS((CONN_ID Idx, CLIENT *Introducer,
60                                        CLIENT *TopServer, int Type, const char *ID,
61                                        const char *User, const char *Hostname, const char *Info,
62                                        int Hops, int Token, const char *Modes,
63                                        bool Idented));
64
65 static void Destroy_UserOrService PARAMS((CLIENT *Client,const char *Txt, const char *FwdMsg,
66                                         bool SendQuit));
67
68 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
69                                        void *i));
70
71 GLOBAL void
72 Client_Init( void )
73 {
74         struct hostent *h;
75         
76         This_Server = New_Client_Struct( );
77         if( ! This_Server )
78         {
79                 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
80                 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE_NAME );
81                 exit( 1 );
82         }
83
84         /* Client structure for this server */
85         This_Server->next = NULL;
86         This_Server->type = CLIENT_SERVER;
87         This_Server->conn_id = NONE;
88         This_Server->introducer = This_Server;
89         This_Server->mytoken = 1;
90         This_Server->hops = 0;
91
92         gethostname( This_Server->host, CLIENT_HOST_LEN );
93         if (Conf_DNS) {
94                 h = gethostbyname( This_Server->host );
95                 if (h) strlcpy(This_Server->host, h->h_name, sizeof(This_Server->host));
96         }
97         Client_SetID( This_Server, Conf_ServerName );
98         Client_SetInfo( This_Server, Conf_ServerInfo );
99
100         My_Clients = This_Server;
101         
102         memset( &My_Whowas, 0, sizeof( My_Whowas ));
103 } /* Client_Init */
104
105
106 GLOBAL void
107 Client_Exit( void )
108 {
109         CLIENT *c, *next;
110         int cnt;
111
112         if( NGIRCd_SignalRestart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, false );
113         else Client_Destroy( This_Server, "Server going down.", NULL, false );
114         
115         cnt = 0;
116         c = My_Clients;
117         while(c) {
118                 cnt++;
119                 next = (CLIENT *)c->next;
120                 Free_Client(&c);
121                 c = next;
122         }
123         if (cnt)
124                 Log(LOG_INFO, "Freed %d client structure%s.",
125                     cnt, cnt == 1 ? "" : "s");
126 } /* Client_Exit */
127
128
129 GLOBAL CLIENT *
130 Client_ThisServer( void )
131 {
132         return This_Server;
133 } /* Client_ThisServer */
134
135
136 /**
137  * Initialize new local client; wrapper function for Init_New_Client().
138  * @return New CLIENT structure.
139  */
140 GLOBAL CLIENT *
141 Client_NewLocal(CONN_ID Idx, const char *Hostname, int Type, bool Idented)
142 {
143         return Init_New_Client(Idx, This_Server, NULL, Type, NULL, NULL,
144                 Hostname, NULL, 0, 0, NULL, Idented);
145 } /* Client_NewLocal */
146
147
148 /**
149  * Initialize new remote server; wrapper function for Init_New_Client().
150  * @return New CLIENT structure.
151  */
152 GLOBAL CLIENT *
153 Client_NewRemoteServer(CLIENT *Introducer, const char *Hostname, CLIENT *TopServer,
154  int Hops, int Token, const char *Info, bool Idented)
155 {
156         return Init_New_Client(NONE, Introducer, TopServer, CLIENT_SERVER,
157                 Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented);
158 } /* Client_NewRemoteServer */
159
160
161 /**
162  * Initialize new remote client; wrapper function for Init_New_Client().
163  * @return New CLIENT structure.
164  */
165 GLOBAL CLIENT *
166 Client_NewRemoteUser(CLIENT *Introducer, const char *Nick, int Hops, const char *User,
167  const char *Hostname, int Token, const char *Modes, const char *Info, bool Idented)
168 {
169         return Init_New_Client(NONE, Introducer, NULL, CLIENT_USER, Nick,
170                 User, Hostname, Info, Hops, Token, Modes, Idented);
171 } /* Client_NewRemoteUser */
172
173
174 /**
175  * Initialize new client and set up the given parameters like client type,
176  * user name, host name, introducing server etc. ...
177  * @return New CLIENT structure.
178  */
179 static CLIENT *
180 Init_New_Client(CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer,
181   int Type, const char *ID, const char *User, const char *Hostname,
182   const char *Info, int Hops, int Token, const char *Modes, bool Idented)
183 {
184         CLIENT *client;
185
186         assert(Idx >= NONE);
187         assert(Introducer != NULL);
188
189         client = New_Client_Struct();
190         if (!client)
191                 return NULL;
192
193         client->starttime = time(NULL);
194         client->conn_id = Idx;
195         client->introducer = Introducer;
196         client->topserver = TopServer;
197         client->type = Type;
198         if (ID)
199                 Client_SetID(client, ID);
200         if (User) {
201                 Client_SetUser(client, User, Idented);
202                 Client_SetOrigUser(client, User);
203         }
204         if (Hostname)
205                 Client_SetHostname(client, Hostname);
206         if (Info)
207                 Client_SetInfo(client, Info);
208         client->hops = Hops;
209         client->token = Token;
210         if (Modes)
211                 Client_SetModes(client, Modes);
212         if (Type == CLIENT_SERVER)
213                 Generate_MyToken(client);
214
215         if (Client_HasMode(client, 'a'))
216                 client->away = strndup(DEFAULT_AWAY_MSG, CLIENT_AWAY_LEN - 1);
217
218         client->next = (POINTER *)My_Clients;
219         My_Clients = client;
220
221         Adjust_Counters(client);
222
223         return client;
224 } /* Init_New_Client */
225
226
227 GLOBAL void
228 Client_Destroy( CLIENT *Client, const char *LogMsg, const char *FwdMsg, bool SendQuit )
229 {
230         /* remove a client */
231         
232         CLIENT *last, *c;
233         char msg[COMMAND_LEN];
234         const char *txt;
235
236         assert( Client != NULL );
237
238         txt = LogMsg ? LogMsg : FwdMsg;
239         if (!txt)
240                 txt = "Reason unknown";
241
242         /* netsplit message */
243         if( Client->type == CLIENT_SERVER ) {
244                 strlcpy(msg, This_Server->id, sizeof (msg));
245                 strlcat(msg, " ", sizeof (msg));
246                 strlcat(msg, Client->id, sizeof (msg));
247         }
248
249         last = NULL;
250         c = My_Clients;
251         while( c )
252         {
253                 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
254                 {
255                         /*
256                          * The client that is about to be removed is a server,
257                          * the client we are checking right now is a child of that
258                          * server and thus has to be removed, too.
259                          *
260                          * Call Client_Destroy() recursively with the server as the
261                          * new "object to be removed". This starts the cycle again, until
262                          * all servers that are linked via the original server have been
263                          * removed.
264                          */
265                         Client_Destroy( c, NULL, msg, false );
266                         last = NULL;
267                         c = My_Clients;
268                         continue;
269                 }
270                 if( c == Client )
271                 {
272                         /* found  the client: remove it */
273                         if( last ) last->next = c->next;
274                         else My_Clients = (CLIENT *)c->next;
275
276                         if(c->type == CLIENT_USER || c->type == CLIENT_SERVICE)
277                                 Destroy_UserOrService(c, txt, FwdMsg, SendQuit);
278                         else if( c->type == CLIENT_SERVER )
279                         {
280                                 if (c != This_Server) {
281                                         if (c->conn_id != NONE)
282                                                 Log(LOG_NOTICE|LOG_snotice,
283                                                     "Server \"%s\" unregistered (connection %d): %s.",
284                                                 c->id, c->conn_id, txt);
285                                         else
286                                                 Log(LOG_NOTICE|LOG_snotice,
287                                                     "Server \"%s\" unregistered: %s.",
288                                                     c->id, txt);
289                                 }
290
291                                 /* inform other servers */
292                                 if( ! NGIRCd_SignalQuit )
293                                 {
294                                         if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
295                                         else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
296                                 }
297                         }
298                         else
299                         {
300                                 if (c->conn_id != NONE) {
301                                         if (c->id[0])
302                                                 Log(LOG_NOTICE,
303                                                     "Client \"%s\" unregistered (connection %d): %s.",
304                                                     c->id, c->conn_id, txt);
305                                         else
306                                                 Log(LOG_NOTICE,
307                                                     "Client unregistered (connection %d): %s.",
308                                                     c->conn_id, txt);
309                                 } else {
310                                         Log(LOG_WARNING,
311                                             "Unregistered unknown client \"%s\": %s",
312                                             c->id[0] ? c->id : "(No Nick)", txt);
313                                 }
314                         }
315
316                         Free_Client(&c);
317                         break;
318                 }
319                 last = c;
320                 c = (CLIENT *)c->next;
321         }
322 } /* Client_Destroy */
323
324
325 /**
326  * Set client hostname.
327  *
328  * If global hostname cloaking is in effect, don't set the real hostname
329  * but the configured one.
330  *
331  * @param Client The client of which the hostname should be set.
332  * @param Hostname The new hostname.
333  */
334 GLOBAL void
335 Client_SetHostname( CLIENT *Client, const char *Hostname )
336 {
337         assert(Client != NULL);
338         assert(Hostname != NULL);
339
340         if (Conf_CloakHost[0]) {
341                 char cloak[GETID_LEN];
342
343                 strlcpy(cloak, Hostname, GETID_LEN);
344                 strlcat(cloak, Conf_CloakHostSalt, GETID_LEN);
345                 snprintf(cloak, GETID_LEN, Conf_CloakHost, Hash(cloak));
346
347                 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
348                         Client_ID(Client), Client->host, cloak);
349                 strlcpy(Client->host, cloak, sizeof(Client->host));
350         } else {
351                 LogDebug("Updating hostname of \"%s\": \"%s\" -> \"%s\"",
352                          Client_ID(Client), Client->host, Hostname);
353                 strlcpy(Client->host, Hostname, sizeof(Client->host));
354         }
355 } /* Client_SetHostname */
356
357
358 /**
359  * Set IP address to display for a client.
360  *
361  * @param Client The client.
362  * @param IPAText Textual representation of the IP address or NULL to unset.
363  */
364 GLOBAL void
365 Client_SetIPAText(CLIENT *Client, const char *IPAText)
366 {
367         assert(Client != NULL);
368
369         if (Client->ipa_text)
370                 free(Client->ipa_text);
371
372         if (*IPAText)
373                 Client->ipa_text = strndup(IPAText, CLIENT_HOST_LEN - 1);
374         else
375                 Client->ipa_text = NULL;
376 }
377
378
379 GLOBAL void
380 Client_SetID( CLIENT *Client, const char *ID )
381 {
382         assert( Client != NULL );
383         assert( ID != NULL );
384         
385         strlcpy( Client->id, ID, sizeof( Client->id ));
386
387         if (Conf_CloakUserToNick) {
388                 strlcpy( Client->user, ID, sizeof( Client->user ));
389                 strlcpy( Client->info, ID, sizeof( Client->info ));
390         }
391
392         /* Hash */
393         Client->hash = Hash( Client->id );
394 } /* Client_SetID */
395
396
397 GLOBAL void
398 Client_SetUser( CLIENT *Client, const char *User, bool Idented )
399 {
400         /* set clients username */
401
402         assert( Client != NULL );
403         assert( User != NULL );
404
405         if (Conf_CloakUserToNick) {
406                 strlcpy(Client->user, Client->id, sizeof(Client->user));
407         } else if (Idented) {
408                 strlcpy(Client->user, User, sizeof(Client->user));
409         } else {
410                 Client->user[0] = '~';
411                 strlcpy(Client->user + 1, User, sizeof(Client->user) - 1);
412         }
413 } /* Client_SetUser */
414
415
416 /**
417  * Set "original" user name of a client.
418  * This function saves the "original" user name, the user name specified by
419  * the peer using the USER command, into the CLIENT structure. This user
420  * name may be used for authentication, for example.
421  * @param Client The client.
422  * @param User User name to set.
423  */
424 GLOBAL void
425 Client_SetOrigUser(CLIENT UNUSED *Client, const char UNUSED *User)
426 {
427         assert(Client != NULL);
428         assert(User != NULL);
429
430 #if defined(PAM) && defined(IDENTAUTH)
431         strlcpy(Client->orig_user, User, sizeof(Client->orig_user));
432 #endif
433 } /* Client_SetOrigUser */
434
435
436 GLOBAL void
437 Client_SetInfo( CLIENT *Client, const char *Info )
438 {
439         /* set client hostname */
440
441         assert( Client != NULL );
442         assert( Info != NULL );
443
444         if (Conf_CloakUserToNick)
445                 strlcpy(Client->info, Client->id, sizeof(Client->info));
446         else
447                 strlcpy(Client->info, Info, sizeof(Client->info));
448 } /* Client_SetInfo */
449
450
451 GLOBAL void
452 Client_SetModes( CLIENT *Client, const char *Modes )
453 {
454         assert( Client != NULL );
455         assert( Modes != NULL );
456
457         strlcpy(Client->modes, Modes, sizeof( Client->modes ));
458 } /* Client_SetModes */
459
460
461 GLOBAL void
462 Client_SetFlags( CLIENT *Client, const char *Flags )
463 {
464         assert( Client != NULL );
465         assert( Flags != NULL );
466
467         strlcpy(Client->flags, Flags, sizeof(Client->flags));
468 } /* Client_SetFlags */
469
470
471 GLOBAL void
472 Client_SetAccountName(CLIENT *Client, const char *AccountName)
473 {
474         assert(Client != NULL);
475
476         if (Client->account_name)
477                 free(Client->account_name);
478
479         if (*AccountName)
480                 Client->account_name = strndup(AccountName,
481                                                CLIENT_NICK_LEN - 1);
482         else
483                 Client->account_name = NULL;
484 }
485
486
487 GLOBAL void
488 Client_SetAway( CLIENT *Client, const char *Txt )
489 {
490         /* Set AWAY reason of client */
491
492         assert( Client != NULL );
493         assert( Txt != NULL );
494
495         if (Client->away)
496                 free(Client->away);
497
498         Client->away = strndup(Txt, CLIENT_AWAY_LEN - 1);
499
500         LogDebug("%s \"%s\" is away: %s", Client_TypeText(Client),
501                  Client_Mask(Client), Txt);
502 } /* Client_SetAway */
503
504
505 GLOBAL void
506 Client_SetType( CLIENT *Client, int Type )
507 {
508         assert( Client != NULL );
509         Client->type = Type;
510         if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
511         Adjust_Counters( Client );
512 } /* Client_SetType */
513
514
515 GLOBAL void
516 Client_SetHops( CLIENT *Client, int Hops )
517 {
518         assert( Client != NULL );
519         Client->hops = Hops;
520 } /* Client_SetHops */
521
522
523 GLOBAL void
524 Client_SetToken( CLIENT *Client, int Token )
525 {
526         assert( Client != NULL );
527         Client->token = Token;
528 } /* Client_SetToken */
529
530
531 GLOBAL void
532 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
533 {
534         assert( Client != NULL );
535         assert( Introducer != NULL );
536         Client->introducer = Introducer;
537 } /* Client_SetIntroducer */
538
539
540 GLOBAL bool
541 Client_ModeAdd( CLIENT *Client, char Mode )
542 {
543         /* Set Mode.
544          * If Client already had Mode, return false.
545          * If the Mode was newly set, return true.
546          */
547
548         char x[2];
549
550         assert( Client != NULL );
551
552         x[0] = Mode; x[1] = '\0';
553         if (!Client_HasMode(Client, x[0])) {
554                 strlcat( Client->modes, x, sizeof( Client->modes ));
555                 return true;
556         }
557         else return false;
558 } /* Client_ModeAdd */
559
560
561 GLOBAL bool
562 Client_ModeDel( CLIENT *Client, char Mode )
563 {
564         /* Delete Mode.
565          * If Mode was removed, return true.
566          * If Client did not have Mode, return false.
567          */
568
569         char x[2], *p;
570
571         assert( Client != NULL );
572
573         x[0] = Mode; x[1] = '\0';
574
575         p = strchr( Client->modes, x[0] );
576         if( ! p ) return false;
577
578         /* Client has Mode -> delete */
579         while( *p )
580         {
581                 *p = *(p + 1);
582                 p++;
583         }
584         return true;
585 } /* Client_ModeDel */
586
587
588 /**
589  * Search CLIENT structure of a given nick name.
590  *
591  * @return Pointer to CLIENT structure or NULL if not found.
592  */
593 GLOBAL CLIENT *
594 Client_Search( const char *Nick )
595 {
596         char search_id[CLIENT_ID_LEN], *ptr;
597         CLIENT *c = NULL;
598         UINT32 search_hash;
599
600         assert( Nick != NULL );
601
602         /* copy Nick and truncate hostmask if necessary */
603         strlcpy( search_id, Nick, sizeof( search_id ));
604         ptr = strchr( search_id, '!' );
605         if( ptr ) *ptr = '\0';
606
607         search_hash = Hash(search_id);
608
609         c = My_Clients;
610         while (c) {
611                 if (c->hash == search_hash && strcasecmp(c->id, search_id) == 0)
612                         return c;
613                 c = (CLIENT *)c->next;
614         }
615         return NULL;
616 }
617
618
619 /**
620  * Search first CLIENT structure matching a given mask of a server.
621  *
622  * The order of servers is arbitrary, but this function makes sure that the
623  * local server is always returned if the mask matches it.
624  *
625  * @return Pointer to CLIENT structure or NULL if no server could be found.
626  */
627 GLOBAL CLIENT *
628 Client_SearchServer(const char *Mask)
629 {
630         CLIENT *c;
631
632         assert(Mask != NULL);
633
634         /* First check if mask matches the local server */
635         if (MatchCaseInsensitive(Mask, Client_ID(Client_ThisServer())))
636                 return Client_ThisServer();
637
638         c = My_Clients;
639         while (c) {
640                 if (Client_Type(c) == CLIENT_SERVER) {
641                         /* This is a server: check if Mask matches */
642                         if (MatchCaseInsensitive(Mask, c->id))
643                                 return c;
644                 }
645                 c = (CLIENT *)c->next;
646         }
647         return NULL;
648 }
649
650
651 /**
652  * Get client structure ("introducer") identfied by a server token.
653  * @return CLIENT structure or NULL if none could be found.
654  */
655 GLOBAL CLIENT *
656 Client_GetFromToken( CLIENT *Client, int Token )
657 {
658         CLIENT *c;
659
660         assert( Client != NULL );
661
662         if (!Token)
663                 return NULL;
664
665         c = My_Clients;
666         while (c) {
667                 if ((c->type == CLIENT_SERVER) && (c->introducer == Client) &&
668                         (c->token == Token))
669                                 return c;
670                 c = (CLIENT *)c->next;
671         }
672         return NULL;
673 } /* Client_GetFromToken */
674
675
676 GLOBAL int
677 Client_Type( CLIENT *Client )
678 {
679         assert( Client != NULL );
680         return Client->type;
681 } /* Client_Type */
682
683
684 GLOBAL CONN_ID
685 Client_Conn( CLIENT *Client )
686 {
687         assert( Client != NULL );
688         return Client->conn_id;
689 } /* Client_Conn */
690
691
692 GLOBAL char *
693 Client_ID( CLIENT *Client )
694 {
695         assert( Client != NULL );
696
697 #ifdef DEBUG
698         if(Client->type == CLIENT_USER)
699                 assert(strlen(Client->id) < Conf_MaxNickLength);
700 #endif
701                                                    
702         if( Client->id[0] ) return Client->id;
703         else return "*";
704 } /* Client_ID */
705
706
707 GLOBAL char *
708 Client_Info( CLIENT *Client )
709 {
710         assert( Client != NULL );
711         return Client->info;
712 } /* Client_Info */
713
714
715 GLOBAL char *
716 Client_User( CLIENT *Client )
717 {
718         assert( Client != NULL );
719         return Client->user[0] ? Client->user : "~";
720 } /* Client_User */
721
722
723 #ifdef PAM
724
725 /**
726  * Get the "original" user name as supplied by the USER command.
727  * The user name as given by the client is used for authentication instead
728  * of the one detected using IDENT requests.
729  * @param Client The client.
730  * @return Original user name.
731  */
732 GLOBAL char *
733 Client_OrigUser(CLIENT *Client) {
734 #ifndef IDENTAUTH
735         char *user = Client->user;
736
737         if (user[0] == '~')
738                 user++;
739         return user;
740 #else
741         return Client->orig_user;
742 #endif
743 } /* Client_OrigUser */
744
745 #endif
746
747 /**
748  * Return the hostname of a client.
749  * @param Client Pointer to client structure
750  * @return Pointer to client hostname
751  */
752 GLOBAL char *
753 Client_Hostname(CLIENT *Client)
754 {
755         assert (Client != NULL);
756         return Client->host;
757 }
758
759 /**
760  * Return the cloaked hostname of a client, if set.
761  * @param Client Pointer to the client structure.
762  * @return Pointer to the cloaked hostname or NULL if not set.
763  */
764 GLOBAL char *
765 Client_HostnameCloaked(CLIENT *Client)
766 {
767         assert(Client != NULL);
768         return Client->cloaked;
769 }
770
771 /**
772  * Get (potentially cloaked) hostname of a client to display it to other users.
773  *
774  * If the client has not enabled cloaking, the real hostname is used.
775  *
776  * @param Client Pointer to client structure
777  * @return Pointer to client hostname
778  */
779 GLOBAL char *
780 Client_HostnameDisplayed(CLIENT *Client)
781 {
782         assert(Client != NULL);
783
784         /* Client isn't cloaked at all, return real hostname: */
785         if (!Client_HasMode(Client, 'x'))
786                 return Client_Hostname(Client);
787
788         /* Use an already saved cloaked hostname, if there is one */
789         if (Client->cloaked)
790                 return Client->cloaked;
791
792         Client_UpdateCloakedHostname(Client, NULL, NULL);
793         return Client->cloaked;
794 }
795
796 GLOBAL const char *
797 Client_IPAText(CLIENT *Client)
798 {
799         assert(Client != NULL);
800
801         /* Not a local client? */
802         if (Client_Conn(Client) <= NONE)
803                 return "0.0.0.0";
804
805         if (!Client->ipa_text)
806                 return Conn_GetIPAInfo(Client_Conn(Client));
807         else
808                 return Client->ipa_text;
809 }
810
811 /**
812  * Update (and generate, if necessary) the cloaked hostname of a client.
813  *
814  * The newly set cloaked hostname is announced in the network using METADATA
815  * commands to peers that support this feature.
816  *
817  * @param Client The client of which the cloaked hostname should be updated.
818  * @param Origin The originator of the hostname change, or NULL if this server.
819  * @param Hostname The new cloaked hostname, or NULL if it should be generated.
820  */
821 GLOBAL void
822 Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
823                              const char *Hostname)
824 {
825         char Cloak_Buffer[CLIENT_HOST_LEN];
826
827         assert(Client != NULL);
828         if (!Origin)
829                 Origin = Client_ThisServer();
830
831         if (!Client->cloaked) {
832                 Client->cloaked = malloc(CLIENT_HOST_LEN);
833                 if (!Client->cloaked)
834                         return;
835         }
836
837         if (!Hostname) {
838                 /* Generate new cloaked hostname */
839                 if (*Conf_CloakHostModeX) {
840                         strlcpy(Cloak_Buffer, Client->host,
841                                 sizeof(Cloak_Buffer));
842                         strlcat(Cloak_Buffer, Conf_CloakHostSalt,
843                                 sizeof(Cloak_Buffer));
844                         snprintf(Client->cloaked, CLIENT_HOST_LEN,
845                                  Conf_CloakHostModeX, Hash(Cloak_Buffer));
846                 } else
847                         strlcpy(Client->cloaked, Client_ID(Client->introducer),
848                                 CLIENT_HOST_LEN);
849         } else
850                 strlcpy(Client->cloaked, Hostname, CLIENT_HOST_LEN);
851         LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
852                  Client_ID(Client), Client->cloaked);
853
854         /* Inform other servers in the network */
855         IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
856                                       "METADATA %s cloakhost :%s",
857                                       Client_ID(Client), Client->cloaked);
858 }
859
860 GLOBAL char *
861 Client_Modes( CLIENT *Client )
862 {
863         assert( Client != NULL );
864         return Client->modes;
865 } /* Client_Modes */
866
867
868 GLOBAL char *
869 Client_Flags( CLIENT *Client )
870 {
871         assert( Client != NULL );
872         return Client->flags;
873 } /* Client_Flags */
874
875
876 GLOBAL int
877 Client_Hops( CLIENT *Client )
878 {
879         assert( Client != NULL );
880         return Client->hops;
881 } /* Client_Hops */
882
883
884 GLOBAL int
885 Client_Token( CLIENT *Client )
886 {
887         assert( Client != NULL );
888         return Client->token;
889 } /* Client_Token */
890
891
892 GLOBAL int
893 Client_MyToken( CLIENT *Client )
894 {
895         assert( Client != NULL );
896         return Client->mytoken;
897 } /* Client_MyToken */
898
899
900 GLOBAL CLIENT *
901 Client_NextHop( CLIENT *Client )
902 {
903         CLIENT *c;
904
905         assert( Client != NULL );
906
907         c = Client;
908         while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
909                 c = c->introducer;
910
911         return c;
912 } /* Client_NextHop */
913
914
915 /**
916  * Return ID of a client: "client!user@host"
917  * This client ID is used for IRC prefixes, for example.
918  * Please note that this function uses a global static buffer, so you can't
919  * nest invocations without overwriting earlier results!
920  * @param Client Pointer to client structure
921  * @return Pointer to global buffer containing the client ID
922  */
923 GLOBAL char *
924 Client_Mask( CLIENT *Client )
925 {
926         static char Mask_Buffer[GETID_LEN];
927
928         assert (Client != NULL);
929
930         /* Servers: return name only, there is no "mask" */
931         if (Client->type == CLIENT_SERVER)
932                 return Client->id;
933
934         snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
935                  Client->id, Client->user, Client->host);
936         return Mask_Buffer;
937 } /* Client_Mask */
938
939
940 /**
941  * Return ID of a client with cloaked hostname: "client!user@server-name"
942  *
943  * This client ID is used for IRC prefixes, for example.
944  * Please note that this function uses a global static buffer, so you can't
945  * nest invocations without overwriting earlier results!
946  * If the client has not enabled cloaking, the real hostname is used.
947  *
948  * @param Client Pointer to client structure
949  * @return Pointer to global buffer containing the client ID
950  */
951 GLOBAL char *
952 Client_MaskCloaked(CLIENT *Client)
953 {
954         static char Mask_Buffer[GETID_LEN];
955
956         assert (Client != NULL);
957
958         /* Is the client using cloaking at all? */
959         if (!Client_HasMode(Client, 'x'))
960                 return Client_Mask(Client);
961
962         snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
963                  Client_HostnameDisplayed(Client));
964
965         return Mask_Buffer;
966 } /* Client_MaskCloaked */
967
968
969 GLOBAL CLIENT *
970 Client_Introducer( CLIENT *Client )
971 {
972         assert( Client != NULL );
973         return Client->introducer;
974 } /* Client_Introducer */
975
976
977 GLOBAL CLIENT *
978 Client_TopServer( CLIENT *Client )
979 {
980         assert( Client != NULL );
981         return Client->topserver;
982 } /* Client_TopServer */
983
984
985 GLOBAL bool
986 Client_HasMode( CLIENT *Client, char Mode )
987 {
988         assert( Client != NULL );
989         return strchr( Client->modes, Mode ) != NULL;
990 } /* Client_HasMode */
991
992
993 GLOBAL bool
994 Client_HasFlag( CLIENT *Client, char Flag )
995 {
996         assert( Client != NULL );
997         return strchr( Client->flags, Flag ) != NULL;
998 } /* Client_HasFlag */
999
1000
1001 GLOBAL char *
1002 Client_Away( CLIENT *Client )
1003 {
1004         assert( Client != NULL );
1005         return Client->away;
1006 } /* Client_Away */
1007
1008
1009 GLOBAL char *
1010 Client_AccountName(CLIENT *Client)
1011 {
1012         assert(Client != NULL);
1013         return Client->account_name;
1014 }
1015
1016
1017 /**
1018  * Make sure that a given nickname is valid.
1019  *
1020  * If the nickname is not valid for the given client, this function sends back
1021  * the appropriate error messages.
1022  *
1023  * @param       Client Client that wants to change the nickname.
1024  * @param       Nick New nickname.
1025  * @returns     true if nickname is valid, false otherwise.
1026  */
1027 GLOBAL bool
1028 Client_CheckNick(CLIENT *Client, char *Nick)
1029 {
1030         assert(Client != NULL);
1031         assert(Nick != NULL);
1032
1033         if (!Client_IsValidNick(Nick)) {
1034                 if (strlen(Nick ) >= Conf_MaxNickLength)
1035                         IRC_WriteErrClient(Client, ERR_NICKNAMETOOLONG_MSG,
1036                                            Client_ID(Client), Nick,
1037                                            Conf_MaxNickLength - 1);
1038                 else
1039                         IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1040                                            Client_ID(Client), Nick);
1041                 return false;
1042         }
1043
1044         if (Client_Type(Client) != CLIENT_SERVER
1045             && Client_Type(Client) != CLIENT_SERVICE) {
1046                 /* Make sure that this isn't a restricted/forbidden nickname */
1047                 if (Conf_NickIsBlocked(Nick)) {
1048                         IRC_WriteErrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
1049                                            Client_ID(Client), Nick);
1050                         return false;
1051                 }
1052         }
1053
1054         /* Nickname already registered? */
1055         if (Client_Search(Nick)) {
1056                 IRC_WriteErrClient(Client, ERR_NICKNAMEINUSE_MSG,
1057                         Client_ID(Client), Nick);
1058                 return false;
1059         }
1060
1061         return true;
1062 } /* Client_CheckNick */
1063
1064
1065 GLOBAL bool
1066 Client_CheckID( CLIENT *Client, char *ID )
1067 {
1068         char str[COMMAND_LEN];
1069         CLIENT *c;
1070
1071         assert( Client != NULL );
1072         assert( Client->conn_id > NONE );
1073         assert( ID != NULL );
1074
1075         /* ID too long? */
1076         if (strlen(ID) > CLIENT_ID_LEN) {
1077                 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1078                                    Client_ID(Client), ID);
1079                 return false;
1080         }
1081
1082         /* ID already in use? */
1083         c = My_Clients;
1084         while (c) {
1085                 if (strcasecmp(c->id, ID) == 0) {
1086                         snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
1087                         if (c->conn_id != NONE)
1088                                 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
1089                         else
1090                                 Log(LOG_ERR, "%s (via network)!", str);
1091                         Conn_Close(Client->conn_id, str, str, true);
1092                         return false;
1093                 }
1094                 c = (CLIENT *)c->next;
1095         }
1096
1097         return true;
1098 } /* Client_CheckID */
1099
1100
1101 GLOBAL CLIENT *
1102 Client_First( void )
1103 {
1104         return My_Clients;
1105 } /* Client_First */
1106
1107
1108 GLOBAL CLIENT *
1109 Client_Next( CLIENT *c )
1110 {
1111         assert( c != NULL );
1112         return (CLIENT *)c->next;
1113 } /* Client_Next */
1114
1115
1116 GLOBAL long
1117 Client_UserCount( void )
1118 {
1119         return Count( CLIENT_USER );
1120 } /* Client_UserCount */
1121
1122
1123 GLOBAL long
1124 Client_ServiceCount( void )
1125 {
1126         return Count( CLIENT_SERVICE );;
1127 } /* Client_ServiceCount */
1128
1129
1130 GLOBAL long
1131 Client_ServerCount( void )
1132 {
1133         return Count( CLIENT_SERVER );
1134 } /* Client_ServerCount */
1135
1136
1137 GLOBAL long
1138 Client_MyUserCount( void )
1139 {
1140         return MyCount( CLIENT_USER );
1141 } /* Client_MyUserCount */
1142
1143
1144 GLOBAL long
1145 Client_MyServiceCount( void )
1146 {
1147         return MyCount( CLIENT_SERVICE );
1148 } /* Client_MyServiceCount */
1149
1150
1151 GLOBAL unsigned long
1152 Client_MyServerCount( void )
1153 {
1154         CLIENT *c;
1155         unsigned long cnt = 0;
1156
1157         c = My_Clients;
1158         while( c )
1159         {
1160                 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
1161                 c = (CLIENT *)c->next;
1162         }
1163         return cnt;
1164 } /* Client_MyServerCount */
1165
1166
1167 GLOBAL unsigned long
1168 Client_OperCount( void )
1169 {
1170         CLIENT *c;
1171         unsigned long cnt = 0;
1172
1173         c = My_Clients;
1174         while( c )
1175         {
1176                 if (c && c->type == CLIENT_USER && Client_HasMode(c, 'o' ))
1177                         cnt++;
1178                 c = (CLIENT *)c->next;
1179         }
1180         return cnt;
1181 } /* Client_OperCount */
1182
1183
1184 GLOBAL unsigned long
1185 Client_UnknownCount( void )
1186 {
1187         CLIENT *c;
1188         unsigned long cnt = 0;
1189
1190         c = My_Clients;
1191         while( c )
1192         {
1193                 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1194                 c = (CLIENT *)c->next;
1195         }
1196
1197         return cnt;
1198 } /* Client_UnknownCount */
1199
1200
1201 GLOBAL long
1202 Client_MaxUserCount( void )
1203 {
1204         return Max_Users;
1205 } /* Client_MaxUserCount */
1206
1207
1208 GLOBAL long
1209 Client_MyMaxUserCount( void )
1210 {
1211         return My_Max_Users;
1212 } /* Client_MyMaxUserCount */
1213
1214
1215 /**
1216  * Check that a given nickname is valid.
1217  *
1218  * @param       Nick the nickname to check.
1219  * @returns     true if nickname is valid, false otherwise.
1220  */
1221 GLOBAL bool
1222 Client_IsValidNick(const char *Nick)
1223 {
1224         const char *ptr;
1225         static const char goodchars[] = ";0123456789-";
1226
1227         assert (Nick != NULL);
1228
1229         if (strchr(goodchars, Nick[0]))
1230                 return false;
1231         if (strlen(Nick ) >= Conf_MaxNickLength)
1232                 return false;
1233
1234         ptr = Nick;
1235         while (*ptr) {
1236                 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1237                         return false;
1238                 if (*ptr > '}')
1239                         return false;
1240                 ptr++;
1241         }
1242
1243         return true;
1244 } /* Client_IsValidNick */
1245
1246
1247 /**
1248  * Return pointer to "My_Whowas" structure.
1249  */
1250 GLOBAL WHOWAS *
1251 Client_GetWhowas( void )
1252 {
1253         return My_Whowas;
1254 } /* Client_GetWhowas */
1255
1256 /**
1257  * Return the index of the last used WHOWAS entry.
1258  */
1259 GLOBAL int
1260 Client_GetLastWhowasIndex( void )
1261 {
1262         return Last_Whowas;
1263 } /* Client_GetLastWhowasIndex */
1264
1265
1266 /**
1267  * Get the start time of this client.
1268  * The result is the start time in seconds since 1970-01-01, as reported
1269  * by the C function time(NULL).
1270  */
1271 GLOBAL time_t
1272 Client_StartTime(CLIENT *Client)
1273 {
1274         assert( Client != NULL );
1275         return Client->starttime;
1276 } /* Client_Uptime */
1277
1278
1279 /**
1280  * Reject a client when logging in.
1281  *
1282  * This function is called when a client isn't allowed to connect to this
1283  * server. Possible reasons are bad server password, bad PAM password,
1284  * or that the client is G/K-Line'd.
1285  *
1286  * After calling this function, the client isn't connected any more.
1287  *
1288  * @param Client The client to reject.
1289  * @param Reason The reason why the client has been rejected.
1290  * @param InformClient If true, send the exact reason to the client.
1291  */
1292 GLOBAL void
1293 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1294 {
1295         char info[COMMAND_LEN];
1296
1297         assert(Client != NULL);
1298         assert(Reason != NULL);
1299
1300         if (InformClient)
1301                 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1302         else
1303                 strcpy(info, "Access denied: Bad password?");
1304
1305         Log(LOG_ERR,
1306             "User \"%s\" rejected (connection %d): %s!",
1307             Client_Mask(Client), Client_Conn(Client), Reason);
1308         Conn_Close(Client_Conn(Client), Reason, info, true);
1309 }
1310
1311
1312 /**
1313  * Introduce a new user or service client in the network.
1314  *
1315  * @param From Remote server introducing the client or NULL (local).
1316  * @param Client New client.
1317  * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1318  */
1319 GLOBAL void
1320 Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
1321 {
1322         /* Set client type (user or service) */
1323         Client_SetType(Client, Type);
1324
1325         if (From) {
1326                 if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
1327                                    Client_ID(Client)))
1328                         Client_SetType(Client, CLIENT_SERVICE);
1329                 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1330                          Client_TypeText(Client), Client_Mask(Client),
1331                          Client_Modes(Client), Client_ID(From),
1332                          Client_ID(Client_Introducer(Client)),
1333                          Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1334         } else {
1335                 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1336                     Client_TypeText(Client), Client_Mask(Client),
1337                     Client_Conn(Client));
1338                 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1339                                  Client_ID(Client), Client_User(Client),
1340                                  Client_Hostname(Client),
1341                                  Conn_IPA(Client_Conn(Client)),
1342                                  Client_TypeText(Client));
1343         }
1344
1345         /* Inform other servers */
1346         IRC_WriteStrServersPrefixFlag_CB(From,
1347                                 From != NULL ? From : Client_ThisServer(),
1348                                 '\0', cb_introduceClient, (void *)Client);
1349 } /* Client_Introduce */
1350
1351
1352 static unsigned long
1353 Count( CLIENT_TYPE Type )
1354 {
1355         CLIENT *c;
1356         unsigned long cnt = 0;
1357
1358         c = My_Clients;
1359         while( c )
1360         {
1361                 if( c->type == Type ) cnt++;
1362                 c = (CLIENT *)c->next;
1363         }
1364         return cnt;
1365 } /* Count */
1366
1367
1368 static unsigned long
1369 MyCount( CLIENT_TYPE Type )
1370 {
1371         CLIENT *c;
1372         unsigned long cnt = 0;
1373
1374         c = My_Clients;
1375         while( c )
1376         {
1377                 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1378                 c = (CLIENT *)c->next;
1379         }
1380         return cnt;
1381 } /* MyCount */
1382
1383
1384 /**
1385  * Allocate and initialize new CLIENT strcuture.
1386  *
1387  * @return Pointer to CLIENT structure or NULL on error.
1388  */
1389 static CLIENT *
1390 New_Client_Struct( void )
1391 {
1392         CLIENT *c;
1393
1394         c = (CLIENT *)malloc( sizeof( CLIENT ));
1395         if( ! c )
1396         {
1397                 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1398                 return NULL;
1399         }
1400
1401         memset( c, 0, sizeof ( CLIENT ));
1402
1403         c->type = CLIENT_UNKNOWN;
1404         c->conn_id = NONE;
1405         c->hops = -1;
1406         c->token = -1;
1407         c->mytoken = -1;
1408
1409         return c;
1410 }
1411
1412 /**
1413  * Free a CLIENT structure and its member variables.
1414  */
1415 static void
1416 Free_Client(CLIENT **Client)
1417 {
1418         assert(Client != NULL);
1419         assert(*Client != NULL);
1420
1421         if ((*Client)->account_name)
1422                 free((*Client)->account_name);
1423         if ((*Client)->away)
1424                 free((*Client)->away);
1425         if ((*Client)->cloaked)
1426                 free((*Client)->cloaked);
1427         if ((*Client)->ipa_text)
1428                 free((*Client)->ipa_text);
1429
1430         free(*Client);
1431         *Client = NULL;
1432 }
1433
1434 static void
1435 Generate_MyToken( CLIENT *Client )
1436 {
1437         CLIENT *c;
1438         int token;
1439
1440         c = My_Clients;
1441         token = 2;
1442         while( c )
1443         {
1444                 if( c->mytoken == token )
1445                 {
1446                         /* The token is already in use */
1447                         token++;
1448                         c = My_Clients;
1449                         continue;
1450                 }
1451                 else c = (CLIENT *)c->next;
1452         }
1453         Client->mytoken = token;
1454         LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1455 } /* Generate_MyToken */
1456
1457
1458 static void
1459 Adjust_Counters( CLIENT *Client )
1460 {
1461         long count;
1462
1463         assert( Client != NULL );
1464
1465         if( Client->type != CLIENT_USER ) return;
1466
1467         if( Client->conn_id != NONE )
1468         {
1469                 /* Local connection */
1470                 count = Client_MyUserCount( );
1471                 if( count > My_Max_Users ) My_Max_Users = count;
1472         }
1473         count = Client_UserCount( );
1474         if( count > Max_Users ) Max_Users = count;
1475 } /* Adjust_Counters */
1476
1477
1478 /**
1479  * Register client in My_Whowas structure for further recall by WHOWAS.
1480  * Note: Only clients that have been connected at least 30 seconds will be
1481  * registered to prevent automated IRC bots to "destroy" a nice server
1482  * history database.
1483  */
1484 GLOBAL void
1485 Client_RegisterWhowas( CLIENT *Client )
1486 {
1487         int slot;
1488         time_t now;
1489
1490         assert( Client != NULL );
1491
1492         /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1493         if (Conf_MorePrivacy)
1494                 return;
1495
1496         now = time(NULL);
1497         /* Don't register clients that were connected less than 30 seconds. */
1498         if( now - Client->starttime < 30 )
1499                 return;
1500
1501         slot = Last_Whowas + 1;
1502         if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1503
1504 #ifdef DEBUG
1505         Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1506 #endif
1507
1508         My_Whowas[slot].time = now;
1509         strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1510                  sizeof( My_Whowas[slot].id ));
1511         strlcpy( My_Whowas[slot].user, Client_User( Client ),
1512                  sizeof( My_Whowas[slot].user ));
1513         strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
1514                  sizeof( My_Whowas[slot].host ));
1515         strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1516                  sizeof( My_Whowas[slot].info ));
1517         strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1518                  sizeof( My_Whowas[slot].server ));
1519
1520         Last_Whowas = slot;
1521 } /* Client_RegisterWhowas */
1522
1523
1524 GLOBAL const char *
1525 Client_TypeText(CLIENT *Client)
1526 {
1527         assert(Client != NULL);
1528         switch (Client_Type(Client)) {
1529                 case CLIENT_USER:
1530                         return "User";
1531                         break;
1532                 case CLIENT_SERVICE:
1533                         return "Service";
1534                         break;
1535                 case CLIENT_SERVER:
1536                         return "Server";
1537                         break;
1538                 default:
1539                         return "Client";
1540         }
1541 } /* Client_TypeText */
1542
1543
1544 /**
1545  * Destroy user or service client.
1546  */
1547 static void
1548 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1549 {
1550         if(Client->conn_id != NONE) {
1551                 /* Local (directly connected) client */
1552                 Log(LOG_NOTICE,
1553                     "%s \"%s\" unregistered (connection %d): %s.",
1554                     Client_TypeText(Client), Client_Mask(Client),
1555                     Client->conn_id, Txt);
1556                 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1557                                  Client_ID(Client), Client_User(Client),
1558                                  Client_Hostname(Client), Txt);
1559
1560                 if (SendQuit) {
1561                         /* Inforam all the other servers */
1562                         if (FwdMsg)
1563                                 IRC_WriteStrServersPrefix(NULL,
1564                                                 Client, "QUIT :%s", FwdMsg );
1565                         else
1566                                 IRC_WriteStrServersPrefix(NULL,
1567                                                 Client, "QUIT :");
1568                 }
1569         } else {
1570                 /* Remote client */
1571                 LogDebug("%s \"%s\" unregistered: %s.",
1572                          Client_TypeText(Client), Client_Mask(Client), Txt);
1573
1574                 if(SendQuit) {
1575                         /* Inform all the other servers, but the ones in the
1576                          * direction we got the QUIT from */
1577                         if(FwdMsg)
1578                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1579                                                 Client, "QUIT :%s", FwdMsg );
1580                         else
1581                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1582                                                 Client, "QUIT :" );
1583                 }
1584         }
1585
1586         /* Unregister client from channels */
1587         Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1588
1589         /* Register client in My_Whowas structure */
1590         Client_RegisterWhowas(Client);
1591 } /* Destroy_UserOrService */
1592
1593
1594 /**
1595  * Introduce a new user or service client to a remote server.
1596  *
1597  * @param To            The remote server to inform.
1598  * @param Prefix        Prefix for the generated commands.
1599  * @param data          CLIENT structure of the new client.
1600  */
1601 static void
1602 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1603 {
1604         CLIENT *c = (CLIENT *)data;
1605
1606         (void)Client_Announce(To, Prefix, c);
1607
1608 } /* cb_introduceClient */
1609
1610
1611 /**
1612  * Announce an user or service to a server.
1613  *
1614  * This function differentiates between RFC1459 and RFC2813 server links and
1615  * generates the appropriate commands to register the user or service.
1616  *
1617  * @param Client        Server
1618  * @param Prefix        Prefix for the generated commands
1619  * @param User          User to announce
1620  */
1621 GLOBAL bool
1622 Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User)
1623 {
1624         CONN_ID conn;
1625         char *modes, *user, *host;
1626
1627         modes = Client_Modes(User);
1628         user = Client_User(User) ? Client_User(User) : "-";
1629         host = Client_Hostname(User) ? Client_Hostname(User) : "-";
1630
1631         conn = Client_Conn(Client);
1632         if (Conn_Options(conn) & CONN_RFC1459) {
1633                 /* RFC 1459 mode: separate NICK and USER commands */
1634                 if (! Conn_WriteStr(conn, "NICK %s :%d",
1635                                     Client_ID(User), Client_Hops(User) + 1))
1636                         return DISCONNECTED;
1637                 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1638                                      Client_ID(User), user, host,
1639                                      Client_ID(Client_Introducer(User)),
1640                                      Client_Info(User)))
1641                         return DISCONNECTED;
1642                 if (modes[0]) {
1643                         if (! Conn_WriteStr(conn, ":%s MODE %s +%s",
1644                                      Client_ID(User), Client_ID(User),
1645                                      modes))
1646                                 return DISCONNECTED;
1647                 }
1648         } else {
1649                 /* RFC 2813 mode: one combined NICK or SERVICE command */
1650                 if (Client_Type(User) == CLIENT_SERVICE
1651                     && Client_HasFlag(Client, 'S')) {
1652                         if (!IRC_WriteStrClientPrefix(Client, Prefix,
1653                                         "SERVICE %s %d * +%s %d :%s",
1654                                         Client_Mask(User),
1655                                         Client_MyToken(Client_Introducer(User)),
1656                                         modes, Client_Hops(User) + 1,
1657                                         Client_Info(User)))
1658                                 return DISCONNECTED;
1659                 } else {
1660                         if (!IRC_WriteStrClientPrefix(Client, Prefix,
1661                                         "NICK %s %d %s %s %d +%s :%s",
1662                                         Client_ID(User), Client_Hops(User) + 1,
1663                                         user, host,
1664                                         Client_MyToken(Client_Introducer(User)),
1665                                         modes, Client_Info(User)))
1666                                 return DISCONNECTED;
1667                 }
1668         }
1669
1670         if (Client_HasFlag(Client, 'M')) {
1671                 /* Synchronize metadata */
1672                 if (Client_HostnameCloaked(User)) {
1673                         if (!IRC_WriteStrClientPrefix(Client, Prefix,
1674                                         "METADATA %s cloakhost :%s",
1675                                         Client_ID(User),
1676                                         Client_HostnameCloaked(User)))
1677                                 return DISCONNECTED;
1678                 }
1679
1680                 if (Client_AccountName(User)) {
1681                         if (!IRC_WriteStrClientPrefix(Client, Prefix,
1682                                         "METADATA %s accountname :%s",
1683                                         Client_ID(User),
1684                                         Client_AccountName(User)))
1685                                 return DISCONNECTED;
1686                 }
1687
1688                 if (Conn_GetCertFp(Client_Conn(User))) {
1689                         if (!IRC_WriteStrClientPrefix(Client, Prefix,
1690                                         "METADATA %s certfp :%s",
1691                                         Client_ID(User),
1692                                         Conn_GetCertFp(Client_Conn(User))))
1693                                 return DISCONNECTED;
1694                 }
1695         }
1696
1697         return CONNECTED;
1698 } /* Client_Announce */
1699
1700
1701 #ifdef DEBUG
1702
1703 GLOBAL void
1704 Client_DebugDump(void)
1705 {
1706         CLIENT *c;
1707
1708         Log(LOG_DEBUG, "Client status:");
1709         c = My_Clients;
1710         while (c) {
1711                 Log(LOG_DEBUG,
1712                     " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1713                    Client_ID(c), Client_Type(c), Client_Hostname(c),
1714                    Client_User(c), Client_Conn(c), Client_StartTime(c),
1715                    Client_Flags(c));
1716                 c = (CLIENT *)c->next;
1717         }
1718 } /* Client_DumpClients */
1719
1720 #endif
1721
1722
1723 /* -eof- */