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);
103 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
105 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
108 dl_error = dlerror();
109 fprintf(stderr, "DLoader_Init dlopen failed (%s)\n", dl_error);
113 h_init_fcn = (struct DLModule_Info * (*)(void))
115 dlsym(fcn_handle, "Dynamic_Module_Init");
117 dlsym(fcn_handle, "_Dynamic_Module_Init");
120 if ((dl_error = dlerror()) != NULL)
122 fprintf(stderr,"DLoader_Init dlsym failed (%s)\n", dl_error);
126 dl_info = h_init_fcn();
128 printf("Loaded module %s v%d.%d\nBy %s (%s)\n", dl_info->module_name,
129 dl_info->major_version, dl_info->minor_version,
130 dl_info->module_author, dl_info->module_author_email);
136 void CtdlRegisterLogHook(void (*fcn_ptr)(char *), int loglevel) {
138 struct LogFunctionHook *newfcn;
140 newfcn = (struct LogFunctionHook *)
141 mallok(sizeof(struct LogFunctionHook));
142 newfcn->next = LogHookTable;
143 newfcn->h_function_pointer = fcn_ptr;
144 newfcn->loglevel = loglevel;
145 LogHookTable = newfcn;
147 lprintf(5, "Registered a new logging function\n");
151 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void)) {
153 struct CleanupFunctionHook *newfcn;
155 newfcn = (struct CleanupFunctionHook *)
156 mallok(sizeof(struct CleanupFunctionHook));
157 newfcn->next = CleanupHookTable;
158 newfcn->h_function_pointer = fcn_ptr;
159 CleanupHookTable = newfcn;
161 lprintf(5, "Registered a new cleanup function\n");
165 void CtdlRegisterSessionHook(void (*fcn_ptr)(void), int EventType) {
167 struct SessionFunctionHook *newfcn;
169 newfcn = (struct SessionFunctionHook *)
170 mallok(sizeof(struct SessionFunctionHook));
171 newfcn->next = SessionHookTable;
172 newfcn->h_function_pointer = fcn_ptr;
173 newfcn->eventtype = EventType;
174 SessionHookTable = newfcn;
176 lprintf(5, "Registered a new session function (type %d)\n",
181 void CtdlRegisterUserHook(void (*fcn_ptr)(char*, long), int EventType) {
183 struct UserFunctionHook *newfcn;
185 newfcn = (struct UserFunctionHook *)
186 mallok(sizeof(struct UserFunctionHook));
187 newfcn->next = UserHookTable;
188 newfcn->h_function_pointer = fcn_ptr;
189 newfcn->eventtype = EventType;
190 UserHookTable = newfcn;
192 lprintf(5, "Registered a new user function (type %d)\n",
197 void PerformSessionHooks(int EventType) {
198 struct SessionFunctionHook *fcn;
200 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
201 if (fcn->eventtype == EventType) {
202 (*fcn->h_function_pointer)();
207 void PerformLogHooks(int loglevel, char *logmsg) {
208 struct LogFunctionHook *fcn;
210 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
211 if (fcn->loglevel >= loglevel) {
212 (*fcn->h_function_pointer)(logmsg);
217 void PerformUserHooks(char *username, long usernum, int EventType) {
218 struct UserFunctionHook *fcn;
220 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
221 if (fcn->eventtype == EventType) {
222 (*fcn->h_function_pointer)(username, usernum);