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"
|
2015-03-27 15:44:40 +00:00
|
|
|
#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>
|
|
|
|
#endif
|
|
|
|
|
2024-02-13 10:56:23 +00:00
|
|
|
struct handler *handlers[512];
|
2024-02-24 01:57:59 +00:00
|
|
|
int handlers_count = 0;
|
2015-04-08 18:56:32 +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;
|
2024-02-13 12:07:59 +00:00
|
|
|
handlers[handlers_count]->count = 0;
|
2024-02-13 10:56:23 +00:00
|
|
|
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()
|
|
|
|
{
|
2024-02-21 14:24:49 +00:00
|
|
|
init_event_type(TICK);
|
2024-02-13 10:56:23 +00:00
|
|
|
init_event_type(PRIVMSG_SELF);
|
|
|
|
init_event_type(PRIVMSG_CHAN);
|
|
|
|
init_event_type(JOIN);
|
2024-02-16 21:28:11 +00:00
|
|
|
init_event_type(JOIN_MYSELF);
|
2024-02-13 10:56:23 +00:00
|
|
|
init_event_type(IRC_CONNECTED);
|
2024-02-16 21:28:11 +00:00
|
|
|
init_event_type(NICK_MYSELF);
|
|
|
|
init_event_type(NICK_INUSE);
|
|
|
|
init_event_type(CTCP);
|
|
|
|
init_event_type(IRC_NAMREPLY);
|
2024-02-17 04:54:16 +00:00
|
|
|
init_event_type(IRC_WHOREPLY);
|
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 12:07:59 +00:00
|
|
|
int i;
|
2024-02-13 07:22:10 +00:00
|
|
|
printf("Installing handler @ %p [type: %s]\n", handler, type);
|
|
|
|
|
2024-02-13 12:07:59 +00:00
|
|
|
for (i = 0; i < handlers_count; i++)
|
2024-02-13 07:22:10 +00:00
|
|
|
{
|
2024-02-13 12:07:59 +00:00
|
|
|
printf("comparing %s to %s\n", handlers[i]->type, type);
|
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++;
|
2024-02-13 12:07:59 +00:00
|
|
|
|
|
|
|
printf("type %s count: %d\n", type, handlers[i]->count);
|
|
|
|
return handlers[i]->count - 1;
|
2024-02-13 10:56:23 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2024-02-13 23:47:20 +00:00
|
|
|
MY_API void del_handler(char *type, void *handler)
|
2015-04-08 18:56:32 +00:00
|
|
|
{
|
2024-02-13 23:47:20 +00:00
|
|
|
int i, j;
|
|
|
|
for (i = 0; i < handlers_count; i++)
|
|
|
|
{
|
|
|
|
if (!strcmp(handlers[i]->type, type))
|
|
|
|
{
|
|
|
|
for (j = 0; j < handlers[i]->count; j++)
|
|
|
|
{
|
|
|
|
if (handlers[i]->evhands[j].handler == handler)
|
|
|
|
{
|
|
|
|
handlers[i]->evhands[j].handler = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-08 18:56:32 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2024-02-24 01:57:59 +00:00
|
|
|
char *usr;
|
|
|
|
char *host;
|
|
|
|
char *chan;
|
|
|
|
char *text;
|
2024-02-13 12:07:59 +00:00
|
|
|
int i, j;
|
|
|
|
void (*handler)();
|
2024-02-14 00:25:08 +00:00
|
|
|
char *cmd, *arg, *modpath;
|
|
|
|
|
|
|
|
modpath = (char *)malloc(sizeof(char)*500);
|
|
|
|
|
|
|
|
if (!strcmp(type, PRIVMSG_SELF))
|
|
|
|
{
|
|
|
|
va_start(args, type);
|
|
|
|
|
|
|
|
usr = va_arg(args, char*);
|
2024-02-16 21:28:11 +00:00
|
|
|
host = va_arg(args, char*);
|
2024-02-14 00:25:08 +00:00
|
|
|
text = va_arg(args, char*);
|
|
|
|
|
|
|
|
cmd = text;
|
|
|
|
arg = skip(cmd, ' ');
|
|
|
|
|
|
|
|
if (!strcmp("JOIN", cmd))
|
|
|
|
{
|
|
|
|
if (!strcmp(bot->admin, usr))
|
|
|
|
{
|
|
|
|
irc_raw(bot, "JOIN :%s", arg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irc_notice(bot, usr, "You are unauthorized to use this command.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (!strcmp("PART", cmd))
|
|
|
|
{
|
|
|
|
if (!strcmp(bot->admin, usr))
|
|
|
|
{
|
|
|
|
irc_raw(bot, "PART %s :Admin made me leave.", arg);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irc_notice(bot, usr, "You are unauthorized to use this command.");
|
|
|
|
}
|
|
|
|
}
|
2024-02-21 14:24:49 +00:00
|
|
|
else if (!strcmp("HANDLERS", cmd))
|
2024-02-14 00:25:08 +00:00
|
|
|
{
|
|
|
|
if (!strcmp(bot->admin, usr))
|
|
|
|
{
|
2024-02-16 21:28:11 +00:00
|
|
|
for (i = 0; i < handlers_count; i++)
|
2024-02-14 00:25:08 +00:00
|
|
|
{
|
2024-02-16 21:28:11 +00:00
|
|
|
irc_notice(bot, usr, "Handler type: %s\n", handlers[i]->type);
|
|
|
|
for (j = 0; j < handlers[i]->count; j++)
|
|
|
|
{
|
|
|
|
irc_notice(bot, usr, "Handler %d: %p\n", j, handlers[i]->evhands[j].handler);
|
|
|
|
}
|
2024-02-14 00:25:08 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-16 21:28:11 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
irc_notice(bot, usr, "You are unauthorized to use this command.");
|
|
|
|
}
|
2024-02-14 00:25:08 +00:00
|
|
|
}
|
|
|
|
else if (!strcmp("LOADMOD", cmd))
|
|
|
|
{
|
|
|
|
if (!strcmp(bot->admin, usr))
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
2024-02-17 06:03:20 +00:00
|
|
|
irc_notice(bot, usr, "Loading module: mods/%s.dll", arg);
|
|
|
|
sprintf(modpath, "./mods/%s.dll", arg);
|
2024-02-14 00:25:08 +00:00
|
|
|
#else
|
2024-02-17 06:03:20 +00:00
|
|
|
irc_notice(bot, usr, "Loading module: mods/%s.so", arg);
|
|
|
|
sprintf(modpath, "./mods/%s.so", arg);
|
2024-02-14 00:25:08 +00:00
|
|
|
#endif
|
|
|
|
load_module(bot, usr, PRIVMSG_SELF, modpath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irc_notice(bot, usr, "You are unauthorized to use this command.");
|
|
|
|
}
|
|
|
|
}
|
2024-02-17 06:03:20 +00:00
|
|
|
else if (!strcmp("UNLOADMOD", cmd))
|
|
|
|
{
|
|
|
|
if (!strcmp(bot->admin, usr))
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
irc_notice(bot, usr, "Unloading module: mods/%s.dll", arg);
|
|
|
|
sprintf(modpath, "./mods/%s.dll", arg);
|
|
|
|
#else
|
|
|
|
irc_notice(bot, usr, "Unloading module: mods/%s.so", arg);
|
|
|
|
sprintf(modpath, "./mods/%s.so", arg);
|
|
|
|
#endif
|
|
|
|
unload_module(bot, usr, modpath);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irc_notice(bot, usr, "You are unauthorized to use this command.");
|
|
|
|
}
|
|
|
|
}
|
2024-02-17 08:21:22 +00:00
|
|
|
else if (!strcmp("MODLIST", cmd))
|
|
|
|
{
|
|
|
|
if (!strcmp(bot->admin, usr))
|
|
|
|
{
|
|
|
|
list_modules(bot, usr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irc_notice(bot, usr, "You are unauthorized to use this command.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end(args);
|
2024-02-14 00:25:08 +00:00
|
|
|
}
|
|
|
|
|
2024-02-13 12:07:59 +00:00
|
|
|
for (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))
|
|
|
|
{
|
2024-02-13 12:07:59 +00:00
|
|
|
for (j = 0; j < handlers[i]->count; j++)
|
2024-02-13 10:56:23 +00:00
|
|
|
{
|
2024-02-13 12:07:59 +00:00
|
|
|
handler = handlers[i]->evhands[j].handler;
|
2024-02-13 10:56:23 +00:00
|
|
|
|
2024-02-13 23:47:20 +00:00
|
|
|
if (handler == NULL)
|
|
|
|
continue;
|
|
|
|
|
2024-02-13 10:56:23 +00:00
|
|
|
if (!strcmp(type, PRIVMSG_SELF))
|
|
|
|
{
|
2024-02-13 12:07:59 +00:00
|
|
|
va_start(args, type);
|
|
|
|
|
|
|
|
usr = va_arg(args, char*);
|
2024-02-16 21:28:11 +00:00
|
|
|
host = va_arg(args, char*);
|
2024-02-13 12:07:59 +00:00
|
|
|
text = va_arg(args, char*);
|
2024-02-13 10:56:23 +00:00
|
|
|
|
2024-02-16 21:28:11 +00:00
|
|
|
(*handler)(bot, usr, host, text);
|
2024-02-13 10:56:23 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
else if (!strcmp(type, PRIVMSG_CHAN))
|
|
|
|
{
|
2024-02-13 12:07:59 +00:00
|
|
|
va_start(args, type);
|
2024-02-13 10:56:23 +00:00
|
|
|
|
2024-02-13 12:07:59 +00:00
|
|
|
usr = va_arg(args, char*);
|
2024-02-16 21:28:11 +00:00
|
|
|
host = va_arg(args, char*);
|
2024-02-13 12:07:59 +00:00
|
|
|
chan = va_arg(args, char*);
|
|
|
|
text = va_arg(args, char*);
|
2024-02-13 10:56:23 +00:00
|
|
|
|
2024-02-16 21:28:11 +00:00
|
|
|
(*handler)(bot, usr, host, chan, text);
|
2024-02-13 12:07:59 +00:00
|
|
|
va_end(args);
|
2024-02-13 10:56:23 +00:00
|
|
|
}
|
|
|
|
else if (!strcmp(type, JOIN))
|
|
|
|
{
|
2024-02-13 12:07:59 +00:00
|
|
|
va_start(args, type);
|
|
|
|
|
|
|
|
chan = va_arg(args, char*);
|
|
|
|
usr = va_arg(args, char*);
|
2024-02-16 21:28:11 +00:00
|
|
|
host = va_arg(args, char*);
|
2024-02-13 10:56:23 +00:00
|
|
|
|
2024-02-16 21:28:11 +00:00
|
|
|
(*handler)(bot, chan, usr, host);
|
2024-02-13 10:56:23 +00:00
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
else if (!strcmp(type, IRC_CONNECTED))
|
|
|
|
{
|
2024-02-13 12:07:59 +00:00
|
|
|
va_start(args, type);
|
2024-02-13 10:56:23 +00:00
|
|
|
|
2024-02-13 12:07:59 +00:00
|
|
|
text = va_arg(args, char*);
|
2024-02-13 10:56:23 +00:00
|
|
|
|
2024-02-13 12:07:59 +00:00
|
|
|
(*handler)(bot, text);
|
|
|
|
va_end(args);
|
2024-02-13 10:56:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-13 07:22:10 +00:00
|
|
|
}
|
2024-02-13 10:56:23 +00:00
|
|
|
|
2024-02-13 07:22:10 +00:00
|
|
|
free(modpath);
|
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
|
|
|
}
|