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