]> arthur.barton.de Git - ngircd.git/blob - src/ngircd/client.c
- Changed semantics of Client_SetAway() [from HEAD].
[ngircd.git] / src / ngircd / client.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
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  * Client management.
12  */
13
14
15 #define __client_c__
16
17
18 #include "portab.h"
19
20 static char UNUSED id[] = "$Id: client.c,v 1.65.2.2 2003/01/08 23:09:04 alex Exp $";
21
22 #include "imp.h"
23 #include <assert.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <netdb.h>
29
30 #include "conn.h"
31
32 #include "exp.h"
33 #include "client.h"
34
35 #include <imp.h>
36 #include "ngircd.h"
37 #include "channel.h"
38 #include "resolve.h"
39 #include "conf.h"
40 #include "hash.h"
41 #include "irc-write.h"
42 #include "log.h"
43 #include "messages.h"
44
45 #include <exp.h>
46
47
48 #define GETID_LEN (CLIENT_NICK_LEN-1) + 1 + (CLIENT_USER_LEN-1) + 1 + (CLIENT_HOST_LEN-1) + 1
49
50
51 LOCAL CLIENT *This_Server, *My_Clients;
52 LOCAL CHAR GetID_Buffer[GETID_LEN];
53
54
55 LOCAL LONG Count PARAMS(( CLIENT_TYPE Type ));
56 LOCAL LONG MyCount PARAMS(( CLIENT_TYPE Type ));
57
58 LOCAL CLIENT *New_Client_Struct PARAMS(( VOID ));
59 LOCAL VOID Generate_MyToken PARAMS(( CLIENT *Client ));
60 LOCAL VOID Adjust_Counters PARAMS(( CLIENT *Client ));
61
62
63 LONG Max_Users = 0, My_Max_Users = 0;
64
65
66 GLOBAL VOID
67 Client_Init( VOID )
68 {
69         struct hostent *h;
70         
71         This_Server = New_Client_Struct( );
72         if( ! This_Server )
73         {
74                 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
75                 Log( LOG_ALERT, "%s exiting due to fatal errors!", PACKAGE );
76                 exit( 1 );
77         }
78
79         /* Client-Struktur dieses Servers */
80         This_Server->next = NULL;
81         This_Server->type = CLIENT_SERVER;
82         This_Server->conn_id = NONE;
83         This_Server->introducer = This_Server;
84         This_Server->mytoken = 1;
85         This_Server->hops = 0;
86
87         gethostname( This_Server->host, CLIENT_HOST_LEN );
88         h = gethostbyname( This_Server->host );
89         if( h ) strcpy( This_Server->host, h->h_name );
90
91         Client_SetID( This_Server, Conf_ServerName );
92         Client_SetInfo( This_Server, Conf_ServerInfo );
93
94         My_Clients = This_Server;
95 } /* Client_Init */
96
97
98 GLOBAL VOID
99 Client_Exit( VOID )
100 {
101         CLIENT *c, *next;
102         INT cnt;
103
104         if( NGIRCd_Restart ) Client_Destroy( This_Server, "Server going down (restarting).", NULL, FALSE );
105         else Client_Destroy( This_Server, "Server going down.", NULL, FALSE );
106         
107         cnt = 0;
108         c = My_Clients;
109         while( c )
110         {
111                 cnt++;
112                 next = (CLIENT *)c->next;
113                 free( c );
114                 c = next;
115         }
116         if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
117 } /* Client_Exit */
118
119
120 GLOBAL CLIENT *
121 Client_ThisServer( VOID )
122 {
123         return This_Server;
124 } /* Client_ThisServer */
125
126
127 GLOBAL CLIENT *
128 Client_NewLocal( CONN_ID Idx, CHAR *Hostname, INT Type, BOOLEAN Idented )
129 {
130         /* Neuen lokalen Client erzeugen: Wrapper-Funktion fuer Client_New(). */
131         return Client_New( Idx, This_Server, NULL, Type, NULL, NULL, Hostname, NULL, 0, 0, NULL, Idented );
132 } /* Client_NewLocal */
133
134
135 GLOBAL CLIENT *
136 Client_NewRemoteServer( CLIENT *Introducer, CHAR *Hostname, CLIENT *TopServer, INT Hops, INT Token, CHAR *Info, BOOLEAN Idented )
137 {
138         /* Neuen Remote-Client erzeugen: Wrapper-Funktion fuer Client_New (). */
139         return Client_New( NONE, Introducer, TopServer, CLIENT_SERVER, Hostname, NULL, Hostname, Info, Hops, Token, NULL, Idented );
140 } /* Client_NewRemoteServer */
141
142
143 GLOBAL CLIENT *
144 Client_NewRemoteUser( CLIENT *Introducer, CHAR *Nick, INT Hops, CHAR *User, CHAR *Hostname, INT Token, CHAR *Modes, CHAR *Info, BOOLEAN Idented )
145 {
146         /* Neuen Remote-Client erzeugen: Wrapper-Funktion fuer Client_New (). */
147         return Client_New( NONE, Introducer, NULL, CLIENT_USER, Nick, User, Hostname, Info, Hops, Token, Modes, Idented );
148 } /* Client_NewRemoteUser */
149
150
151 GLOBAL CLIENT *
152 Client_New( CONN_ID Idx, CLIENT *Introducer, CLIENT *TopServer, INT Type, CHAR *ID, CHAR *User, CHAR *Hostname, CHAR *Info, INT Hops, INT Token, CHAR *Modes, BOOLEAN Idented )
153 {
154         CLIENT *client;
155
156         assert( Idx >= NONE );
157         assert( Introducer != NULL );
158         assert( Hostname != NULL );
159
160         client = New_Client_Struct( );
161         if( ! client ) return NULL;
162
163         /* Initialisieren */
164         client->conn_id = Idx;
165         client->introducer = Introducer;
166         client->topserver = TopServer;
167         client->type = Type;
168         if( ID ) Client_SetID( client, ID );
169         if( User ) Client_SetUser( client, User, Idented );
170         if( Hostname ) Client_SetHostname( client, Hostname );
171         if( Info ) Client_SetInfo( client, Info );
172         client->hops = Hops;
173         client->token = Token;
174         if( Modes ) Client_SetModes( client, Modes );
175         if( Type == CLIENT_SERVER ) Generate_MyToken( client );
176
177         /* ist der User away? */
178         if( strchr( client->modes, 'a' )) strcpy( client->away, DEFAULT_AWAY_MSG );
179
180         /* Verketten */
181         client->next = (POINTER *)My_Clients;
182         My_Clients = client;
183
184         /* Adjust counters */
185         Adjust_Counters( client );
186
187         return client;
188 } /* Client_New */
189
190
191 GLOBAL VOID
192 Client_Destroy( CLIENT *Client, CHAR *LogMsg, CHAR *FwdMsg, BOOLEAN SendQuit )
193 {
194         /* Client entfernen. */
195         
196         CLIENT *last, *c;
197         CHAR msg[LINE_LEN], *txt;
198
199         assert( Client != NULL );
200
201         if( LogMsg ) txt = LogMsg;
202         else txt = FwdMsg;
203         if( ! txt ) txt = "Reason unknown.";
204
205         /* Netz-Split-Nachricht vorbereiten (noch nicht optimal) */
206         if( Client->type == CLIENT_SERVER ) sprintf( msg, "%s: lost server %s", This_Server->id, Client->id );
207
208         last = NULL;
209         c = My_Clients;
210         while( c )
211         {
212                 if(( Client->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c != Client ))
213                 {
214                         /* der Client, der geloescht wird ist ein Server. Der Client, den wir gerade
215                          * pruefen, ist ein Child von diesem und muss daher auch entfernt werden */
216                         Client_Destroy( c, NULL, msg, FALSE );
217                         last = NULL;
218                         c = My_Clients;
219                         continue;
220                 }
221                 if( c == Client )
222                 {
223                         /* Wir haben den Client gefunden: entfernen */
224                         if( last ) last->next = c->next;
225                         else My_Clients = (CLIENT *)c->next;
226
227                         if( c->type == CLIENT_USER )
228                         {
229                                 if( c->conn_id != NONE )
230                                 {
231                                         /* Ein lokaler User */
232                                         Log( LOG_NOTICE, "User \"%s\" unregistered (connection %d): %s", Client_Mask( c ), c->conn_id, txt );
233
234                                         if( SendQuit )
235                                         {
236                                                 /* Alle andere Server informieren! */
237                                                 if( FwdMsg ) IRC_WriteStrServersPrefix( NULL, c, "QUIT :%s", FwdMsg );
238                                                 else IRC_WriteStrServersPrefix( NULL, c, "QUIT :" );
239                                         }
240                                 }
241                                 else
242                                 {
243                                         /* Remote User */
244                                         Log( LOG_DEBUG, "User \"%s\" unregistered: %s", Client_Mask( c ), txt );
245
246                                         if( SendQuit )
247                                         {
248                                                 /* Andere Server informieren, ausser denen, die "in
249                                                  * Richtung dem liegen", auf dem der User registriert
250                                                  * ist. Von denen haben wir das QUIT ja wohl bekommen. */
251                                                 if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "QUIT :%s", FwdMsg );
252                                                 else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "QUIT :" );
253                                         }
254                                 }
255                                 Channel_Quit( c, FwdMsg ? FwdMsg : c->id );
256                         }
257                         else if( c->type == CLIENT_SERVER )
258                         {
259                                 if( c != This_Server )
260                                 {
261                                         if( c->conn_id != NONE ) Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
262                                         else Log( LOG_NOTICE|LOG_snotice, "Server \"%s\" unregistered: %s", c->id, txt );
263                                 }
264
265                                 /* andere Server informieren */
266                                 if( ! NGIRCd_Quit )
267                                 {
268                                         if( FwdMsg ) IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :%s", c->id, FwdMsg );
269                                         else IRC_WriteStrServersPrefix( Client_NextHop( c ), c, "SQUIT %s :", c->id );
270                                 }
271                         }
272                         else
273                         {
274                                 if( c->conn_id != NONE )
275                                 {
276                                         if( c->id[0] ) Log( LOG_NOTICE, "Client \"%s\" unregistered (connection %d): %s", c->id, c->conn_id, txt );
277                                         else Log( LOG_NOTICE, "Client unregistered (connection %d): %s", c->conn_id, txt );
278                                 }
279                                 else
280                                 {
281                                         if( c->id[0] ) Log( LOG_WARNING, "Unregistered unknown client \"%s\": %s", c->id, txt );
282                                         else Log( LOG_WARNING, "Unregistered unknown client: %s", c->id, txt );
283                                 }
284                         }
285
286                         free( c );
287                         break;
288                 }
289                 last = c;
290                 c = (CLIENT *)c->next;
291         }
292 } /* Client_Destroy */
293
294
295 GLOBAL VOID
296 Client_SetHostname( CLIENT *Client, CHAR *Hostname )
297 {
298         /* Hostname eines Clients setzen */
299         
300         assert( Client != NULL );
301         assert( Hostname != NULL );
302         
303         strncpy( Client->host, Hostname, CLIENT_HOST_LEN - 1 );
304         Client->host[CLIENT_HOST_LEN - 1] = '\0';
305 } /* Client_SetHostname */
306
307
308 GLOBAL VOID
309 Client_SetID( CLIENT *Client, CHAR *ID )
310 {
311         /* Hostname eines Clients setzen, Hash-Wert berechnen */
312
313         assert( Client != NULL );
314         assert( ID != NULL );
315         
316         strncpy( Client->id, ID, CLIENT_ID_LEN - 1 );
317         Client->id[CLIENT_ID_LEN - 1] = '\0';
318
319         /* Hash */
320         Client->hash = Hash( Client->id );
321 } /* Client_SetID */
322
323
324 GLOBAL VOID
325 Client_SetUser( CLIENT *Client, CHAR *User, BOOLEAN Idented )
326 {
327         /* Username eines Clients setzen */
328
329         assert( Client != NULL );
330         assert( User != NULL );
331         
332         if( Idented ) strncpy( Client->user, User, CLIENT_USER_LEN - 1 );
333         else
334         {
335                 Client->user[0] = '~';
336                 strncpy( Client->user + 1, User, CLIENT_USER_LEN - 2 );
337         }
338         Client->user[CLIENT_USER_LEN - 1] = '\0';
339 } /* Client_SetUser */
340
341
342 GLOBAL VOID
343 Client_SetInfo( CLIENT *Client, CHAR *Info )
344 {
345         /* Hostname eines Clients setzen */
346
347         assert( Client != NULL );
348         assert( Info != NULL );
349         
350         strncpy( Client->info, Info, CLIENT_INFO_LEN - 1 );
351         Client->info[CLIENT_INFO_LEN - 1] = '\0';
352 } /* Client_SetInfo */
353
354
355 GLOBAL VOID
356 Client_SetModes( CLIENT *Client, CHAR *Modes )
357 {
358         /* Modes eines Clients setzen */
359
360         assert( Client != NULL );
361         assert( Modes != NULL );
362
363         strncpy( Client->modes, Modes, CLIENT_MODE_LEN - 1 );
364         Client->modes[CLIENT_MODE_LEN - 1] = '\0';
365 } /* Client_SetModes */
366
367
368 GLOBAL VOID
369 Client_SetFlags( CLIENT *Client, CHAR *Flags )
370 {
371         /* Flags eines Clients setzen */
372
373         assert( Client != NULL );
374         assert( Flags != NULL );
375
376         strncpy( Client->flags, Flags, CLIENT_FLAGS_LEN - 1 );
377         Client->flags[CLIENT_FLAGS_LEN - 1] = '\0';
378 } /* Client_SetFlags */
379
380
381 GLOBAL VOID
382 Client_SetPassword( CLIENT *Client, CHAR *Pwd )
383 {
384         /* Von einem Client geliefertes Passwort */
385
386         assert( Client != NULL );
387         assert( Pwd != NULL );
388         
389         strncpy( Client->pwd, Pwd, CLIENT_PASS_LEN - 1 );
390         Client->pwd[CLIENT_PASS_LEN - 1] = '\0';
391 } /* Client_SetPassword */
392
393
394 GLOBAL VOID
395 Client_SetAway( CLIENT *Client, CHAR *Txt )
396 {
397         /* Set AWAY reason of client */
398         
399         assert( Client != NULL );
400         assert( Txt != NULL );
401
402         strlcpy( Client->away, Txt, sizeof( Client->away ));
403         Log( LOG_DEBUG, "User \"%s\" is away: %s", Client_Mask( Client ), Txt );
404 } /* Client_SetAway */
405
406
407 GLOBAL VOID
408 Client_SetType( CLIENT *Client, INT Type )
409 {
410         assert( Client != NULL );
411         Client->type = Type;
412         if( Type == CLIENT_SERVER ) Generate_MyToken( Client );
413         Adjust_Counters( Client );
414 } /* Client_SetType */
415
416
417 GLOBAL VOID
418 Client_SetHops( CLIENT *Client, INT Hops )
419 {
420         assert( Client != NULL );
421         Client->hops = Hops;
422 } /* Client_SetHops */
423
424
425 GLOBAL VOID
426 Client_SetToken( CLIENT *Client, INT Token )
427 {
428         assert( Client != NULL );
429         Client->token = Token;
430 } /* Client_SetToken */
431
432
433 GLOBAL VOID
434 Client_SetIntroducer( CLIENT *Client, CLIENT *Introducer )
435 {
436         assert( Client != NULL );
437         assert( Introducer != NULL );
438         Client->introducer = Introducer;
439 } /* Client_SetIntroducer */
440
441
442 GLOBAL VOID
443 Client_SetOperByMe( CLIENT *Client, BOOLEAN OperByMe )
444 {
445         assert( Client != NULL );
446         Client->oper_by_me = OperByMe;
447 } /* Client_SetOperByMe */
448
449
450 GLOBAL BOOLEAN
451 Client_ModeAdd( CLIENT *Client, CHAR Mode )
452 {
453         /* Mode soll gesetzt werden. TRUE wird geliefert, wenn der
454          * Mode neu gesetzt wurde, FALSE, wenn der Client den Mode
455          * bereits hatte. */
456
457         CHAR x[2];
458         
459         assert( Client != NULL );
460
461         x[0] = Mode; x[1] = '\0';
462         if( ! strchr( Client->modes, x[0] ))
463         {
464                 /* Client hat den Mode noch nicht -> setzen */
465                 strcat( Client->modes, x );
466                 return TRUE;
467         }
468         else return FALSE;
469 } /* Client_ModeAdd */
470
471
472 GLOBAL BOOLEAN
473 Client_ModeDel( CLIENT *Client, CHAR Mode )
474 {
475         /* Mode soll geloescht werden. TRUE wird geliefert, wenn der
476         * Mode entfernt wurde, FALSE, wenn der Client den Mode
477         * ueberhaupt nicht hatte. */
478
479         CHAR x[2], *p;
480
481         assert( Client != NULL );
482
483         x[0] = Mode; x[1] = '\0';
484
485         p = strchr( Client->modes, x[0] );
486         if( ! p ) return FALSE;
487
488         /* Client hat den Mode -> loeschen */
489         while( *p )
490         {
491                 *p = *(p + 1);
492                 p++;
493         }
494         return TRUE;
495 } /* Client_ModeDel */
496
497
498 GLOBAL CLIENT *
499 Client_GetFromConn( CONN_ID Idx )
500 {
501         /* Client-Struktur, die zur lokalen Verbindung Idx gehoert,
502          * liefern. Wird keine gefunden, so wird NULL geliefert. */
503
504         CLIENT *c;
505
506         assert( Idx >= 0 );
507         
508         c = My_Clients;
509         while( c )
510         {
511                 if( c->conn_id == Idx ) return c;
512                 c = (CLIENT *)c->next;
513         }
514         return NULL;
515 } /* Client_GetFromConn */
516
517
518 GLOBAL CLIENT *
519 Client_Search( CHAR *Nick )
520 {
521         /* Client-Struktur, die den entsprechenden Nick hat, liefern.
522          * Wird keine gefunden, so wird NULL geliefert. */
523
524         CHAR search_id[CLIENT_ID_LEN], *ptr;
525         CLIENT *c = NULL;
526         UINT32 search_hash;
527
528         assert( Nick != NULL );
529
530         /* Nick kopieren und ggf. Host-Mask abschneiden */
531         strncpy( search_id, Nick, CLIENT_ID_LEN - 1 );
532         search_id[CLIENT_ID_LEN - 1] = '\0';
533         ptr = strchr( search_id, '!' );
534         if( ptr ) *ptr = '\0';
535
536         search_hash = Hash( search_id );
537
538         c = My_Clients;
539         while( c )
540         {
541                 if( c->hash == search_hash )
542                 {
543                         /* lt. Hash-Wert: Treffer! */
544                         if( strcasecmp( c->id, search_id ) == 0 ) return c;
545                 }
546                 c = (CLIENT *)c->next;
547         }
548         return NULL;
549 } /* Client_Search */
550
551
552 GLOBAL CLIENT *
553 Client_GetFromToken( CLIENT *Client, INT Token )
554 {
555         /* Client-Struktur, die den entsprechenden Introducer (=Client)
556          * und das gegebene Token hat, liefern. Wird keine gefunden,
557          * so wird NULL geliefert. */
558
559         CLIENT *c;
560
561         assert( Client != NULL );
562         assert( Token > 0 );
563
564         c = My_Clients;
565         while( c )
566         {
567                 if(( c->type == CLIENT_SERVER ) && ( c->introducer == Client ) && ( c->token == Token )) return c;
568                 c = (CLIENT *)c->next;
569         }
570         return NULL;
571 } /* Client_GetFromToken */
572
573
574 GLOBAL INT
575 Client_Type( CLIENT *Client )
576 {
577         assert( Client != NULL );
578         return Client->type;
579 } /* Client_Type */
580
581
582 GLOBAL CONN_ID
583 Client_Conn( CLIENT *Client )
584 {
585         assert( Client != NULL );
586         return Client->conn_id;
587 } /* Client_Conn */
588
589
590 GLOBAL CHAR *
591 Client_ID( CLIENT *Client )
592 {
593         assert( Client != NULL );
594
595 #ifdef DEBUG
596         if( Client->type == CLIENT_USER ) assert( strlen( Client->id ) < CLIENT_NICK_LEN );
597 #endif
598                                                    
599         if( Client->id[0] ) return Client->id;
600         else return "*";
601 } /* Client_ID */
602
603
604 GLOBAL CHAR *
605 Client_Info( CLIENT *Client )
606 {
607         assert( Client != NULL );
608         return Client->info;
609 } /* Client_Info */
610
611
612 GLOBAL CHAR *
613 Client_User( CLIENT *Client )
614 {
615         assert( Client != NULL );
616         if( Client->user[0] ) return Client->user;
617         else return "~";
618 } /* Client_User */
619
620
621 GLOBAL CHAR *
622 Client_Hostname( CLIENT *Client )
623 {
624         assert( Client != NULL );
625         return Client->host;
626 } /* Client_Hostname */
627
628
629 GLOBAL CHAR *
630 Client_Password( CLIENT *Client )
631 {
632         assert( Client != NULL );
633         return Client->pwd;
634 } /* Client_Password */
635
636
637 GLOBAL CHAR *
638 Client_Modes( CLIENT *Client )
639 {
640         assert( Client != NULL );
641         return Client->modes;
642 } /* Client_Modes */
643
644
645 GLOBAL CHAR *
646 Client_Flags( CLIENT *Client )
647 {
648         assert( Client != NULL );
649         return Client->flags;
650 } /* Client_Flags */
651
652
653 GLOBAL BOOLEAN
654 Client_OperByMe( CLIENT *Client )
655 {
656         assert( Client != NULL );
657         return Client->oper_by_me;
658 } /* Client_OperByMe */
659
660
661 GLOBAL INT
662 Client_Hops( CLIENT *Client )
663 {
664         assert( Client != NULL );
665         return Client->hops;
666 } /* Client_Hops */
667
668
669 GLOBAL INT
670 Client_Token( CLIENT *Client )
671 {
672         assert( Client != NULL );
673         return Client->token;
674 } /* Client_Token */
675
676
677 GLOBAL INT
678 Client_MyToken( CLIENT *Client )
679 {
680         assert( Client != NULL );
681         return Client->mytoken;
682 } /* Client_MyToken */
683
684
685 GLOBAL CLIENT *
686 Client_NextHop( CLIENT *Client )
687 {
688         CLIENT *c;
689         
690         assert( Client != NULL );
691
692         c = Client;
693         while( c->introducer && ( c->introducer != c ) && ( c->introducer != This_Server )) c = c->introducer;
694         return c;
695 } /* Client_NextHop */
696
697
698 GLOBAL CHAR *
699 Client_Mask( CLIENT *Client )
700 {
701         /* Client-"ID" liefern, wie sie z.B. fuer
702          * Prefixe benoetigt wird. */
703
704         assert( Client != NULL );
705         
706         if( Client->type == CLIENT_SERVER ) return Client->id;
707
708         snprintf( GetID_Buffer, GETID_LEN, "%s!%s@%s", Client->id, Client->user, Client->host );
709         return GetID_Buffer;
710 } /* Client_Mask */
711
712
713 GLOBAL CLIENT *
714 Client_Introducer( CLIENT *Client )
715 {
716         assert( Client != NULL );
717         return Client->introducer;
718 } /* Client_Introducer */
719
720
721 GLOBAL CLIENT *
722 Client_TopServer( CLIENT *Client )
723 {
724         assert( Client != NULL );
725         return Client->topserver;
726 } /* Client_TopServer */
727
728
729 GLOBAL BOOLEAN
730 Client_HasMode( CLIENT *Client, CHAR Mode )
731 {
732         assert( Client != NULL );
733         return strchr( Client->modes, Mode ) != NULL;
734 } /* Client_HasMode */
735
736
737 GLOBAL CHAR *
738 Client_Away( CLIENT *Client )
739 {
740         /* AWAY-Text liefern */
741
742         assert( Client != NULL );
743         return Client->away;
744 } /* Client_Away */
745
746
747 GLOBAL BOOLEAN
748 Client_CheckNick( CLIENT *Client, CHAR *Nick )
749 {
750         /* Nick ueberpruefen */
751
752         assert( Client != NULL );
753         assert( Nick != NULL );
754         
755         /* Nick ungueltig? */
756         if( ! Client_IsValidNick( Nick ))
757         {
758                 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), Nick );
759                 return FALSE;
760         }
761
762         /* Nick bereits vergeben? */
763         if( Client_Search( Nick ))
764         {
765                 /* den Nick gibt es bereits */
766                 IRC_WriteStrClient( Client, ERR_NICKNAMEINUSE_MSG, Client_ID( Client ), Nick );
767                 return FALSE;
768         }
769
770         return TRUE;
771 } /* Client_CheckNick */
772
773
774 GLOBAL BOOLEAN
775 Client_CheckID( CLIENT *Client, CHAR *ID )
776 {
777         /* Nick ueberpruefen */
778
779         CHAR str[COMMAND_LEN];
780         CLIENT *c;
781
782         assert( Client != NULL );
783         assert( Client->conn_id > NONE );
784         assert( ID != NULL );
785
786         /* Nick zu lang? */
787         if( strlen( ID ) > CLIENT_ID_LEN )
788         {
789                 IRC_WriteStrClient( Client, ERR_ERRONEUSNICKNAME_MSG, Client_ID( Client ), ID );
790                 return FALSE;
791         }
792
793         /* ID bereits vergeben? */
794         c = My_Clients;
795         while( c )
796         {
797                 if( strcasecmp( c->id, ID ) == 0 )
798                 {
799                         /* die Server-ID gibt es bereits */
800                         sprintf( str, "ID \"%s\" already registered", ID );
801                         if( Client->conn_id != c->conn_id ) Log( LOG_ERR, "%s (on connection %d)!", str, c->conn_id );
802                         else Log( LOG_ERR, "%s (via network)!", str );
803                         Conn_Close( Client->conn_id, str, str, TRUE );
804                         return FALSE;
805                 }
806                 c = (CLIENT *)c->next;
807         }
808
809         return TRUE;
810 } /* Client_CheckID */
811
812
813 GLOBAL CLIENT *
814 Client_First( VOID )
815 {
816         /* Ersten Client liefern. */
817
818         return My_Clients;
819 } /* Client_First */
820
821
822 GLOBAL CLIENT *
823 Client_Next( CLIENT *c )
824 {
825         /* Naechsten Client liefern. Existiert keiner,
826          * so wird NULL geliefert. */
827
828         assert( c != NULL );
829         return (CLIENT *)c->next;
830 } /* Client_Next */
831
832
833 GLOBAL LONG
834 Client_UserCount( VOID )
835 {
836         return Count( CLIENT_USER );
837 } /* Client_UserCount */
838
839
840 GLOBAL LONG
841 Client_ServiceCount( VOID )
842 {
843         return Count( CLIENT_SERVICE );;
844 } /* Client_ServiceCount */
845
846
847 GLOBAL LONG
848 Client_ServerCount( VOID )
849 {
850         return Count( CLIENT_SERVER );
851 } /* Client_ServerCount */
852
853
854 GLOBAL LONG
855 Client_MyUserCount( VOID )
856 {
857         return MyCount( CLIENT_USER );
858 } /* Client_MyUserCount */
859
860
861 GLOBAL LONG
862 Client_MyServiceCount( VOID )
863 {
864         return MyCount( CLIENT_SERVICE );
865 } /* Client_MyServiceCount */
866
867
868 GLOBAL LONG
869 Client_MyServerCount( VOID )
870 {
871         CLIENT *c;
872         LONG cnt;
873
874         cnt = 0;
875         c = My_Clients;
876         while( c )
877         {
878                 if(( c->type == CLIENT_SERVER ) && ( c->hops == 1 )) cnt++;
879                 c = (CLIENT *)c->next;
880         }
881         return cnt;
882 } /* Client_MyServerCount */
883
884
885 GLOBAL LONG
886 Client_OperCount( VOID )
887 {
888         CLIENT *c;
889         LONG cnt;
890
891         cnt = 0;
892         c = My_Clients;
893         while( c )
894         {
895                 if( c && ( c->type == CLIENT_USER ) && ( strchr( c->modes, 'o' ))) cnt++;
896                 c = (CLIENT *)c->next;
897         }
898         return cnt;
899 } /* Client_OperCount */
900
901
902 GLOBAL LONG
903 Client_UnknownCount( VOID )
904 {
905         CLIENT *c;
906         LONG cnt;
907
908         cnt = 0;
909         c = My_Clients;
910         while( c )
911         {
912                 if( c && ( c->type != CLIENT_USER ) && ( c->type != CLIENT_SERVICE ) && ( c->type != CLIENT_SERVER )) cnt++;
913                 c = (CLIENT *)c->next;
914         }
915         return cnt;
916 } /* Client_UnknownCount */
917
918
919 GLOBAL LONG
920 Client_MaxUserCount( VOID )
921 {
922         return Max_Users;
923 } /* Client_MaxUserCount */
924
925
926 GLOBAL LONG
927 Client_MyMaxUserCount( VOID )
928 {
929         return My_Max_Users;
930 } /* Client_MyMaxUserCount */
931
932
933 GLOBAL BOOLEAN
934 Client_IsValidNick( CHAR *Nick )
935 {
936         /* Ist der Nick gueltig? */
937
938         CHAR *ptr, goodchars[20];
939         
940         assert( Nick != NULL );
941
942         strcpy( goodchars, ";0123456789-" );
943
944         if( Nick[0] == '#' ) return FALSE;
945         if( strchr( goodchars, Nick[0] )) return FALSE;
946         if( strlen( Nick ) >= CLIENT_NICK_LEN ) return FALSE;
947
948         ptr = Nick;
949         while( *ptr )
950         {
951                 if(( *ptr < 'A' ) && ( ! strchr( goodchars, *ptr ))) return FALSE;
952                 if(( *ptr > '}' ) && ( ! strchr( goodchars, *ptr ))) return FALSE;
953                 ptr++;
954         }
955         
956         return TRUE;
957 } /* Client_IsValidNick */
958
959
960 LOCAL LONG
961 Count( CLIENT_TYPE Type )
962 {
963         CLIENT *c;
964         LONG cnt;
965
966         cnt = 0;
967         c = My_Clients;
968         while( c )
969         {
970                 if( c->type == Type ) cnt++;
971                 c = (CLIENT *)c->next;
972         }
973         return cnt;
974 } /* Count */
975
976
977 LOCAL LONG
978 MyCount( CLIENT_TYPE Type )
979 {
980         CLIENT *c;
981         LONG cnt;
982
983         cnt = 0;
984         c = My_Clients;
985         while( c )
986         {
987                 if(( c->introducer == This_Server ) && ( c->type == Type )) cnt++;
988                 c = (CLIENT *)c->next;
989         }
990         return cnt;
991 } /* MyCount */
992
993
994 LOCAL CLIENT *
995 New_Client_Struct( VOID )
996 {
997         /* Neue CLIENT-Struktur pre-initialisieren */
998         
999         CLIENT *c;
1000         
1001         c = malloc( sizeof( CLIENT ));
1002         if( ! c )
1003         {
1004                 Log( LOG_EMERG, "Can't allocate memory! [New_Client_Struct]" );
1005                 return NULL;
1006         }
1007
1008         c->next = NULL;
1009         c->hash = 0;
1010         c->type = CLIENT_UNKNOWN;
1011         c->conn_id = NONE;
1012         c->introducer = NULL;
1013         c->topserver = NULL;
1014         strcpy( c->id, "" );
1015         strcpy( c->pwd, "" );
1016         strcpy( c->host, "" );
1017         strcpy( c->user, "" );
1018         strcpy( c->info, "" );
1019         strcpy( c->modes, "" );
1020         c->oper_by_me = FALSE;
1021         c->hops = -1;
1022         c->token = -1;
1023         c->mytoken = -1;
1024         strcpy( c->away, "" );
1025         strcpy( c->flags, "" );
1026
1027         return c;
1028 } /* New_Client */
1029
1030
1031 LOCAL VOID
1032 Generate_MyToken( CLIENT *Client )
1033 {
1034         CLIENT *c;
1035         INT token;
1036
1037         c = My_Clients;
1038         token = 2;
1039         while( c )
1040         {
1041                 if( c->mytoken == token )
1042                 {
1043                         /* Das Token wurde bereits vergeben */
1044                         token++;
1045                         c = My_Clients;
1046                         continue;
1047                 }
1048                 else c = (CLIENT *)c->next;
1049         }
1050         Client->mytoken = token;
1051         Log( LOG_DEBUG, "Assigned token %d to server \"%s\".", token, Client->id );
1052 } /* Generate_MyToken */
1053
1054
1055 LOCAL VOID
1056 Adjust_Counters( CLIENT *Client )
1057 {
1058         LONG count;
1059
1060         assert( Client != NULL );
1061
1062         if( Client->type != CLIENT_USER ) return;
1063         
1064         if( Client->conn_id != NONE )
1065         {
1066                 /* Local connection */
1067                 count = Client_MyUserCount( );
1068                 if( count > My_Max_Users ) My_Max_Users = count;
1069         }
1070         count = Client_UserCount( );
1071         if( count > Max_Users ) Max_Users = count;
1072 } /* Adjust_Counters */
1073
1074
1075 /* -eof- */