Added event handling

This commit is contained in:
Aaron Blakely 2015-03-26 18:20:59 -05:00
parent 74f0b04ad0
commit dbd669011a
9 changed files with 135 additions and 5 deletions

View File

@ -1,6 +1,6 @@
CC=gcc CC=gcc
CFLAGS=-c -lconfig -I./lib CFLAGS=-g -std=gnu99 -c -lconfig -I./lib
BINFLAGS=-lconfig BINFLAGS=-g -lconfig
SRC=./src SRC=./src
OBJ=./build OBJ=./build
OBJECTS=$(OBJ)/*.o OBJECTS=$(OBJ)/*.o

View File

@ -3,7 +3,21 @@
#include "irc.h" #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_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_self_privmsg(struct irc_conn *bot, char *user, char *text);
void handle_join(struct irc_conn *bot, char *user, char *chan);
#endif #endif

View File

@ -27,7 +27,7 @@ struct irc_conn
void irc_connect(struct irc_conn *bot); void irc_connect(struct irc_conn *bot);
void irc_auth(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_raw(struct irc_conn *bot, char *fmt, ...);
void irc_parse_raw(struct irc_conn *bot, char *raw); void irc_parse_raw(struct irc_conn *bot, char *raw);

0
lib/module.h Normal file
View File

View File

@ -1,19 +1,78 @@
#include "irc.h" #include "irc.h"
#include "util.h" #include "util.h"
#include "events.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include <dlfcn.h>
#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 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 handle_self_privmsg(struct irc_conn *bot, char *user, char *text)
{ {
void (*handler)();
char *cmd, *arg; char *cmd, *arg;
cmd = text; cmd = text;
arg = skip(cmd, ' '); 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("JOIN", cmd))
{ {
if (strcmp(bot->admin, user)) 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."); 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);
} }

View File

@ -9,6 +9,7 @@
#include "util.h" #include "util.h"
#include "events.h" #include "events.h"
#include <string.h> #include <string.h>
#include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
@ -62,9 +63,16 @@ void irc_auth(struct irc_conn *bot)
fflush(bot->srv_fd); 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, ...) 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); handle_chan_privmsg(bot, user, par, text);
} }
} }
else if (!strcmp("JOIN", raw))
{
handle_join(bot, user, par);
}
else if (!strcmp("PING", raw)) else if (!strcmp("PING", raw))
{ {
irc_raw(bot, "PONG %s", text); irc_raw(bot, "PONG %s", text);

View File

@ -9,8 +9,16 @@
#include <sys/select.h> #include <sys/select.h>
#include <libconfig.h> #include <libconfig.h>
#include "irc.h" #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() int main()
{ {
fd_set rd; fd_set rd;
@ -19,6 +27,8 @@ int main()
const char *base = NULL; const char *base = NULL;
struct irc_conn bot; struct irc_conn bot;
init_events();
// Init the config parser // Init the config parser
cf = &cfg; cf = &cfg;
config_init(cf); config_init(cf);
@ -54,6 +64,9 @@ int main()
// Free the config before entering the main loop // Free the config before entering the main loop
config_destroy(cf); config_destroy(cf);
add_handler(PRIVMSG_SELF, hello_cmd);
add_handler(PRIVMSG_SELF, hello_cmd);
for (;;) for (;;)
{ {
FD_ZERO(&rd); FD_ZERO(&rd);

0
src/module.c Normal file
View File

View File

@ -9,6 +9,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h>
void eprint(char *fmt, ...) void eprint(char *fmt, ...)
{ {