4 * Citadel Dynamic Loading Module
5 * Written by Brian Costello <btx@calyx.net>
23 #include <sys/types.h>
31 #include "dynloader.h"
32 #include "sysdep_decls.h"
42 struct LogFunctionHook *LogHookTable = NULL;
43 struct CleanupFunctionHook *CleanupHookTable = NULL;
44 struct SessionFunctionHook *SessionHookTable = NULL;
45 struct UserFunctionHook *UserHookTable = NULL;
46 struct XmsgFunctionHook *XmsgHookTable = NULL;
47 struct MessageFunctionHook *MessageHookTable = NULL;
48 struct ServiceFunctionHook *ServiceHookTable = NULL;
50 struct ProtoFunctionHook {
51 void (*handler) (char *cmdbuf);
54 struct ProtoFunctionHook *next;
55 } *ProtoHookList = NULL;
57 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
59 struct ProtoFunctionHook *p = mallok(sizeof *p);
62 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
68 p->next = ProtoHookList;
70 lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
73 int DLoader_Exec_Cmd(char *cmdbuf)
75 struct ProtoFunctionHook *p;
77 for (p = ProtoHookList; p; p = p->next) {
78 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
79 p->handler(&cmdbuf[5]);
86 void DLoader_Init(char *pathname)
93 char *(*h_init_fcn) (void);
96 char pathbuf[PATH_MAX];
98 if ((dir = opendir(pathname)) == NULL) {
102 while ((dptr = readdir(dir)) != NULL) {
103 if (strlen(dptr->d_name) < 4)
106 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-3], ".so"))
108 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-4], ".dll"))
112 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
113 lprintf(7, "Initializing %s...\n", pathbuf);
116 if (!(fcn_handle = dlopen(pathbuf, RTLD_LAZY)))
118 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
121 safestrncpy(dl_error, dlerror(), sizeof dl_error);
122 for (i=0; i<strlen(dl_error); ++i)
123 if (!isprint(dl_error[i]))
125 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
129 h_init_fcn = (char * (*)(void))
131 dlsym(fcn_handle, "Dynamic_Module_Init");
133 dlsym(fcn_handle, "_Dynamic_Module_Init");
136 if (dlerror() != NULL) {
137 fprintf(stderr, "DLoader_Init dlsym failed\n");
140 dl_info = h_init_fcn();
142 lprintf(3, "Loaded module: %s\n", dl_info);
148 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
151 struct LogFunctionHook *newfcn;
153 newfcn = (struct LogFunctionHook *)
154 mallok(sizeof(struct LogFunctionHook));
155 newfcn->next = LogHookTable;
156 newfcn->h_function_pointer = fcn_ptr;
157 newfcn->loglevel = loglevel;
158 LogHookTable = newfcn;
160 lprintf(5, "Registered a new logging function\n");
164 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
167 struct CleanupFunctionHook *newfcn;
169 newfcn = (struct CleanupFunctionHook *)
170 mallok(sizeof(struct CleanupFunctionHook));
171 newfcn->next = CleanupHookTable;
172 newfcn->h_function_pointer = fcn_ptr;
173 CleanupHookTable = newfcn;
175 lprintf(5, "Registered a new cleanup function\n");
179 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
182 struct SessionFunctionHook *newfcn;
184 newfcn = (struct SessionFunctionHook *)
185 mallok(sizeof(struct SessionFunctionHook));
186 newfcn->next = SessionHookTable;
187 newfcn->h_function_pointer = fcn_ptr;
188 newfcn->eventtype = EventType;
189 SessionHookTable = newfcn;
191 lprintf(5, "Registered a new session function (type %d)\n",
196 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
199 struct UserFunctionHook *newfcn;
201 newfcn = (struct UserFunctionHook *)
202 mallok(sizeof(struct UserFunctionHook));
203 newfcn->next = UserHookTable;
204 newfcn->h_function_pointer = fcn_ptr;
205 newfcn->eventtype = EventType;
206 UserHookTable = newfcn;
208 lprintf(5, "Registered a new user function (type %d)\n",
213 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
217 struct MessageFunctionHook *newfcn;
219 newfcn = (struct MessageFunctionHook *)
220 mallok(sizeof(struct MessageFunctionHook));
221 newfcn->next = MessageHookTable;
222 newfcn->h_function_pointer = handler;
223 newfcn->eventtype = EventType;
224 MessageHookTable = newfcn;
226 lprintf(5, "Registered a new message function (type %d)\n",
231 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
234 struct XmsgFunctionHook *newfcn;
236 newfcn = (struct XmsgFunctionHook *)
237 mallok(sizeof(struct XmsgFunctionHook));
238 newfcn->next = XmsgHookTable;
239 newfcn->order = order;
240 newfcn->h_function_pointer = fcn_ptr;
241 XmsgHookTable = newfcn;
242 lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
245 void CtdlRegisterServiceHook(int tcp_port,
247 void (*h_greeting_function) (void),
248 void (*h_command_function) (void) )
250 struct ServiceFunctionHook *newfcn;
253 newfcn = (struct ServiceFunctionHook *)
254 mallok(sizeof(struct ServiceFunctionHook));
255 newfcn->next = ServiceHookTable;
256 newfcn->tcp_port = tcp_port;
257 newfcn->sockpath = sockpath;
258 newfcn->h_greeting_function = h_greeting_function;
259 newfcn->h_command_function = h_command_function;
261 if (sockpath != NULL) {
262 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
263 sprintf(message, "Unix domain socket '%s': ", sockpath);
265 else if (tcp_port <= 0) { /* port -1 to disable */
266 lprintf(7, "Service has been manually disabled, skipping\n");
271 newfcn->msock = ig_tcp_server(tcp_port, config.c_maxsessions);
272 sprintf(message, "TCP port %d: ", tcp_port);
275 if (newfcn->msock > 0) {
276 ServiceHookTable = newfcn;
277 strcat(message, "registered.");
278 lprintf(5, "%s\n", message);
281 strcat(message, "FAILED.");
282 lprintf(2, "%s\n", message);
289 void PerformSessionHooks(int EventType)
291 struct SessionFunctionHook *fcn;
293 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
294 if (fcn->eventtype == EventType) {
295 (*fcn->h_function_pointer) ();
300 void PerformLogHooks(int loglevel, char *logmsg)
302 struct LogFunctionHook *fcn;
304 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
305 if (fcn->loglevel >= loglevel) {
306 (*fcn->h_function_pointer) (logmsg);
311 void PerformUserHooks(char *username, long usernum, int EventType)
313 struct UserFunctionHook *fcn;
315 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
316 if (fcn->eventtype == EventType) {
317 (*fcn->h_function_pointer) (username, usernum);
322 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
324 struct MessageFunctionHook *fcn;
325 int total_retval = 0;
327 /* Other code may elect to protect this message from server-side
328 * handlers; if this is the case, don't do anything.
329 lprintf(9, "** Event type is %d, flags are %d\n",
330 EventType, msg->cm_flags);
332 if (msg->cm_flags & CM_SKIP_HOOKS) {
333 lprintf(9, "Skipping hooks\n");
337 /* Otherwise, run all the hooks appropriate to this event type.
339 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
340 if (fcn->eventtype == EventType) {
341 total_retval = total_retval +
342 (*fcn->h_function_pointer) (msg);
346 /* Return the sum of the return codes from the hook functions. If
347 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
348 * the save operation to abort.
355 int PerformXmsgHooks(char *sender, char *recp, char *msg)
357 struct XmsgFunctionHook *fcn;
361 for (p=0; p<MAX_XMSG_PRI; ++p) {
362 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
363 if (fcn->order == p) {
365 (*fcn->h_function_pointer)
369 /* Break out of the loop if a higher-priority function
370 * successfully delivered the message. This prevents duplicate
371 * deliveries to local users simultaneously signed onto
374 if (total_sent) goto DONE;
376 DONE: return total_sent;