]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/client.c
- Aenderungen an den Debug- und Logging-Funktionen.
[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.5 2001/12/25 22:04:26 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.5  2001/12/25 22:04:26  alex
25  * - Aenderungen an den Debug- und Logging-Funktionen.
26  *
27  * Revision 1.4  2001/12/25 19:21:26  alex
28  * - Client-Typ ("Status") besser unterteilt, My_Clients ist zudem nun global.
29  *
30  * Revision 1.3  2001/12/24 01:31:14  alex
31  * - einige assert()'s eingestraeut.
32  *
33  * Revision 1.2  2001/12/23 22:04:37  alex
34  * - einige neue Funktionen,
35  * - CLIENT-Struktur erweitert.
36  *
37  * Revision 1.1  2001/12/14 08:13:43  alex
38  * - neues Modul begonnen :-)
39  *
40  */
41
42
43 #include <portab.h>
44 #include "global.h"
45
46 #include <imp.h>
47 #include <assert.h>
48 #include <unistd.h>
49 #include <string.h>
50
51 #include "channel.h"
52 #include "conn.h"
53 #include "log.h"
54
55 #include <exp.h>
56 #include "client.h"
57
58
59 LOCAL CLIENT *New_Client_Struct( VOID );
60
61
62 GLOBAL VOID Client_Init( VOID )
63 {
64         This_Server = New_Client_Struct( );
65         if( ! This_Server )
66         {
67                 Log( LOG_EMERG, "Can't allocate client structure for server! Going down." );
68                 exit( 1 );
69         }
70
71         /* Client-Struktur dieses Servers */
72         This_Server->next = NULL;
73         This_Server->type = CLIENT_SERVER;
74         This_Server->conn_id = NONE;
75         This_Server->introducer = This_Server;
76         gethostname( This_Server->host, CLIENT_HOST_LEN );
77         strcpy( This_Server->nick, This_Server->host );
78
79         My_Clients = This_Server;
80 } /* Client_Init */
81
82
83 GLOBAL VOID Client_Exit( VOID )
84 {
85         CLIENT *c, *next;
86         INT cnt;
87
88         Client_Destroy( This_Server );
89         
90         cnt = 0;
91         c = My_Clients;
92         while( c )
93         {
94                 cnt++;
95                 next = c->next;
96                 free( c );
97                 c = next;
98         }
99         if( cnt ) Log( LOG_INFO, "Freed %d client structure%s.", cnt, cnt == 1 ? "" : "s" );
100 } /* Client Exit */
101
102
103 GLOBAL CLIENT *Client_New_Local( CONN_ID Idx, CHAR *Hostname )
104 {
105         /* Neuen lokalen Client erzeugen. */
106         
107         CLIENT *client;
108
109         assert( Idx >= 0 );
110         assert( Hostname != NULL );
111         
112         client = New_Client_Struct( );
113         if( ! client ) return NULL;
114
115         /* Initgialisieren */
116         client->conn_id = Idx;
117         client->introducer = This_Server;
118         strncpy( client->host, Hostname, CLIENT_HOST_LEN );
119         client->host[CLIENT_HOST_LEN] = '\0';
120
121         /* Verketten */
122         client->next = My_Clients;
123         My_Clients = client;
124         
125         return client;
126 } /* Client_New_Local */
127
128
129 GLOBAL VOID Client_Destroy( CLIENT *Client )
130 {
131         /* Client entfernen. */
132         
133         CLIENT *last, *c;
134
135         assert( Client != NULL );
136         
137         last = NULL;
138         c = My_Clients;
139         while( c )
140         {
141                 if( c == Client )
142                 {
143                         if( last ) last->next = c->next;
144                         else My_Clients = c->next;
145                         free( c );
146                         break;
147                 }
148                 last = c;
149                 c = c->next;
150         }
151 } /* Client_Destroy */
152
153
154 GLOBAL CLIENT *Client_GetFromConn( CONN_ID Idx )
155 {
156         /* Client-Struktur, die zur lokalen Verbindung Idx gehoert
157          * liefern. Wird keine gefunden, so wird NULL geliefert. */
158
159         CLIENT *c;
160
161         assert( Idx >= 0 );
162         
163         c = My_Clients;
164         while( c )
165         {
166                 if( c->conn_id == Idx ) return c;
167                 c = c->next;
168         }
169         return NULL;
170 } /* Client_GetFromConn */
171
172
173 LOCAL CLIENT *New_Client_Struct( VOID )
174 {
175         /* Neue CLIENT-Struktur pre-initialisieren */
176         
177         CLIENT *c;
178         INT i;
179         
180         c = malloc( sizeof( CLIENT ));
181         if( ! c )
182         {
183                 Log( LOG_EMERG, "Can't allocate memory!" );
184                 return NULL;
185         }
186
187         c->next = NULL;
188         c->type = CLIENT_UNKNOWN;
189         c->conn_id = NONE;
190         c->introducer = NULL;
191         strcpy( c->nick, "" );
192         strcpy( c->pass, "" );
193         strcpy( c->host, "" );
194         strcpy( c->user, "" );
195         strcpy( c->name, "" );
196         for( i = 0; i < MAX_CHANNELS; c->channels[i++] = NULL );
197
198         return c;
199 } /* New_Client */
200
201
202 /* -eof- */