4 * Citadel Dynamic Loading Module
5 * Written by Brian Costello <btx@calyx.net>
13 #include <sys/types.h>
21 #include "dynloader.h"
22 #include "sysdep_decls.h"
32 struct LogFunctionHook *LogHookTable = NULL;
33 struct CleanupFunctionHook *CleanupHookTable = NULL;
34 struct SessionFunctionHook *SessionHookTable = NULL;
35 struct UserFunctionHook *UserHookTable = NULL;
36 struct XmsgFunctionHook *XmsgHookTable = NULL;
37 struct MessageFunctionHook *MessageHookTable = NULL;
38 struct ServiceFunctionHook *ServiceHookTable = NULL;
40 struct ProtoFunctionHook {
41 void (*handler) (char *cmdbuf);
44 struct ProtoFunctionHook *next;
45 } *ProtoHookList = NULL;
47 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
49 struct ProtoFunctionHook *p = mallok(sizeof *p);
52 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
58 p->next = ProtoHookList;
60 lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
63 int DLoader_Exec_Cmd(char *cmdbuf)
65 struct ProtoFunctionHook *p;
67 for (p = ProtoHookList; p; p = p->next) {
68 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
69 p->handler(&cmdbuf[5]);
76 void DLoader_Init(char *pathname)
83 char *(*h_init_fcn) (void);
86 char pathbuf[PATH_MAX];
88 if ((dir = opendir(pathname)) == NULL) {
92 while ((dptr = readdir(dir)) != NULL) {
93 if (strlen(dptr->d_name) < 4)
95 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-3], ".so"))
98 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
100 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
102 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
105 safestrncpy(dl_error, dlerror(), sizeof dl_error);
106 for (i=0; i<strlen(dl_error); ++i)
107 if (!isprint(dl_error[i]))
109 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
113 h_init_fcn = (char * (*)(void))
115 dlsym(fcn_handle, "Dynamic_Module_Init");
117 dlsym(fcn_handle, "_Dynamic_Module_Init");
120 if (dlerror() != NULL) {
121 fprintf(stderr, "DLoader_Init dlsym failed\n");
124 dl_info = h_init_fcn();
126 lprintf(3, "Loaded module: %s\n", dl_info);
132 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
135 struct LogFunctionHook *newfcn;
137 newfcn = (struct LogFunctionHook *)
138 mallok(sizeof(struct LogFunctionHook));
139 newfcn->next = LogHookTable;
140 newfcn->h_function_pointer = fcn_ptr;
141 newfcn->loglevel = loglevel;
142 LogHookTable = newfcn;
144 lprintf(5, "Registered a new logging function\n");
148 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
151 struct CleanupFunctionHook *newfcn;
153 newfcn = (struct CleanupFunctionHook *)
154 mallok(sizeof(struct CleanupFunctionHook));
155 newfcn->next = CleanupHookTable;
156 newfcn->h_function_pointer = fcn_ptr;
157 CleanupHookTable = newfcn;
159 lprintf(5, "Registered a new cleanup function\n");
163 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
166 struct SessionFunctionHook *newfcn;
168 newfcn = (struct SessionFunctionHook *)
169 mallok(sizeof(struct SessionFunctionHook));
170 newfcn->next = SessionHookTable;
171 newfcn->h_function_pointer = fcn_ptr;
172 newfcn->eventtype = EventType;
173 SessionHookTable = newfcn;
175 lprintf(5, "Registered a new session function (type %d)\n",
180 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
183 struct UserFunctionHook *newfcn;
185 newfcn = (struct UserFunctionHook *)
186 mallok(sizeof(struct UserFunctionHook));
187 newfcn->next = UserHookTable;
188 newfcn->h_function_pointer = fcn_ptr;
189 newfcn->eventtype = EventType;
190 UserHookTable = newfcn;
192 lprintf(5, "Registered a new user function (type %d)\n",
197 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
201 struct MessageFunctionHook *newfcn;
203 newfcn = (struct MessageFunctionHook *)
204 mallok(sizeof(struct MessageFunctionHook));
205 newfcn->next = MessageHookTable;
206 newfcn->h_function_pointer = handler;
207 newfcn->eventtype = EventType;
208 MessageHookTable = newfcn;
210 lprintf(5, "Registered a new message function (type %d)\n",
215 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
218 struct XmsgFunctionHook *newfcn;
220 newfcn = (struct XmsgFunctionHook *)
221 mallok(sizeof(struct XmsgFunctionHook));
222 newfcn->next = XmsgHookTable;
223 newfcn->order = order;
224 newfcn->h_function_pointer = fcn_ptr;
225 XmsgHookTable = newfcn;
226 lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
229 void CtdlRegisterServiceHook(int tcp_port,
231 void (*h_greeting_function) (void),
232 void (*h_command_function) (void) )
234 struct ServiceFunctionHook *newfcn;
236 newfcn = (struct ServiceFunctionHook *)
237 mallok(sizeof(struct ServiceFunctionHook));
238 newfcn->next = ServiceHookTable;
239 newfcn->tcp_port = tcp_port;
240 newfcn->sockpath = sockpath;
241 newfcn->h_greeting_function = h_greeting_function;
242 newfcn->h_command_function = h_command_function;
244 if (sockpath != NULL) {
245 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
247 else if (tcp_port < 0) { /* port -1 to disable */
248 lprintf(7, "Service has been manually disabled, skipping\n");
253 newfcn->msock = ig_tcp_server(tcp_port, config.c_maxsessions);
256 if (newfcn->msock >= 0) {
257 ServiceHookTable = newfcn;
258 lprintf(5, "Registered a new service (TCP port %d)\n",
262 lprintf(2, "ERROR: could not bind to TCP port %d.\n",
270 void PerformSessionHooks(int EventType)
272 struct SessionFunctionHook *fcn;
274 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
275 if (fcn->eventtype == EventType) {
276 (*fcn->h_function_pointer) ();
281 void PerformLogHooks(int loglevel, char *logmsg)
283 struct LogFunctionHook *fcn;
285 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
286 if (fcn->loglevel >= loglevel) {
287 (*fcn->h_function_pointer) (logmsg);
292 void PerformUserHooks(char *username, long usernum, int EventType)
294 struct UserFunctionHook *fcn;
296 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
297 if (fcn->eventtype == EventType) {
298 (*fcn->h_function_pointer) (username, usernum);
303 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
305 struct MessageFunctionHook *fcn;
306 int total_retval = 0;
308 /* Other code may elect to protect this message from server-side
309 * handlers; if this is the case, don't do anything.
311 lprintf(9, "** Event type is %d, flags are %d\n",
312 EventType, msg->cm_flags);
313 if (msg->cm_flags & CM_SKIP_HOOKS) {
314 lprintf(9, "Skipping hooks\n");
318 /* Otherwise, run all the hooks appropriate to this event type.
320 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
321 if (fcn->eventtype == EventType) {
322 total_retval = total_retval +
323 (*fcn->h_function_pointer) (msg);
327 /* Return the sum of the return codes from the hook functions. If
328 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
329 * the save operation to abort.
336 int PerformXmsgHooks(char *sender, char *recp, char *msg)
338 struct XmsgFunctionHook *fcn;
342 for (p=0; p<MAX_XMSG_PRI; ++p) {
343 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
344 if (fcn->order == p) {
346 (*fcn->h_function_pointer)
350 /* Break out of the loop if a higher-priority function
351 * successfully delivered the message. This prevents duplicate
352 * deliveries to local users simultaneously signed onto
355 if (total_sent) goto DONE;
357 DONE: return total_sent;