1 /*******************************************************
3 * Citadel Dynamic Loading Module
4 * Written by Brian Costello
9 ******************************************************/
16 #include <sys/types.h>
24 #include "dynloader.h"
27 #include "sysdep_decls.h"
34 struct LogFunctionHook *LogHookTable = NULL;
35 struct CleanupFunctionHook *CleanupHookTable = NULL;
36 struct SessionFunctionHook *SessionHookTable = NULL;
37 struct UserFunctionHook *UserHookTable = NULL;
39 struct ProtoFunctionHook
41 void (*handler)(char *cmdbuf);
44 struct ProtoFunctionHook *next;
45 } *ProtoHookList = NULL;
47 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc)
49 struct ProtoFunctionHook *p = mallok(sizeof *p);
53 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
60 p->next = ProtoHookList;
64 int DLoader_Exec_Cmd(char *cmdbuf)
66 struct ProtoFunctionHook *p;
68 for (p = ProtoHookList; p; p = p->next)
70 if (!strncasecmp(cmdbuf, p->cmd, 4))
72 p->handler(&cmdbuf[5]);
79 void DLoader_Init(char *pathname)
85 struct DLModule_Info* (*h_init_fcn)(void);
86 struct DLModule_Info *dl_info;
88 char pathbuf[PATH_MAX];
90 if ((dir = opendir(pathname))==NULL)
96 while ((dptr=readdir(dir))!= NULL)
98 if (dptr->d_name[0] == '.')
101 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
102 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
104 dl_error = dlerror();
105 fprintf(stderr, "DLoader_Init dlopen failed (%s)\n", dl_error);
109 h_init_fcn = (struct DLModule_Info * (*)(void))
110 dlsym(fcn_handle, "Dynamic_Module_Init");
112 if ((dl_error = dlerror()) != NULL)
114 fprintf(stderr,"DLoader_Init dlsym failed (%s)\n", dl_error);
118 dl_info = h_init_fcn();
120 printf("Loaded module %s v%d.%d\nBy %s (%s)\n", dl_info->module_name,
121 dl_info->major_version, dl_info->minor_version,
122 dl_info->module_author, dl_info->module_author_email);
128 void CtdlRegisterLogHook(void (*fcn_ptr)(char *), int loglevel) {
130 struct LogFunctionHook *newfcn;
132 newfcn = (struct LogFunctionHook *)
133 mallok(sizeof(struct LogFunctionHook));
134 newfcn->next = LogHookTable;
135 newfcn->h_function_pointer = fcn_ptr;
136 newfcn->loglevel = loglevel;
137 LogHookTable = newfcn;
139 lprintf(5, "Registered a new logging function\n");
143 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void)) {
145 struct CleanupFunctionHook *newfcn;
147 newfcn = (struct CleanupFunctionHook *)
148 mallok(sizeof(struct CleanupFunctionHook));
149 newfcn->next = CleanupHookTable;
150 newfcn->h_function_pointer = fcn_ptr;
151 CleanupHookTable = newfcn;
153 lprintf(5, "Registered a new cleanup function\n");
157 void CtdlRegisterSessionHook(void (*fcn_ptr)(void), int EventType) {
159 struct SessionFunctionHook *newfcn;
161 newfcn = (struct SessionFunctionHook *)
162 mallok(sizeof(struct SessionFunctionHook));
163 newfcn->next = SessionHookTable;
164 newfcn->h_function_pointer = fcn_ptr;
165 newfcn->eventtype = EventType;
166 SessionHookTable = newfcn;
168 lprintf(5, "Registered a new session function (type %d)\n",
173 void CtdlRegisterUserHook(void (*fcn_ptr)(char*, long), int EventType) {
175 struct UserFunctionHook *newfcn;
177 newfcn = (struct UserFunctionHook *)
178 mallok(sizeof(struct UserFunctionHook));
179 newfcn->next = UserHookTable;
180 newfcn->h_function_pointer = fcn_ptr;
181 newfcn->eventtype = EventType;
182 UserHookTable = newfcn;
184 lprintf(5, "Registered a new user function (type %d)\n",
189 void PerformSessionHooks(int EventType) {
190 struct SessionFunctionHook *fcn;
192 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
193 if (fcn->eventtype == EventType) {
194 (*fcn->h_function_pointer)();
199 void PerformLogHooks(int loglevel, char *logmsg) {
200 struct LogFunctionHook *fcn;
202 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
203 if (fcn->loglevel >= loglevel) {
204 (*fcn->h_function_pointer)(logmsg);
209 void PerformUserHooks(char *username, long usernum, int EventType) {
210 struct UserFunctionHook *fcn;
212 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
213 if (fcn->eventtype == EventType) {
214 (*fcn->h_function_pointer)(username, usernum);