summaryrefslogtreecommitdiff
path: root/src/modules.c
diff options
context:
space:
mode:
authorsjaz <sjaz@5417fbe8-f217-4b02-8779-1006273d7864>2009-01-01 12:00:20 +0000
committersjaz <sjaz@5417fbe8-f217-4b02-8779-1006273d7864>2009-01-01 12:00:20 +0000
commitc777c8d9aa7cd5c2e9a399727a7fa9985a77fb1c (patch)
tree9e996ae4a1bbb833cec036c5cd4d87a590149e85 /src/modules.c
Anope Stable Branch
git-svn-id: http://anope.svn.sourceforge.net/svnroot/anope/stable@1902 5417fbe8-f217-4b02-8779-1006273d7864
Diffstat (limited to 'src/modules.c')
-rw-r--r--src/modules.c2874
1 files changed, 2874 insertions, 0 deletions
diff --git a/src/modules.c b/src/modules.c
new file mode 100644
index 000000000..99974d2ef
--- /dev/null
+++ b/src/modules.c
@@ -0,0 +1,2874 @@
+
+/* Modular support
+ *
+ * (C) 2003-2008 Anope Team
+ * Contact us at info@anope.org
+ *
+ * Please read COPYING and README for further details.
+ *
+ * Based on the original code of Epona by Lara.
+ * Based on the original code of Services by Andy Church.
+ *
+ * $Id$
+ *
+ */
+#include "modules.h"
+#include "language.h"
+#include "version.h"
+
+#if defined(USE_MODULES) && !defined(_WIN32)
+#include <dlfcn.h>
+/* Define these for systems without them */
+#ifndef RTLD_NOW
+#define RTLD_NOW 0
+#endif
+#ifndef RTLD_LAZY
+#define RTLD_LAZY RTLD_NOW
+#endif
+#ifndef RTLD_GLOBAL
+#define RTLD_GLOBAL 0
+#endif
+#ifndef RTLD_LOCAL
+#define RTLD_LOCAL 0
+#endif
+#endif
+
+#ifdef _WIN32
+const char *ano_moderr(void);
+#endif
+
+/**
+ * Declare all the list's we want to use here
+ **/
+CommandHash *HOSTSERV[MAX_CMD_HASH];
+CommandHash *BOTSERV[MAX_CMD_HASH];
+CommandHash *MEMOSERV[MAX_CMD_HASH];
+CommandHash *NICKSERV[MAX_CMD_HASH];
+CommandHash *CHANSERV[MAX_CMD_HASH];
+CommandHash *HELPSERV[MAX_CMD_HASH];
+CommandHash *OPERSERV[MAX_CMD_HASH];
+MessageHash *IRCD[MAX_CMD_HASH];
+ModuleHash *MODULE_HASH[MAX_CMD_HASH];
+
+Module *mod_current_module;
+char *mod_current_module_name = NULL;
+char *mod_current_buffer = NULL;
+User *mod_current_user;
+ModuleCallBack *moduleCallBackHead = NULL;
+ModuleQueue *mod_operation_queue = NULL;
+
+int displayCommand(Command * c);
+int displayCommandFromHash(CommandHash * cmdTable[], char *name);
+int displayMessageFromHash(char *name);
+int displayMessage(Message * m);
+char *ModuleGetErrStr(int status);
+
+char *ModuleGetErrStr(int status)
+{
+ const char *module_err_str[] = {
+ "Module, Okay - No Error", /* MOD_ERR_OK */
+ "Module Error, Allocating memory", /* MOD_ERR_MEMORY */
+ "Module Error, Not enough parameters", /* MOD_ERR_PARAMS */
+ "Module Error, Already loaded", /* MOD_ERR_EXISTS */
+ "Module Error, File does not exist", /* MOD_ERR_NOEXIST */
+ "Module Error, No User", /* MOD_ERR_NOUSER */
+ "Module Error, Error during load time or module returned MOD_STOP", /* MOD_ERR_NOLOAD */
+ "Module Error, Unable to unload", /* MOD_ERR_NOUNLOAD */
+ "Module Error, Incorrect syntax", /* MOD_ERR_SYNTAX */
+ "Module Error, Unable to delete", /* MOD_ERR_NODELETE */
+ "Module Error, Unknown Error occuried", /* MOD_ERR_UNKOWN */
+ "Module Error, File I/O Error", /* MOD_ERR_FILE_IO */
+ "Module Error, No Service found for request", /* MOD_ERR_NOSERVICE */
+ "Module Error, No module name for request" /* MOD_ERR_NO_MOD_NAME */
+ };
+ return (char *) module_err_str[status];
+}
+
+/**
+ * Automaticaly load modules at startup.
+ * This will load modules at startup before the IRCD link is attempted, this
+ * allows admins to have a module relating to ircd support load
+ */
+void modules_init(void)
+{
+#ifdef USE_MODULES
+ int idx;
+ int ret;
+ Module *m;
+
+ if(nothird) {
+ return;
+ }
+
+ for (idx = 0; idx < ModulesNumber; idx++) {
+ m = findModule(ModulesAutoload[idx]);
+ if (!m) {
+ m = createModule(ModulesAutoload[idx]);
+ mod_current_module = m;
+ mod_current_user = NULL;
+ alog("trying to load [%s]", mod_current_module->name);
+ ret = loadModule(mod_current_module, NULL);
+ alog("status: [%d][%s]", ret, ModuleGetErrStr(ret));
+ if (ret != MOD_ERR_OK)
+ destroyModule(m);
+ mod_current_module = NULL;
+ mod_current_user = NULL;
+ }
+ }
+#endif
+}
+
+/**
+ * Load up a list of core modules from the conf.
+ * @param number The number of modules to load
+ * @param list The list of modules to load
+ **/
+void modules_core_init(int number, char **list)
+{
+ int idx;
+ Module *m;
+ int status = 0;
+ for (idx = 0; idx < number; idx++) {
+ m = findModule(list[idx]);
+ if (!m) {
+ m = createModule(list[idx]);
+ mod_current_module = m;
+ mod_current_user = NULL;
+ status = loadModule(mod_current_module, NULL);
+ if (debug || status) {
+ alog("debug: trying to load core module [%s]",
+ mod_current_module->name);
+ alog("debug: status: [%d][%s]", status, ModuleGetErrStr(status));
+ if (status != MOD_ERR_OK)
+ destroyModule(mod_current_module);
+ }
+ mod_current_module = NULL;
+ mod_current_user = NULL;
+ }
+ }
+}
+/**
+ *
+ **/
+int encryption_module_init(void) {
+ int ret = 0;
+ Module *m;
+
+ m = createModule(EncModule);
+ mod_current_module = m;
+ mod_current_user = NULL;
+ alog("Loading Encryption Module: [%s]", mod_current_module->name);
+ ret = loadModule(mod_current_module, NULL);
+ moduleSetType(ENCRYPTION);
+ alog("status: [%d][%s]", ret, ModuleGetErrStr(ret));
+ mod_current_module = NULL;
+ if (ret != MOD_ERR_OK) {
+ destroyModule(m);
+ }
+ return ret;
+}
+
+/**
+ * Load the ircd protocol module up
+ **/
+int protocol_module_init(void)
+{
+ int ret = 0;
+ Module *m;
+
+ m = createModule(IRCDModule);
+ mod_current_module = m;
+ mod_current_user = NULL;
+ alog("Loading IRCD Protocol Module: [%s]", mod_current_module->name);
+ ret = loadModule(mod_current_module, NULL);
+ moduleSetType(PROTOCOL);
+ alog("status: [%d][%s]", ret, ModuleGetErrStr(ret));
+ mod_current_module = NULL;
+
+ if (ret == MOD_ERR_OK) {
+ /* This is really NOT the correct place to do config checks, but
+ * as we only have the ircd struct filled here, we have to over
+ * here. -GD
+ */
+ if (UseTokens && !(ircd->token)) {
+ alog("Anope does not support TOKENS for this ircd setting; unsetting UseToken");
+ UseTokens = 0;
+ }
+
+ if (UseTS6 && !(ircd->ts6)) {
+ alog("Chosen IRCd does not support TS6, unsetting UseTS6");
+ UseTS6 = 0;
+ }
+
+ /* We can assume the ircd supports TS6 here */
+ if (UseTS6 && !Numeric) {
+ alog("UseTS6 requires the setting of Numeric to be enabled.");
+ ret = -1;
+ }
+ } else {
+ destroyModule(m);
+ }
+
+ return ret;
+}
+
+/**
+ * Automaticaly load modules at startup, delayed.
+ * This function waits until the IRCD link has been made, and then attempts
+ * to load the specified modules.
+ */
+void modules_delayed_init(void)
+{
+#ifdef USE_MODULES
+ int idx;
+ int ret;
+ Module *m;
+
+ if(nothird) {
+ return;
+ }
+
+ for (idx = 0; idx < ModulesDelayedNumber; idx++) {
+ m = findModule(ModulesDelayedAutoload[idx]);
+ if (!m) {
+ m = createModule(ModulesDelayedAutoload[idx]);
+ mod_current_module = m;
+ mod_current_user = NULL;
+ alog("trying to load [%s]", mod_current_module->name);
+ ret = loadModule(mod_current_module, NULL);
+ alog("status: [%d][%s]", ret, ModuleGetErrStr(ret));
+ mod_current_module = NULL;
+ mod_current_user = NULL;
+ if (ret != MOD_ERR_OK)
+ destroyModule(m);
+ }
+ }
+#endif
+}
+
+/**
+ * Unload ALL loaded modules, no matter what kind of module it is.
+ * Do NEVER EVER, and i mean NEVER (and if that isn't clear enough
+ * yet, i mean: NEVER AT ALL) call this unless we're shutting down,
+ * or we'll fuck up Anope badly (protocol handling won't work for
+ * example). If anyone calls this function without a justified need
+ * for it, i reserve the right to break their legs in a painful way.
+ * And if that isn't enough discouragement, you'll wake up with your
+ * both legs broken tomorrow ;) -GD
+ */
+void modules_unload_all(boolean fini, boolean unload_proto)
+{
+#ifdef USE_MODULES
+ int idx;
+ ModuleHash *mh, *next;
+ void (*func) (void);
+
+ for (idx = 0; idx < MAX_CMD_HASH; idx++) {
+ mh = MODULE_HASH[idx];
+ while (mh) {
+ next = mh->next;
+ if (unload_proto || (mh->m->type != PROTOCOL)) {
+ mod_current_module = mh->m;
+ if(fini) {
+ func = (void (*)(void))ano_modsym(mh->m->handle, "AnopeFini");
+ if (func) {
+ mod_current_module_name = mh->m->name;
+ func(); /* exec AnopeFini */
+ mod_current_module_name = NULL;
+ }
+
+ if (prepForUnload(mh->m) != MOD_ERR_OK) {
+ mh = next;
+ continue;
+ }
+
+ if ((ano_modclose(mh->m->handle)) != 0)
+ alog(ano_moderr());
+ else
+ delModule(mh->m);
+ } else {
+ delModule(mh->m);
+ }
+ }
+ mh = next;
+ }
+ }
+#endif
+}
+
+/**
+ * Create a new module, setting up the default values as needed.
+ * @param filename the filename of the new module
+ * @return a newly created module struct
+ */
+Module *createModule(char *filename)
+{
+ Module *m;
+ int i = 0;
+ if (!filename) {
+ return NULL;
+ }
+ if ((m = malloc(sizeof(Module))) == NULL) {
+ fatal("Out of memory!");
+ }
+
+ m->name = sstrdup(filename); /* Our Name */
+ m->handle = NULL; /* Handle */
+ m->version = NULL;
+ m->author = NULL;
+ m->nickHelp = NULL;
+ m->chanHelp = NULL;
+ m->memoHelp = NULL;
+ m->botHelp = NULL;
+ m->operHelp = NULL;
+ m->hostHelp = NULL;
+ m->helpHelp = NULL;
+
+ m->type = THIRD;
+ for (i = 0; i < NUM_LANGS; i++) {
+ m->lang[i].argc = 0;
+ }
+ return m; /* return a nice new module */
+}
+
+/**
+ * Destory the module.
+ * free up all memory used by our module struct.
+ * @param m the module to free
+ * @return MOD_ERR_OK on success, anything else on fail
+ */
+int destroyModule(Module * m)
+{
+ int i = 0;
+ if (!m) {
+ return MOD_ERR_PARAMS;
+ }
+
+ mod_current_module = m;
+ for (i = 0; i < NUM_LANGS; i++) {
+ moduleDeleteLanguage(i);
+ }
+
+ if (m->name) {
+ free(m->name);
+ }
+ if (m->filename) {
+ remove(m->filename);
+ free(m->filename);
+ }
+ m->handle = NULL;
+ if (m->author) {
+ free(m->author);
+ }
+ if (m->version) {
+ free(m->version);
+ }
+
+ /* No need to free our cmd/msg list, as they will always be empty by the module is destroyed */
+ free(m);
+ return MOD_ERR_OK;
+}
+
+/**
+ * Add the module to the list of currently loaded modules.
+ * @param m the currently loaded module
+ * @return MOD_ERR_OK on success, anything else on fail
+ */
+int addModule(Module * m)
+{
+ int index = 0;
+ ModuleHash *current = NULL;
+ ModuleHash *newHash = NULL;
+ ModuleHash *lastHash = NULL;
+
+ index = CMD_HASH(m->name);
+
+ for (current = MODULE_HASH[index]; current; current = current->next) {
+ if (stricmp(m->name, current->name) == 0)
+ return MOD_ERR_EXISTS;
+ lastHash = current;
+ }
+
+ if ((newHash = malloc(sizeof(ModuleHash))) == NULL) {
+ fatal("Out of memory");
+ }
+ m->time = time(NULL);
+ newHash->next = NULL;
+ newHash->name = sstrdup(m->name);
+ newHash->m = m;
+
+ if (lastHash == NULL)
+ MODULE_HASH[index] = newHash;
+ else
+ lastHash->next = newHash;
+ return MOD_ERR_OK;
+}
+
+/**
+ * Remove the module from the list of loaded modules.
+ * @param m module to remove
+ * @return MOD_ERR_OK on success anything else on fail
+ */
+int delModule(Module * m)
+{
+ int index = 0;
+ ModuleHash *current = NULL;
+ ModuleHash *lastHash = NULL;
+
+ if (!m) {
+ return MOD_ERR_PARAMS;
+ }
+
+ index = CMD_HASH(m->name);
+
+ for (current = MODULE_HASH[index]; current; current = current->next) {
+ if (stricmp(m->name, current->name) == 0) {
+ if (!lastHash) {
+ MODULE_HASH[index] = current->next;
+ } else {
+ lastHash->next = current->next;
+ }
+ destroyModule(current->m);
+ free(current->name);
+ free(current);
+ return MOD_ERR_OK;
+ }
+ lastHash = current;
+ }
+ return MOD_ERR_NOEXIST;
+}
+
+/**
+ * Search the list of loaded modules for the given name.
+ * @param name the name of the module to find
+ * @return a pointer to the module found, or NULL
+ */
+Module *findModule(char *name)
+{
+ int idx;
+ ModuleHash *current = NULL;
+ if (!name) {
+ return NULL;
+ }
+ idx = CMD_HASH(name);
+
+ for (current = MODULE_HASH[idx]; current; current = current->next) {
+ if (stricmp(name, current->name) == 0) {
+ return current->m;
+ }
+ }
+ return NULL;
+
+}
+
+/**
+ * Search all loaded modules looking for a protocol module.
+ * @return 1 if one is found.
+ **/
+int protocolModuleLoaded()
+{
+ int idx = 0;
+ ModuleHash *current = NULL;
+
+ for (idx = 0; idx != MAX_CMD_HASH; idx++) {
+ for (current = MODULE_HASH[idx]; current; current = current->next) {
+ if (current->m->type == PROTOCOL) {
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+/**
+ * Search all loaded modules looking for an encryption module.
+ * @ return 1 if one is loaded
+ **/
+int encryptionModuleLoaded()
+{
+ int idx = 0;
+ ModuleHash *current = NULL;
+
+ for (idx = 0; idx != MAX_CMD_HASH; idx++) {
+ for (current = MODULE_HASH[idx]; current; current = current->next) {
+ if (current->m->type == ENCRYPTION) {
+ return 1;
+ }
+ }
+ }
+ return 0;
+}
+
+/**
+ * Copy the module from the modules folder to the runtime folder.
+ * This will prevent module updates while the modules is loaded from
+ * triggering a segfault, as the actaul file in use will be in the
+ * runtime folder.
+ * @param name the name of the module to copy
+ * @param output the destination to copy the module to
+ * @return MOD_ERR_OK on success
+ */
+int moduleCopyFile(char *name, char *output)
+{
+#ifdef USE_MODULES
+ int ch;
+ FILE *source, *target;
+ int srcfp;
+ char input[4096];
+ int len;
+
+ strncpy(input, MODULE_PATH, 4095); /* Get full path with module extension */
+ len = strlen(input);
+ strncat(input, name, 4095 - len);
+ len = strlen(output);
+ strncat(input, MODULE_EXT, 4095 - len);
+
+#ifndef _WIN32
+ if ((srcfp = mkstemp(output)) == -1)
+ return MOD_ERR_FILE_IO;
+#else
+ if (!mktemp(output))
+ return MOD_ERR_FILE_IO;
+#endif
+
+ if (debug)
+ alog("Runtime module location: %s", output);
+
+ /* Linux/UNIX should ignore the b param, why do we still have seperate
+ * calls for it here? -GD
+ */
+#ifndef _WIN32
+ if ((source = fopen(input, "r")) == NULL) {
+#else
+ if ((source = fopen(input, "rb")) == NULL) {
+#endif
+ return MOD_ERR_NOEXIST;
+ }
+#ifndef _WIN32
+ if ((target = fdopen(srcfp, "w")) == NULL) {
+#else
+ if ((target = fopen(output, "wb")) == NULL) {
+#endif
+ return MOD_ERR_FILE_IO;
+ }
+ while ((ch = fgetc(source)) != EOF) {
+ fputc(ch, target);
+ }
+ fclose(source);
+ if (fclose(target) != 0) {
+ return MOD_ERR_FILE_IO;
+ }
+#endif
+ return MOD_ERR_OK;
+}
+
+/**
+ * Loads a given module.
+ * @param m the module to load
+ * @param u the user who loaded it, NULL for auto-load
+ * @return MOD_ERR_OK on success, anything else on fail
+ */
+int loadModule(Module * m, User * u)
+{
+#ifdef USE_MODULES
+ char buf[4096];
+ int len;
+ const char *err;
+ int (*func) (int, char **);
+ int (*version)();
+ int ret = 0;
+ char *argv[1];
+ int argc = 0;
+
+ Module *m2;
+ if (!m || !m->name) {
+ return MOD_ERR_PARAMS;
+ }
+ if (m->handle) {
+ return MOD_ERR_EXISTS;
+ }
+ if ((m2 = findModule(m->name)) != NULL) {
+ return MOD_ERR_EXISTS;
+ }
+
+ /* Generate the filename for the temporary copy of the module */
+ strncpy(buf, MODULE_PATH, 4095); /* Get full path with module extension */
+ len = strlen(buf);
+#ifndef _WIN32
+ strncat(buf, "runtime/", 4095 - len);
+#else
+ strncat(buf, "runtime\\", 4095 - len);
+#endif
+ len = strlen(buf);
+ strncat(buf, m->name, 4095 - len);
+ len = strlen(buf);
+ strncat(buf, MODULE_EXT, 4095 - len);
+ len = strlen(buf);
+ strncat(buf, ".", 4095 - len);
+ len = strlen(buf);
+ strncat(buf, "XXXXXX", 4095 - len);
+ buf[4095] = '\0';
+ /* Don't skip return value checking! -GD */
+ if ((ret = moduleCopyFile(m->name, buf)) != MOD_ERR_OK) {
+ m->filename = sstrdup(buf);
+ return ret;
+ }
+
+ m->filename = sstrdup(buf);
+ ano_modclearerr();
+ m->handle = ano_modopen(m->filename);
+ if ( m->handle == NULL && (err = ano_moderr()) != NULL) {
+ alog(err);
+ return MOD_ERR_NOLOAD;
+ }
+ ano_modclearerr();
+ func = (int (*)(int, char **))ano_modsym(m->handle, "AnopeInit");
+ if ( func == NULL && (err = ano_moderr()) != NULL) {
+ ano_modclose(m->handle); /* If no AnopeInit - it isnt an Anope Module, close it */
+ return MOD_ERR_NOLOAD;
+ }
+ if (func) {
+ version = (int (*)())ano_modsym(m->handle,"getAnopeBuildVersion");
+ if (version) {
+ if (version() >= VERSION_BUILD ) {
+ if(debug) {
+ alog("Module %s compiled against current or newer anope revision %d, this is %d",m->name,version(),VERSION_BUILD);
+ }
+ } else {
+ alog("Module %s is compiled against an old version of anope (%d) current is %d", m->name, version(), VERSION_BUILD);
+ alog("Rebuild module %s against the current version to resolve this error", m->name);
+ ano_modclose(m->handle);
+ ano_modclearerr();
+ return MOD_ERR_NOLOAD;
+ }
+ } else {
+ ano_modclose(m->handle);
+ ano_modclearerr();
+ alog("Module %s is compiled against an older version of anope (unknown)", m->name);
+ alog("Rebuild module %s against the current version to resolve this error", m->name);
+ return MOD_ERR_NOLOAD;
+ }
+ /* TODO */
+ mod_current_module_name = m->name;
+ /* argv[0] is the user if there was one, or NULL if not */
+ if (u) {
+ argv[0] = sstrdup(u->nick);
+ } else {
+ argv[0] = NULL;
+ }
+ argc++;
+
+ ret = func(argc, argv); /* exec AnopeInit */
+ if (u) {
+ free(argv[0]);
+ }
+ if (m->type == PROTOCOL && protocolModuleLoaded()) {
+ alog("You cannot load two protocol modules");
+ ret = MOD_STOP;
+ } else if (m->type == ENCRYPTION && encryptionModuleLoaded()) {
+ alog("You cannot load two encryption modules");
+ ret = MOD_STOP;
+ }
+ if (ret == MOD_STOP) {
+ alog("%s requested unload...", m->name);
+ unloadModule(m, NULL);
+ mod_current_module_name = NULL;
+ return MOD_ERR_NOLOAD;
+ }
+
+ mod_current_module_name = NULL;
+ }
+
+ if (u) {
+ anope_cmd_global(s_OperServ, "%s loaded module %s", u->nick,
+ m->name);
+ notice_lang(s_OperServ, u, OPER_MODULE_LOADED, m->name);
+ }
+ addModule(m);
+ return MOD_ERR_OK;
+
+#else
+ return MOD_ERR_NOLOAD;
+#endif
+}
+
+/**
+ * Unload the given module.
+ * @param m the module to unload
+ * @param u the user who unloaded it
+ * @return MOD_ERR_OK on success, anything else on fail
+ */
+int unloadModule(Module * m, User * u)
+{
+#ifdef USE_MODULES
+ void (*func) (void);
+
+ if (!m || !m->handle) {
+ if (u) {
+ notice_lang(s_OperServ, u, OPER_MODULE_REMOVE_FAIL, m->name);
+ }
+ return MOD_ERR_PARAMS;
+ }
+
+ if (m->type == PROTOCOL) {
+ if (u) {
+ notice_lang(s_OperServ, u, OPER_MODULE_NO_UNLOAD);
+ }
+ return MOD_ERR_NOUNLOAD;
+ } else if(m->type == ENCRYPTION) {
+ if (u) {
+ notice_lang(s_OperServ, u, OPER_MODULE_NO_UNLOAD);
+ }
+ return MOD_ERR_NOUNLOAD;
+ }
+
+ func = (void (*)(void))ano_modsym(m->handle, "AnopeFini");
+ if (func) {
+ mod_current_module_name = m->name;
+ func(); /* exec AnopeFini */
+ mod_current_module_name = NULL;
+ }
+
+ if (prepForUnload(m) != MOD_ERR_OK) {
+ return MOD_ERR_UNKNOWN;
+ }
+
+ if ((ano_modclose(m->handle)) != 0) {
+ alog(ano_moderr());
+ if (u) {
+ notice_lang(s_OperServ, u, OPER_MODULE_REMOVE_FAIL, m->name);
+ }
+ return MOD_ERR_NOUNLOAD;
+ } else {
+ if (u) {
+ anope_cmd_global(s_OperServ, "%s unloaded module %s", u->nick,
+ m->name);
+ notice_lang(s_OperServ, u, OPER_MODULE_UNLOADED, m->name);
+ }
+ delModule(m);
+ return MOD_ERR_OK;
+ }
+#else
+ return MOD_ERR_NOUNLOAD;
+#endif
+}
+
+/**
+ * Module setType()
+ * Lets the module set a type, CORE,PROTOCOL,3RD etc..
+ **/
+void moduleSetType(MODType type)
+{
+ if ((mod_current_module_name) && (!mod_current_module)) {
+ mod_current_module = findModule(mod_current_module_name);
+ }
+ mod_current_module->type = type;
+}
+
+/**
+ * Prepare a module to be unloaded.
+ * Remove all commands and messages this module is providing, and delete
+ * any callbacks which are still pending.
+ * @param m the module to prepare for unload
+ * @return MOD_ERR_OK on success
+ */
+int prepForUnload(Module * m)
+{
+ int idx;
+ CommandHash *current = NULL;
+ MessageHash *mcurrent = NULL;
+ EvtMessageHash *ecurrent = NULL;
+ EvtHookHash *ehcurrent = NULL;
+
+ Command *c;
+ Message *msg;
+ EvtMessage *eMsg;
+ EvtHook *eHook;
+ int status = 0;
+
+ if (!m) {
+ return MOD_ERR_PARAMS;
+ }
+
+ /* Kill any active callbacks this module has */
+ moduleCallBackPrepForUnload(m->name);
+
+ /* Remove any stored data this module has */
+ moduleDelAllDataMod(m);
+
+ /**
+ * ok, im going to walk every hash looking for commands we own, now, not exactly elegant or efficiant :)
+ **/
+ for (idx = 0; idx < MAX_CMD_HASH; idx++) {
+ for (current = HS_cmdTable[idx]; current; current = current->next) {
+ for (c = current->c; c; c = c->next) {
+ if ((c->mod_name) && (strcmp(c->mod_name, m->name) == 0)) {
+ moduleDelCommand(HOSTSERV, c->name);
+ }
+ }
+ }
+
+ for (current = BS_cmdTable[idx]; current; current = current->next) {
+ for (c = current->c; c; c = c->next) {
+ if ((c->mod_name) && (strcmp(c->mod_name, m->name) == 0)) {
+ moduleDelCommand(BOTSERV, c->name);
+ }
+ }
+ }
+
+ for (current = MS_cmdTable[idx]; current; current = current->next) {
+ for (c = current->c; c; c = c->next) {
+ if ((c->mod_name) && (strcmp(c->mod_name, m->name) == 0)) {
+ moduleDelCommand(MEMOSERV, c->name);
+ }
+ }
+ }
+
+ for (current = NS_cmdTable[idx]; current; current = current->next) {
+ for (c = current->c; c; c = c->next) {
+ if ((c->mod_name) && (strcmp(c->mod_name, m->name) == 0)) {
+ moduleDelCommand(NICKSERV, c->name);
+ }
+ }
+ }
+
+ for (current = CS_cmdTable[idx]; current; current = current->next) {
+ for (c = current->c; c; c = c->next) {
+ if ((c->mod_name) && (strcmp(c->mod_name, m->name) == 0)) {
+ moduleDelCommand(CHANSERV, c->name);
+ }
+ }
+ }
+
+ for (current = HE_cmdTable[idx]; current; current = current->next) {
+ for (c = current->c; c; c = c->next) {
+ if ((c->mod_name) && (strcmp(c->mod_name, m->name) == 0)) {
+ moduleDelCommand(HELPSERV, c->name);
+ }
+ }
+ }
+
+ for (current = OS_cmdTable[idx]; current; current = current->next) {
+ for (c = current->c; c; c = c->next) {
+ if ((c->mod_name) && (stricmp(c->mod_name, m->name) == 0)) {
+ moduleDelCommand(OPERSERV, c->name);
+ }
+ }
+ }
+
+ for (mcurrent = IRCD[idx]; mcurrent; mcurrent = mcurrent->next) {
+ for (msg = mcurrent->m; msg; msg = msg->next) {
+ if ((msg->mod_name)
+ && (stricmp(msg->mod_name, m->name) == 0)) {
+ moduleDelMessage(msg->name);
+ }
+ }
+ }
+
+ for (ecurrent = EVENT[idx]; ecurrent; ecurrent = ecurrent->next) {
+ for (eMsg = ecurrent->evm; eMsg; eMsg = eMsg->next) {
+ if ((eMsg->mod_name)
+ && (stricmp(eMsg->mod_name, m->name) == 0)) {
+ status = delEventHandler(EVENT, eMsg, m->name);
+ }
+ }
+ }
+ for (ehcurrent = EVENTHOOKS[idx]; ehcurrent;
+ ehcurrent = ehcurrent->next) {
+ for (eHook = ehcurrent->evh; eHook; eHook = eHook->next) {
+ if ((eHook->mod_name)
+ && (stricmp(eHook->mod_name, m->name) == 0)) {
+ status = delEventHook(EVENTHOOKS, eHook, m->name);
+ }
+ }
+ }
+
+ }
+ return MOD_ERR_OK;
+}
+
+/*******************************************************************************
+ * Command Functions
+ *******************************************************************************/
+/**
+ * Create a Command struct ready for use in anope.
+ * @param name the name of the command
+ * @param func pointer to the function to execute when command is given
+ * @param has_priv pointer to function to check user priv's
+ * @param help_all help file index for all users
+ * @param help_reg help file index for all regustered users
+ * @param help_oper help file index for all opers
+ * @param help_admin help file index for all admins
+ * @param help_root help file indenx for all services roots
+ * @return a "ready to use" Command struct will be returned
+ */
+Command *createCommand(const char *name, int (*func) (User * u),
+ int (*has_priv) (User * u), int help_all,
+ int help_reg, int help_oper, int help_admin,
+ int help_root)
+{
+ Command *c;
+ if (!name || !*name) {
+ return NULL;
+ }
+
+ if ((c = malloc(sizeof(Command))) == NULL) {
+ fatal("Out of memory!");
+ }
+ c->name = sstrdup(name);
+ c->routine = func;
+ c->has_priv = has_priv;
+ c->helpmsg_all = help_all;
+ c->helpmsg_reg = help_reg;
+ c->helpmsg_oper = help_oper;
+ c->helpmsg_admin = help_admin;
+ c->helpmsg_root = help_root;
+ c->help_param1 = NULL;
+ c->help_param2 = NULL;
+ c->help_param3 = NULL;
+ c->help_param4 = NULL;
+ c->next = NULL;
+ c->mod_name = NULL;
+ c->service = NULL;
+ c->all_help = NULL;
+ c->regular_help = NULL;
+ c->oper_help = NULL;
+ c->admin_help = NULL;
+ c->root_help = NULL;
+ return c;
+}
+
+/**
+ * Destroy a command struct freeing any memory.
+ * @param c Command to destroy
+ * @return MOD_ERR_OK on success, anything else on fail
+ */
+int destroyCommand(Command * c)
+{
+ if (!c) {
+ return MOD_ERR_PARAMS;
+ }
+ if (c->core == 1) {
+ return MOD_ERR_UNKNOWN;
+ }
+ if (c->name) {
+ free(c->name);
+ }
+ c->routine = NULL;
+ c->has_priv = NULL;
+ c->helpmsg_all = -1;
+ c->helpmsg_reg = -1;
+ c->helpmsg_oper = -1;
+ c->helpmsg_admin = -1;
+ c->helpmsg_root = -1;
+ if (c->help_param1) {
+ free(c->help_param1);
+ }
+ if (c->help_param2) {
+ free(c->help_param2);
+ }
+ if (c->help_param3) {
+ free(c->help_param3);
+ }
+ if (c->help_param4) {
+ free(c->help_param4);
+ }
+ if (c->mod_name) {
+ free(c->mod_name);
+ }
+ if (c->service) {
+ free(c->service);
+ }
+ c->next = NULL;
+ free(c);
+ return MOD_ERR_OK;
+}
+
+/**
+ * Add a CORE command ot the given command hash
+ * @param cmdTable the command table to add the command to
+ * @param c the command to add
+ * @return MOD_ERR_OK on success
+ */
+int addCoreCommand(CommandHash * cmdTable[], Command * c)
+{
+ if (!cmdTable || !c) {
+ return MOD_ERR_PARAMS;
+ }
+ c->core = 1;
+ c->next = NULL;
+ return addCommand(cmdTable, c, 0);
+}
+
+/**
+ * Add a module provided command to the given service.
+ * e.g. moduleAddCommand(NICKSERV,c,MOD_HEAD);
+ * @param cmdTable the services to add the command to
+ * @param c the command to add
+ * @param pos the position to add to, MOD_HEAD, MOD_TAIL, MOD_UNIQUE
+ * @see createCommand
+ * @return MOD_ERR_OK on successfully adding the command
+ */
+int moduleAddCommand(CommandHash * cmdTable[], Command * c, int pos)
+{
+ int status;
+
+ if (!cmdTable || !c) {
+ return MOD_ERR_PARAMS;
+ }
+ /* ok, this appears to be a module adding a command from outside of AnopeInit, try to look up its module struct for it */
+ if ((mod_current_module_name) && (!mod_current_module)) {
+ mod_current_module = findModule(mod_current_module_name);
+ }
+
+ if (!mod_current_module) {
+ return MOD_ERR_UNKNOWN;
+ } /* shouldnt happen */
+ c->core = 0;
+ if (!c->mod_name) {
+ c->mod_name = sstrdup(mod_current_module->name);
+ }
+
+
+ if (cmdTable == HOSTSERV) {
+ if (s_HostServ) {
+ c->service = sstrdup(s_HostServ);
+ } else {
+ return MOD_ERR_NOSERVICE;
+ }
+ } else if (cmdTable == BOTSERV) {
+ if (s_BotServ) {
+ c->service = sstrdup(s_BotServ);
+ } else {
+ return MOD_ERR_NOSERVICE;
+ }
+ } else if (cmdTable == MEMOSERV) {
+ if (s_MemoServ) {
+ c->service = sstrdup(s_MemoServ);
+ } else {
+ return MOD_ERR_NOSERVICE;
+ }
+ } else if (cmdTable == CHANSERV) {
+ if (s_ChanServ) {
+ c->service = sstrdup(s_ChanServ);
+ } else {
+ return MOD_ERR_NOSERVICE;
+ }
+ } else if (cmdTable == NICKSERV) {
+ if (s_NickServ) {
+ c->service = sstrdup(s_NickServ);
+ } else {
+ return MOD_ERR_NOSERVICE;
+ }
+ } else if (cmdTable == HELPSERV) {
+ if (s_HelpServ) {
+ c->service = sstrdup(s_HelpServ);
+ } else {
+ return MOD_ERR_NOSERVICE;
+ }
+ } else if (cmdTable == OPERSERV) {
+ if (s_OperServ) {
+ c->service = sstrdup(s_OperServ);
+ } else {
+ return MOD_ERR_NOSERVICE;
+ }
+ } else
+ c->service = sstrdup("Unknown");
+
+ if (debug >= 2)
+ displayCommandFromHash(cmdTable, c->name);
+ status = addCommand(cmdTable, c, pos);
+ if (debug >= 2)
+ displayCommandFromHash(cmdTable, c->name);
+ if (status != MOD_ERR_OK) {
+ alog("ERROR! [%d]", status);
+ }
+ return status;
+}
+
+/**
+ * Delete a command from the service given.
+ * @param cmdTable the cmdTable for the services to remove the command from
+ * @param name the name of the command to delete from the service
+ * @return returns MOD_ERR_OK on success
+ */
+int moduleDelCommand(CommandHash * cmdTable[], char *name)
+{
+ Command *c = NULL;
+ Command *cmd = NULL;
+ int status = 0;
+
+ if (!mod_current_module) {
+ return MOD_ERR_UNKNOWN;
+ }
+
+ c = findCommand(cmdTable, name);
+ if (!c) {
+ return MOD_ERR_NOEXIST;
+ }
+
+
+ for (cmd = c; cmd; cmd = cmd->next) {
+ if (cmd->mod_name
+ && stricmp(cmd->mod_name, mod_current_module->name) == 0) {
+ if (debug >= 2) {
+ displayCommandFromHash(cmdTable, name);
+ }
+ status = delCommand(cmdTable, cmd, mod_current_module->name);
+ if (debug >= 2) {
+ displayCommandFromHash(cmdTable, name);
+ }
+ }
+ }
+ return status;
+}
+
+/**
+ * Output the command stack into the log files.
+ * This will print the call-stack for a given command into the log files, very useful for debugging.
+ * @param cmdTable the command table to read from
+ * @param name the name of the command to print
+ * @return 0 is ret