]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-func.c
Rename ShowInvitesBans() to ShowChannelList(), make it more flexible
[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
12 #define CONN_MODULE
13
14 #include "portab.h"
15
16 /**
17  * @file
18  * Connection management: Global functions
19  */
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         assert( Idx > NONE );
37         My_Connections[Idx].lastprivmsg = time( NULL );
38 }
39
40
41 /*
42  * Get signon time of a connection.
43  */
44 GLOBAL time_t
45 Conn_GetSignon(CONN_ID Idx)
46 {
47         assert(Idx > NONE);
48         return My_Connections[Idx].signon;
49 }
50
51 GLOBAL time_t
52 Conn_GetIdle( CONN_ID Idx )
53 {
54         /* Return Idle-Timer of a connetion */
55         assert( Idx > NONE );
56         return time( NULL ) - My_Connections[Idx].lastprivmsg;
57 } /* Conn_GetIdle */
58
59
60 GLOBAL time_t
61 Conn_LastPing( CONN_ID Idx )
62 {
63         assert( Idx > NONE );
64         return My_Connections[Idx].lastping;
65 } /* Conn_LastPing */
66
67
68 /**
69  * Add "penalty time" for a connection.
70  *
71  * During the "penalty time" the socket is ignored completely, no new data
72  * is read. This function only increases the penalty, it is not possible to
73  * decrease the penalty time.
74  *
75  * @param Idex Connection index.
76  * @param Seconds Seconds to add.
77  * @see Conn_ResetPenalty
78  */
79 GLOBAL void
80 Conn_SetPenalty(CONN_ID Idx, time_t Seconds)
81 {
82         time_t t;
83
84         assert(Idx > NONE);
85         assert(Seconds >= 0);
86
87         t = time(NULL);
88         if (My_Connections[Idx].delaytime < t)
89                 My_Connections[Idx].delaytime = t;
90
91         My_Connections[Idx].delaytime += Seconds;
92
93 #ifdef DEBUG
94         Log(LOG_DEBUG,
95             "Add penalty time on connection %d: %ld second%s, total %ld second%s.",
96             Idx, (long)Seconds, Seconds != 1 ? "s" : "",
97             My_Connections[Idx].delaytime - t,
98             My_Connections[Idx].delaytime - t != 1 ? "s" : "");
99 #endif
100 } /* Conn_SetPenalty */
101
102
103 /**
104  * Reset the "penalty time" for one connection.
105  *
106  * @param Idx Connection index.
107  * @see Conn_SetPenalty
108  */
109 GLOBAL void
110 Conn_ResetPenalty(CONN_ID Idx)
111 {
112         assert(Idx > NONE);
113
114         My_Connections[Idx].delaytime = 0;
115 #ifdef DEBUG
116         Log(LOG_DEBUG, "Penalty time on connection %d has been reset.");
117 #endif
118 } /* Conn_ResetPenalty */
119
120
121 GLOBAL void
122 Conn_ClearFlags( void )
123 {
124         CONN_ID i;
125
126         for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
127 } /* Conn_ClearFlags */
128
129
130 GLOBAL int
131 Conn_Flag( CONN_ID Idx )
132 {
133         assert( Idx > NONE );
134         return My_Connections[Idx].flag;
135 } /* Conn_Flag */
136
137
138 GLOBAL void
139 Conn_SetFlag( CONN_ID Idx, int Flag )
140 {
141         /* Connection markieren */
142
143         assert( Idx > NONE );
144         My_Connections[Idx].flag = Flag;
145 } /* Conn_SetFlag */
146
147
148 GLOBAL CONN_ID
149 Conn_First( void )
150 {
151         /* Connection-Struktur der ersten Verbindung liefern;
152          * Ist keine Verbindung vorhanden, wird NONE geliefert. */
153
154         CONN_ID i;
155         
156         for( i = 0; i < Pool_Size; i++ )
157         {
158                 if( My_Connections[i].sock != NONE ) return i;
159         }
160         return NONE;
161 } /* Conn_First */
162
163
164 GLOBAL CONN_ID
165 Conn_Next( CONN_ID Idx )
166 {
167         /* Naechste Verbindungs-Struktur liefern; existiert keine
168          * weitere, so wird NONE geliefert. */
169
170         CONN_ID i = NONE;
171
172         assert( Idx > NONE );
173         
174         for( i = Idx + 1; i < Pool_Size; i++ )
175         {
176                 if( My_Connections[i].sock != NONE ) return i;
177         }
178         return NONE;
179 } /* Conn_Next */
180
181
182 GLOBAL UINT16
183 Conn_Options( CONN_ID Idx )
184 {
185         assert( Idx > NONE );
186         return My_Connections[Idx].options;
187 } /* Conn_Options */
188
189
190 /**
191  * Set connection option.
192  */
193 GLOBAL void
194 Conn_SetOption(CONN_ID Idx, int Option)
195 {
196         assert(Idx > NONE);
197         Conn_OPTION_ADD(&My_Connections[Idx], Option);
198 } /* Conn_SetOption */
199
200
201 /**
202  * Get the start time of the connection.
203  * The result is the start time in seconds since 1970-01-01, as reported
204  * by the C function time(NULL).
205  */
206 GLOBAL time_t
207 Conn_StartTime( CONN_ID Idx )
208 {
209         CLIENT *c;
210
211         assert(Idx > NONE);
212
213         /* Search client structure for this link ... */
214         c = Conn_GetClient(Idx);
215         if(c != NULL)
216                 return Client_StartTime(c);
217
218         return 0;
219 } /* Conn_StartTime */
220
221 /**
222  * return number of bytes queued for writing
223  */
224 GLOBAL size_t
225 Conn_SendQ( CONN_ID Idx )
226 {
227         assert( Idx > NONE );
228 #ifdef ZLIB
229         if( My_Connections[Idx].options & CONN_ZIP )
230                 return array_bytes(&My_Connections[Idx].zip.wbuf);
231         else
232 #endif
233         return array_bytes(&My_Connections[Idx].wbuf);
234 } /* Conn_SendQ */
235
236
237 /**
238  * return number of messages sent on this connection so far
239  */
240 GLOBAL long
241 Conn_SendMsg( CONN_ID Idx )
242 {
243
244         assert( Idx > NONE );
245         return My_Connections[Idx].msg_out;
246 } /* Conn_SendMsg */
247
248
249 /**
250  * return number of (uncompressed) bytes sent
251  * on this connection so far
252  */
253 GLOBAL long
254 Conn_SendBytes( CONN_ID Idx )
255 {
256         assert( Idx > NONE );
257         return My_Connections[Idx].bytes_out;
258 } /* Conn_SendBytes */
259
260
261 /**
262  * return number of bytes pending in read buffer
263  */
264 GLOBAL size_t
265 Conn_RecvQ( CONN_ID Idx )
266 {
267         assert( Idx > NONE );
268 #ifdef ZLIB
269         if( My_Connections[Idx].options & CONN_ZIP )
270                 return array_bytes(&My_Connections[Idx].zip.rbuf);
271         else
272 #endif
273         return array_bytes(&My_Connections[Idx].rbuf);
274 } /* Conn_RecvQ */
275
276
277 /**
278  * return number of messages received on this connection so far
279  */
280 GLOBAL long
281 Conn_RecvMsg( CONN_ID Idx )
282 {
283         assert( Idx > NONE );
284         return My_Connections[Idx].msg_in;
285 } /* Conn_RecvMsg */
286
287
288 /**
289  * return number of (uncompressed) bytes received on this
290  * connection so far
291  */
292 GLOBAL long
293 Conn_RecvBytes( CONN_ID Idx )
294 {
295         assert( Idx > NONE );
296         return My_Connections[Idx].bytes_in;
297 } /* Conn_RecvBytes */
298
299 /**
300  * Return the remote IP address of this connection as string.
301  */
302 GLOBAL const char *
303 Conn_IPA(CONN_ID Idx)
304 {
305         assert (Idx > NONE);
306         return ng_ipaddr_tostr(&My_Connections[Idx].addr);
307 }
308
309
310 GLOBAL void
311 Conn_ResetWCounter( void )
312 {
313         WCounter = 0;
314 } /* Conn_ResetWCounter */
315
316
317 GLOBAL long
318 Conn_WCounter( void )
319 {
320         return WCounter;
321 } /* Conn_WCounter */
322
323
324 /* -eof- */