rewriting event system

This commit is contained in:
Aaron Blakely 2024-02-13 04:56:23 -06:00
parent 74fdcb139d
commit 706b3f28eb
5 changed files with 131 additions and 96 deletions

View File

@ -13,27 +13,23 @@
#define IRC_MOTD "372"
#define IRC_END_MOTD "376"
struct ev_handler
{
int id;
void *handler;
};
struct handler
{
char *type;
int count;
void **handlers;
struct ev_handler *evhands;
};
struct event
{
char *type;
char *user;
char *chan;
char *text;
};
void init_events();
MY_API int add_handler(char *type, void *handler);
void del_handler(int num, char *type);
void handle_connected(struct irc_conn *bot, char *text);
void handle_chan_privmsg(struct irc_conn *bot, char *user, char *chan, char *text);
void handle_self_privmsg(struct irc_conn *bot, char *user, char *text);
void handle_join(struct irc_conn *bot, char *user, char *chan);
void fire_handler(struct irc_conn *bot, char *type, ...);
#endif

View File

@ -2,14 +2,14 @@
#define MODULE_H
#include "irc.h"
#include "events.h"
struct module {
char *name;
//event_handler handlers[50];
char file[256];
struct ev_handler *handlers;
};
typedef struct module module;
MY_API void load_module(struct irc_conn *bot, char *where, char *stype, char *file);

View File

@ -10,9 +10,9 @@ struct irc_conn read_config(struct irc_conn bot, char *file)
int count, n;
config_t cfg, *cf;
const config_setting_t *autoload;
const char *base = (const char*)malloc(sizeof(char *) * 1024);
const char *base = (const char*)malloc(sizeof(char) * 1024);
const char *mod = NULL;
char *modpath = (char *)malloc(sizeof(char *) * 500);
char *modpath = (char *)malloc(sizeof(char) * 500);
cf = &cfg;

View File

@ -5,8 +5,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define HANDLERARRY_LEN 500
#include <stdarg.h>
#ifdef _WIN32
#include <ctype.h>
@ -16,98 +15,133 @@
#define SPF sprintf
#endif
struct handler privmsg_self;
struct handler privmsg_chan;
struct handler chan_join;
struct handler irc_connected;
struct handler *privmsg_self;
struct handler *privmsg_chan;
struct handler *chan_join;
struct handler *irc_connected;
int handlers_count = 0;
struct handler *handlers[512];
// TODO:
// redo this module, unified api
void init_event_type(char *type)
{
handlers[handlers_count] = calloc(1, sizeof(struct handler));
handlers[handlers_count]->type = type;
handlers[handlers_count]->evhands = calloc(128, sizeof(struct ev_handler));
handlers_count++;
}
void init_events()
{
privmsg_self.type = PRIVMSG_SELF;
privmsg_chan.type = PRIVMSG_CHAN;
chan_join.type = JOIN;
irc_connected.type = IRC_CONNECTED;
privmsg_self.count = 0;
privmsg_chan.count = 0;
chan_join.count = 0;
irc_connected.count = 0;
privmsg_self.handlers = malloc(sizeof(void *) * HANDLERARRY_LEN);
privmsg_chan.handlers = malloc(sizeof(void *) * HANDLERARRY_LEN);
chan_join.handlers = malloc(sizeof(void *) * HANDLERARRY_LEN);
irc_connected.handlers = malloc(sizeof(void *) * HANDLERARRY_LEN);
init_event_type(PRIVMSG_SELF);
init_event_type(PRIVMSG_CHAN);
init_event_type(JOIN);
init_event_type(IRC_CONNECTED);
}
MY_API int add_handler(char *type, void *handler)
{
int handler_count;
printf("Installing handler @ %p [type: %s]\n", handler, type);
if (!strcmp(PRIVMSG_SELF, type))
for (int i = 0; i < handlers_count; i++)
{
privmsg_self.handlers[privmsg_self.count] = handler;
privmsg_self.count++;
return privmsg_self.count - 1;
}
else if (!strcmp(PRIVMSG_CHAN, type))
if (!strcmp(handlers[i]->type, type))
{
privmsg_chan.handlers[privmsg_chan.count] = handler;
privmsg_chan.count++;
return privmsg_chan.count - 1;
}
else if (!strcmp(JOIN, type))
if (handlers[i]->count < 128)
{
chan_join.handlers[chan_join.count] = handler;
chan_join.count++;
handlers[i]->evhands[handlers[i]->count].id = handlers[i]->count;
handlers[i]->evhands[handlers[i]->count].handler = handler;
return chan_join.count - 1;
handlers[i]->count++;
return 0;
}
else if (!strcmp(IRC_CONNECTED, type))
else
{
irc_connected.handlers[irc_connected.count] = handler;
irc_connected.count++;
return irc_connected.count - 1;
}
printf("Handler array is full, cannot add more handlers.\n");
return -1;
}
}
}
}
void del_handler(int num, char *type)
{
if (type == PRIVMSG_SELF)
privmsg_self.handlers[num] = NULL;
else if (type == PRIVMSG_CHAN)
privmsg_chan.handlers[num] = NULL;
}
void handle_connected(struct irc_conn *bot, char *text)
void fire_handler(struct irc_conn *bot, char *type, ...)
{
int i;
void (*handler)();
va_list args;
va_start(args, type);
for (i = 0; i < irc_connected.count; i++)
for (int i = 0; i < handlers_count; i++)
{
if ((handler = irc_connected.handlers[i]) != NULL)
(*handler)(bot, text);
if (!strcmp(handlers[i]->type, type))
{
for (int j = 0; j < handlers[i]->count; j++)
{
void (*handler)() = handlers[i]->evhands[j].handler;
if (!strcmp(type, PRIVMSG_SELF))
{
char *usr = va_arg(args, char*);
char *text = va_arg(args, char*);
handler(bot, usr, text);
va_end(args);
return;
}
else if (!strcmp(type, PRIVMSG_CHAN))
{
char *usr = va_arg(args, char*);
char *chan = va_arg(args, char*);
char *text = va_arg(args, char*);
handler(bot, usr, chan, text);
va_end(args);
return;
}
else if (!strcmp(type, JOIN))
{
char *chan = va_arg(args, char*);
char *usr = va_arg(args, char*);
handler(bot, chan, usr);
va_end(args);
return;
}
else if (!strcmp(type, IRC_CONNECTED))
{
char *text = va_arg(args, char*);
handler(bot, text);
va_end(args);
return;
}
}
}
}
va_end(args);
}
/*
void handle_chan_privmsg(struct irc_conn *bot, char *user, char *chan, char *text)
{
int i;
void (*handler)();
for (i = 0; i < privmsg_chan.count; i++)
for (i = 0; i < privmsg_chan->count; i++)
{
if ((handler = privmsg_chan.handlers[i]) != NULL)
if ((handler = privmsg_chan->handlers[i]) != NULL)
(*handler)(bot, user, chan, text);
}
}
@ -122,9 +156,9 @@ void handle_self_privmsg(struct irc_conn *bot, char *user, char *text)
modpath = (char *)malloc(sizeof(char)*500);
for (i = 0; i < privmsg_self.count; i++)
for (i = 0; i < privmsg_self->count; i++)
{
handler = privmsg_self.handlers[i];
handler = privmsg_self->handlers[i];
((void(*)())handler)(bot, user, text);
}
@ -156,19 +190,19 @@ void handle_self_privmsg(struct irc_conn *bot, char *user, char *text)
{
if (!strcmp(bot->admin, user))
{
for (i = 0; i < privmsg_chan.count; i++)
for (i = 0; i < privmsg_chan->count; i++)
{
irc_notice(bot, user, "handler[%i:%s]: %p", i, privmsg_chan.type, privmsg_chan.handlers[i]);
irc_notice(bot, user, "handler[%i:%s]: %p", i, privmsg_chan->type, privmsg_chan->handlers[i]);
}
for (i = 0; i < privmsg_self.count; i++)
for (i = 0; i < privmsg_self->count; i++)
{
irc_notice(bot, user, "handler[%i:%s]: %p", i, privmsg_self.type, privmsg_self.handlers[i]);
irc_notice(bot, user, "handler[%i:%s]: %p", i, privmsg_self->type, privmsg_self->handlers[i]);
}
for (i = 0; i < irc_connected.count; i++)
for (i = 0; i < irc_connected->count; i++)
{
irc_notice(bot, user, "handler[%i:%s]: %p", i , irc_connected.type, irc_connected.handlers[i]);
irc_notice(bot, user, "handler[%i:%s]: %p", i , irc_connected->type, irc_connected->handlers[i]);
}
}
}
@ -189,22 +223,22 @@ void handle_self_privmsg(struct irc_conn *bot, char *user, char *text)
free(modpath);
}
void handle_join(struct irc_conn *bot, char *user, char *chan)
{
int i;
void (*handler)();
for (i = 0; i < chan_join.count; i++)
for (i = 0; i < chan_join->count; i++)
{
handler = chan_join.handlers[i];
handler = chan_join->handlers[i];
((void(*)())handler)(bot, user, chan);
}
}
*/
void free_events()
{
free(privmsg_self.handlers);
free(privmsg_chan.handlers);
free(chan_join.handlers);
free(irc_connected.handlers);
//free(handlers);
}

View File

@ -189,16 +189,21 @@ void irc_parse_raw(struct irc_conn *bot, char *raw)
{
if (!strcmp(par, bot->nick))
{
handle_self_privmsg(bot, user, text);
//handle_self_privmsg(bot, user, text);
fire_handler(bot, PRIVMSG_CHAN, user, text);
}
else
{
handle_chan_privmsg(bot, user, par, text);
//handle_chan_privmsg(bot, user, par, text);
//
fire_handler(bot, PRIVMSG_CHAN, user, par, text);
}
}
else if (!strcmp("JOIN", raw))
{
handle_join(bot, user, par);
//handle_join(bot, user, par);
fire_handler(bot, JOIN, user, par);
}
else if (!strcmp("PING", raw))
@ -207,7 +212,7 @@ void irc_parse_raw(struct irc_conn *bot, char *raw)
}
else if (!strcmp("001", raw))
{
handle_connected(bot, text);
fire_handler(bot, IRC_CONNECTED, text);
}
else
{