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