]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-func.c
Allow pre-defined server local channels ("&").
[ngircd-alex.git] / src / ngircd / conn-func.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  *
11  * Connection management: Global functions
12  */
13
14
15 #define CONN_MODULE
16
17 #include "portab.h"
18
19 static char UNUSED id[] = "$Id: conn-func.c,v 1.12 2008/03/11 14:05:27 alex Exp $";
20
21 #include "imp.h"
22 #include <assert.h>
23 #include <string.h>
24 #include "log.h"
25
26 #include "conn.h"
27 #include "client.h"
28
29 #include "exp.h"
30 #include "conn-func.h"
31
32
33 GLOBAL void
34 Conn_UpdateIdle( CONN_ID Idx )
35 {
36         /* Idle-Timer zuruecksetzen */
37
38         assert( Idx > NONE );
39         My_Connections[Idx].lastprivmsg = time( NULL );
40 }
41
42
43 /*
44  * Get signon time of a connection.
45  */
46 GLOBAL time_t
47 Conn_GetSignon(CONN_ID Idx)
48 {
49         assert(Idx > NONE);
50         return My_Connections[Idx].signon;
51 }
52
53 GLOBAL time_t
54 Conn_GetIdle( CONN_ID Idx )
55 {
56         /* Idle-Time einer Verbindung liefern (in Sekunden) */
57
58         assert( Idx > NONE );
59         return time( NULL ) - My_Connections[Idx].lastprivmsg;
60 } /* Conn_GetIdle */
61
62
63 GLOBAL time_t
64 Conn_LastPing( CONN_ID Idx )
65 {
66         /* Zeitpunkt des letzten PING liefern */
67
68         assert( Idx > NONE );
69         return My_Connections[Idx].lastping;
70 } /* Conn_LastPing */
71
72
73 GLOBAL void
74 Conn_SetPenalty( CONN_ID Idx, time_t Seconds )
75 {
76         /* Penalty-Delay fuer eine Verbindung (in Sekunden) setzen;
77          * waehrend dieser Zeit wird der entsprechende Socket vom Server
78          * bei Lese-Operationen komplett ignoriert. Der Delay kann mit
79          * dieser Funktion nur erhoeht, nicht aber verringert werden. */
80         
81         time_t t;
82         
83         assert( Idx > NONE );
84         assert( Seconds >= 0 );
85
86         t = time( NULL ) + Seconds;
87         if (t > My_Connections[Idx].delaytime)
88                 My_Connections[Idx].delaytime = t;
89
90 #ifdef DEBUG
91         Log(LOG_DEBUG, "Add penalty time on connection %d: %ld second(s).",
92                         Idx, (long)Seconds);
93 #endif
94 } /* Conn_SetPenalty */
95
96
97 GLOBAL void
98 Conn_ResetPenalty( CONN_ID Idx )
99 {
100         assert( Idx > NONE );
101         My_Connections[Idx].delaytime = 0;
102 } /* Conn_ResetPenalty */
103
104
105 GLOBAL void
106 Conn_ClearFlags( void )
107 {
108         /* Alle Connection auf "nicht-markiert" setzen */
109
110         CONN_ID i;
111
112         for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
113 } /* Conn_ClearFlags */
114
115
116 GLOBAL int
117 Conn_Flag( CONN_ID Idx )
118 {
119         /* Ist eine Connection markiert (true) oder nicht? */
120
121         assert( Idx > NONE );
122         return My_Connections[Idx].flag;
123 } /* Conn_Flag */
124
125
126 GLOBAL void
127 Conn_SetFlag( CONN_ID Idx, int Flag )
128 {
129         /* Connection markieren */
130
131         assert( Idx > NONE );
132         My_Connections[Idx].flag = Flag;
133 } /* Conn_SetFlag */
134
135
136 GLOBAL CONN_ID
137 Conn_First( void )
138 {
139         /* Connection-Struktur der ersten Verbindung liefern;
140          * Ist keine Verbindung vorhanden, wird NONE geliefert. */
141
142         CONN_ID i;
143         
144         for( i = 0; i < Pool_Size; i++ )
145         {
146                 if( My_Connections[i].sock != NONE ) return i;
147         }
148         return NONE;
149 } /* Conn_First */
150
151
152 GLOBAL CONN_ID
153 Conn_Next( CONN_ID Idx )
154 {
155         /* Naechste Verbindungs-Struktur liefern; existiert keine
156          * weitere, so wird NONE geliefert. */
157
158         CONN_ID i = NONE;
159
160         assert( Idx > NONE );
161         
162         for( i = Idx + 1; i < Pool_Size; i++ )
163         {
164                 if( My_Connections[i].sock != NONE ) return i;
165         }
166         return NONE;
167 } /* Conn_Next */
168
169
170 GLOBAL UINT16
171 Conn_Options( CONN_ID Idx )
172 {
173         assert( Idx > NONE );
174         return My_Connections[Idx].options;
175 } /* Conn_Options */
176
177
178 /**
179  * Set connection option.
180  */
181 GLOBAL void
182 Conn_SetOption(CONN_ID Idx, int Option)
183 {
184         assert(Idx > NONE);
185         Conn_OPTION_ADD(&My_Connections[Idx], Option);
186 } /* Conn_SetOption */
187
188
189 /**
190  * Get the start time of the connection.
191  * The result is the start time in seconds since 1970-01-01, as reported
192  * by the C function time(NULL).
193  */
194 GLOBAL time_t
195 Conn_StartTime( CONN_ID Idx )
196 {
197         CLIENT *c;
198
199         assert(Idx > NONE);
200
201         /* Search client structure for this link ... */
202         c = Conn_GetClient(Idx);
203         if(c != NULL)
204                 return Client_StartTime(c);
205
206         return 0;
207 } /* Conn_StartTime */
208
209
210 GLOBAL size_t
211 Conn_SendQ( CONN_ID Idx )
212 {
213         /* Laenge der Daten im Schreibbuffer liefern */
214
215         assert( Idx > NONE );
216 #ifdef ZLIB
217         if( My_Connections[Idx].options & CONN_ZIP )
218                 return array_bytes(&My_Connections[Idx].zip.wbuf);
219         else
220 #endif
221         return array_bytes(&My_Connections[Idx].wbuf);
222 } /* Conn_SendQ */
223
224
225 GLOBAL long
226 Conn_SendMsg( CONN_ID Idx )
227 {
228         /* Anzahl gesendeter Nachrichten liefern */
229
230         assert( Idx > NONE );
231         return My_Connections[Idx].msg_out;
232 } /* Conn_SendMsg */
233
234
235 GLOBAL long
236 Conn_SendBytes( CONN_ID Idx )
237 {
238         /* Anzahl gesendeter Bytes (unkomprimiert) liefern */
239
240         assert( Idx > NONE );
241         return My_Connections[Idx].bytes_out;
242 } /* Conn_SendBytes */
243
244
245 GLOBAL size_t
246 Conn_RecvQ( CONN_ID Idx )
247 {
248         /* Laenge der Daten im Lesebuffer liefern */
249
250         assert( Idx > NONE );
251 #ifdef ZLIB
252         if( My_Connections[Idx].options & CONN_ZIP )
253                 return array_bytes(&My_Connections[Idx].zip.rbuf);
254         else
255 #endif
256         return array_bytes(&My_Connections[Idx].rbuf);
257 } /* Conn_RecvQ */
258
259
260 GLOBAL long
261 Conn_RecvMsg( CONN_ID Idx )
262 {
263         /* Anzahl empfangener Nachrichten liefern */
264
265         assert( Idx > NONE );
266         return My_Connections[Idx].msg_in;
267 } /* Conn_RecvMsg */
268
269
270 GLOBAL long
271 Conn_RecvBytes( CONN_ID Idx )
272 {
273         /* Anzahl empfangener Bytes (unkomprimiert) liefern */
274
275         assert( Idx > NONE );
276         return My_Connections[Idx].bytes_in;
277 } /* Conn_RecvBytes */
278
279
280 GLOBAL void
281 Conn_ResetWCounter( void )
282 {
283         WCounter = 0;
284 } /* Conn_ResetWCounter */
285
286
287 GLOBAL long
288 Conn_WCounter( void )
289 {
290         return WCounter;
291 } /* Conn_WCounter */
292
293
294 /* -eof- */