1 /*******************************************************
3 * Citadel Dynamic Loading Module
4 * Written by Brian Costello
9 ******************************************************/
16 #include <sys/types.h>
27 #include "dynloader.h"
28 #include "sysdep_decls.h"
37 struct LogFunctionHook *LogHookTable = NULL;
38 struct CleanupFunctionHook *CleanupHookTable = NULL;
39 struct SessionFunctionHook *SessionHookTable = NULL;
40 struct UserFunctionHook *UserHookTable = NULL;
41 struct XmsgFunctionHook *XmsgHookTable = NULL;
42 struct MessageFunctionHook *MessageHookTable = NULL;
44 struct ProtoFunctionHook {
45 void (*handler) (char *cmdbuf);
48 struct ProtoFunctionHook *next;
49 } *ProtoHookList = NULL;
51 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
53 struct ProtoFunctionHook *p = mallok(sizeof *p);
56 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
62 p->next = ProtoHookList;
64 lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
67 int DLoader_Exec_Cmd(char *cmdbuf)
69 struct ProtoFunctionHook *p;
71 for (p = ProtoHookList; p; p = p->next) {
72 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
73 p->handler(&cmdbuf[5]);
80 void DLoader_Init(char *pathname)
87 char *(*h_init_fcn) (void);
90 char pathbuf[PATH_MAX];
92 if ((dir = opendir(pathname)) == NULL) {
96 while ((dptr = readdir(dir)) != NULL) {
97 if (dptr->d_name[0] == '.')
100 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
102 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
104 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
107 safestrncpy(dl_error, dlerror(), sizeof dl_error);
108 for (i=0; i<strlen(dl_error); ++i)
109 if (!isprint(dl_error[i]))
111 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
115 h_init_fcn = (char * (*)(void))
117 dlsym(fcn_handle, "Dynamic_Module_Init");
119 dlsym(fcn_handle, "_Dynamic_Module_Init");
122 if (dlerror() != NULL) {
123 fprintf(stderr, "DLoader_Init dlsym failed\n");
126 dl_info = h_init_fcn();
128 lprintf(3, "Loaded module: %s\n", dl_info);
134 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
137 struct LogFunctionHook *newfcn;
139 newfcn = (struct LogFunctionHook *)
140 mallok(sizeof(struct LogFunctionHook));
141 newfcn->next = LogHookTable;
142 newfcn->h_function_pointer = fcn_ptr;
143 newfcn->loglevel = loglevel;
144 LogHookTable = newfcn;
146 lprintf(5, "Registered a new logging function\n");
150 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)
168 struct SessionFunctionHook *newfcn;
170 newfcn = (struct SessionFunctionHook *)
171 mallok(sizeof(struct SessionFunctionHook));
172 newfcn->next = SessionHookTable;
173 newfcn->h_function_pointer = fcn_ptr;
174 newfcn->eventtype = EventType;
175 SessionHookTable = newfcn;
177 lprintf(5, "Registered a new session function (type %d)\n",
182 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
185 struct UserFunctionHook *newfcn;
187 newfcn = (struct UserFunctionHook *)
188 mallok(sizeof(struct UserFunctionHook));
189 newfcn->next = UserHookTable;
190 newfcn->h_function_pointer = fcn_ptr;
191 newfcn->eventtype = EventType;
192 UserHookTable = newfcn;
194 lprintf(5, "Registered a new user function (type %d)\n",
199 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
203 struct MessageFunctionHook *newfcn;
205 newfcn = (struct MessageFunctionHook *)
206 mallok(sizeof(struct MessageFunctionHook));
207 newfcn->next = MessageHookTable;
208 newfcn->h_function_pointer = handler;
209 newfcn->eventtype = EventType;
210 MessageHookTable = newfcn;
212 lprintf(5, "Registered a new message function (type %d)\n",
217 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
220 struct XmsgFunctionHook *newfcn;
222 newfcn = (struct XmsgFunctionHook *)
223 mallok(sizeof(struct XmsgFunctionHook));
224 newfcn->next = XmsgHookTable;
225 newfcn->order = order;
226 newfcn->h_function_pointer = fcn_ptr;
227 XmsgHookTable = newfcn;
228 lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
232 void PerformSessionHooks(int EventType)
234 struct SessionFunctionHook *fcn;
236 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
237 if (fcn->eventtype == EventType) {
238 (*fcn->h_function_pointer) ();
243 void PerformLogHooks(int loglevel, char *logmsg)
245 struct LogFunctionHook *fcn;
247 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
248 if (fcn->loglevel >= loglevel) {
249 (*fcn->h_function_pointer) (logmsg);
254 void PerformUserHooks(char *username, long usernum, int EventType)
256 struct UserFunctionHook *fcn;
258 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
259 if (fcn->eventtype == EventType) {
260 (*fcn->h_function_pointer) (username, usernum);
265 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
267 struct MessageFunctionHook *fcn;
268 int total_retval = 0;
270 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
271 if (fcn->eventtype == EventType) {
272 total_retval = total_retval +
273 (*fcn->h_function_pointer) (msg);
280 int PerformXmsgHooks(char *sender, char *recp, char *msg)
282 struct XmsgFunctionHook *fcn;
286 for (p=0; p<MAX_XMSG_PRI; ++p) {
287 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
288 if (fcn->order == p) {
290 (*fcn->h_function_pointer)
294 /* Break out of the loop if a higher-priority function
295 * successfully delivered the message. This prevents duplicate
296 * deliveries to local users simultaneously signed onto
299 if (total_sent) goto DONE;
301 DONE: return total_sent;