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 (strlen(dptr->d_name) < 4)
99 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-3], ".so"))
102 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
104 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
106 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
109 safestrncpy(dl_error, dlerror(), sizeof dl_error);
110 for (i=0; i<strlen(dl_error); ++i)
111 if (!isprint(dl_error[i]))
113 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
117 h_init_fcn = (char * (*)(void))
119 dlsym(fcn_handle, "Dynamic_Module_Init");
121 dlsym(fcn_handle, "_Dynamic_Module_Init");
124 if (dlerror() != NULL) {
125 fprintf(stderr, "DLoader_Init dlsym failed\n");
128 dl_info = h_init_fcn();
130 lprintf(3, "Loaded module: %s\n", dl_info);
136 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
139 struct LogFunctionHook *newfcn;
141 newfcn = (struct LogFunctionHook *)
142 mallok(sizeof(struct LogFunctionHook));
143 newfcn->next = LogHookTable;
144 newfcn->h_function_pointer = fcn_ptr;
145 newfcn->loglevel = loglevel;
146 LogHookTable = newfcn;
148 lprintf(5, "Registered a new logging function\n");
152 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
155 struct CleanupFunctionHook *newfcn;
157 newfcn = (struct CleanupFunctionHook *)
158 mallok(sizeof(struct CleanupFunctionHook));
159 newfcn->next = CleanupHookTable;
160 newfcn->h_function_pointer = fcn_ptr;
161 CleanupHookTable = newfcn;
163 lprintf(5, "Registered a new cleanup function\n");
167 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
170 struct SessionFunctionHook *newfcn;
172 newfcn = (struct SessionFunctionHook *)
173 mallok(sizeof(struct SessionFunctionHook));
174 newfcn->next = SessionHookTable;
175 newfcn->h_function_pointer = fcn_ptr;
176 newfcn->eventtype = EventType;
177 SessionHookTable = newfcn;
179 lprintf(5, "Registered a new session function (type %d)\n",
184 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
187 struct UserFunctionHook *newfcn;
189 newfcn = (struct UserFunctionHook *)
190 mallok(sizeof(struct UserFunctionHook));
191 newfcn->next = UserHookTable;
192 newfcn->h_function_pointer = fcn_ptr;
193 newfcn->eventtype = EventType;
194 UserHookTable = newfcn;
196 lprintf(5, "Registered a new user function (type %d)\n",
201 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
205 struct MessageFunctionHook *newfcn;
207 newfcn = (struct MessageFunctionHook *)
208 mallok(sizeof(struct MessageFunctionHook));
209 newfcn->next = MessageHookTable;
210 newfcn->h_function_pointer = handler;
211 newfcn->eventtype = EventType;
212 MessageHookTable = newfcn;
214 lprintf(5, "Registered a new message function (type %d)\n",
219 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
222 struct XmsgFunctionHook *newfcn;
224 newfcn = (struct XmsgFunctionHook *)
225 mallok(sizeof(struct XmsgFunctionHook));
226 newfcn->next = XmsgHookTable;
227 newfcn->order = order;
228 newfcn->h_function_pointer = fcn_ptr;
229 XmsgHookTable = newfcn;
230 lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
234 void PerformSessionHooks(int EventType)
236 struct SessionFunctionHook *fcn;
238 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
239 if (fcn->eventtype == EventType) {
240 (*fcn->h_function_pointer) ();
245 void PerformLogHooks(int loglevel, char *logmsg)
247 struct LogFunctionHook *fcn;
249 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
250 if (fcn->loglevel >= loglevel) {
251 (*fcn->h_function_pointer) (logmsg);
256 void PerformUserHooks(char *username, long usernum, int EventType)
258 struct UserFunctionHook *fcn;
260 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
261 if (fcn->eventtype == EventType) {
262 (*fcn->h_function_pointer) (username, usernum);
267 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
269 struct MessageFunctionHook *fcn;
270 int total_retval = 0;
272 /* Other code may elect to protect this message from server-side
273 * handlers; if this is the case, don't do anything.
275 lprintf(9, "** Event type is %d, flags are %d\n",
276 EventType, msg->cm_flags);
277 if (msg->cm_flags & CM_SKIP_HOOKS) {
278 lprintf(9, "Skipping hooks\n");
282 /* Otherwise, run all the hooks appropriate to this event type.
284 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
285 if (fcn->eventtype == EventType) {
286 total_retval = total_retval +
287 (*fcn->h_function_pointer) (msg);
291 /* Return the sum of the return codes from the hook functions. If
292 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
293 * the save operation to abort.
300 int PerformXmsgHooks(char *sender, char *recp, char *msg)
302 struct XmsgFunctionHook *fcn;
306 for (p=0; p<MAX_XMSG_PRI; ++p) {
307 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
308 if (fcn->order == p) {
310 (*fcn->h_function_pointer)
314 /* Break out of the loop if a higher-priority function
315 * successfully delivered the message. This prevents duplicate
316 * deliveries to local users simultaneously signed onto
319 if (total_sent) goto DONE;
321 DONE: return total_sent;