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