From a484282df323d90c53bb06a818976674924dfd5a Mon Sep 17 00:00:00 2001 From: Aaron Blakely Date: Wed, 8 Apr 2015 13:56:32 -0500 Subject: [PATCH] added autojoin module --- .gitignore | 2 +- Makefile | 1 + lib/events.h | 13 ++++--- lib/module.h | 2 +- mods/autojoin/Makefile | 7 ++++ mods/autojoin/autojoin.c | 48 +++++++++++++++++++++++++ mods/test.so | Bin 8146 -> 8222 bytes mods/test/test.c | 7 ++-- src/config.c | 2 +- src/events.c | 73 ++++++++++++++++++++++++++++++--------- src/irc.c | 4 +++ src/main.c | 3 -- src/module.c | 13 +++---- xbot.cfg | 7 ++-- 14 files changed, 144 insertions(+), 38 deletions(-) create mode 100644 mods/autojoin/Makefile create mode 100644 mods/autojoin/autojoin.c diff --git a/.gitignore b/.gitignore index abfd277..2e9627f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ xbot build - +*.so diff --git a/Makefile b/Makefile index c950972..80e61b5 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ main: mods: $(MAKE) -C mods/test + $(MAKE) -C mods/autojoin clean: @rm -rf build $(EXEC) \ No newline at end of file diff --git a/lib/events.h b/lib/events.h index 8bf39fe..5afc4be 100644 --- a/lib/events.h +++ b/lib/events.h @@ -3,19 +3,22 @@ #include "irc.h" -#define PRIVMSG_SELF 1 -#define PRIVMSG_CHAN 2 -#define JOIN 3 +#define PRIVMSG_SELF "CMSG" +#define PRIVMSG_CHAN "PMSG" +#define JOIN "JOIN" +#define IRC_CONNECTED "001" struct handler { - int type; + char *type; int count; void **handlers; }; void init_events(); -void add_handler(int type, void *handler); +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); diff --git a/lib/module.h b/lib/module.h index 3b130c0..ade5c42 100644 --- a/lib/module.h +++ b/lib/module.h @@ -3,6 +3,6 @@ #include "irc.h" -void load_module(struct irc_conn *bot, char *where, int stype, char *file); +void load_module(struct irc_conn *bot, char *where, char *stype, char *file); #endif \ No newline at end of file diff --git a/mods/autojoin/Makefile b/mods/autojoin/Makefile new file mode 100644 index 0000000..9575b9e --- /dev/null +++ b/mods/autojoin/Makefile @@ -0,0 +1,7 @@ +CC=gcc +CFLAGS=-g -std=gnu99 -lconfig -fPIC -I../../lib +OBJ=../autojoin.so + +main: + $(CC) -shared -o $(OBJ) $(CFLAGS) ./autojoin.c + @echo "All Done!" \ No newline at end of file diff --git a/mods/autojoin/autojoin.c b/mods/autojoin/autojoin.c new file mode 100644 index 0000000..749333f --- /dev/null +++ b/mods/autojoin/autojoin.c @@ -0,0 +1,48 @@ +#include "irc.h" +#include "events.h" +#include +#include +#include + +void aj(struct irc_conn *bot, char *text) +{ + + // TODO: + // Config Parser API + + int count, n; + config_t cfg, *cf; + const config_setting_t *autojoin; + const char *base; + const char *chan = NULL; + + cf = &cfg; + config_init(cf); + + if (!config_read_file(cf, "./xbot.cfg")) + { + printf("[xbot.cfg:%d] Configuration error: %s\n", + config_error_line(cf), + config_error_text(cf) + ); + + config_destroy(cf); + return; + } + + autojoin = config_lookup(cf, "mods.mod_autojoin"); + count = config_setting_length(autojoin); + + for (n = 0; n < count; n++) + { + chan = config_setting_get_string_elem(autojoin, n); + irc_raw(bot, "JOIN :%s", chan); + } + + config_destroy(cf); +} + +void mod_init() +{ + add_handler(IRC_CONNECTED, aj); +} \ No newline at end of file diff --git a/mods/test.so b/mods/test.so index dc996a1abf054e9fb50c27f219d4763d15bfcaa2..1633a329fb98c982cf3a8dd5ccef9051a99598fa 100755 GIT binary patch literal 8222 zcmeHMTWnOv8J_iGdnw>Glv{w7BZO54HCqP)bwZu2?+0WIF*u?n=&;^h<3)RMcF$6S zk`~D%%GR;!^3W#ALwIQ-K`R9*t*Fw=A|gW7sB$kLw=c08QLPI~!$TvYs&T)6&iuRQ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r!&dVl|549ky&bfsPwMt zw+8?67Y-pUEXQ3dRN}$01~2$0Yjx)}(o*qEX610XrD^Rrd zo$GZKr|kT4qO}e*e~30>4c#|I!4DiSMddL+_#5H-phHuvCXGo@I+!2-#lZt!-pOHuCkE@u2VEonmhi zU-brD^DkVyj?KaLaz~F=`xc?)?o}E_Vq5X$zs2G+h%@N24KWMLB+ zQMb>t8C3SWo_oUUI#}xS6HN&>*C7&N??Us*ae0#jQ}TC^2&QY;0cWk$f!VK} z-2<$(K~J!{CeO0L=5>Ii`Nb9B{1_Yy>hvi5j=mN@?fknUQlI0h!F;vEDY zgDk2Ccy1 z5KhM3SbJ;?;5BL@FCjUGuRMOsvv_W-U1HrHo+U`c3O=ldh*-*pb%khnAmi>ZNh7N4 zR3VFER?Co0LRTsdTT>~Tfl<^|Vj$|SoEOfgmQoFFr&Oq~P~;3orC59ns3}*IWBee9 zsoHHT8@vIn)+4oKH(Fg-q|=Zs*J(YRKp%zw%McO1up_Ye*UMF$_inkK%um`RS3)KgX+l#tOB!A^{hjyX8%Rl z$~f$*l$Om`XooLo?~gqC)`x~;3Xi*$DP LywL+YtH1dV2aVZO diff --git a/mods/test/test.c b/mods/test/test.c index cf64871..617572f 100644 --- a/mods/test/test.c +++ b/mods/test/test.c @@ -4,6 +4,8 @@ #include #include +int HANDLER = 0; + void hello(struct irc_conn *bot, char *user, char *chan, char *text) { char *buf = (char *)malloc(sizeof(char *) * 500); @@ -11,7 +13,8 @@ void hello(struct irc_conn *bot, char *user, char *chan, char *text) if (!strcmp(text, buf)) { - irc_privmsg(bot, chan, "hi %s", user); + irc_privmsg(bot, chan, "%i", HANDLER); + del_handler(HANDLER, PRIVMSG_CHAN); } free(buf); @@ -19,5 +22,5 @@ void hello(struct irc_conn *bot, char *user, char *chan, char *text) void mod_init() { - add_handler(PRIVMSG_CHAN, hello); + HANDLER = add_handler(PRIVMSG_CHAN, hello); } \ No newline at end of file diff --git a/src/config.c b/src/config.c index 52054e5..754a9d8 100644 --- a/src/config.c +++ b/src/config.c @@ -48,7 +48,7 @@ struct irc_conn read_config(struct irc_conn bot, char *file) { mod = config_setting_get_string_elem(autoload, n); sprintf(modpath, "./mods/%s.so", mod); - load_module(&bot, "main", 3, modpath); + load_module(&bot, "main", "runtime", modpath); } config_destroy(cf); diff --git a/src/events.c b/src/events.c index 5bf7054..7d27747 100644 --- a/src/events.c +++ b/src/events.c @@ -12,43 +12,82 @@ struct handler privmsg_self; struct handler privmsg_chan; struct handler chan_join; +struct handler irc_connected; + +// TODO: +// redo this module, unified api void init_events() { - privmsg_self.type = PRIVMSG_SELF; - privmsg_chan.type = PRIVMSG_CHAN; - chan_join.type = JOIN; + 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; + 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); - 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 add_handler(char *type, void *handler) { - printf("Installing handler @ %p [type: %i]\n", handler, type); + printf("Installing handler @ %p [type: %s]\n", handler, type); int handler_count; - if (type == PRIVMSG_SELF) + if (!strcmp(PRIVMSG_SELF, type)) { privmsg_self.handlers[privmsg_self.count] = handler; privmsg_self.count++; + + return privmsg_self.count - 1; } - else if (type == PRIVMSG_CHAN) + else if (!strcmp(PRIVMSG_CHAN, type)) { privmsg_chan.handlers[privmsg_chan.count] = handler; privmsg_chan.count++; + + return privmsg_chan.count - 1; } - else if (type == JOIN) + else if (!strcmp(JOIN, type)) { chan_join.handlers[chan_join.count] = handler; chan_join.count++; - } + return chan_join.count - 1; + } + else if (!strcmp(IRC_CONNECTED, type)) + { + irc_connected.handlers[irc_connected.count] = handler; + irc_connected.count++; + + return irc_connected.count - 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 (*handler)(); + + for (int i = 0; i < irc_connected.count; i++) + { + if ((handler = irc_connected.handlers[i]) != NULL) + (*handler)(bot, text); + } } void handle_chan_privmsg(struct irc_conn *bot, char *user, char *chan, char *text) @@ -57,8 +96,8 @@ void handle_chan_privmsg(struct irc_conn *bot, char *user, char *chan, char *tex for (int i = 0; i < privmsg_chan.count; i++) { - handler = privmsg_chan.handlers[i]; - (*handler)(bot, user, chan, text); + if ((handler = privmsg_chan.handlers[i]) != NULL) + (*handler)(bot, user, chan, text); } } diff --git a/src/irc.c b/src/irc.c index 8a6cfa9..b2e7ac6 100644 --- a/src/irc.c +++ b/src/irc.c @@ -152,6 +152,10 @@ void irc_parse_raw(struct irc_conn *bot, char *raw) { irc_raw(bot, "PONG %s", text); } + else if (!strcmp("001", raw)) + { + handle_connected(bot, text); + } else { if (!strcmp("NICK", raw) && !strcmp(user, bot->nick)) diff --git a/src/main.c b/src/main.c index 8474fd9..ee3eb47 100644 --- a/src/main.c +++ b/src/main.c @@ -12,8 +12,6 @@ #include "irc.h" #include "util.h" #include "events.h" -#include "module.h" - int main() { @@ -30,7 +28,6 @@ int main() irc_connect(&bot); irc_auth(&bot); - for (;;) { FD_ZERO(&rd); diff --git a/src/module.c b/src/module.c index 50ec8f7..ba73be2 100644 --- a/src/module.c +++ b/src/module.c @@ -3,9 +3,10 @@ #include "events.h" #include #include +#include #include -void load_module(struct irc_conn *bot, char *where, int stype, char *file) +void load_module(struct irc_conn *bot, char *where, char *stype, char *file) { void *handle; void (*mod_init)(); @@ -17,12 +18,12 @@ void load_module(struct irc_conn *bot, char *where, int stype, char *file) { sprintf(error, "Error: %s", dlerror()); - if (stype == 3) + if (strcmp("runtime", stype)) { eprint("%s\n", error); return; } - else if (stype == PRIVMSG_CHAN) + else if (strcmp(PRIVMSG_CHAN, stype)) { irc_privmsg(bot, where, error); } @@ -43,11 +44,11 @@ void load_module(struct irc_conn *bot, char *where, int stype, char *file) //sprintf(error, "Error: %s", error); eprint("Error: %s\n", error); - if (stype == 3) + if (strcmp("runtime", stype)) { return; } - else if (stype == PRIVMSG_CHAN) + else if (strcmp(PRIVMSG_CHAN, stype)) { irc_privmsg(bot, where, error); } @@ -61,7 +62,7 @@ void load_module(struct irc_conn *bot, char *where, int stype, char *file) dlclose(handle); - if (stype != 3) + if (strcmp("runtime", stype)) { irc_privmsg(bot, where, "Module '%s' loaded.", file); } diff --git a/xbot.cfg b/xbot.cfg index 1e7208d..d79f026 100644 --- a/xbot.cfg +++ b/xbot.cfg @@ -15,6 +15,9 @@ server: mods: { - autoload = ("test"); + autoload = ("autojoin"); blacklist = (); -}; \ No newline at end of file + + # config option for mods/autojoin.so + mod_autojoin = ("#ircbots"); +};