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