]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
- einige assert()'s eingestraeut.
[ngircd-alex.git] / src / ngircd / client.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001 by Alexander Barton (alex@barton.de)
4  *
5  * Dieses Programm ist freie Software. Sie koennen es unter den Bedingungen
6  * der GNU General Public License (GPL), wie von der Free Software Foundation
7  * herausgegeben, weitergeben und/oder modifizieren, entweder unter Version 2
8  * der Lizenz oder (wenn Sie es wuenschen) jeder spaeteren Version.
9  * Naehere Informationen entnehmen Sie bitter der Datei COPYING. Eine Liste
10  * der an comBase beteiligten Autoren finden Sie in der Datei AUTHORS.
11  *
12  * $Id: client.c,v 1.3 2001/12/24 01:31:14 alex Exp $
13  *
14  * client.c: Management aller Clients
15  *
16  * Der Begriff "Client" ist in diesem Fall evtl. etwas verwirrend: Clients sind
17  * alle Verbindungen, die im gesamten(!) IRC-Netzwerk bekannt sind. Das sind IRC-
18  * Clients (User), andere Server und IRC-Services.
19  * Ueber welchen IRC-Server die Verbindung nun tatsaechlich in das Netzwerk her-
20  * gestellt wurde, muss der jeweiligen Struktur entnommen werden. Ist es dieser
21  * Server gewesen, so existiert eine entsprechende CONNECTION-Struktur.
22  *
23  * $Log: client.c,v $
24  * Revision 1.3  2001/12/24 01:31:14  alex
25  * - einige assert()'s eingestraeut.
26  *
27  * Revision 1.2  2001/12/23 22:04:37  alex
28  * - einige neue Funktionen,
29  * - CLIENT-Struktur erweitert.
30  *
31  * Revision 1.1  2001/12/14 08:13:43  alex
32  * - neues Modul begonnen :-)
33  *
34  */
35
36
37 #include <portab.h>
38 #include "global.h"
39
40 #include <imp.h>
41 #include <assert.h>
42 #include <unistd.h>
43 #include <string.h>
44
45 #include "channel.h"
46 #include "conn.h"
47 #include "log.h"
48
49 #include <exp.h>
50 #include "client.h"
51
52
53 LOCAL CLIENT *My_Clients;
54
55
56 LOCAL CLIENT *New_Client_Struct( VOID );
57
58
59 GLOBAL VOID Client_Init( VOID )
60 {
61         This_Server = New_Client_Struct( );
62         if( ! This_Server )
63         {
64                 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
65                 exit( 1 );
66         }
67
68         /* Client-Struktur dieses Servers */
69         This_Server->next = NULL;
70         This_Server->type = CLIENT_SERVER;
71         This_Server->conn_id = NONE;
72         This_Server->introducer = This_Server;
73         gethostname( This_Server->host, CLIENT_HOST_LEN );
74         strcpy( This_Server->nick, This_Server->host );
75
76         My_Clients = This_Server;
77 } /* Client_Init */
78
79
80 GLOBAL VOID Client_Exit( VOID )
81 {
82         CLIENT *c, *next;
83         INT cnt;
84
85         Client_Destroy( This_Server );
86         
87         cnt = 0;
88         c = My_Clients;
89         while( c )
90         {
91                 cnt++;
92                 next = c->next;
93                 free( c );
94                 c = next;
95         }
96         if( cnt ) Log( LOG_DEBUG, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
97 } /* Client Exit */
98
99
100 GLOBAL CLIENT *Client_New_Local( CONN_ID Idx, CHAR *Hostname )
101 {
102         /* Neuen lokalen Client erzeugen. */
103         
104         CLIENT *client;
105
106         assert( Idx >= 0 );
107         assert( Hostname != NULL );
108         
109         client = New_Client_Struct( );
110         if( ! client ) return NULL;
111
112         /* Initgialisieren */
113         client->conn_id = Idx;
114         client->introducer = This_Server;
115         strncpy( client->host, Hostname, CLIENT_HOST_LEN );
116         client->host[CLIENT_HOST_LEN] = '\0';
117
118         /* Verketten */
119         client->next = My_Clients;
120         My_Clients = client;
121         
122         return client;
123 } /* Client_New_Local */
124
125
126 GLOBAL VOID Client_Destroy( CLIENT *Client )
127 {
128         /* Client entfernen. */
129         
130         CLIENT *last, *c;
131
132         assert( Client != NULL );
133         
134         last = NULL;
135         c = My_Clients;
136         while( c )
137         {
138                 if( c == Client )
139                 {
140                         if( last ) last->next = c->next;
141                         else My_Clients = c->next;
142                         free( c );
143                         break;
144                 }
145                 last = c;
146                 c = c->next;
147         }
148 } /* Client_Destroy */
149
150
151 GLOBAL CLIENT *Client_GetFromConn( CONN_ID Idx )
152 {
153         /* Client-Struktur, die zur lokalen Verbindung Idx gehoert
154          * liefern. Wird keine gefunden, so wird NULL geliefert. */
155
156         CLIENT *c;
157
158         assert( Idx >= 0 );
159         
160         c = My_Clients;
161         while( c )
162         {
163                 if( c->conn_id == Idx ) return c;
164                 c = c->next;
165         }
166         return NULL;
167 } /* Client_GetFromConn */
168
169
170 LOCAL CLIENT *New_Client_Struct( VOID )
171 {
172         /* Neue CLIENT-Struktur pre-initialisieren */
173         
174         CLIENT *c;
175         INT i;
176         
177         c = malloc( sizeof( CLIENT ));
178         if( ! c )
179         {
180                 Log( LOG_EMERG, "Can't allocate memory!" );
181                 return NULL;
182         }
183
184         c->next = NULL;
185         c->type = CLIENT_UNKNOWN;
186         c->conn_id = NONE;
187         c->introducer = NULL;
188         strcpy( c->nick, "" );
189         strcpy( c->pass, "" );
190         strcpy( c->host, "" );
191         strcpy( c->user, "" );
192         strcpy( c->name, "" );
193         for( i = 0; i < MAX_CHANNELS; c->channels[i++] = NULL );
194
195         return c;
196 } /* New_Client */
197
198
199 /* -eof- */