1 /*******************************************************
3 * Citadel Dynamic Loading Module
4 * Written by Brian Costello
9 ******************************************************/
16 #include <sys/types.h>
25 #include "dynloader.h"
28 #include "sysdep_decls.h"
36 struct LogFunctionHook *LogHookTable = NULL;
37 struct CleanupFunctionHook *CleanupHookTable = NULL;
38 struct SessionFunctionHook *SessionHookTable = NULL;
39 struct UserFunctionHook *UserHookTable = NULL;
40 struct XmsgFunctionHook *XmsgHookTable = NULL;
42 struct ProtoFunctionHook {
43 void (*handler) (char *cmdbuf);
46 struct ProtoFunctionHook *next;
47 } *ProtoHookList = NULL;
49 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
51 struct ProtoFunctionHook *p = mallok(sizeof *p);
54 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
60 p->next = ProtoHookList;
62 lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
65 int DLoader_Exec_Cmd(char *cmdbuf)
67 struct ProtoFunctionHook *p;
69 for (p = ProtoHookList; p; p = p->next) {
70 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
71 p->handler(&cmdbuf[5]);
78 void DLoader_Init(char *pathname)
85 char *(*h_init_fcn) (void);
88 char pathbuf[PATH_MAX];
90 if ((dir = opendir(pathname)) == NULL) {
94 while ((dptr = readdir(dir)) != NULL) {
95 if (dptr->d_name[0] == '.')
98 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
100 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
102 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
105 safestrncpy(dl_error, dlerror(), sizeof dl_error);
106 for (i=0; i<strlen(dl_error); ++i)
107 if (!isprint(dl_error[i]))
109 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
113 h_init_fcn = (char * (*)(void))
115 dlsym(fcn_handle, "Dynamic_Module_Init");
117 dlsym(fcn_handle, "_Dynamic_Module_Init");
120 if (dlerror() != NULL) {
121 fprintf(stderr, "DLoader_Init dlsym failed\n");
124 dl_info = h_init_fcn();
126 lprintf(3, "Loaded module: %s\n", dl_info);
132 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
135 struct LogFunctionHook *newfcn;
137 newfcn = (struct LogFunctionHook *)
138 mallok(sizeof(struct LogFunctionHook));
139 newfcn->next = LogHookTable;
140 newfcn->h_function_pointer = fcn_ptr;
141 newfcn->loglevel = loglevel;
142 LogHookTable = newfcn;
144 lprintf(5, "Registered a new logging function\n");
148 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
151 struct CleanupFunctionHook *newfcn;
153 newfcn = (struct CleanupFunctionHook *)
154 mallok(sizeof(struct CleanupFunctionHook));
155 newfcn->next = CleanupHookTable;
156 newfcn->h_function_pointer = fcn_ptr;
157 CleanupHookTable = newfcn;
159 lprintf(5, "Registered a new cleanup function\n");
163 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
166 struct SessionFunctionHook *newfcn;
168 newfcn = (struct SessionFunctionHook *)
169 mallok(sizeof(struct SessionFunctionHook));
170 newfcn->next = SessionHookTable;
171 newfcn->h_function_pointer = fcn_ptr;
172 newfcn->eventtype = EventType;
173 SessionHookTable = newfcn;
175 lprintf(5, "Registered a new session function (type %d)\n",
180 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 CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *) )
200 struct XmsgFunctionHook *newfcn;
202 newfcn = (struct XmsgFunctionHook *)
203 mallok(sizeof(struct XmsgFunctionHook));
204 newfcn->next = XmsgHookTable;
205 newfcn->h_function_pointer = fcn_ptr;
206 XmsgHookTable = newfcn;
208 lprintf(5, "Registered a new x-msg function\n");
212 void PerformSessionHooks(int EventType)
214 struct SessionFunctionHook *fcn;
216 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
217 if (fcn->eventtype == EventType) {
218 (*fcn->h_function_pointer) ();
223 void PerformLogHooks(int loglevel, char *logmsg)
225 struct LogFunctionHook *fcn;
227 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
228 if (fcn->loglevel >= loglevel) {
229 (*fcn->h_function_pointer) (logmsg);
234 void PerformUserHooks(char *username, long usernum, int EventType)
236 struct UserFunctionHook *fcn;
238 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
239 if (fcn->eventtype == EventType) {
240 (*fcn->h_function_pointer) (username, usernum);
245 int PerformXmsgHooks(char *sender, char *recp, char *msg)
247 struct XmsgFunctionHook *fcn;
250 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
252 (*fcn->h_function_pointer) (sender, recp, msg);