1 /*******************************************************
3 * Citadel Dynamic Loading Module
4 * Written by Brian Costello
9 ******************************************************/
16 #include <sys/types.h>
24 #include "dynloader.h"
25 #include "sysdep_decls.h"
34 struct LogFunctionHook *LogHookTable = NULL;
35 struct CleanupFunctionHook *CleanupHookTable = NULL;
36 struct SessionFunctionHook *SessionHookTable = NULL;
37 struct UserFunctionHook *UserHookTable = NULL;
38 struct XmsgFunctionHook *XmsgHookTable = NULL;
39 struct MessageFunctionHook *MessageHookTable = NULL;
41 struct ProtoFunctionHook {
42 void (*handler) (char *cmdbuf);
45 struct ProtoFunctionHook *next;
46 } *ProtoHookList = NULL;
48 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
50 struct ProtoFunctionHook *p = mallok(sizeof *p);
53 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
59 p->next = ProtoHookList;
61 lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
64 int DLoader_Exec_Cmd(char *cmdbuf)
66 struct ProtoFunctionHook *p;
68 for (p = ProtoHookList; p; p = p->next) {
69 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
70 p->handler(&cmdbuf[5]);
77 void DLoader_Init(char *pathname)
84 char *(*h_init_fcn) (void);
87 char pathbuf[PATH_MAX];
89 if ((dir = opendir(pathname)) == NULL) {
93 while ((dptr = readdir(dir)) != NULL) {
94 if (strlen(dptr->d_name) < 4)
96 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-3], ".so"))
99 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
101 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
103 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
106 safestrncpy(dl_error, dlerror(), sizeof dl_error);
107 for (i=0; i<strlen(dl_error); ++i)
108 if (!isprint(dl_error[i]))
110 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
114 h_init_fcn = (char * (*)(void))
116 dlsym(fcn_handle, "Dynamic_Module_Init");
118 dlsym(fcn_handle, "_Dynamic_Module_Init");
121 if (dlerror() != NULL) {
122 fprintf(stderr, "DLoader_Init dlsym failed\n");
125 dl_info = h_init_fcn();
127 lprintf(3, "Loaded module: %s\n", dl_info);
133 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
136 struct LogFunctionHook *newfcn;
138 newfcn = (struct LogFunctionHook *)
139 mallok(sizeof(struct LogFunctionHook));
140 newfcn->next = LogHookTable;
141 newfcn->h_function_pointer = fcn_ptr;
142 newfcn->loglevel = loglevel;
143 LogHookTable = newfcn;
145 lprintf(5, "Registered a new logging function\n");
149 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
152 struct CleanupFunctionHook *newfcn;
154 newfcn = (struct CleanupFunctionHook *)
155 mallok(sizeof(struct CleanupFunctionHook));
156 newfcn->next = CleanupHookTable;
157 newfcn->h_function_pointer = fcn_ptr;
158 CleanupHookTable = newfcn;
160 lprintf(5, "Registered a new cleanup function\n");
164 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)
184 struct UserFunctionHook *newfcn;
186 newfcn = (struct UserFunctionHook *)
187 mallok(sizeof(struct UserFunctionHook));
188 newfcn->next = UserHookTable;
189 newfcn->h_function_pointer = fcn_ptr;
190 newfcn->eventtype = EventType;
191 UserHookTable = newfcn;
193 lprintf(5, "Registered a new user function (type %d)\n",
198 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
202 struct MessageFunctionHook *newfcn;
204 newfcn = (struct MessageFunctionHook *)
205 mallok(sizeof(struct MessageFunctionHook));
206 newfcn->next = MessageHookTable;
207 newfcn->h_function_pointer = handler;
208 newfcn->eventtype = EventType;
209 MessageHookTable = newfcn;
211 lprintf(5, "Registered a new message function (type %d)\n",
216 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
219 struct XmsgFunctionHook *newfcn;
221 newfcn = (struct XmsgFunctionHook *)
222 mallok(sizeof(struct XmsgFunctionHook));
223 newfcn->next = XmsgHookTable;
224 newfcn->order = order;
225 newfcn->h_function_pointer = fcn_ptr;
226 XmsgHookTable = newfcn;
227 lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
231 void PerformSessionHooks(int EventType)
233 struct SessionFunctionHook *fcn;
235 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
236 if (fcn->eventtype == EventType) {
237 (*fcn->h_function_pointer) ();
242 void PerformLogHooks(int loglevel, char *logmsg)
244 struct LogFunctionHook *fcn;
246 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
247 if (fcn->loglevel >= loglevel) {
248 (*fcn->h_function_pointer) (logmsg);
253 void PerformUserHooks(char *username, long usernum, int EventType)
255 struct UserFunctionHook *fcn;
257 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
258 if (fcn->eventtype == EventType) {
259 (*fcn->h_function_pointer) (username, usernum);
264 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
266 struct MessageFunctionHook *fcn;
267 int total_retval = 0;
269 /* Other code may elect to protect this message from server-side
270 * handlers; if this is the case, don't do anything.
272 lprintf(9, "** Event type is %d, flags are %d\n",
273 EventType, msg->cm_flags);
274 if (msg->cm_flags & CM_SKIP_HOOKS) {
275 lprintf(9, "Skipping hooks\n");
279 /* Otherwise, run all the hooks appropriate to this event type.
281 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
282 if (fcn->eventtype == EventType) {
283 total_retval = total_retval +
284 (*fcn->h_function_pointer) (msg);
288 /* Return the sum of the return codes from the hook functions. If
289 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
290 * the save operation to abort.
297 int PerformXmsgHooks(char *sender, char *recp, char *msg)
299 struct XmsgFunctionHook *fcn;
303 for (p=0; p<MAX_XMSG_PRI; ++p) {
304 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
305 if (fcn->order == p) {
307 (*fcn->h_function_pointer)
311 /* Break out of the loop if a higher-priority function
312 * successfully delivered the message. This prevents duplicate
313 * deliveries to local users simultaneously signed onto
316 if (total_sent) goto DONE;
318 DONE: return total_sent;