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