modules.h

Go to the documentation of this file.
00001 /* Modular support
00002  *
00003  * (C) 2003-2013 Anope Team
00004  * Contact us at team@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church. 
00010  * 
00011  */
00012 
00013 #ifndef MODULES_H
00014 #define MODULES_H
00015 
00016 #include <time.h>
00017 #include "services.h"
00018 #include <stdio.h>
00019 
00020 /* Cross OS compatibility macros */
00021 #ifdef _WIN32
00022 typedef HMODULE ano_module_t;
00023 
00024 #define ano_modopen(file)               LoadLibrary(file)
00025 /* ano_moderr in modules.c */
00026 #define ano_modsym(file, symbol)        (void *)GetProcAddress(file, symbol)
00027 #define ano_modclose(file)              FreeLibrary(file) ? 0 : 1
00028 #define ano_modclearerr()               SetLastError(0)
00029 #define MODULE_EXT                      ".dll"
00030 
00031 #else
00032 typedef void *  ano_module_t;
00033 
00034 #define ano_modopen(file)               dlopen(file, RTLD_LAZY)
00035 #define ano_moderr()                    dlerror()
00036 #define ano_modsym(file, symbol)        dlsym(file, DL_PREFIX symbol)
00037 #define ano_modclose(file)              dlclose(file)
00038 /* We call dlerror() here because it clears the module error after being
00039  * called. This previously read 'errno = 0', but that didn't work on
00040  * all POSIX-compliant architectures. This way the error is guaranteed
00041  * to be cleared, POSIX-wise. -GD
00042  */
00043 #define ano_modclearerr()               dlerror()
00044 #define MODULE_EXT                      ".so"
00045 
00046 #endif
00047 
00048 
00049 /*************************************************************************/
00050 #define CMD_HASH(x)      (((x)[0]&31)<<5 | ((x)[1]&31)) /* Will gen a hash from a string :) */
00051 #define MAX_CMD_HASH 1024
00052 
00053 #define MOD_STOP 1
00054 #define MOD_CONT 0
00055 
00056 #define HOSTSERV HS_cmdTable /* using HOSTSERV etc. looks nicer than HS_cmdTable for modules */
00057 #define BOTSERV BS_cmdTable
00058 #define MEMOSERV MS_cmdTable
00059 #define NICKSERV NS_cmdTable
00060 #define CHANSERV CS_cmdTable
00061 #define HELPSERV HE_cmdTable
00062 #define OPERSERV OS_cmdTable
00063 #define IRCD IRCD_cmdTable
00064 #define MODULE_HASH Module_table
00065 #define EVENT EVENT_cmdTable
00066 #define EVENTHOOKS HOOK_cmdTable
00067 
00068 /**********************************************************************
00069  * Module Returns
00070  **********************************************************************/
00071  #define MOD_ERR_OK          0
00072  #define MOD_ERR_MEMORY      1
00073  #define MOD_ERR_PARAMS      2
00074  #define MOD_ERR_EXISTS      3
00075  #define MOD_ERR_NOEXIST     4
00076  #define MOD_ERR_NOUSER      5
00077  #define MOD_ERR_NOLOAD      6
00078  #define MOD_ERR_NOUNLOAD    7
00079  #define MOD_ERR_SYNTAX      8
00080  #define MOD_ERR_NODELETE    9
00081  #define MOD_ERR_UNKNOWN     10
00082  #define MOD_ERR_FILE_IO     11
00083  #define MOD_ERR_NOSERVICE   12
00084  #define MOD_ERR_NO_MOD_NAME 13
00085 
00086 /*************************************************************************/
00087 /* Macros to export the Module API functions/variables */
00088 #ifndef _WIN32
00089 #define MDE
00090 #else
00091 #ifndef MODULE_COMPILE
00092 #define MDE __declspec(dllexport)
00093 #else
00094 #define MDE __declspec(dllimport)
00095 #endif
00096 #endif
00097 /*************************************************************************/
00098 
00099 typedef enum { CORE,PROTOCOL,THIRD,SUPPORTED,QATESTED,ENCRYPTION } MODType;
00100 typedef enum { MOD_OP_LOAD, MOD_OP_UNLOAD } ModuleOperation;
00101 
00102 /*************************************************************************/
00103 /* Structure for information about a *Serv command. */
00104 
00105 typedef struct Command_ Command;
00106 typedef struct CommandHash_ CommandHash;
00107 typedef struct Module_ Module;
00108 typedef struct ModuleLang_ ModuleLang;
00109 typedef struct ModuleHash_ ModuleHash;
00110 typedef struct ModuleQueue_ ModuleQueue;
00111 typedef struct Message_ Message;
00112 typedef struct MessageHash_ MessageHash;
00113 typedef struct ModuleCallBack_ ModuleCallBack;
00114 typedef struct EvtMessage_ EvtMessage;
00115 typedef struct EvtMessageHash_ EvtMessageHash;
00116 typedef struct EvtHook_ EvtHook;
00117 typedef struct EvtHookHash_ EvtHookHash;
00118 
00119 extern MDE CommandHash *HOSTSERV[MAX_CMD_HASH];
00120 extern MDE CommandHash  *BOTSERV[MAX_CMD_HASH];
00121 extern MDE CommandHash *MEMOSERV[MAX_CMD_HASH];
00122 extern MDE CommandHash *NICKSERV[MAX_CMD_HASH];
00123 extern MDE CommandHash *CHANSERV[MAX_CMD_HASH];
00124 extern MDE CommandHash *HELPSERV[MAX_CMD_HASH];
00125 extern MDE CommandHash *OPERSERV[MAX_CMD_HASH];
00126 extern MDE MessageHash *IRCD[MAX_CMD_HASH];
00127 extern MDE ModuleHash *MODULE_HASH[MAX_CMD_HASH];
00128 extern MDE EvtMessageHash *EVENT[MAX_CMD_HASH];
00129 extern MDE EvtHookHash *EVENTHOOKS[MAX_CMD_HASH];
00130 
00131 struct ModuleLang_ {
00132     int argc;
00133     char **argv;
00134 };
00135 
00136 struct Module_ {
00137         char *name;
00138         char *filename;
00139         void *handle;
00140         time_t time;
00141         char *version;
00142         char *author;
00143 
00144         MODType type;
00145 
00146         void (*nickHelp)(User *u); /* service 1 */
00147         void (*chanHelp)(User *u); /* 2 */
00148         void (*memoHelp)(User *u); /* 3 */
00149         void (*botHelp)(User *u); /* 4 */
00150         void (*operHelp)(User *u); /* 5 */
00151         void (*hostHelp)(User *u); /* 6 */
00152         void (*helpHelp)(User *u); /* 7 */
00153 
00154 /*      CommandHash *cmdList[MAX_CMD_HASH]; */
00155         MessageHash *msgList[MAX_CMD_HASH];
00156         ModuleLang lang[NUM_LANGS];
00157 };
00158 
00159 struct ModuleHash_ {
00160         char *name;
00161         Module *m;
00162         ModuleHash *next;
00163 };
00164 
00165 struct ModuleQueue_ {
00166         Module *m;
00167         ModuleOperation op;
00168         User *u;
00169         
00170         ModuleQueue *next;
00171 };
00172 
00173 struct Command_ {
00174     char *name;
00175     int (*routine)(User *u);
00176     int (*has_priv)(User *u);   /* Returns 1 if user may use command, else 0 */
00177 
00178     /* Regrettably, these are hard-coded to correspond to current privilege
00179      * levels (v4.0).  Suggestions for better ways to do this are
00180      * appreciated.
00181      */
00182     int helpmsg_all;    /* Displayed to all users; -1 = no message */
00183     int helpmsg_reg;    /* Displayed to regular users only */
00184     int helpmsg_oper;   /* Displayed to Services operators only */
00185     int helpmsg_admin;  /* Displayed to Services admins only */
00186     int helpmsg_root;   /* Displayed to Services root only */
00187     char *help_param1;
00188     char *help_param2;
00189     char *help_param3;
00190     char *help_param4;
00191 
00192     /* Module related stuff */
00193     int core;           /* Can this command be deleted? */
00194     char *mod_name;     /* Name of the module who owns us, NULL for core's  */
00195     char *service;      /* Service we provide this command for */
00196     int (*all_help)(User *u);
00197     int (*regular_help)(User *u);
00198     int (*oper_help)(User *u);
00199     int (*admin_help)(User *u);
00200     int (*root_help)(User *u);
00201 
00202     Command *next;      /* Next command responsible for the same command */
00203 };
00204 
00205 struct CommandHash_ {
00206         char *name;     /* Name of the command */
00207         Command *c;     /* Actual command */
00208         CommandHash *next; /* Next command */
00209 };
00210 
00211 struct Message_ {
00212     char *name;
00213     int (*func)(char *source, int ac, char **av);
00214     int core;
00215     char *mod_name;
00216     Message *next;
00217 };
00218 
00219 struct MessageHash_ {
00220         char *name;
00221         Message *m;
00222         MessageHash *next;
00223 };
00224 
00225 struct ModuleCallBack_ {
00226         char *name;
00227         char *owner_name;
00228         time_t when;
00229         int (*func)(int argc, char *argv[]);
00230         int argc;
00231         char **argv;
00232         ModuleCallBack *next;
00233 };
00234 
00235 struct EvtMessage_ {
00236     char *name;
00237     int (*func)(char *source, int ac, char **av);
00238     int core;
00239     char *mod_name;
00240     EvtMessage *next;
00241 };
00242 
00243 struct EvtMessageHash_ {
00244         char *name;
00245         EvtMessage *evm;
00246         EvtMessageHash *next;
00247 };
00248 
00249 
00250 struct EvtHook_ {
00251     int (*func)(int argc, char **argv);
00252     int core;
00253         char *name;
00254     char *mod_name;
00255     EvtHook *next;
00256 };
00257 
00258 struct EvtHookHash_ {
00259         char *name;
00260         EvtHook *evh;
00261         EvtHookHash *next;
00262 };
00263 
00264 
00265 /*************************************************************************/
00266 /* Module Managment Functions */
00267 MDE Module *createModule(char *filename);        /* Create a new module, using the given name */
00268 int destroyModule(Module *m);           /* Delete the module */
00269 int addModule(Module *m);               /* Add a module to the module hash */
00270 int delModule(Module *m);               /* Remove a module from the module hash */
00271 MDE Module *findModule(char *name);                /* Find a module */
00272 int loadModule(Module *m,User *u);      /* Load the given module into the program */
00273 int encryption_module_init(void); /* Load the encryption module */
00274 int protocol_module_init(void); /* Load the IRCD Protocol Module up*/
00275 int unloadModule(Module *m, User *u);   /* Unload the given module from the pro */
00276 int prepForUnload(Module *m);           /* Prepare the module for unload */
00277 MDE void moduleAddVersion(const char *version);
00278 MDE void moduleAddAuthor(const char *author);
00279 void modules_init(void);
00280 void modules_delayed_init(void);
00281 void moduleCallBackPrepForUnload(char *mod_name);
00282 MDE void moduleCallBackDeleteEntry(ModuleCallBack * prev);
00283 MDE char *moduleGetLastBuffer(void);
00284 MDE void moduleSetHelpHelp(void (*func) (User * u));
00285 MDE void moduleDisplayHelp(int service, User *u);
00286 MDE void moduleSetHostHelp(void (*func) (User * u));
00287 MDE void moduleSetOperHelp(void (*func) (User * u));
00288 MDE void moduleSetBotHelp(void (*func) (User * u));
00289 MDE void moduleSetMemoHelp(void (*func) (User * u));
00290 MDE void moduleSetChanHelp(void (*func) (User * u));
00291 MDE void moduleSetNickHelp(void (*func) (User * u));
00292 MDE int moduleAddHelp(Command * c, int (*func) (User * u));
00293 MDE int moduleAddRegHelp(Command * c, int (*func) (User * u));
00294 MDE int moduleAddOperHelp(Command * c, int (*func) (User * u));
00295 MDE int moduleAddAdminHelp(Command * c, int (*func) (User * u));
00296 MDE int moduleAddRootHelp(Command * c, int (*func) (User * u));
00297 MDE void moduleSetType(MODType type);
00298 extern MDE Module *mod_current_module;
00299 extern MDE char *mod_current_module_name;
00300 extern MDE char *mod_current_buffer;
00301 extern MDE int mod_current_op;
00302 extern MDE User *mod_current_user;
00303 
00304 MDE int moduleGetConfigDirective(Directive *h);
00305 /*************************************************************************/
00306 /*************************************************************************/
00307 /* Command Managment Functions */
00308 MDE 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);
00309 MDE int destroyCommand(Command *c);                                     /* destroy a command */
00310 MDE int addCoreCommand(CommandHash *cmdTable[], Command *c);    /* Add a command to a command table */
00311 MDE int moduleAddCommand(CommandHash *cmdTable[], Command *c, int pos);
00312 MDE int addCommand(CommandHash *cmdTable[], Command *c,int pos);
00313 MDE int delCommand(CommandHash *cmdTable[], Command *c,char *mod_name);         /* Del a command from a cmd table */
00314 MDE int moduleDelCommand(CommandHash *cmdTable[],char *name);           /* Del a command from a cmd table */
00315 MDE Command *findCommand(CommandHash *cmdTable[], const char *name);    /* Find a command */
00316 
00317 /*************************************************************************/
00318 
00319 /* Message Managment Functions */
00320 MDE Message *createMessage(const char *name,int (*func)(char *source, int ac, char **av));
00321 Message *findMessage(MessageHash *msgTable[], const char *name);        /* Find a Message */
00322 MDE int addMessage(MessageHash *msgTable[], Message *m, int pos);               /* Add a Message to a Message table */
00323 MDE int addCoreMessage(MessageHash *msgTable[], Message *m);            /* Add a Message to a Message table */
00324 MDE int moduleAddMessage(Message *m, int pos);
00325 int delMessage(MessageHash *msgTable[], Message *m, char *mod_name);            /* Del a Message from a msg table */
00326 MDE int moduleDelMessage(char *name);
00327 int destroyMessage(Message *m);                                 /* destroy a Message*/
00328 
00329 /*************************************************************************/
00330 
00331 MDE EvtMessage *createEventHandler(char *name, int (*func) (char *source, int ac, char **av));
00332 EvtMessage *findEventHandler(EvtMessageHash * msgEvtTable[], const char *name);
00333 int addCoreEventHandler(EvtMessageHash * msgEvtTable[], EvtMessage * evm);
00334 MDE int moduleAddEventHandler(EvtMessage * evm);
00335 MDE int moduleEventDelHandler(char *name);
00336 int delEventHandler(EvtMessageHash * msgEvtTable[], EvtMessage * evm, char *mod_name);
00337 int destroyEventHandler(EvtMessage * evm);
00338 int addEventHandler(EvtMessageHash * msgEvtTable[], EvtMessage * evm);
00339 
00340 MDE EvtHook *createEventHook(char *name, int (*func) (int argc, char **argv));
00341 EvtHook *findEventHook(EvtHookHash * HookEvtTable[], const char *name);
00342 int addCoreEventHook(EvtHookHash * HookEvtTable[], EvtHook * evh);
00343 MDE int moduleAddEventHook(EvtHook * evh);
00344 MDE int moduleEventDelHook(const char *name);
00345 int delEventHook(EvtHookHash * HookEvtTable[], EvtHook * evh, char *mod_name);
00346 int destroyEventHook(EvtHook * evh);
00347 extern char *mod_current_evtbuffer;
00348 
00349 MDE void moduleInsertLanguage(int langNumber, int ac, char **av);
00350 MDE void moduleNoticeLang(char *source, User *u, int number, ...);
00351 MDE char *moduleGetLangString(User * u, int number);
00352 MDE void moduleDeleteLanguage(int langNumber);
00353 
00354 /*************************************************************************/
00355 
00356 MDE int moduleAddCallback(char *name,time_t when,int (*func)(int argc, char *argv[]),int argc, char **argv);
00357 MDE void moduleDelCallback(char *name);
00358 
00359 MDE char *moduleGetData(ModuleData **md, char *key);                    /* Get the value for this key from this struct */
00360 MDE int moduleAddData(ModuleData **md, char *key, char *value);         /* Set the value for this key for this struct */
00361 MDE void moduleDelData(ModuleData **md, char *key);                             /* Delete this key/value pair */
00362 MDE void moduleDelAllData(ModuleData **md);                                     /* Delete all key/value pairs for this module for this struct */
00363 void moduleDelAllDataMod(Module *m);                                    /* remove all module data from all structs for this module */
00364 int moduleDataDebug(ModuleData **md);                                   /* Allow for debug output of a moduleData struct */
00365 MDE boolean moduleMinVersion(int major,int minor,int patch,int build);  /* Checks if the current version of anope is before or after a given verison */
00366 
00367 /*************************************************************************/
00368 /* Module Queue Operations */
00369 MDE int queueModuleLoad(char *name, User *u);
00370 MDE int queueModuleUnload(char *name, User *u);
00371 MDE void handleModuleOperationQueue(void);
00372 
00373 /*************************************************************************/
00374 /* Some IRCD protocol module support functions */
00375 
00377 MDE void updateProtectDetails(char *level_info_protect_word, char *level_info_protectme_word, char *fant_protect_add, char *fant_protect_del, char *level_protect_word, char *protect_set_mode, char *protect_unset_mode);
00378 
00379 /************************************************************************/
00380 
00381 #endif
00382 /* EOF */