]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
- Unterstuetzung fuer die Konfigurationsdatei eingebaut.
[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.13 2001/12/30 19:26:11 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.13  2001/12/30 19:26:11  alex
25  * - Unterstuetzung fuer die Konfigurationsdatei eingebaut.
26  *
27  * Revision 1.12  2001/12/29 20:18:18  alex
28  * - neue Funktion Client_SetHostname().
29  *
30  * Revision 1.11  2001/12/29 03:10:47  alex
31  * - Client-Modes implementiert; Loglevel mal wieder angepasst.
32  *
33  * Revision 1.10  2001/12/27 19:13:47  alex
34  * - neue Funktion Client_Search(), besseres Logging.
35  *
36  * Revision 1.9  2001/12/27 17:15:29  alex
37  * - der eigene Hostname wird nun komplet (als FQDN) ermittelt.
38  *
39  * Revision 1.8  2001/12/27 16:54:51  alex
40  * - neue Funktion Client_GetID(), liefert die "Client ID".
41  *
42  * Revision 1.7  2001/12/26 14:45:37  alex
43  * - "Code Cleanups".
44  *
45  * Revision 1.6  2001/12/26 03:19:16  alex
46  * - neue Funktion Client_Name().
47  *
48  * Revision 1.5  2001/12/25 22:04:26  alex
49  * - Aenderungen an den Debug- und Logging-Funktionen.
50  *
51  * Revision 1.4  2001/12/25 19:21:26  alex
52  * - Client-Typ ("Status") besser unterteilt, My_Clients ist zudem nun global.
53  *
54  * Revision 1.3  2001/12/24 01:31:14  alex
55  * - einige assert()'s eingestraeut.
56  *
57  * Revision 1.2  2001/12/23 22:04:37  alex
58  * - einige neue Funktionen,
59  * - CLIENT-Struktur erweitert.
60  *
61  * Revision 1.1  2001/12/14 08:13:43  alex
62  * - neues Modul begonnen :-)
63  */
64
65
66 #include <portab.h>
67 #include "global.h"
68
69 #include <imp.h>
70 #include <assert.h>
71 #include <unistd.h>
72 #include <stdio.h>
73 #include <string.h>
74 #include <netdb.h>
75
76 #include <exp.h>
77 #include "client.h"
78
79 #include <imp.h>
80 #include "channel.h"
81 #include "conf.h"
82 #include "conn.h"
83 #include "irc.h"
84 #include "log.h"
85 #include "messages.h"
86
87 #include <exp.h>
88
89
90 LOCAL CLIENT *My_Clients;
91 LOCAL CHAR GetID_Buffer[CLIENT_ID_LEN];
92
93
94 LOCAL CLIENT *New_Client_Struct( VOID );
95
96
97 GLOBAL VOID Client_Init( VOID )
98 {
99         struct hostent *h;
100         
101         This_Server = New_Client_Struct( );
102         if( ! This_Server )
103         {
104                 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
105                 Log( LOG_ALERT, PACKAGE" exiting due to fatal errors!" );
106                 exit( 1 );
107         }
108
109         /* Client-Struktur dieses Servers */
110         This_Server->next = NULL;
111         This_Server->type = CLIENT_SERVER;
112         This_Server->conn_id = NONE;
113         This_Server->introducer = This_Server;
114
115         gethostname( This_Server->host, CLIENT_HOST_LEN );
116         h = gethostbyname( This_Server->host );
117         if( h ) strcpy( This_Server->host, h->h_name );
118
119         strcpy( This_Server->nick, Conf_ServerName );
120
121         My_Clients = This_Server;
122 } /* Client_Init */
123
124
125 GLOBAL VOID Client_Exit( VOID )
126 {
127         CLIENT *c, *next;
128         INT cnt;
129
130         Client_Destroy( This_Server );
131         
132         cnt = 0;
133         c = My_Clients;
134         while( c )
135         {
136                 cnt++;
137                 next = c->next;
138                 free( c );
139                 c = next;
140         }
141         if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
142 } /* Client Exit */
143
144
145 GLOBAL CLIENT *Client_NewLocal( CONN_ID Idx, CHAR *Hostname )
146 {
147         /* Neuen lokalen Client erzeugen. */
148         
149         CLIENT *client;
150
151         assert( Idx >= 0 );
152         assert( Hostname != NULL );
153         
154         client = New_Client_Struct( );
155         if( ! client ) return NULL;
156
157         /* Initialisieren */
158         client->conn_id = Idx;
159         client->introducer = This_Server;
160         Client_SetHostname( client, Hostname );
161
162         /* Verketten */
163         client->next = My_Clients;
164         My_Clients = client;
165         
166         return client;
167 } /* Client_NewLocal */
168
169
170 GLOBAL VOID Client_Destroy( CLIENT *Client )
171 {
172         /* Client entfernen. */
173         
174         CLIENT *last, *c;
175
176         assert( Client != NULL );
177         
178         last = NULL;
179         c = My_Clients;
180         while( c )
181         {
182                 if( c == Client )
183                 {
184                         if( last ) last->next = c->next;
185                         else My_Clients = c->next;
186
187                         if( c->type == CLIENT_USER ) Log( LOG_NOTICE, "User \"%s!%s@%s\" (%s) exited (connection %d).", c->nick, c->user, c->host, c->name, c->conn_id );
188
189                         free( c );
190                         break;
191                 }
192                 last = c;
193                 c = c->next;
194         }
195 } /* Client_Destroy */
196
197
198 GLOBAL VOID Client_SetHostname( CLIENT *Client, CHAR *Hostname )
199 {
200         /* Hostname eines Clients setzen */
201         
202         assert( Client != NULL );
203         strncpy( Client->host, Hostname, CLIENT_HOST_LEN );
204         Client->host[CLIENT_HOST_LEN] = '\0';
205 } /* Client_SetHostname */
206
207
208 GLOBAL CLIENT *Client_GetFromConn( CONN_ID Idx )
209 {
210         /* Client-Struktur, die zur lokalen Verbindung Idx gehoert
211          * liefern. Wird keine gefunden, so wird NULL geliefert. */
212
213         CLIENT *c;
214
215         assert( Idx >= 0 );
216         
217         c = My_Clients;
218         while( c )
219         {
220                 if( c->conn_id == Idx ) return c;
221                 c = c->next;
222         }
223         return NULL;
224 } /* Client_GetFromConn */
225
226
227 GLOBAL CHAR *Client_Name( CLIENT *Client )
228 {
229         assert( Client != NULL );
230
231         if( Client->nick[0] ) return Client->nick;
232         else return "*";
233 } /* Client_Name */
234
235
236 GLOBAL BOOLEAN Client_CheckNick( CLIENT *Client, CHAR *Nick )
237 {
238         /* Nick ueberpruefen */
239
240         CLIENT *c;
241         
242         assert( Client != NULL );
243         assert( Nick != NULL );
244         
245         /* Nick zu lang? */
246         if( strlen( Nick ) > CLIENT_NICK_LEN ) return IRC_WriteStrClient( Client, This_Server, ERR_ERRONEUSNICKNAME_MSG, Client_Name( Client ), Nick );
247
248         /* Nick bereits vergeben? */
249         c = My_Clients;
250         while( c )
251         {
252                 if( strcasecmp( c->nick, Nick ) == 0 )
253                 {
254                         /* den Nick gibt es bereits */
255                         IRC_WriteStrClient( Client, This_Server, ERR_NICKNAMEINUSE_MSG, Client_Name( Client ), Nick );
256                         return FALSE;
257                 }
258                 c = c->next;
259         }
260
261         return TRUE;
262 } /* Client_CheckNick */
263
264
265 GLOBAL CHAR *Client_GetID( CLIENT *Client )
266 {
267         /* Client-"ID" liefern, wie sie z.B. fuer
268          * Prefixe benoetigt wird. */
269
270         assert( Client != NULL );
271         
272         if( Client->type == CLIENT_SERVER ) return Client->nick;
273
274         sprintf( GetID_Buffer, "%s!%s@%s", Client->nick, Client->user, Client->host );
275         return GetID_Buffer;
276 } /* Client_GetID */
277
278
279 GLOBAL CLIENT *Client_Search( CHAR *ID )
280 {
281         /* Client suchen, auf den ID passt */
282
283         CLIENT *c;
284
285         assert( ID != NULL );
286
287         c = My_Clients;
288         while( c )
289         {
290                 if( strcasecmp( c->nick, ID ) == 0 ) return c;
291                 c = c->next;
292         }
293         
294         return NULL;
295 } /* Client_Search */
296
297
298 LOCAL CLIENT *New_Client_Struct( VOID )
299 {
300         /* Neue CLIENT-Struktur pre-initialisieren */
301         
302         CLIENT *c;
303         INT i;
304         
305         c = malloc( sizeof( CLIENT ));
306         if( ! c )
307         {
308                 Log( LOG_EMERG, "Can't allocate memory!" );
309                 return NULL;
310         }
311
312         c->next = NULL;
313         c->type = CLIENT_UNKNOWN;
314         c->conn_id = NONE;
315         c->introducer = NULL;
316         strcpy( c->nick, "" );
317         strcpy( c->pass, "" );
318         strcpy( c->host, "" );
319         strcpy( c->user, "" );
320         strcpy( c->name, "" );
321         for( i = 0; i < MAX_CHANNELS; c->channels[i++] = NULL );
322         strcpy( c->modes, "" );
323
324         return c;
325 } /* New_Client */
326
327
328 /* -eof- */