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