Clean a bit - to be continued...
[seven-1.x.git] / libseven / event.c
blobf6848fc19cfb0c81cbf9f5bdb4f362601725ed8e
1 /*
2 * ircd-ratbox: A slightly useful ircd.
3 * event.c: Event functions.
5 * Copyright (C) 1998-2000 Regents of the University of California
6 * Copyright (C) 2001-2002 Hybrid Development Team
7 * Copyright (C) 2002-2005 ircd-ratbox development team
9 * Code borrowed from the squid web cache by Adrian Chadd.
10 * Original header:
12 * DEBUG: section 41 Event Processing
13 * AUTHOR: Henrik Nordstrom
15 * SQUID Internet Object Cache http://squid.nlanr.net/Squid/
16 * ----------------------------------------------------------
18 * Squid is the result of efforts by numerous individuals from the
19 * Internet community. Development is led by Duane Wessels of the
20 * National Laboratory for Applied Network Research and funded by the
21 * National Science Foundation. Squid is Copyrighted (C) 1998 by
22 * the Regents of the University of California. Please see the
23 * COPYRIGHT file for full details. Squid incorporates software
24 * developed and/or copyrighted by other sources. Please see the
25 * CREDITS file for full details.
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write to the Free Software
39 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
40 * USA
45 * How its used:
47 * Should be pretty self-explanatory. Events are added to the static
48 * array event_table with a frequency time telling eventRun how often
49 * to execute it.
52 #include "stdinc.h"
53 #include "config.h"
55 #include "ircd.h"
56 #include "event.h"
57 #include "client.h"
58 #include "send.h"
59 #include "memory.h"
60 #include "s_log.h"
61 #include "numeric.h"
63 static const char *last_event_ran = NULL;
64 struct ev_entry event_table[MAX_EVENTS];
65 static time_t event_time_min = -1;
68 * void eventAdd(const char *name, EVH *func, void *arg, time_t when)
70 * Input: Name of event, function to call, arguments to pass, and frequency
71 * of the event.
72 * Output: None
73 * Side Effects: Adds the event to the event list.
75 void
76 eventAdd(const char *name, EVH * func, void *arg, time_t when)
78 int i;
80 /* find first inactive index */
81 for (i = 0; i < MAX_EVENTS; i++)
83 if(event_table[i].active == 0)
85 event_table[i].func = func;
86 event_table[i].name = name;
87 event_table[i].arg = arg;
88 event_table[i].when = CurrentTime + when;
89 event_table[i].frequency = when;
90 event_table[i].active = 1;
92 if((event_table[i].when < event_time_min) || (event_time_min == -1))
93 event_time_min = event_table[i].when;
95 return;
99 /* erk! couldnt add to event table */
100 sendto_realops_snomask(SNO_DEBUG, L_ALL, "Unable to add event [%s] to event table", name);
104 void
105 eventAddOnce(const char *name, EVH *func, void *arg, time_t when)
107 int i;
109 /* find first inactive index */
110 for (i = 0; i < MAX_EVENTS; i++)
112 if(event_table[i].active == 0)
114 event_table[i].func = func;
115 event_table[i].name = name;
116 event_table[i].arg = arg;
117 event_table[i].when = CurrentTime + when;
118 event_table[i].frequency = 0;
119 event_table[i].active = 1;
121 if ((event_table[i].when < event_time_min) || (event_time_min == -1))
122 event_time_min = event_table[i].when;
124 return;
128 /* erk! couldnt add to event table */
129 sendto_realops_snomask(SNO_DEBUG, L_ALL,
130 "Unable to add event [%s] to event table", name);
134 * void eventDelete(EVH *func, void *arg)
136 * Input: Function handler, argument that was passed.
137 * Output: None
138 * Side Effects: Removes the event from the event list
140 void
141 eventDelete(EVH * func, void *arg)
143 int i;
145 i = eventFind(func, arg);
147 if(i == -1)
148 return;
150 event_table[i].name = NULL;
151 event_table[i].func = NULL;
152 event_table[i].arg = NULL;
153 event_table[i].active = 0;
157 * void eventAddIsh(const char *name, EVH *func, void *arg, time_t delta_isa)
159 * Input: Name of event, function to call, arguments to pass, and frequency
160 * of the event.
161 * Output: None
162 * Side Effects: Adds the event to the event list within +- 1/3 of the
163 * specified frequency.
165 void
166 eventAddIsh(const char *name, EVH * func, void *arg, time_t delta_ish)
168 if(delta_ish >= 3.0)
170 const time_t two_third = (2 * delta_ish) / 3;
171 delta_ish = two_third + ((rand() % 1000) * two_third) / 1000;
173 * XXX I hate the above magic, I don't even know if its right.
174 * Grr. -- adrian
177 eventAdd(name, func, arg, delta_ish);
181 * void eventRun(void)
183 * Input: None
184 * Output: None
185 * Side Effects: Runs pending events in the event list
187 void
188 eventRun(void)
190 int i;
192 for (i = 0; i < MAX_EVENTS; i++)
194 if(event_table[i].active && (event_table[i].when <= CurrentTime))
196 last_event_ran = event_table[i].name;
197 event_table[i].func(event_table[i].arg);
198 event_time_min = -1;
200 /* event is scheduled more than once */
201 if(event_table[i].frequency)
202 event_table[i].when = CurrentTime + event_table[i].frequency;
203 else
205 event_table[i].name = NULL;
206 event_table[i].func = NULL;
207 event_table[i].arg = NULL;
208 event_table[i].active = 0;
216 * time_t eventNextTime(void)
218 * Input: None
219 * Output: Specifies the next time eventRun() should be run
220 * Side Effects: None
222 time_t
223 eventNextTime(void)
225 int i;
227 if(event_time_min == -1)
229 for (i = 0; i < MAX_EVENTS; i++)
231 if(event_table[i].active &&
232 ((event_table[i].when < event_time_min) || (event_time_min == -1)))
233 event_time_min = event_table[i].when;
237 return event_time_min;
241 * void eventInit(void)
243 * Input: None
244 * Output: None
245 * Side Effects: Initializes the event system.
247 void
248 eventInit(void)
250 last_event_ran = NULL;
251 memset((void *) event_table, 0, sizeof(event_table));
255 * int eventFind(EVH *func, void *arg)
257 * Input: Event function and the argument passed to it
258 * Output: Index to the slow in the event_table
259 * Side Effects: None
262 eventFind(EVH * func, void *arg)
264 int i;
266 for (i = 0; i < MAX_EVENTS; i++)
268 if((event_table[i].func == func) &&
269 (event_table[i].arg == arg) && event_table[i].active)
270 return i;
273 return -1;
277 * void show_events(struct Client *source_p)
279 * Input: Client requesting the event
280 * Output: List of events
281 * Side Effects: None
283 void
284 show_events(struct Client *source_p)
286 int i;
288 if(last_event_ran)
289 sendto_one_numeric(source_p, RPL_STATSDEBUG,
290 "E :Last event to run: %s",
291 last_event_ran);
293 sendto_one_numeric(source_p, RPL_STATSDEBUG,
294 "E :Operation Next Execution");
296 for (i = 0; i < MAX_EVENTS; i++)
298 if(event_table[i].active)
300 sendto_one_numeric(source_p, RPL_STATSDEBUG,
301 "E :%-28s %-4d seconds",
302 event_table[i].name,
303 (int)(event_table[i].when - CurrentTime));
309 * void set_back_events(time_t by)
310 * Input: Time to set back events by.
311 * Output: None.
312 * Side-effects: Sets back all events by "by" seconds.
314 void
315 set_back_events(time_t by)
317 int i;
319 for (i = 0; i < MAX_EVENTS; i++)
321 if(event_table[i].when > by)
322 event_table[i].when -= by;
323 else
324 event_table[i].when = 0;
328 void
329 eventUpdate(const char *name, time_t freq)
331 int i;
333 for(i = 0; i < MAX_EVENTS; i++)
335 if(event_table[i].active &&
336 !irccmp(event_table[i].name, name))
338 event_table[i].frequency = freq;
340 /* update when its scheduled to run if its higher
341 * than the new frequency
343 if((CurrentTime + freq) < event_table[i].when)
344 event_table[i].when = CurrentTime + freq;
346 return;