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;
40 struct ServiceFunctionHook *ServiceHookTable = 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 (strlen(dptr->d_name) < 4)
97 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-3], ".so"))
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);
231 void CtdlRegisterServiceHook(int tcp_port,
232 void (*h_greeting_function) (void),
233 void (*h_command_function) (void) )
235 struct ServiceFunctionHook *newfcn;
237 newfcn = (struct ServiceFunctionHook *)
238 mallok(sizeof(struct ServiceFunctionHook));
239 newfcn->next = ServiceHookTable;
240 newfcn->tcp_port = tcp_port;
241 newfcn->h_greeting_function = h_greeting_function;
242 newfcn->h_command_function = h_command_function;
243 ServiceHookTable = newfcn;
244 lprintf(5, "Registered a new service (TCP port %d)\n", tcp_port);
249 void PerformSessionHooks(int EventType)
251 struct SessionFunctionHook *fcn;
253 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
254 if (fcn->eventtype == EventType) {
255 (*fcn->h_function_pointer) ();
260 void PerformLogHooks(int loglevel, char *logmsg)
262 struct LogFunctionHook *fcn;
264 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
265 if (fcn->loglevel >= loglevel) {
266 (*fcn->h_function_pointer) (logmsg);
271 void PerformUserHooks(char *username, long usernum, int EventType)
273 struct UserFunctionHook *fcn;
275 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
276 if (fcn->eventtype == EventType) {
277 (*fcn->h_function_pointer) (username, usernum);
282 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
284 struct MessageFunctionHook *fcn;
285 int total_retval = 0;
287 /* Other code may elect to protect this message from server-side
288 * handlers; if this is the case, don't do anything.
290 lprintf(9, "** Event type is %d, flags are %d\n",
291 EventType, msg->cm_flags);
292 if (msg->cm_flags & CM_SKIP_HOOKS) {
293 lprintf(9, "Skipping hooks\n");
297 /* Otherwise, run all the hooks appropriate to this event type.
299 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
300 if (fcn->eventtype == EventType) {
301 total_retval = total_retval +
302 (*fcn->h_function_pointer) (msg);
306 /* Return the sum of the return codes from the hook functions. If
307 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
308 * the save operation to abort.
315 int PerformXmsgHooks(char *sender, char *recp, char *msg)
317 struct XmsgFunctionHook *fcn;
321 for (p=0; p<MAX_XMSG_PRI; ++p) {
322 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
323 if (fcn->order == p) {
325 (*fcn->h_function_pointer)
329 /* Break out of the loop if a higher-priority function
330 * successfully delivered the message. This prevents duplicate
331 * deliveries to local users simultaneously signed onto
334 if (total_sent) goto DONE;
336 DONE: return total_sent;