]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-func.c
First check if channel has mode "N", then check membership
[ngircd-alex.git] / src / ngircd / conn-func.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2014 Alexander Barton (alex@barton.de) and Contributors.
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 <assert.h>
22 #include <time.h>
23
24 #ifdef DEBUG
25 # include "log.h"
26 #endif
27 #include "conn.h"
28
29 #include "conn-func.h"
30
31 /**
32  * Update "idle timestamp", the time of the last visible user action
33  * (e. g. like sending messages, joining or leaving channels).
34  *
35  * @param Idx Connection index.
36  */
37 GLOBAL void
38 Conn_UpdateIdle(CONN_ID Idx)
39 {
40         assert(Idx > NONE);
41         My_Connections[Idx].lastprivmsg = time(NULL);
42 }
43
44 /**
45  * Update "ping timestamp", the time of the last outgoing PING request.
46  *
47  * @param Idx Connection index.
48  */
49 GLOBAL void
50 Conn_UpdatePing(CONN_ID Idx)
51 {
52         assert(Idx > NONE);
53         My_Connections[Idx].lastping = time(NULL);
54 }
55
56 /*
57  * Get signon time of a connection.
58  */
59 GLOBAL time_t
60 Conn_GetSignon(CONN_ID Idx)
61 {
62         assert(Idx > NONE);
63         return My_Connections[Idx].signon;
64 }
65
66 GLOBAL time_t
67 Conn_GetIdle( CONN_ID Idx )
68 {
69         /* Return Idle-Timer of a connetion */
70         assert( Idx > NONE );
71         return time( NULL ) - My_Connections[Idx].lastprivmsg;
72 } /* Conn_GetIdle */
73
74 GLOBAL time_t
75 Conn_LastPing( CONN_ID Idx )
76 {
77         assert( Idx > NONE );
78         return My_Connections[Idx].lastping;
79 } /* Conn_LastPing */
80
81 /**
82  * Add "penalty time" for a connection.
83  *
84  * During the "penalty time" the socket is ignored completely, no new data
85  * is read. This function only increases the penalty, it is not possible to
86  * decrease the penalty time.
87  *
88  * @param Idex Connection index.
89  * @param Seconds Seconds to add.
90  * @see Conn_ResetPenalty
91  */
92 GLOBAL void
93 Conn_SetPenalty(CONN_ID Idx, time_t Seconds)
94 {
95         time_t t;
96
97         assert(Idx > NONE);
98         assert(Seconds >= 0);
99
100         t = time(NULL);
101         if (My_Connections[Idx].delaytime < t)
102                 My_Connections[Idx].delaytime = t;
103
104         My_Connections[Idx].delaytime += Seconds;
105
106 #ifdef DEBUG
107         Log(LOG_DEBUG,
108             "Add penalty time on connection %d: %ld second%s, total %ld second%s.",
109             Idx, (long)Seconds, Seconds != 1 ? "s" : "",
110             My_Connections[Idx].delaytime - t,
111             My_Connections[Idx].delaytime - t != 1 ? "s" : "");
112 #endif
113 } /* Conn_SetPenalty */
114
115 GLOBAL void
116 Conn_ClearFlags( void )
117 {
118         CONN_ID i;
119
120         for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
121 } /* Conn_ClearFlags */
122
123 GLOBAL int
124 Conn_Flag( CONN_ID Idx )
125 {
126         assert( Idx > NONE );
127         return My_Connections[Idx].flag;
128 } /* Conn_Flag */
129
130 GLOBAL void
131 Conn_SetFlag( CONN_ID Idx, int Flag )
132 {
133         assert( Idx > NONE );
134         My_Connections[Idx].flag = Flag;
135 } /* Conn_SetFlag */
136
137 GLOBAL CONN_ID
138 Conn_First( void )
139 {
140         CONN_ID i;
141         
142         for( i = 0; i < Pool_Size; i++ )
143         {
144                 if( My_Connections[i].sock != NONE ) return i;
145         }
146         return NONE;
147 } /* Conn_First */
148
149 GLOBAL CONN_ID
150 Conn_Next( CONN_ID Idx )
151 {
152         CONN_ID i = NONE;
153
154         assert( Idx > NONE );
155         
156         for( i = Idx + 1; i < Pool_Size; i++ )
157         {
158                 if( My_Connections[i].sock != NONE ) return i;
159         }
160         return NONE;
161 } /* Conn_Next */
162
163 GLOBAL UINT16
164 Conn_Options( CONN_ID Idx )
165 {
166         assert( Idx > NONE );
167         return My_Connections[Idx].options;
168 } /* Conn_Options */
169
170 /**
171  * Set connection option.
172  */
173 GLOBAL void
174 Conn_SetOption(CONN_ID Idx, int Option)
175 {
176         assert(Idx > NONE);
177         Conn_OPTION_ADD(&My_Connections[Idx], Option);
178 } /* Conn_SetOption */
179
180 /**
181  * Get the start time of the connection.
182  * The result is the start time in seconds since 1970-01-01, as reported
183  * by the C function time(NULL).
184  */
185 GLOBAL time_t
186 Conn_StartTime( CONN_ID Idx )
187 {
188         CLIENT *c;
189
190         assert(Idx > NONE);
191
192         /* Search client structure for this link ... */
193         c = Conn_GetClient(Idx);
194         if(c != NULL)
195                 return Client_StartTime(c);
196
197         return 0;
198 } /* Conn_StartTime */
199
200 /**
201  * return number of bytes queued for writing
202  */
203 GLOBAL size_t
204 Conn_SendQ( CONN_ID Idx )
205 {
206         assert( Idx > NONE );
207 #ifdef ZLIB
208         if( My_Connections[Idx].options & CONN_ZIP )
209                 return array_bytes(&My_Connections[Idx].zip.wbuf);
210         else
211 #endif
212         return array_bytes(&My_Connections[Idx].wbuf);
213 } /* Conn_SendQ */
214
215 /**
216  * return number of messages sent on this connection so far
217  */
218 GLOBAL long
219 Conn_SendMsg( CONN_ID Idx )
220 {
221
222         assert( Idx > NONE );
223         return My_Connections[Idx].msg_out;
224 } /* Conn_SendMsg */
225
226 /**
227  * return number of (uncompressed) bytes sent
228  * on this connection so far
229  */
230 GLOBAL long
231 Conn_SendBytes( CONN_ID Idx )
232 {
233         assert( Idx > NONE );
234         return My_Connections[Idx].bytes_out;
235 } /* Conn_SendBytes */
236
237 /**
238  * return number of bytes pending in read buffer
239  */
240 GLOBAL size_t
241 Conn_RecvQ( CONN_ID Idx )
242 {
243         assert( Idx > NONE );
244 #ifdef ZLIB
245         if( My_Connections[Idx].options & CONN_ZIP )
246                 return array_bytes(&My_Connections[Idx].zip.rbuf);
247         else
248 #endif
249         return array_bytes(&My_Connections[Idx].rbuf);
250 } /* Conn_RecvQ */
251
252 /**
253  * return number of messages received on this connection so far
254  */
255 GLOBAL long
256 Conn_RecvMsg( CONN_ID Idx )
257 {
258         assert( Idx > NONE );
259         return My_Connections[Idx].msg_in;
260 } /* Conn_RecvMsg */
261
262 /**
263  * return number of (uncompressed) bytes received on this
264  * connection so far
265  */
266 GLOBAL long
267 Conn_RecvBytes( CONN_ID Idx )
268 {
269         assert( Idx > NONE );
270         return My_Connections[Idx].bytes_in;
271 } /* Conn_RecvBytes */
272
273 /**
274  * Return the remote IP address of this connection as string.
275  */
276 GLOBAL const char *
277 Conn_IPA(CONN_ID Idx)
278 {
279         assert (Idx > NONE);
280         return ng_ipaddr_tostr(&My_Connections[Idx].addr);
281 }
282
283 GLOBAL void
284 Conn_ResetWCounter( void )
285 {
286         WCounter = 0;
287 } /* Conn_ResetWCounter */
288
289 GLOBAL long
290 Conn_WCounter( void )
291 {
292         return WCounter;
293 } /* Conn_WCounter */
294
295 /* -eof- */