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