4 * Citadel Dynamic Loading Module
5 * Written by Brian Costello <btx@calyx.net>
19 #include <sys/types.h>
27 #include "dynloader.h"
28 #include "sysdep_decls.h"
38 struct LogFunctionHook *LogHookTable = NULL;
39 struct CleanupFunctionHook *CleanupHookTable = NULL;
40 struct SessionFunctionHook *SessionHookTable = NULL;
41 struct UserFunctionHook *UserHookTable = NULL;
42 struct XmsgFunctionHook *XmsgHookTable = NULL;
43 struct MessageFunctionHook *MessageHookTable = NULL;
44 struct ServiceFunctionHook *ServiceHookTable = NULL;
46 struct ProtoFunctionHook {
47 void (*handler) (char *cmdbuf);
50 struct ProtoFunctionHook *next;
51 } *ProtoHookList = NULL;
53 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
55 struct ProtoFunctionHook *p = mallok(sizeof *p);
58 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
64 p->next = ProtoHookList;
66 lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
69 int DLoader_Exec_Cmd(char *cmdbuf)
71 struct ProtoFunctionHook *p;
73 for (p = ProtoHookList; p; p = p->next) {
74 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
75 p->handler(&cmdbuf[5]);
82 void DLoader_Init(char *pathname)
89 char *(*h_init_fcn) (void);
92 char pathbuf[PATH_MAX];
94 if ((dir = opendir(pathname)) == NULL) {
98 while ((dptr = readdir(dir)) != NULL) {
99 if (strlen(dptr->d_name) < 4)
101 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-3], ".so"))
104 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
105 lprintf(7, "Initializing %s...\n", pathbuf);
108 if (!(fcn_handle = dlopen(pathbuf, RTLD_LAZY)))
110 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
113 safestrncpy(dl_error, dlerror(), sizeof dl_error);
114 for (i=0; i<strlen(dl_error); ++i)
115 if (!isprint(dl_error[i]))
117 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
121 h_init_fcn = (char * (*)(void))
123 dlsym(fcn_handle, "Dynamic_Module_Init");
125 dlsym(fcn_handle, "_Dynamic_Module_Init");
128 if (dlerror() != NULL) {
129 fprintf(stderr, "DLoader_Init dlsym failed\n");
132 dl_info = h_init_fcn();
134 lprintf(3, "Loaded module: %s\n", dl_info);
140 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
143 struct LogFunctionHook *newfcn;
145 newfcn = (struct LogFunctionHook *)
146 mallok(sizeof(struct LogFunctionHook));
147 newfcn->next = LogHookTable;
148 newfcn->h_function_pointer = fcn_ptr;
149 newfcn->loglevel = loglevel;
150 LogHookTable = newfcn;
152 lprintf(5, "Registered a new logging function\n");
156 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
159 struct CleanupFunctionHook *newfcn;
161 newfcn = (struct CleanupFunctionHook *)
162 mallok(sizeof(struct CleanupFunctionHook));
163 newfcn->next = CleanupHookTable;
164 newfcn->h_function_pointer = fcn_ptr;
165 CleanupHookTable = newfcn;
167 lprintf(5, "Registered a new cleanup function\n");
171 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
174 struct SessionFunctionHook *newfcn;
176 newfcn = (struct SessionFunctionHook *)
177 mallok(sizeof(struct SessionFunctionHook));
178 newfcn->next = SessionHookTable;
179 newfcn->h_function_pointer = fcn_ptr;
180 newfcn->eventtype = EventType;
181 SessionHookTable = newfcn;
183 lprintf(5, "Registered a new session function (type %d)\n",
188 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
191 struct UserFunctionHook *newfcn;
193 newfcn = (struct UserFunctionHook *)
194 mallok(sizeof(struct UserFunctionHook));
195 newfcn->next = UserHookTable;
196 newfcn->h_function_pointer = fcn_ptr;
197 newfcn->eventtype = EventType;
198 UserHookTable = newfcn;
200 lprintf(5, "Registered a new user function (type %d)\n",
205 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
209 struct MessageFunctionHook *newfcn;
211 newfcn = (struct MessageFunctionHook *)
212 mallok(sizeof(struct MessageFunctionHook));
213 newfcn->next = MessageHookTable;
214 newfcn->h_function_pointer = handler;
215 newfcn->eventtype = EventType;
216 MessageHookTable = newfcn;
218 lprintf(5, "Registered a new message function (type %d)\n",
223 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
226 struct XmsgFunctionHook *newfcn;
228 newfcn = (struct XmsgFunctionHook *)
229 mallok(sizeof(struct XmsgFunctionHook));
230 newfcn->next = XmsgHookTable;
231 newfcn->order = order;
232 newfcn->h_function_pointer = fcn_ptr;
233 XmsgHookTable = newfcn;
234 lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
237 void CtdlRegisterServiceHook(int tcp_port,
239 void (*h_greeting_function) (void),
240 void (*h_command_function) (void) )
242 struct ServiceFunctionHook *newfcn;
245 newfcn = (struct ServiceFunctionHook *)
246 mallok(sizeof(struct ServiceFunctionHook));
247 newfcn->next = ServiceHookTable;
248 newfcn->tcp_port = tcp_port;
249 newfcn->sockpath = sockpath;
250 newfcn->h_greeting_function = h_greeting_function;
251 newfcn->h_command_function = h_command_function;
253 if (sockpath != NULL) {
254 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
255 sprintf(message, "Unix domain socket '%s': ", sockpath);
257 else if (tcp_port <= 0) { /* port -1 to disable */
258 lprintf(7, "Service has been manually disabled, skipping\n");
263 newfcn->msock = ig_tcp_server(tcp_port, config.c_maxsessions);
264 sprintf(message, "TCP port %d: ", tcp_port);
267 if (newfcn->msock > 0) {
268 ServiceHookTable = newfcn;
269 strcat(message, "registered.");
270 lprintf(5, "%s\n", message);
273 strcat(message, "FAILED.");
274 lprintf(2, "%s\n", message);
281 void PerformSessionHooks(int EventType)
283 struct SessionFunctionHook *fcn;
285 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
286 if (fcn->eventtype == EventType) {
287 (*fcn->h_function_pointer) ();
292 void PerformLogHooks(int loglevel, char *logmsg)
294 struct LogFunctionHook *fcn;
296 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
297 if (fcn->loglevel >= loglevel) {
298 (*fcn->h_function_pointer) (logmsg);
303 void PerformUserHooks(char *username, long usernum, int EventType)
305 struct UserFunctionHook *fcn;
307 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
308 if (fcn->eventtype == EventType) {
309 (*fcn->h_function_pointer) (username, usernum);
314 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
316 struct MessageFunctionHook *fcn;
317 int total_retval = 0;
319 /* Other code may elect to protect this message from server-side
320 * handlers; if this is the case, don't do anything.
321 lprintf(9, "** Event type is %d, flags are %d\n",
322 EventType, msg->cm_flags);
324 if (msg->cm_flags & CM_SKIP_HOOKS) {
325 lprintf(9, "Skipping hooks\n");
329 /* Otherwise, run all the hooks appropriate to this event type.
331 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
332 if (fcn->eventtype == EventType) {
333 total_retval = total_retval +
334 (*fcn->h_function_pointer) (msg);
338 /* Return the sum of the return codes from the hook functions. If
339 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
340 * the save operation to abort.
347 int PerformXmsgHooks(char *sender, char *recp, char *msg)
349 struct XmsgFunctionHook *fcn;
353 for (p=0; p<MAX_XMSG_PRI; ++p) {
354 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
355 if (fcn->order == p) {
357 (*fcn->h_function_pointer)
361 /* Break out of the loop if a higher-priority function
362 * successfully delivered the message. This prevents duplicate
363 * deliveries to local users simultaneously signed onto
366 if (total_sent) goto DONE;
368 DONE: return total_sent;