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