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