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