]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
Don't send nick name as default PART reason
[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)
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         return Client->orig_user;
735 } /* Client_OrigUser */
736
737 #endif
738
739 /**
740  * Return the hostname of a client.
741  * @param Client Pointer to client structure
742  * @return Pointer to client hostname
743  */
744 GLOBAL char *
745 Client_Hostname(CLIENT *Client)
746 {
747         assert (Client != NULL);
748         return Client->host;
749 }
750
751 /**
752  * Return the cloaked hostname of a client, if set.
753  * @param Client Pointer to the client structure.
754  * @return Pointer to the cloaked hostname or NULL if not set.
755  */
756 GLOBAL char *
757 Client_HostnameCloaked(CLIENT *Client)
758 {
759         assert(Client != NULL);
760         return Client->cloaked;
761 }
762
763 /**
764  * Get (potentially cloaked) hostname of a client to display it to other users.
765  *
766  * If the client has not enabled cloaking, the real hostname is used.
767  *
768  * @param Client Pointer to client structure
769  * @return Pointer to client hostname
770  */
771 GLOBAL char *
772 Client_HostnameDisplayed(CLIENT *Client)
773 {
774         assert(Client != NULL);
775
776         /* Client isn't cloaked at all, return real hostname: */
777         if (!Client_HasMode(Client, 'x'))
778                 return Client_Hostname(Client);
779
780         /* Use an already saved cloaked hostname, if there is one */
781         if (Client->cloaked)
782                 return Client->cloaked;
783
784         Client_UpdateCloakedHostname(Client, NULL, NULL);
785         return Client->cloaked;
786 }
787
788 GLOBAL const char *
789 Client_IPAText(CLIENT *Client)
790 {
791         assert(Client != NULL);
792
793         /* Not a local client? */
794         if (Client_Conn(Client) <= NONE)
795                 return "0.0.0.0";
796
797         if (!Client->ipa_text)
798                 return Conn_GetIPAInfo(Client_Conn(Client));
799         else
800                 return Client->ipa_text;
801 }
802
803 /**
804  * Update (and generate, if necessary) the cloaked hostname of a client.
805  *
806  * The newly set cloaked hostname is announced in the network using METADATA
807  * commands to peers that support this feature.
808  *
809  * @param Client The client of which the cloaked hostname should be updated.
810  * @param Origin The originator of the hostname change, or NULL if this server.
811  * @param Hostname The new cloaked hostname, or NULL if it should be generated.
812  */
813 GLOBAL void
814 Client_UpdateCloakedHostname(CLIENT *Client, CLIENT *Origin,
815                              const char *Hostname)
816 {
817         char Cloak_Buffer[CLIENT_HOST_LEN];
818
819         assert(Client != NULL);
820         if (!Origin)
821                 Origin = Client_ThisServer();
822
823         if (!Client->cloaked) {
824                 Client->cloaked = malloc(CLIENT_HOST_LEN);
825                 if (!Client->cloaked)
826                         return;
827         }
828
829         if (!Hostname) {
830                 /* Generate new cloaked hostname */
831                 if (*Conf_CloakHostModeX) {
832                         strlcpy(Cloak_Buffer, Client->host,
833                                 sizeof(Cloak_Buffer));
834                         strlcat(Cloak_Buffer, Conf_CloakHostSalt,
835                                 sizeof(Cloak_Buffer));
836                         snprintf(Client->cloaked, CLIENT_HOST_LEN,
837                                  Conf_CloakHostModeX, Hash(Cloak_Buffer));
838                 } else
839                         strlcpy(Client->cloaked, Client_ID(Client->introducer),
840                                 CLIENT_HOST_LEN);
841         } else
842                 strlcpy(Client->cloaked, Hostname, CLIENT_HOST_LEN);
843         LogDebug("Cloaked hostname of \"%s\" updated to \"%s\"",
844                  Client_ID(Client), Client->cloaked);
845
846         /* Inform other servers in the network */
847         IRC_WriteStrServersPrefixFlag(Client_NextHop(Origin), Origin, 'M',
848                                       "METADATA %s cloakhost :%s",
849                                       Client_ID(Client), Client->cloaked);
850 }
851
852 GLOBAL char *
853 Client_Modes( CLIENT *Client )
854 {
855         assert( Client != NULL );
856         return Client->modes;
857 } /* Client_Modes */
858
859
860 GLOBAL char *
861 Client_Flags( CLIENT *Client )
862 {
863         assert( Client != NULL );
864         return Client->flags;
865 } /* Client_Flags */
866
867
868 GLOBAL int
869 Client_Hops( CLIENT *Client )
870 {
871         assert( Client != NULL );
872         return Client->hops;
873 } /* Client_Hops */
874
875
876 GLOBAL int
877 Client_Token( CLIENT *Client )
878 {
879         assert( Client != NULL );
880         return Client->token;
881 } /* Client_Token */
882
883
884 GLOBAL int
885 Client_MyToken( CLIENT *Client )
886 {
887         assert( Client != NULL );
888         return Client->mytoken;
889 } /* Client_MyToken */
890
891
892 GLOBAL CLIENT *
893 Client_NextHop( CLIENT *Client )
894 {
895         CLIENT *c;
896
897         assert( Client != NULL );
898
899         c = Client;
900         while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server ))
901                 c = c->introducer;
902
903         return c;
904 } /* Client_NextHop */
905
906
907 /**
908  * Return ID of a client: "client!user@host"
909  * This client ID is used for IRC prefixes, for example.
910  * Please note that this function uses a global static buffer, so you can't
911  * nest invocations without overwriting earlier results!
912  * @param Client Pointer to client structure
913  * @return Pointer to global buffer containing the client ID
914  */
915 GLOBAL char *
916 Client_Mask( CLIENT *Client )
917 {
918         static char Mask_Buffer[GETID_LEN];
919
920         assert (Client != NULL);
921
922         /* Servers: return name only, there is no "mask" */
923         if (Client->type == CLIENT_SERVER)
924                 return Client->id;
925
926         snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s",
927                  Client->id, Client->user, Client->host);
928         return Mask_Buffer;
929 } /* Client_Mask */
930
931
932 /**
933  * Return ID of a client with cloaked hostname: "client!user@server-name"
934  *
935  * This client ID is used for IRC prefixes, for example.
936  * Please note that this function uses a global static buffer, so you can't
937  * nest invocations without overwriting earlier results!
938  * If the client has not enabled cloaking, the real hostname is used.
939  *
940  * @param Client Pointer to client structure
941  * @return Pointer to global buffer containing the client ID
942  */
943 GLOBAL char *
944 Client_MaskCloaked(CLIENT *Client)
945 {
946         static char Mask_Buffer[GETID_LEN];
947
948         assert (Client != NULL);
949
950         /* Is the client using cloaking at all? */
951         if (!Client_HasMode(Client, 'x'))
952                 return Client_Mask(Client);
953
954         snprintf(Mask_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user,
955                  Client_HostnameDisplayed(Client));
956
957         return Mask_Buffer;
958 } /* Client_MaskCloaked */
959
960
961 GLOBAL CLIENT *
962 Client_Introducer( CLIENT *Client )
963 {
964         assert( Client != NULL );
965         return Client->introducer;
966 } /* Client_Introducer */
967
968
969 GLOBAL CLIENT *
970 Client_TopServer( CLIENT *Client )
971 {
972         assert( Client != NULL );
973         return Client->topserver;
974 } /* Client_TopServer */
975
976
977 GLOBAL bool
978 Client_HasMode( CLIENT *Client, char Mode )
979 {
980         assert( Client != NULL );
981         return strchr( Client->modes, Mode ) != NULL;
982 } /* Client_HasMode */
983
984
985 GLOBAL bool
986 Client_HasFlag( CLIENT *Client, char Flag )
987 {
988         assert( Client != NULL );
989         return strchr( Client->flags, Flag ) != NULL;
990 } /* Client_HasFlag */
991
992
993 GLOBAL char *
994 Client_Away( CLIENT *Client )
995 {
996         assert( Client != NULL );
997         return Client->away;
998 } /* Client_Away */
999
1000
1001 GLOBAL char *
1002 Client_AccountName(CLIENT *Client)
1003 {
1004         assert(Client != NULL);
1005         return Client->account_name;
1006 }
1007
1008
1009 /**
1010  * Make sure that a given nickname is valid.
1011  *
1012  * If the nickname is not valid for the given client, this function sends back
1013  * the appropriate error messages.
1014  *
1015  * @param       Client Client that wants to change the nickname.
1016  * @param       Nick New nickname.
1017  * @returns     true if nickname is valid, false otherwise.
1018  */
1019 GLOBAL bool
1020 Client_CheckNick(CLIENT *Client, char *Nick)
1021 {
1022         assert(Client != NULL);
1023         assert(Nick != NULL);
1024
1025         if (!Client_IsValidNick(Nick)) {
1026                 if (strlen(Nick ) >= Conf_MaxNickLength)
1027                         IRC_WriteErrClient(Client, ERR_NICKNAMETOOLONG_MSG,
1028                                            Client_ID(Client), Nick,
1029                                            Conf_MaxNickLength - 1);
1030                 else
1031                         IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1032                                            Client_ID(Client), Nick);
1033                 return false;
1034         }
1035
1036         if (Client_Type(Client) != CLIENT_SERVER
1037             && Client_Type(Client) != CLIENT_SERVICE) {
1038                 /* Make sure that this isn't a restricted/forbidden nickname */
1039                 if (Conf_NickIsBlocked(Nick)) {
1040                         IRC_WriteErrClient(Client, ERR_FORBIDDENNICKNAME_MSG,
1041                                            Client_ID(Client), Nick);
1042                         return false;
1043                 }
1044         }
1045
1046         /* Nickname already registered? */
1047         if (Client_Search(Nick)) {
1048                 IRC_WriteErrClient(Client, ERR_NICKNAMEINUSE_MSG,
1049                         Client_ID(Client), Nick);
1050                 return false;
1051         }
1052
1053         return true;
1054 } /* Client_CheckNick */
1055
1056
1057 GLOBAL bool
1058 Client_CheckID( CLIENT *Client, char *ID )
1059 {
1060         char str[COMMAND_LEN];
1061         CLIENT *c;
1062
1063         assert( Client != NULL );
1064         assert( Client->conn_id > NONE );
1065         assert( ID != NULL );
1066
1067         /* ID too long? */
1068         if (strlen(ID) > CLIENT_ID_LEN) {
1069                 IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
1070                                    Client_ID(Client), ID);
1071                 return false;
1072         }
1073
1074         /* ID already in use? */
1075         c = My_Clients;
1076         while (c) {
1077                 if (strcasecmp(c->id, ID) == 0) {
1078                         snprintf(str, sizeof(str), "ID \"%s\" already registered", ID);
1079                         if (c->conn_id != NONE)
1080                                 Log(LOG_ERR, "%s (on connection %d)!", str, c->conn_id);
1081                         else
1082                                 Log(LOG_ERR, "%s (via network)!", str);
1083                         Conn_Close(Client->conn_id, str, str, true);
1084                         return false;
1085                 }
1086                 c = (CLIENT *)c->next;
1087         }
1088
1089         return true;
1090 } /* Client_CheckID */
1091
1092
1093 GLOBAL CLIENT *
1094 Client_First( void )
1095 {
1096         return My_Clients;
1097 } /* Client_First */
1098
1099
1100 GLOBAL CLIENT *
1101 Client_Next( CLIENT *c )
1102 {
1103         assert( c != NULL );
1104         return (CLIENT *)c->next;
1105 } /* Client_Next */
1106
1107
1108 GLOBAL long
1109 Client_UserCount( void )
1110 {
1111         return Count( CLIENT_USER );
1112 } /* Client_UserCount */
1113
1114
1115 GLOBAL long
1116 Client_ServiceCount( void )
1117 {
1118         return Count( CLIENT_SERVICE );;
1119 } /* Client_ServiceCount */
1120
1121
1122 GLOBAL long
1123 Client_ServerCount( void )
1124 {
1125         return Count( CLIENT_SERVER );
1126 } /* Client_ServerCount */
1127
1128
1129 GLOBAL long
1130 Client_MyUserCount( void )
1131 {
1132         return MyCount( CLIENT_USER );
1133 } /* Client_MyUserCount */
1134
1135
1136 GLOBAL long
1137 Client_MyServiceCount( void )
1138 {
1139         return MyCount( CLIENT_SERVICE );
1140 } /* Client_MyServiceCount */
1141
1142
1143 GLOBAL unsigned long
1144 Client_MyServerCount( void )
1145 {
1146         CLIENT *c;
1147         unsigned long cnt = 0;
1148
1149         c = My_Clients;
1150         while( c )
1151         {
1152                 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
1153                 c = (CLIENT *)c->next;
1154         }
1155         return cnt;
1156 } /* Client_MyServerCount */
1157
1158
1159 GLOBAL unsigned long
1160 Client_OperCount( void )
1161 {
1162         CLIENT *c;
1163         unsigned long cnt = 0;
1164
1165         c = My_Clients;
1166         while( c )
1167         {
1168                 if (c && c->type == CLIENT_USER && Client_HasMode(c, 'o' ))
1169                         cnt++;
1170                 c = (CLIENT *)c->next;
1171         }
1172         return cnt;
1173 } /* Client_OperCount */
1174
1175
1176 GLOBAL unsigned long
1177 Client_UnknownCount( void )
1178 {
1179         CLIENT *c;
1180         unsigned long cnt = 0;
1181
1182         c = My_Clients;
1183         while( c )
1184         {
1185                 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
1186                 c = (CLIENT *)c->next;
1187         }
1188
1189         return cnt;
1190 } /* Client_UnknownCount */
1191
1192
1193 GLOBAL long
1194 Client_MaxUserCount( void )
1195 {
1196         return Max_Users;
1197 } /* Client_MaxUserCount */
1198
1199
1200 GLOBAL long
1201 Client_MyMaxUserCount( void )
1202 {
1203         return My_Max_Users;
1204 } /* Client_MyMaxUserCount */
1205
1206
1207 /**
1208  * Check that a given nickname is valid.
1209  *
1210  * @param       Nick the nickname to check.
1211  * @returns     true if nickname is valid, false otherwise.
1212  */
1213 GLOBAL bool
1214 Client_IsValidNick(const char *Nick)
1215 {
1216         const char *ptr;
1217         static const char goodchars[] = ";0123456789-";
1218
1219         assert (Nick != NULL);
1220
1221         if (strchr(goodchars, Nick[0]))
1222                 return false;
1223         if (strlen(Nick ) >= Conf_MaxNickLength)
1224                 return false;
1225
1226         ptr = Nick;
1227         while (*ptr) {
1228                 if (*ptr < 'A' && !strchr(goodchars, *ptr ))
1229                         return false;
1230                 if (*ptr > '}')
1231                         return false;
1232                 ptr++;
1233         }
1234
1235         return true;
1236 } /* Client_IsValidNick */
1237
1238
1239 /**
1240  * Return pointer to "My_Whowas" structure.
1241  */
1242 GLOBAL WHOWAS *
1243 Client_GetWhowas( void )
1244 {
1245         return My_Whowas;
1246 } /* Client_GetWhowas */
1247
1248 /**
1249  * Return the index of the last used WHOWAS entry.
1250  */
1251 GLOBAL int
1252 Client_GetLastWhowasIndex( void )
1253 {
1254         return Last_Whowas;
1255 } /* Client_GetLastWhowasIndex */
1256
1257
1258 /**
1259  * Get the start time of this client.
1260  * The result is the start time in seconds since 1970-01-01, as reported
1261  * by the C function time(NULL).
1262  */
1263 GLOBAL time_t
1264 Client_StartTime(CLIENT *Client)
1265 {
1266         assert( Client != NULL );
1267         return Client->starttime;
1268 } /* Client_Uptime */
1269
1270
1271 /**
1272  * Reject a client when logging in.
1273  *
1274  * This function is called when a client isn't allowed to connect to this
1275  * server. Possible reasons are bad server password, bad PAM password,
1276  * or that the client is G/K-Line'd.
1277  *
1278  * After calling this function, the client isn't connected any more.
1279  *
1280  * @param Client The client to reject.
1281  * @param Reason The reason why the client has been rejected.
1282  * @param InformClient If true, send the exact reason to the client.
1283  */
1284 GLOBAL void
1285 Client_Reject(CLIENT *Client, const char *Reason, bool InformClient)
1286 {
1287         char info[COMMAND_LEN];
1288
1289         assert(Client != NULL);
1290         assert(Reason != NULL);
1291
1292         if (InformClient)
1293                 snprintf(info, sizeof(info), "Access denied: %s", Reason);
1294         else
1295                 strcpy(info, "Access denied: Bad password?");
1296
1297         Log(LOG_ERR,
1298             "User \"%s\" rejected (connection %d): %s!",
1299             Client_Mask(Client), Client_Conn(Client), Reason);
1300         Conn_Close(Client_Conn(Client), Reason, info, true);
1301 }
1302
1303
1304 /**
1305  * Introduce a new user or service client in the network.
1306  *
1307  * @param From Remote server introducing the client or NULL (local).
1308  * @param Client New client.
1309  * @param Type Type of the client (CLIENT_USER or CLIENT_SERVICE).
1310  */
1311 GLOBAL void
1312 Client_Introduce(CLIENT *From, CLIENT *Client, int Type)
1313 {
1314         /* Set client type (user or service) */
1315         Client_SetType(Client, Type);
1316
1317         if (From) {
1318                 if (Conf_NickIsService(Conf_GetServer(Client_Conn(From)),
1319                                    Client_ID(Client)))
1320                         Client_SetType(Client, CLIENT_SERVICE);
1321                 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
1322                          Client_TypeText(Client), Client_Mask(Client),
1323                          Client_Modes(Client), Client_ID(From),
1324                          Client_ID(Client_Introducer(Client)),
1325                          Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
1326         } else {
1327                 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
1328                     Client_TypeText(Client), Client_Mask(Client),
1329                     Client_Conn(Client));
1330                 Log_ServerNotice('c', "Client connecting: %s (%s@%s) [%s] - %s",
1331                                  Client_ID(Client), Client_User(Client),
1332                                  Client_Hostname(Client),
1333                                  Conn_IPA(Client_Conn(Client)),
1334                                  Client_TypeText(Client));
1335         }
1336
1337         /* Inform other servers */
1338         IRC_WriteStrServersPrefixFlag_CB(From,
1339                                 From != NULL ? From : Client_ThisServer(),
1340                                 '\0', cb_introduceClient, (void *)Client);
1341 } /* Client_Introduce */
1342
1343
1344 static unsigned long
1345 Count( CLIENT_TYPE Type )
1346 {
1347         CLIENT *c;
1348         unsigned long cnt = 0;
1349
1350         c = My_Clients;
1351         while( c )
1352         {
1353                 if( c->type == Type ) cnt++;
1354                 c = (CLIENT *)c->next;
1355         }
1356         return cnt;
1357 } /* Count */
1358
1359
1360 static unsigned long
1361 MyCount( CLIENT_TYPE Type )
1362 {
1363         CLIENT *c;
1364         unsigned long cnt = 0;
1365
1366         c = My_Clients;
1367         while( c )
1368         {
1369                 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
1370                 c = (CLIENT *)c->next;
1371         }
1372         return cnt;
1373 } /* MyCount */
1374
1375
1376 /**
1377  * Allocate and initialize new CLIENT strcuture.
1378  *
1379  * @return Pointer to CLIENT structure or NULL on error.
1380  */
1381 static CLIENT *
1382 New_Client_Struct( void )
1383 {
1384         CLIENT *c;
1385
1386         c = (CLIENT *)malloc( sizeof( CLIENT ));
1387         if( ! c )
1388         {
1389                 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1390                 return NULL;
1391         }
1392
1393         memset( c, 0, sizeof ( CLIENT ));
1394
1395         c->type = CLIENT_UNKNOWN;
1396         c->conn_id = NONE;
1397         c->hops = -1;
1398         c->token = -1;
1399         c->mytoken = -1;
1400
1401         return c;
1402 }
1403
1404 /**
1405  * Free a CLIENT structure and its member variables.
1406  */
1407 static void
1408 Free_Client(CLIENT **Client)
1409 {
1410         assert(Client != NULL);
1411         assert(*Client != NULL);
1412
1413         if ((*Client)->account_name)
1414                 free((*Client)->account_name);
1415         if ((*Client)->away)
1416                 free((*Client)->away);
1417         if ((*Client)->cloaked)
1418                 free((*Client)->cloaked);
1419         if ((*Client)->ipa_text)
1420                 free((*Client)->ipa_text);
1421
1422         free(*Client);
1423         *Client = NULL;
1424 }
1425
1426 static void
1427 Generate_MyToken( CLIENT *Client )
1428 {
1429         CLIENT *c;
1430         int token;
1431
1432         c = My_Clients;
1433         token = 2;
1434         while( c )
1435         {
1436                 if( c->mytoken == token )
1437                 {
1438                         /* The token is already in use */
1439                         token++;
1440                         c = My_Clients;
1441                         continue;
1442                 }
1443                 else c = (CLIENT *)c->next;
1444         }
1445         Client->mytoken = token;
1446         LogDebug("Assigned token %d to server \"%s\".", token, Client->id);
1447 } /* Generate_MyToken */
1448
1449
1450 static void
1451 Adjust_Counters( CLIENT *Client )
1452 {
1453         long count;
1454
1455         assert( Client != NULL );
1456
1457         if( Client->type != CLIENT_USER ) return;
1458
1459         if( Client->conn_id != NONE )
1460         {
1461                 /* Local connection */
1462                 count = Client_MyUserCount( );
1463                 if( count > My_Max_Users ) My_Max_Users = count;
1464         }
1465         count = Client_UserCount( );
1466         if( count > Max_Users ) Max_Users = count;
1467 } /* Adjust_Counters */
1468
1469
1470 /**
1471  * Register client in My_Whowas structure for further recall by WHOWAS.
1472  * Note: Only clients that have been connected at least 30 seconds will be
1473  * registered to prevent automated IRC bots to "destroy" a nice server
1474  * history database.
1475  */
1476 GLOBAL void
1477 Client_RegisterWhowas( CLIENT *Client )
1478 {
1479         int slot;
1480         time_t now;
1481
1482         assert( Client != NULL );
1483
1484         /* Don't register WHOWAS information when "MorePrivacy" is enabled. */
1485         if (Conf_MorePrivacy)
1486                 return;
1487
1488         now = time(NULL);
1489         /* Don't register clients that were connected less than 30 seconds. */
1490         if( now - Client->starttime < 30 )
1491                 return;
1492
1493         slot = Last_Whowas + 1;
1494         if( slot >= MAX_WHOWAS || slot < 0 ) slot = 0;
1495
1496 #ifdef DEBUG
1497         Log( LOG_DEBUG, "Saving WHOWAS information to slot %d ...", slot );
1498 #endif
1499
1500         My_Whowas[slot].time = now;
1501         strlcpy( My_Whowas[slot].id, Client_ID( Client ),
1502                  sizeof( My_Whowas[slot].id ));
1503         strlcpy( My_Whowas[slot].user, Client_User( Client ),
1504                  sizeof( My_Whowas[slot].user ));
1505         strlcpy( My_Whowas[slot].host, Client_HostnameDisplayed( Client ),
1506                  sizeof( My_Whowas[slot].host ));
1507         strlcpy( My_Whowas[slot].info, Client_Info( Client ),
1508                  sizeof( My_Whowas[slot].info ));
1509         strlcpy( My_Whowas[slot].server, Client_ID( Client_Introducer( Client )),
1510                  sizeof( My_Whowas[slot].server ));
1511
1512         Last_Whowas = slot;
1513 } /* Client_RegisterWhowas */
1514
1515
1516 GLOBAL const char *
1517 Client_TypeText(CLIENT *Client)
1518 {
1519         assert(Client != NULL);
1520         switch (Client_Type(Client)) {
1521                 case CLIENT_USER:
1522                         return "User";
1523                         break;
1524                 case CLIENT_SERVICE:
1525                         return "Service";
1526                         break;
1527                 case CLIENT_SERVER:
1528                         return "Server";
1529                         break;
1530                 default:
1531                         return "Client";
1532         }
1533 } /* Client_TypeText */
1534
1535
1536 /**
1537  * Destroy user or service client.
1538  */
1539 static void
1540 Destroy_UserOrService(CLIENT *Client, const char *Txt, const char *FwdMsg, bool SendQuit)
1541 {
1542         if(Client->conn_id != NONE) {
1543                 /* Local (directly connected) client */
1544                 Log(LOG_NOTICE,
1545                     "%s \"%s\" unregistered (connection %d): %s.",
1546                     Client_TypeText(Client), Client_Mask(Client),
1547                     Client->conn_id, Txt);
1548                 Log_ServerNotice('c', "Client exiting: %s (%s@%s) [%s]",
1549                                  Client_ID(Client), Client_User(Client),
1550                                  Client_Hostname(Client), Txt);
1551
1552                 if (SendQuit) {
1553                         /* Inforam all the other servers */
1554                         if (FwdMsg)
1555                                 IRC_WriteStrServersPrefix(NULL,
1556                                                 Client, "QUIT :%s", FwdMsg );
1557                         else
1558                                 IRC_WriteStrServersPrefix(NULL,
1559                                                 Client, "QUIT :");
1560                 }
1561         } else {
1562                 /* Remote client */
1563                 LogDebug("%s \"%s\" unregistered: %s.",
1564                          Client_TypeText(Client), Client_Mask(Client), Txt);
1565
1566                 if(SendQuit) {
1567                         /* Inform all the other servers, but the ones in the
1568                          * direction we got the QUIT from */
1569                         if(FwdMsg)
1570                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1571                                                 Client, "QUIT :%s", FwdMsg );
1572                         else
1573                                 IRC_WriteStrServersPrefix(Client_NextHop(Client),
1574                                                 Client, "QUIT :" );
1575                 }
1576         }
1577
1578         /* Unregister client from channels */
1579         Channel_Quit(Client, FwdMsg ? FwdMsg : Client->id);
1580
1581         /* Register client in My_Whowas structure */
1582         Client_RegisterWhowas(Client);
1583 } /* Destroy_UserOrService */
1584
1585
1586 /**
1587  * Introduce a new user or service client to a remote server.
1588  *
1589  * @param To            The remote server to inform.
1590  * @param Prefix        Prefix for the generated commands.
1591  * @param data          CLIENT structure of the new client.
1592  */
1593 static void
1594 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
1595 {
1596         CLIENT *c = (CLIENT *)data;
1597
1598         (void)Client_Announce(To, Prefix, c);
1599
1600 } /* cb_introduceClient */
1601
1602
1603 /**
1604  * Announce an user or service to a server.
1605  *
1606  * This function differentiates between RFC1459 and RFC2813 server links and
1607  * generates the appropriate commands to register the user or service.
1608  *
1609  * @param Client        Server
1610  * @param Prefix        Prefix for the generated commands
1611  * @param User          User to announce
1612  */
1613 GLOBAL bool
1614 Client_Announce(CLIENT * Client, CLIENT * Prefix, CLIENT * User)
1615 {
1616         CONN_ID conn;
1617         char *modes, *user, *host;
1618
1619         modes = Client_Modes(User);
1620         user = Client_User(User) ? Client_User(User) : "-";
1621         host = Client_Hostname(User) ? Client_Hostname(User) : "-";
1622
1623         conn = Client_Conn(Client);
1624         if (Conn_Options(conn) & CONN_RFC1459) {
1625                 /* RFC 1459 mode: separate NICK and USER commands */
1626                 if (! Conn_WriteStr(conn, "NICK %s :%d",
1627                                     Client_ID(User), Client_Hops(User) + 1))
1628                         return DISCONNECTED;
1629                 if (! Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
1630                                      Client_ID(User), user, host,
1631                                      Client_ID(Client_Introducer(User)),
1632                                      Client_Info(User)))
1633                         return DISCONNECTED;
1634                 if (modes[0]) {
1635                         if (! Conn_WriteStr(conn, ":%s MODE %s +%s",
1636                                      Client_ID(User), Client_ID(User),
1637                                      modes))
1638                                 return DISCONNECTED;
1639                 }
1640         } else {
1641                 /* RFC 2813 mode: one combined NICK or SERVICE command */
1642                 if (Client_Type(User) == CLIENT_SERVICE
1643                     && Client_HasFlag(Client, 'S')) {
1644                         if (!IRC_WriteStrClientPrefix(Client, Prefix,
1645                                         "SERVICE %s %d * +%s %d :%s",
1646                                         Client_Mask(User),
1647                                         Client_MyToken(Client_Introducer(User)),
1648                                         modes, Client_Hops(User) + 1,
1649                                         Client_Info(User)))
1650                                 return DISCONNECTED;
1651                 } else {
1652                         if (!IRC_WriteStrClientPrefix(Client, Prefix,
1653                                         "NICK %s %d %s %s %d +%s :%s",
1654                                         Client_ID(User), Client_Hops(User) + 1,
1655                                         user, host,
1656                                         Client_MyToken(Client_Introducer(User)),
1657                                         modes, Client_Info(User)))
1658                                 return DISCONNECTED;
1659                 }
1660         }
1661
1662         if (Client_HasFlag(Client, 'M')) {
1663                 /* Synchronize metadata */
1664                 if (Client_HostnameCloaked(User)) {
1665                         if (!IRC_WriteStrClientPrefix(Client, Prefix,
1666                                         "METADATA %s cloakhost :%s",
1667                                         Client_ID(User),
1668                                         Client_HostnameCloaked(User)))
1669                                 return DISCONNECTED;
1670                 }
1671
1672                 if (Client_AccountName(User)) {
1673                         if (!IRC_WriteStrClientPrefix(Client, Prefix,
1674                                         "METADATA %s accountname :%s",
1675                                         Client_ID(User),
1676                                         Client_AccountName(User)))
1677                                 return DISCONNECTED;
1678                 }
1679
1680                 if (Conn_GetCertFp(Client_Conn(User))) {
1681                         if (!IRC_WriteStrClientPrefix(Client, Prefix,
1682                                         "METADATA %s certfp :%s",
1683                                         Client_ID(User),
1684                                         Conn_GetCertFp(Client_Conn(User))))
1685                                 return DISCONNECTED;
1686                 }
1687         }
1688
1689         return CONNECTED;
1690 } /* Client_Announce */
1691
1692
1693 #ifdef DEBUG
1694
1695 GLOBAL void
1696 Client_DebugDump(void)
1697 {
1698         CLIENT *c;
1699
1700         Log(LOG_DEBUG, "Client status:");
1701         c = My_Clients;
1702         while (c) {
1703                 Log(LOG_DEBUG,
1704                     " - %s: type=%d, host=%s, user=%s, conn=%d, start=%ld, flags=%s",
1705                    Client_ID(c), Client_Type(c), Client_Hostname(c),
1706                    Client_User(c), Client_Conn(c), Client_StartTime(c),
1707                    Client_Flags(c));
1708                 c = (CLIENT *)c->next;
1709         }
1710 } /* Client_DumpClients */
1711
1712 #endif
1713
1714
1715 /* -eof- */