2015-03-27 08:44:40 -07:00
|
|
|
#include "irc.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "events.h"
|
2024-02-13 15:47:20 -08:00
|
|
|
#include "module.h"
|
2015-03-27 08:44:40 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-04-08 11:56:32 -07:00
|
|
|
#include <string.h>
|
2024-02-12 23:22:10 -08:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
2015-03-27 08:44:40 -07:00
|
|
|
#include <dlfcn.h>
|
2024-02-12 23:22:10 -08:00
|
|
|
#endif
|
2015-03-27 08:44:40 -07:00
|
|
|
|
2024-02-13 15:47:20 -08:00
|
|
|
struct mods *mods;
|
|
|
|
|
|
|
|
void init_mods()
|
|
|
|
{
|
|
|
|
mods = calloc(1, sizeof(struct mods));
|
|
|
|
mods->count = 0;
|
|
|
|
mods->modules = calloc(512, sizeof(struct module));
|
|
|
|
}
|
|
|
|
|
2015-04-08 11:56:32 -07:00
|
|
|
void load_module(struct irc_conn *bot, char *where, char *stype, char *file)
|
2015-03-27 08:44:40 -07:00
|
|
|
{
|
|
|
|
char *error = (char *)malloc(sizeof(char *)*1024);
|
2024-02-13 15:47:20 -08:00
|
|
|
mods->modules[mods->count].fname = file;
|
|
|
|
#ifdef _WIN32
|
2015-03-27 08:44:40 -07:00
|
|
|
|
2024-02-13 15:47:20 -08:00
|
|
|
mods->modules[mods->count].handle = LoadLibrary(file);
|
|
|
|
if (mods->modules[mods->count].handle == NULL)
|
2015-03-27 08:44:40 -07:00
|
|
|
{
|
2024-02-12 23:22:10 -08:00
|
|
|
sprintf(error, "Error loading %s\n", file);
|
2015-03-27 08:44:40 -07:00
|
|
|
|
2015-04-08 11:56:32 -07:00
|
|
|
if (strcmp("runtime", stype))
|
2015-04-07 11:18:24 -07:00
|
|
|
{
|
|
|
|
eprint("%s\n", error);
|
|
|
|
return;
|
|
|
|
}
|
2024-02-12 23:22:10 -08:00
|
|
|
else if (strcmp(PRIVMSG_CHAN, stype))
|
|
|
|
{
|
|
|
|
irc_privmsg(bot, where, error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irc_notice(bot, where, error);
|
|
|
|
}
|
2015-03-27 08:44:40 -07:00
|
|
|
|
2024-02-12 23:22:10 -08:00
|
|
|
return;
|
|
|
|
}
|
2015-03-27 08:44:40 -07:00
|
|
|
|
2024-02-13 15:47:20 -08:00
|
|
|
mods->modules[mods->count].init = GetProcAddress(mods->modules[mods->count].handle, "mod_init");
|
|
|
|
if (mods->modules[mods->count].init == NULL)
|
2015-03-27 08:44:40 -07:00
|
|
|
{
|
2024-02-12 23:22:10 -08:00
|
|
|
DWORD err = GetLastError();
|
|
|
|
|
|
|
|
sprintf(error, "Error loading mod_init() pointer for %s: %lu", file, err);
|
2015-03-27 08:44:40 -07:00
|
|
|
eprint("Error: %s\n", error);
|
2015-04-07 11:18:24 -07:00
|
|
|
|
2024-02-12 23:22:10 -08:00
|
|
|
if (strcmp("runtime", stype))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (strcmp(PRIVMSG_CHAN, stype))
|
|
|
|
{
|
|
|
|
irc_privmsg(bot, where, error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irc_notice(bot, where, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2015-03-27 08:44:40 -07:00
|
|
|
|
2024-02-13 15:47:20 -08:00
|
|
|
((void(*)(void))mods->modules[mods->count].init)();
|
2015-03-27 08:44:40 -07:00
|
|
|
|
2024-02-12 23:22:10 -08:00
|
|
|
//FreeLibrary(libHandle);
|
2015-03-27 08:44:40 -07:00
|
|
|
|
2015-04-08 11:56:32 -07:00
|
|
|
if (strcmp("runtime", stype))
|
2024-02-12 23:22:10 -08:00
|
|
|
{
|
|
|
|
irc_privmsg(bot, where, "Module '%s' loaded.", file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Module '%s' loaded.\n", file);
|
|
|
|
}
|
|
|
|
free(error);
|
|
|
|
#else
|
|
|
|
void (*mod_init)();
|
|
|
|
|
2024-02-13 15:47:20 -08:00
|
|
|
mods->modules[mods->count].handle = dlopen(file, RTLD_NOW | RTLD_GLOBAL | RTLD_NODELETE);
|
|
|
|
if (!mods->modules[mods->count].handle)
|
2024-02-12 23:22:10 -08:00
|
|
|
{
|
|
|
|
sprintf(error, "Error: %s", dlerror());
|
|
|
|
|
|
|
|
if (strcmp("runtime", stype))
|
|
|
|
{
|
|
|
|
eprint("%s\n", error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (strcmp(PRIVMSG_CHAN, stype))
|
|
|
|
{
|
|
|
|
irc_privmsg(bot, where, error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irc_notice(bot, where, error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dlerror();
|
|
|
|
|
2024-02-13 15:47:20 -08:00
|
|
|
*(void **)(&mods->modules[mods->count].init) = dlsym(mods->modules[mods->count].handle , "mod_init");
|
|
|
|
|
|
|
|
if ((error = dlerror()) != NULL)
|
|
|
|
{
|
|
|
|
//sprintf(error, "Error: %s", error);
|
|
|
|
eprint("Error: %s\n", error);
|
|
|
|
|
|
|
|
if (strcmp("runtime", stype))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (strcmp(PRIVMSG_CHAN, stype))
|
|
|
|
{
|
|
|
|
irc_privmsg(bot, where, error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irc_notice(bot, where, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
(*mods->modules[mods->count].init)();
|
2024-02-12 23:22:10 -08:00
|
|
|
|
2024-02-13 15:47:20 -08:00
|
|
|
//dlclose(handle);
|
|
|
|
|
|
|
|
|
|
|
|
*(void **)(&mods->modules[mods->count].unload) = dlsym(mods->modules[mods->count].handle , "mod_unload");
|
2024-02-12 23:22:10 -08:00
|
|
|
if ((error = dlerror()) != NULL)
|
|
|
|
{
|
|
|
|
//sprintf(error, "Error: %s", error);
|
|
|
|
eprint("Error: %s\n", error);
|
|
|
|
|
|
|
|
if (strcmp("runtime", stype))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (strcmp(PRIVMSG_CHAN, stype))
|
|
|
|
{
|
|
|
|
irc_privmsg(bot, where, error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irc_notice(bot, where, error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-13 15:47:20 -08:00
|
|
|
if ((error = dlerror()) != NULL)
|
|
|
|
{
|
|
|
|
//sprintf(error, "Error: %s", error);
|
|
|
|
eprint("Error: %s\n", error);
|
|
|
|
|
|
|
|
if (strcmp("runtime", stype))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (strcmp(PRIVMSG_CHAN, stype))
|
|
|
|
{
|
|
|
|
irc_privmsg(bot, where, error);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
irc_notice(bot, where, error);
|
|
|
|
}
|
|
|
|
}
|
2024-02-12 23:22:10 -08:00
|
|
|
|
|
|
|
|
|
|
|
if (strcmp("runtime", stype))
|
|
|
|
{
|
|
|
|
irc_privmsg(bot, where, "Module '%s' loaded.", file);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("Module '%s' loaded.\n", file);
|
|
|
|
}
|
|
|
|
free(error);
|
|
|
|
#endif
|
2024-02-13 15:47:20 -08:00
|
|
|
|
|
|
|
mods->count++;
|
2024-02-12 23:22:10 -08:00
|
|
|
}
|