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