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