From dbd669011aa05744a78e4e00e2bf3d091f28f726 Mon Sep 17 00:00:00 2001 From: Aaron Blakely Date: Thu, 26 Mar 2015 18:20:59 -0500 Subject: [PATCH] Added event handling --- Makefile | 4 +-- lib/events.h | 14 +++++++++ lib/irc.h | 2 +- lib/module.h | 0 src/events.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/irc.c | 17 ++++++++-- src/main.c | 13 ++++++++ src/module.c | 0 src/util.c | 1 + 9 files changed, 135 insertions(+), 5 deletions(-) create mode 100644 lib/module.h create mode 100644 src/module.c diff --git a/Makefile b/Makefile index b3364f8..667beeb 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CC=gcc -CFLAGS=-c -lconfig -I./lib -BINFLAGS=-lconfig +CFLAGS=-g -std=gnu99 -c -lconfig -I./lib +BINFLAGS=-g -lconfig SRC=./src OBJ=./build OBJECTS=$(OBJ)/*.o diff --git a/lib/events.h b/lib/events.h index e1b7a78..8bf39fe 100644 --- a/lib/events.h +++ b/lib/events.h @@ -3,7 +3,21 @@ #include "irc.h" +#define PRIVMSG_SELF 1 +#define PRIVMSG_CHAN 2 +#define JOIN 3 + +struct handler +{ + int type; + int count; + void **handlers; +}; + +void init_events(); +void add_handler(int type, void *handler); 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); #endif \ No newline at end of file diff --git a/lib/irc.h b/lib/irc.h index aff1749..13876c5 100644 --- a/lib/irc.h +++ b/lib/irc.h @@ -27,7 +27,7 @@ struct irc_conn void irc_connect(struct irc_conn *bot); void irc_auth(struct irc_conn *bot); -void irc_notice(struct irc_conn *bot, char *to, char *msg); +void irc_notice(struct irc_conn *bot, char *to, char *fmt, ...); void irc_raw(struct irc_conn *bot, char *fmt, ...); void irc_parse_raw(struct irc_conn *bot, char *raw); diff --git a/lib/module.h b/lib/module.h new file mode 100644 index 0000000..e69de29 diff --git a/src/events.c b/src/events.c index 9e83453..d6c268c 100644 --- a/src/events.c +++ b/src/events.c @@ -1,19 +1,78 @@ #include "irc.h" #include "util.h" +#include "events.h" #include +#include #include +#include + +#define HANDLERARRY_LEN 500 + +struct handler privmsg_self; +struct handler privmsg_chan; +struct handler chan_join; + +void init_events() +{ + privmsg_self.type = PRIVMSG_SELF; + privmsg_chan.type = PRIVMSG_CHAN; + chan_join.type = JOIN; + + privmsg_self.count = 0; + privmsg_chan.count = 0; + chan_join.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); +} + +void add_handler(int type, void *handler) +{ + int handler_count; + + if (type == PRIVMSG_SELF) + { + privmsg_self.handlers[privmsg_self.count] = handler; + privmsg_self.count++; + } + else if (type == PRIVMSG_CHAN) + { + privmsg_chan.handlers[privmsg_chan.count] = handler; + privmsg_chan.count++; + } + else if (type == JOIN) + { + chan_join.handlers[chan_join.count] = handler; + chan_join.count++; + } + +} void handle_chan_privmsg(struct irc_conn *bot, char *user, char *chan, char *text) { + void (*handler)(); + for (int i = 0; i < privmsg_chan.count; i++) + { + handler = privmsg_chan.handlers[i]; + ((void(*)())handler)(bot, user, chan, text); + } } void handle_self_privmsg(struct irc_conn *bot, char *user, char *text) { + void (*handler)(); char *cmd, *arg; cmd = text; arg = skip(cmd, ' '); + for (int i = 0; i < privmsg_self.count; i++) + { + handler = privmsg_self.handlers[i]; + ((void(*)())handler)(bot, user, text); + } + if (!strcmp("JOIN", cmd)) { if (strcmp(bot->admin, user)) @@ -25,4 +84,34 @@ void handle_self_privmsg(struct irc_conn *bot, char *user, char *text) irc_notice(bot, user, "You are unauthorized to use this command."); } } + + if (!strcmp("LOADMOD", cmd)) + { + if (strcmp(bot->admin, user)) + { + irc_notice(bot, user, "Loading module: mods/%s.so", arg); + } + else + { + irc_notice(bot, user, "You are unauthorized to use this command."); + } + } +} + +void handle_join(struct irc_conn *bot, char *user, char *chan) +{ + void (*handler)(); + + for (int i = 0; i < chan_join.count; 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); } \ No newline at end of file diff --git a/src/irc.c b/src/irc.c index 6a202ab..766948e 100644 --- a/src/irc.c +++ b/src/irc.c @@ -9,6 +9,7 @@ #include "util.h" #include "events.h" #include +#include #include #include #include @@ -62,9 +63,16 @@ void irc_auth(struct irc_conn *bot) fflush(bot->srv_fd); } -void irc_notice(struct irc_conn *bot, char *to, char *msg) +void irc_notice(struct irc_conn *bot, char *to, char *fmt, ...) { - irc_raw(bot, "NOTICE %s :%s", to, msg); + char msg_[4096]; + va_list ap; + + va_start(ap, fmt); + vsnprintf(msg_, sizeof msg_, fmt, ap); + va_end(ap); + + irc_raw(bot, "NOTICE %s :%s", to, msg_); } void irc_raw(struct irc_conn *bot, char *fmt, ...) @@ -123,6 +131,11 @@ void irc_parse_raw(struct irc_conn *bot, char *raw) handle_chan_privmsg(bot, user, par, text); } } + else if (!strcmp("JOIN", raw)) + { + handle_join(bot, user, par); + } + else if (!strcmp("PING", raw)) { irc_raw(bot, "PONG %s", text); diff --git a/src/main.c b/src/main.c index 34ec8f8..77aa9c2 100644 --- a/src/main.c +++ b/src/main.c @@ -9,8 +9,16 @@ #include #include #include "irc.h" +#include "util.h" +#include "events.h" +void hello_cmd(struct irc_conn *bot, char *user, char *text) +{ + printf("cmd exec\n"); + irc_notice(bot, user, "Hello"); +} + int main() { fd_set rd; @@ -19,6 +27,8 @@ int main() const char *base = NULL; struct irc_conn bot; + init_events(); + // Init the config parser cf = &cfg; config_init(cf); @@ -54,6 +64,9 @@ int main() // Free the config before entering the main loop config_destroy(cf); + add_handler(PRIVMSG_SELF, hello_cmd); + add_handler(PRIVMSG_SELF, hello_cmd); + for (;;) { FD_ZERO(&rd); diff --git a/src/module.c b/src/module.c new file mode 100644 index 0000000..e69de29 diff --git a/src/util.c b/src/util.c index 952b590..08e9249 100644 --- a/src/util.c +++ b/src/util.c @@ -9,6 +9,7 @@ #include #include #include +#include void eprint(char *fmt, ...) {