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.
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
47 * Should be pretty self-explanatory. Events are added to the static
48 * array event_table with a frequency time telling eventRun how often
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
73 * Side Effects: Adds the event to the event list.
76 eventAdd(const char *name
, EVH
* func
, void *arg
, time_t when
)
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
;
99 /* erk! couldnt add to event table */
100 sendto_realops_snomask(SNO_DEBUG
, L_ALL
, "Unable to add event [%s] to event table", name
);
105 eventAddOnce(const char *name
, EVH
*func
, void *arg
, time_t when
)
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
;
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.
138 * Side Effects: Removes the event from the event list
141 eventDelete(EVH
* func
, void *arg
)
145 i
= eventFind(func
, arg
);
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
162 * Side Effects: Adds the event to the event list within +- 1/3 of the
163 * specified frequency.
166 eventAddIsh(const char *name
, EVH
* func
, void *arg
, time_t delta_ish
)
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.
177 eventAdd(name
, func
, arg
, delta_ish
);
181 * void eventRun(void)
185 * Side Effects: Runs pending events in the event list
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
);
200 /* event is scheduled more than once */
201 if(event_table
[i
].frequency
)
202 event_table
[i
].when
= CurrentTime
+ event_table
[i
].frequency
;
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)
219 * Output: Specifies the next time eventRun() should be run
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)
245 * Side Effects: Initializes the event system.
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
262 eventFind(EVH
* func
, void *arg
)
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
)
277 * void show_events(struct Client *source_p)
279 * Input: Client requesting the event
280 * Output: List of events
284 show_events(struct Client
*source_p
)
289 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,
290 "E :Last event to run: %s",
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",
303 (int)(event_table
[i
].when
- CurrentTime
));
309 * void set_back_events(time_t by)
310 * Input: Time to set back events by.
312 * Side-effects: Sets back all events by "by" seconds.
315 set_back_events(time_t by
)
319 for (i
= 0; i
< MAX_EVENTS
; i
++)
321 if(event_table
[i
].when
> by
)
322 event_table
[i
].when
-= by
;
324 event_table
[i
].when
= 0;
329 eventUpdate(const char *name
, time_t freq
)
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
;