added autojoin module

This commit is contained in:
Aaron Blakely 2015-04-08 13:56:32 -05:00
parent 0f4468a4c2
commit a484282df3
14 changed files with 144 additions and 38 deletions

2
.gitignore vendored
View File

@ -1,3 +1,3 @@
xbot xbot
build build
*.so

View File

@ -22,6 +22,7 @@ main:
mods: mods:
$(MAKE) -C mods/test $(MAKE) -C mods/test
$(MAKE) -C mods/autojoin
clean: clean:
@rm -rf build $(EXEC) @rm -rf build $(EXEC)

View File

@ -3,19 +3,22 @@
#include "irc.h" #include "irc.h"
#define PRIVMSG_SELF 1 #define PRIVMSG_SELF "CMSG"
#define PRIVMSG_CHAN 2 #define PRIVMSG_CHAN "PMSG"
#define JOIN 3 #define JOIN "JOIN"
#define IRC_CONNECTED "001"
struct handler struct handler
{ {
int type; char *type;
int count; int count;
void **handlers; void **handlers;
}; };
void init_events(); 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_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); void handle_join(struct irc_conn *bot, char *user, char *chan);

View File

@ -3,6 +3,6 @@
#include "irc.h" #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 #endif

7
mods/autojoin/Makefile Normal file
View File

@ -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!"

48
mods/autojoin/autojoin.c Normal file
View File

@ -0,0 +1,48 @@
#include "irc.h"
#include "events.h"
#include <stdio.h>
#include <stdlib.h>
#include <libconfig.h>
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);
}

Binary file not shown.

View File

@ -4,6 +4,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
int HANDLER = 0;
void hello(struct irc_conn *bot, char *user, char *chan, char *text) void hello(struct irc_conn *bot, char *user, char *chan, char *text)
{ {
char *buf = (char *)malloc(sizeof(char *) * 500); 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)) if (!strcmp(text, buf))
{ {
irc_privmsg(bot, chan, "hi %s", user); irc_privmsg(bot, chan, "%i", HANDLER);
del_handler(HANDLER, PRIVMSG_CHAN);
} }
free(buf); free(buf);
@ -19,5 +22,5 @@ void hello(struct irc_conn *bot, char *user, char *chan, char *text)
void mod_init() void mod_init()
{ {
add_handler(PRIVMSG_CHAN, hello); HANDLER = add_handler(PRIVMSG_CHAN, hello);
} }

View File

@ -48,7 +48,7 @@ struct irc_conn read_config(struct irc_conn bot, char *file)
{ {
mod = config_setting_get_string_elem(autoload, n); mod = config_setting_get_string_elem(autoload, n);
sprintf(modpath, "./mods/%s.so", mod); sprintf(modpath, "./mods/%s.so", mod);
load_module(&bot, "main", 3, modpath); load_module(&bot, "main", "runtime", modpath);
} }
config_destroy(cf); config_destroy(cf);

View File

@ -12,43 +12,82 @@
struct handler privmsg_self; struct handler privmsg_self;
struct handler privmsg_chan; struct handler privmsg_chan;
struct handler chan_join; struct handler chan_join;
struct handler irc_connected;
// TODO:
// redo this module, unified api
void init_events() void init_events()
{ {
privmsg_self.type = PRIVMSG_SELF; privmsg_self.type = PRIVMSG_SELF;
privmsg_chan.type = PRIVMSG_CHAN; privmsg_chan.type = PRIVMSG_CHAN;
chan_join.type = JOIN; chan_join.type = JOIN;
irc_connected.type = IRC_CONNECTED;
privmsg_self.count = 0; privmsg_self.count = 0;
privmsg_chan.count = 0; privmsg_chan.count = 0;
chan_join.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; int handler_count;
if (type == PRIVMSG_SELF) if (!strcmp(PRIVMSG_SELF, type))
{ {
privmsg_self.handlers[privmsg_self.count] = handler; privmsg_self.handlers[privmsg_self.count] = handler;
privmsg_self.count++; 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.handlers[privmsg_chan.count] = handler;
privmsg_chan.count++; 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.handlers[chan_join.count] = handler;
chan_join.count++; 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) 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++) for (int i = 0; i < privmsg_chan.count; i++)
{ {
handler = privmsg_chan.handlers[i]; if ((handler = privmsg_chan.handlers[i]) != NULL)
(*handler)(bot, user, chan, text); (*handler)(bot, user, chan, text);
} }
} }

View File

@ -152,6 +152,10 @@ void irc_parse_raw(struct irc_conn *bot, char *raw)
{ {
irc_raw(bot, "PONG %s", text); irc_raw(bot, "PONG %s", text);
} }
else if (!strcmp("001", raw))
{
handle_connected(bot, text);
}
else else
{ {
if (!strcmp("NICK", raw) && !strcmp(user, bot->nick)) if (!strcmp("NICK", raw) && !strcmp(user, bot->nick))

View File

@ -12,8 +12,6 @@
#include "irc.h" #include "irc.h"
#include "util.h" #include "util.h"
#include "events.h" #include "events.h"
#include "module.h"
int main() int main()
{ {
@ -30,7 +28,6 @@ int main()
irc_connect(&bot); irc_connect(&bot);
irc_auth(&bot); irc_auth(&bot);
for (;;) for (;;)
{ {
FD_ZERO(&rd); FD_ZERO(&rd);

View File

@ -3,9 +3,10 @@
#include "events.h" #include "events.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <dlfcn.h> #include <dlfcn.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)
{ {
void *handle; void *handle;
void (*mod_init)(); 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()); sprintf(error, "Error: %s", dlerror());
if (stype == 3) if (strcmp("runtime", stype))
{ {
eprint("%s\n", error); eprint("%s\n", error);
return; return;
} }
else if (stype == PRIVMSG_CHAN) else if (strcmp(PRIVMSG_CHAN, stype))
{ {
irc_privmsg(bot, where, error); 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); //sprintf(error, "Error: %s", error);
eprint("Error: %s\n", error); eprint("Error: %s\n", error);
if (stype == 3) if (strcmp("runtime", stype))
{ {
return; return;
} }
else if (stype == PRIVMSG_CHAN) else if (strcmp(PRIVMSG_CHAN, stype))
{ {
irc_privmsg(bot, where, error); irc_privmsg(bot, where, error);
} }
@ -61,7 +62,7 @@ void load_module(struct irc_conn *bot, char *where, int stype, char *file)
dlclose(handle); dlclose(handle);
if (stype != 3) if (strcmp("runtime", stype))
{ {
irc_privmsg(bot, where, "Module '%s' loaded.", file); irc_privmsg(bot, where, "Module '%s' loaded.", file);
} }

View File

@ -15,6 +15,9 @@ server:
mods: mods:
{ {
autoload = ("test"); autoload = ("autojoin");
blacklist = (); blacklist = ();
# config option for mods/autojoin.so
mod_autojoin = ("#ircbots");
}; };