From aa7a1485d02c0bed4aebfa24d9db5d1817f9978a Mon Sep 17 00:00:00 2001 From: stskeeps Date: Mon, 14 May 2001 16:52:17 +0000 Subject: [PATCH] events code --- Changes | 1 + include/events.h | 3 +- include/h.h | 1 + include/proto.h | 8 +++- include/struct.h | 2 +- src/events.c | 118 +++++++++++++++++++++++++++++++++++++++++++---- src/ircd.c | 114 ++++++++++++++++++++++----------------------- src/ircsprintf.c | 4 +- src/s_kline.c | 12 ++--- src/s_serv.c | 3 +- src/s_user.c | 2 +- 11 files changed, 187 insertions(+), 81 deletions(-) diff --git a/Changes b/Changes index 68b992e2e..5091ae319 100644 --- a/Changes +++ b/Changes @@ -495,3 +495,4 @@ seen. gmtime warning still there - Made the topic for a +s channel hidden from non-members (reported by Bagge) - Removed ce_vardatanum (wasted CPU and memory) - Added code to make the config parser try and stay alive when an error is encountered +- Added events code and converted some stuff to events diff --git a/include/events.h b/include/events.h index 01794c626..5516f9f66 100644 --- a/include/events.h +++ b/include/events.h @@ -27,10 +27,11 @@ void EventAdd(char *name, long every, long howmany, typedef struct _event Event; struct _event { + Event *next, *prev; char *name; time_t every; long howmany; - vFP event; + vFP event; void *data; time_t last; }; diff --git a/include/h.h b/include/h.h index 03395bb42..254fab637 100644 --- a/include/h.h +++ b/include/h.h @@ -77,6 +77,7 @@ extern ConfigItem_allow_channel *conf_allow_channel; extern ConfigItem_deny_version *conf_deny_version; extern ConfigItem_log *conf_log; +EVENT(tkl_check_expire); ConfigItem_class *Find_class(char *name); ConfigItem_deny_dcc *Find_deny_dcc(char *name); diff --git a/include/proto.h b/include/proto.h index 164c954b6..88dbe065a 100644 --- a/include/proto.h +++ b/include/proto.h @@ -43,17 +43,21 @@ void sendto_serv_butone_token(aClient *one, char *prefix, char *command, char *t void sendto_serv_butone_token_opt(aClient *one, int opt, char *prefix, char *command, char *token, char *pattern, ...); void sendto_channel_ntadmins(aClient *from, aChannel *chptr, char *pattern, ...); +/* ircd.c */ +EVENT(e_check_fdlists); +EVENT(garbage_collect); +EVENT(loop_event); /* support.c */ char *my_itoa(int i); /* s_kline.c */ int find_tkline_match(aClient *cptr, int xx); -void tkl_check_expire(void); +extern EVENT(tkl_check_expire); int tkl_sweep(void); /* s_serv.c */ void load_tunefile(void); -void save_tunefile(void); +extern EVENT(save_tunefile); aMotd *read_rules(char *filename); aMotd *read_motd(char *filename); diff --git a/include/struct.h b/include/struct.h index 03b3e6a3c..06fa3941b 100644 --- a/include/struct.h +++ b/include/struct.h @@ -1346,7 +1346,7 @@ struct DSlink { #define MyClient(x) (MyConnect(x) && IsClient(x)) #define MyOper(x) (MyConnect(x) && IsOper(x)) -#define TStime() (timeofday) +#define TStime() (timeofday == 0 ? (timeofday = time(NULL) + TSoffset) : timeofday) /* Lifted somewhat from Undernet code --Rak */ diff --git a/src/events.c b/src/events.c index 7fdb5c16f..00ebb08b4 100644 --- a/src/events.c +++ b/src/events.c @@ -28,6 +28,7 @@ #include "channel.h" #include "userload.h" #include "version.h" +#include "proto.h" #include #include #include @@ -39,27 +40,128 @@ #include #include "h.h" -ID_Copyright("(C) Carsten Munk 1999"); +ID_Copyright("(C) Carsten Munk 2001"); + + Event *events = NULL; -void DoEvents(void) +void EventAdd(char *name, long every, long howmany, + vFP event, void *data) +{ + Event *newevent; + + if (!name || (every < 0) || (howmany < 0) || !event) + { + return; + } + newevent = (Event *) MyMallocEx(sizeof(Event)); + newevent->name = strdup(name); + newevent->howmany = howmany; + newevent->every = every; + newevent->event = event; + newevent->data = data; + newevent->next = events; + newevent->prev = NULL; + /* We don't want a quick execution */ + newevent->last = TStime(); + if (events) + events->prev = newevent; + events = newevent; +} + +Event *EventDel(char *name) +{ + Event *p, *q; + + for (p = events; p; p = p->next) + { + if (!strcmp(p->name, name)) + { + q = p->next; + MyFree(p->name); + if (p->prev) + p->prev->next = p->next; + else + events = p->next; + + if (p->next) + p->next->prev = p->prev; + MyFree(p); + return q; + } + } + return NULL; +} + +Event *EventFind(char *name) +{ + Event *eventptr; + + for (eventptr = events; eventptr; eventptr = eventptr->next) + if (!strcmp(eventptr->name, name)) + return (eventptr); + return NULL; +} + +void EventModEvery(char *name, int every) { Event *eventptr; - for (eventptr = events; events; events = events->next) - if ((TStime() - eventptr->last) >= eventptr->every) + eventptr = EventFind(name); + if (eventptr) + eventptr->every = every; +} + +inline void DoEvents(void) +{ + Event *eventptr; + Event temp; + + for (eventptr = events; eventptr; eventptr = eventptr->next) + if ((eventptr->every == 0) || ((TStime() - eventptr->last) >= eventptr->every)) { if (eventptr->howmany > 0) { + eventptr->howmany--; if (eventptr->howmany == 0) { - /* Mark event for deletion, or delete it - */ + temp.next = EventDel(eventptr->name); + eventptr = &temp; + continue; } } eventptr->last = TStime(); - (*evenptr->event)(eventptr->data); + (*eventptr->event)(eventptr->data); } -} \ No newline at end of file +} + +void EventStatus(aClient *sptr) +{ + Event *eventptr; + + if (!events) + { + sendto_one(sptr, ":%s NOTICE %s :*** No events", + me.name, sptr->name); + } + for (eventptr = events; eventptr; eventptr = eventptr->next) + { + sendto_one(sptr, ":%s NOTICE %s :*** Event %s: e/%i h/%i n/%i l/%i", me.name, + sptr->name, eventptr->name, eventptr->every, eventptr->howmany, + TStime() - eventptr->last, (eventptr->last + eventptr->every) - TStime()); + } +} + +void SetupEvents(void) +{ + /* Start events */ + EventAdd("tklexpire", 5, 0, tkl_check_expire, NULL); + EventAdd("tunefile", 300, 0, save_tunefile, NULL); + EventAdd("garbage", GARBAGE_COLLECT_EVERY, 0, garbage_collect, NULL); + EventAdd("loop", 0, 0, loop_event, NULL); +#ifndef NO_FDLISTS + EventAdd("fdlistcheck", 1, 0, e_check_fdlists, NULL); +#endif +} diff --git a/src/ircd.c b/src/ircd.c index b31bb27d1..bf56cd720 100644 --- a/src/ircd.c +++ b/src/ircd.c @@ -404,6 +404,51 @@ void server_reboot(mesg) char *areason; +EVENT(loop_event) +{ + if (loop.do_tkl_sweep) + { + tkl_sweep(); + loop.do_tkl_sweep = 0; + } + if (loop.do_garbage_collect == 1) + { + garbage_collect(NULL); + } +} + +EVENT(garbage_collect) +{ + extern int freelinks; + extern Link *freelink; + Link p; + int ii; + + if (loop.do_garbage_collect == 1) + sendto_realops("Doing garbage collection .."); + if (freelinks > HOW_MANY_FREELINKS_ALLOWED) + { + ii = freelinks; + while (freelink + && (freelinks > HOW_MANY_FREELINKS_ALLOWED)) + { + freelinks--; + p.next = freelink; + freelink = freelink->next; + MyFree(p.next); + } + if (loop.do_garbage_collect == 1) + { + loop.do_garbage_collect = 0; + sendto_realops + ("Cleaned up %i garbage blocks", + (ii - freelinks)); + } + } + if (loop.do_garbage_collect == 1) + loop.do_garbage_collect = 0; +} + /* ** try_connections ** @@ -1147,12 +1192,15 @@ int InitwIRCD(argc, argv) #ifdef USE_SYSLOG syslog(LOG_NOTICE, "Server Ready"); #endif + SetupEvents(); loop.ircd_booted = 1; module_loadall(); #ifndef NO_FDLIST check_fdlists(TStime()); #endif nextkillcheck = TStime() + (TS)1; + + #ifdef _WIN32 return 1; } @@ -1166,64 +1214,14 @@ void SocketLoop(void *dummy) #ifndef NO_FDLIST TS nextfdlistcheck = 0; /*end of priority code */ #endif + while (1) #else for (;;) #endif { timeofday = time(NULL) + TSoffset; - if ((timeofday - lastglinecheck) > 4) - { - tkl_check_expire(); - lastglinecheck = timeofday; -#ifdef STATSWRITING - save_stats(); -#endif - } - if (loop.do_tkl_sweep) - { - tkl_sweep(); - loop.do_tkl_sweep = 0; - } - /* we want accuarte time here not the fucked up TStime() :P -Stskeeps */ - if ((timeofday - last_tune) > 300) - { - last_tune = timeofday; - save_tunefile(); - } - - if (((timeofday - last_garbage_collect) > - GARBAGE_COLLECT_EVERY || (loop.do_garbage_collect == 1))) - { - extern int freelinks; - extern Link *freelink; - Link p; - int ii; - if (loop.do_garbage_collect == 1) - sendto_realops("Doing garbage collection .."); - if (freelinks > HOW_MANY_FREELINKS_ALLOWED) - { - ii = freelinks; - while (freelink - && (freelinks > HOW_MANY_FREELINKS_ALLOWED)) - { - freelinks--; - p.next = freelink; - freelink = freelink->next; - MyFree(p.next); - } - if (loop.do_garbage_collect == 1) - { - loop.do_garbage_collect = 0; - sendto_realops - ("Cleaned up %i garbage blocks", - (ii - freelinks)); - } - } - if (loop.do_garbage_collect == 1) - loop.do_garbage_collect = 0; - last_garbage_collect = timeofday; - } + DoEvents(); /* ** Run through the hashes and check lusers every ** second @@ -1401,16 +1399,16 @@ void SocketLoop(void *dummy) ** have data in them (or at least try to flush) ** -avalon */ -#ifndef NO_FDLIST - /* check which clients are active */ - if (timeofday > nextfdlistcheck) - nextfdlistcheck = check_fdlists(timeofday); -#endif flush_connections(me.fd); } } #ifndef NO_FDLIST -TS check_fdlists(now) +EVENT(e_check_fdlists) +{ + check_fdlists(TStime()); +} + +inline TS check_fdlists(now) TS now; { aClient *cptr; diff --git a/src/ircsprintf.c b/src/ircsprintf.c index af4be0ab1..37974fa66 100644 --- a/src/ircsprintf.c +++ b/src/ircsprintf.c @@ -237,7 +237,7 @@ const char atoi_tab[4000] = { /* *INDENT-ON* */ static char scratch_buffer[32]; -#ifndef DEBUGMODE + /* * sprintf_irc * @@ -472,5 +472,3 @@ char *ircsprintf(char *str, const char *format, ...) va_end(vl); return ret; } - -#endif diff --git a/src/s_kline.c b/src/s_kline.c index d114afdc4..c0e8e26ec 100644 --- a/src/s_kline.c +++ b/src/s_kline.c @@ -225,7 +225,7 @@ aTKline *tkl_expire(aTKline * tmp) return (tkl_del_line(tmp)); } -void tkl_check_expire(void) +EVENT(tkl_check_expire) { aTKline *gp, t; TS nowtime; @@ -381,7 +381,7 @@ int tkl_sweep() aClient *acptr; long i; - tkl_check_expire(); + tkl_check_expire(NULL); for (i = 0; i <= (MAXCONNECTIONS - 1); i++) { if (acptr = local[i]) @@ -402,7 +402,7 @@ void tkl_stats(aClient *cptr) Character: G, Z, K, z */ - tkl_check_expire(); + tkl_check_expire(NULL); curtime = TStime(); for (tk = tklines; tk; tk = tk->next) { @@ -502,7 +502,7 @@ int m_tkl(aClient *cptr, aClient *sptr, int parc, char *parv[]) if (parc < 2) return 0; - tkl_check_expire(); + tkl_check_expire(NULL); switch (*parv[1]) { @@ -802,7 +802,7 @@ int m_gline(aClient *cptr, aClient *sptr, int parc, char *parv[]) nochecks: usermask = strtok(mask, "@"); hostmask = strtok(NULL, ""); - tkl_check_expire(); + tkl_check_expire(NULL); for (tk = tklines; tk; tk = tk->next) { @@ -1008,7 +1008,7 @@ int m_shun(aClient *cptr, aClient *sptr, int parc, char *parv[]) nochecks: usermask = strtok(mask, "@"); hostmask = strtok(NULL, ""); - tkl_check_expire(); + tkl_check_expire(NULL); for (tk = tklines; tk; tk = tk->next) { diff --git a/src/s_serv.c b/src/s_serv.c index 2ede0428b..537332a28 100644 --- a/src/s_serv.c +++ b/src/s_serv.c @@ -2818,6 +2818,7 @@ int m_stats(cptr, sptr, parc, parv) break; } case 'V': + EventStatus(sptr); break; case 'X': case 'x': @@ -3146,7 +3147,7 @@ int m_lusers(cptr, sptr, parc, parv) } -void save_tunefile(void) +EVENT(save_tunefile) { FILE *tunefile; diff --git a/src/s_user.c b/src/s_user.c index 958b5f384..1d3aa03e4 100644 --- a/src/s_user.c +++ b/src/s_user.c @@ -940,7 +940,7 @@ static int register_user(cptr, sptr, nick, username, umode, virthost) return exit_client(cptr, sptr, &me, "Your GECOS (real name) is banned from this server"); } - tkl_check_expire(); + tkl_check_expire(NULL); if ((xx = find_tkline_match(sptr, 0)) != -1) { ircstp->is_ref++;