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;
61 p = (struct ProtoFunctionHook *)
62 mallok(sizeof(struct ProtoFunctionHook));
65 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
71 p->next = ProtoHookList;
73 lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
76 int DLoader_Exec_Cmd(char *cmdbuf)
78 struct ProtoFunctionHook *p;
80 for (p = ProtoHookList; p; p = p->next) {
81 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
82 p->handler(&cmdbuf[5]);
89 void DLoader_Init(char *pathname)
96 char *(*h_init_fcn) (void);
99 char pathbuf[PATH_MAX];
101 if ((dir = opendir(pathname)) == NULL) {
105 while ((dptr = readdir(dir)) != NULL) {
106 if (strlen(dptr->d_name) < 4)
109 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-3], ".so"))
111 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-4], ".dll"))
115 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
116 lprintf(7, "Initializing %s...\n", pathbuf);
119 if (!(fcn_handle = dlopen(pathbuf, RTLD_LAZY)))
121 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
124 safestrncpy(dl_error, dlerror(), sizeof dl_error);
125 for (i=0; i<strlen(dl_error); ++i)
126 if (!isprint(dl_error[i]))
128 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
132 h_init_fcn = (char * (*)(void))
134 dlsym(fcn_handle, "Dynamic_Module_Init");
136 dlsym(fcn_handle, "_Dynamic_Module_Init");
139 if (dlerror() != NULL) {
140 fprintf(stderr, "DLoader_Init dlsym failed\n");
143 dl_info = h_init_fcn();
145 lprintf(3, "Loaded module: %s\n", dl_info);
151 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
154 struct LogFunctionHook *newfcn;
156 newfcn = (struct LogFunctionHook *)
157 mallok(sizeof(struct LogFunctionHook));
158 newfcn->next = LogHookTable;
159 newfcn->h_function_pointer = fcn_ptr;
160 newfcn->loglevel = loglevel;
161 LogHookTable = newfcn;
163 lprintf(5, "Registered a new logging function\n");
167 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
170 struct CleanupFunctionHook *newfcn;
172 newfcn = (struct CleanupFunctionHook *)
173 mallok(sizeof(struct CleanupFunctionHook));
174 newfcn->next = CleanupHookTable;
175 newfcn->h_function_pointer = fcn_ptr;
176 CleanupHookTable = newfcn;
178 lprintf(5, "Registered a new cleanup function\n");
182 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
185 struct SessionFunctionHook *newfcn;
187 newfcn = (struct SessionFunctionHook *)
188 mallok(sizeof(struct SessionFunctionHook));
189 newfcn->next = SessionHookTable;
190 newfcn->h_function_pointer = fcn_ptr;
191 newfcn->eventtype = EventType;
192 SessionHookTable = newfcn;
194 lprintf(5, "Registered a new session function (type %d)\n",
199 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
202 struct UserFunctionHook *newfcn;
204 newfcn = (struct UserFunctionHook *)
205 mallok(sizeof(struct UserFunctionHook));
206 newfcn->next = UserHookTable;
207 newfcn->h_function_pointer = fcn_ptr;
208 newfcn->eventtype = EventType;
209 UserHookTable = newfcn;
211 lprintf(5, "Registered a new user function (type %d)\n",
216 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
220 struct MessageFunctionHook *newfcn;
222 newfcn = (struct MessageFunctionHook *)
223 mallok(sizeof(struct MessageFunctionHook));
224 newfcn->next = MessageHookTable;
225 newfcn->h_function_pointer = handler;
226 newfcn->eventtype = EventType;
227 MessageHookTable = newfcn;
229 lprintf(5, "Registered a new message function (type %d)\n",
234 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
237 struct XmsgFunctionHook *newfcn;
239 newfcn = (struct XmsgFunctionHook *)
240 mallok(sizeof(struct XmsgFunctionHook));
241 newfcn->next = XmsgHookTable;
242 newfcn->order = order;
243 newfcn->h_function_pointer = fcn_ptr;
244 XmsgHookTable = newfcn;
245 lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
248 void CtdlRegisterServiceHook(int tcp_port,
250 void (*h_greeting_function) (void),
251 void (*h_command_function) (void) )
253 struct ServiceFunctionHook *newfcn;
256 newfcn = (struct ServiceFunctionHook *)
257 mallok(sizeof(struct ServiceFunctionHook));
258 newfcn->next = ServiceHookTable;
259 newfcn->tcp_port = tcp_port;
260 newfcn->sockpath = sockpath;
261 newfcn->h_greeting_function = h_greeting_function;
262 newfcn->h_command_function = h_command_function;
264 if (sockpath != NULL) {
265 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
266 sprintf(message, "Unix domain socket '%s': ", sockpath);
268 else if (tcp_port <= 0) { /* port -1 to disable */
269 lprintf(7, "Service has been manually disabled, skipping\n");
274 newfcn->msock = ig_tcp_server(tcp_port, config.c_maxsessions);
275 sprintf(message, "TCP port %d: ", tcp_port);
278 if (newfcn->msock > 0) {
279 ServiceHookTable = newfcn;
280 strcat(message, "registered.");
281 lprintf(5, "%s\n", message);
284 strcat(message, "FAILED.");
285 lprintf(2, "%s\n", message);
292 void PerformSessionHooks(int EventType)
294 struct SessionFunctionHook *fcn;
296 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
297 if (fcn->eventtype == EventType) {
298 (*fcn->h_function_pointer) ();
303 void PerformLogHooks(int loglevel, char *logmsg)
305 struct LogFunctionHook *fcn;
307 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
308 if (fcn->loglevel >= loglevel) {
309 (*fcn->h_function_pointer) (logmsg);
314 void PerformUserHooks(char *username, long usernum, int EventType)
316 struct UserFunctionHook *fcn;
318 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
319 if (fcn->eventtype == EventType) {
320 (*fcn->h_function_pointer) (username, usernum);
325 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
327 struct MessageFunctionHook *fcn;
328 int total_retval = 0;
330 /* Other code may elect to protect this message from server-side
331 * handlers; if this is the case, don't do anything.
332 lprintf(9, "** Event type is %d, flags are %d\n",
333 EventType, msg->cm_flags);
335 if (msg->cm_flags & CM_SKIP_HOOKS) {
336 lprintf(9, "Skipping hooks\n");
340 /* Otherwise, run all the hooks appropriate to this event type.
342 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
343 if (fcn->eventtype == EventType) {
344 total_retval = total_retval +
345 (*fcn->h_function_pointer) (msg);
349 /* Return the sum of the return codes from the hook functions. If
350 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
351 * the save operation to abort.
358 int PerformXmsgHooks(char *sender, char *recp, char *msg)
360 struct XmsgFunctionHook *fcn;
364 for (p=0; p<MAX_XMSG_PRI; ++p) {
365 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
366 if (fcn->order == p) {
368 (*fcn->h_function_pointer)
372 /* Break out of the loop if a higher-priority function
373 * successfully delivered the message. This prevents duplicate
374 * deliveries to local users simultaneously signed onto
377 if (total_sent) goto DONE;
379 DONE: return total_sent;