xbot/src/events.c

245 lines
5.8 KiB
C
Raw Normal View History

2015-03-24 17:48:11 +00:00
#include "irc.h"
#include "util.h"
2015-03-26 23:20:59 +00:00
#include "events.h"
#include "module.h"
2015-03-24 17:48:11 +00:00
#include <stdio.h>
2015-03-26 23:20:59 +00:00
#include <stdlib.h>
2015-03-24 17:48:11 +00:00
#include <string.h>
2024-02-13 10:56:23 +00:00
#include <stdarg.h>
2015-03-26 23:20:59 +00:00
2024-02-13 07:22:10 +00:00
#ifdef _WIN32
#include <ctype.h>
#define SPF sprintf_s
#else
#define SPF sprintf
#endif
2024-02-13 10:56:23 +00:00
struct handler *privmsg_self;
struct handler *privmsg_chan;
struct handler *chan_join;
struct handler *irc_connected;
int handlers_count = 0;
struct handler *handlers[512];
2015-04-08 18:56:32 +00:00
// TODO:
// redo this module, unified api
2015-03-26 23:20:59 +00:00
2024-02-13 10:56:23 +00:00
void init_event_type(char *type)
2015-03-26 23:20:59 +00:00
{
2024-02-13 10:56:23 +00:00
handlers[handlers_count] = calloc(1, sizeof(struct handler));
handlers[handlers_count]->type = type;
handlers[handlers_count]->evhands = calloc(128, sizeof(struct ev_handler));
2015-04-08 18:56:32 +00:00
2024-02-13 10:56:23 +00:00
handlers_count++;
}
2015-03-26 23:20:59 +00:00
2024-02-13 10:56:23 +00:00
void init_events()
{
init_event_type(PRIVMSG_SELF);
init_event_type(PRIVMSG_CHAN);
init_event_type(JOIN);
init_event_type(IRC_CONNECTED);
2015-03-26 23:20:59 +00:00
}
2024-02-13 07:22:10 +00:00
MY_API int add_handler(char *type, void *handler)
2015-03-26 23:20:59 +00:00
{
2024-02-13 07:22:10 +00:00
printf("Installing handler @ %p [type: %s]\n", handler, type);
2024-02-13 10:56:23 +00:00
for (int i = 0; i < handlers_count; i++)
2024-02-13 07:22:10 +00:00
{
2024-02-13 10:56:23 +00:00
if (!strcmp(handlers[i]->type, type))
{
if (handlers[i]->count < 128)
{
handlers[i]->evhands[handlers[i]->count].id = handlers[i]->count;
handlers[i]->evhands[handlers[i]->count].handler = handler;
2024-02-13 07:22:10 +00:00
2024-02-13 10:56:23 +00:00
handlers[i]->count++;
return 0;
}
else
{
printf("Handler array is full, cannot add more handlers.\n");
return -1;
}
}
2024-02-13 07:22:10 +00:00
}
2015-04-08 18:56:32 +00:00
}
2015-03-26 23:20:59 +00:00
2015-04-08 18:56:32 +00:00
void del_handler(int num, char *type)
{
}
2024-02-13 10:56:23 +00:00
void fire_handler(struct irc_conn *bot, char *type, ...)
2015-04-08 18:56:32 +00:00
{
2024-02-13 10:56:23 +00:00
va_list args;
va_start(args, type);
2015-04-08 18:56:32 +00:00
2024-02-13 10:56:23 +00:00
for (int i = 0; i < handlers_count; i++)
2024-02-13 07:22:10 +00:00
{
2024-02-13 10:56:23 +00:00
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;
}
}
}
2024-02-13 07:22:10 +00:00
}
2024-02-13 10:56:23 +00:00
va_end(args);
2015-03-26 23:20:59 +00:00
}
2015-03-24 17:48:11 +00:00
2024-02-13 10:56:23 +00:00
/*
2015-03-24 17:48:11 +00:00
void handle_chan_privmsg(struct irc_conn *bot, char *user, char *chan, char *text)
{
2024-02-13 07:22:10 +00:00
int i;
void (*handler)();
2015-03-24 17:48:11 +00:00
2024-02-13 10:56:23 +00:00
for (i = 0; i < privmsg_chan->count; i++)
2024-02-13 07:22:10 +00:00
{
2024-02-13 10:56:23 +00:00
if ((handler = privmsg_chan->handlers[i]) != NULL)
2024-02-13 07:22:10 +00:00
(*handler)(bot, user, chan, text);
}
2015-03-24 17:48:11 +00:00
}
void handle_self_privmsg(struct irc_conn *bot, char *user, char *text)
{
2024-02-13 07:22:10 +00:00
void (*handler)();
int i;
char *cmd, *arg, *modpath;
cmd = text;
arg = skip(cmd, ' ');
modpath = (char *)malloc(sizeof(char)*500);
2024-02-13 10:56:23 +00:00
for (i = 0; i < privmsg_self->count; i++)
2024-02-13 07:22:10 +00:00
{
2024-02-13 10:56:23 +00:00
handler = privmsg_self->handlers[i];
2024-02-13 07:22:10 +00:00
((void(*)())handler)(bot, user, text);
}
if (!strcmp("JOIN", cmd))
{
printf("dbug: cmp (%s : %s)\n", (char*)bot->admin, user);
if (!strcmp(bot->admin, user))
{
irc_raw(bot, "JOIN :%s", arg);
}
else
{
irc_notice(bot, user, "You are unauthorized to use this command.");
}
}
else if (!strcmp("PART", cmd))
{
if (!strcmp(bot->admin, user))
{
irc_raw(bot, "PART %s :Admin made me leave.", arg);
}
else
{
irc_notice(bot, user, "You are unauthorized to use this command.");
}
}
else if (!strcmp("PRINT_HANDLERS", cmd))
{
if (!strcmp(bot->admin, user))
{
2024-02-13 10:56:23 +00:00
for (i = 0; i < privmsg_chan->count; i++)
2024-02-13 07:22:10 +00:00
{
2024-02-13 10:56:23 +00:00
irc_notice(bot, user, "handler[%i:%s]: %p", i, privmsg_chan->type, privmsg_chan->handlers[i]);
2024-02-13 07:22:10 +00:00
}
2024-02-13 10:56:23 +00:00
for (i = 0; i < privmsg_self->count; i++)
2024-02-13 07:22:10 +00:00
{
2024-02-13 10:56:23 +00:00
irc_notice(bot, user, "handler[%i:%s]: %p", i, privmsg_self->type, privmsg_self->handlers[i]);
2024-02-13 07:22:10 +00:00
}
2024-02-13 10:56:23 +00:00
for (i = 0; i < irc_connected->count; i++)
2024-02-13 07:22:10 +00:00
{
2024-02-13 10:56:23 +00:00
irc_notice(bot, user, "handler[%i:%s]: %p", i , irc_connected->type, irc_connected->handlers[i]);
2024-02-13 07:22:10 +00:00
}
}
}
else if (!strcmp("LOADMOD", cmd))
{
if (!strcmp(bot->admin, user))
{
irc_notice(bot, user, "Loading module: mods/%s.so", arg);
SPF(modpath, "./mods/%s.so", arg);
load_module(bot, user, PRIVMSG_SELF, modpath);
2024-02-13 07:22:10 +00:00
}
else
{
irc_notice(bot, user, "You are unauthorized to use this command.");
}
}
free(modpath);
2015-03-26 23:20:59 +00:00
}
2024-02-13 10:56:23 +00:00
2015-03-26 23:20:59 +00:00
void handle_join(struct irc_conn *bot, char *user, char *chan)
{
2024-02-13 07:22:10 +00:00
int i;
void (*handler)();
2015-03-26 23:20:59 +00:00
2024-02-13 10:56:23 +00:00
for (i = 0; i < chan_join->count; i++)
2024-02-13 07:22:10 +00:00
{
2024-02-13 10:56:23 +00:00
handler = chan_join->handlers[i];
2024-02-13 07:22:10 +00:00
((void(*)())handler)(bot, user, chan);
}
2015-03-26 23:20:59 +00:00
}
2024-02-13 10:56:23 +00:00
*/
2015-03-26 23:20:59 +00:00
void free_events()
{
2024-02-13 10:56:23 +00:00
//free(handlers);
2024-02-13 07:22:10 +00:00
}