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);
106 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
108 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
111 safestrncpy(dl_error, dlerror(), sizeof dl_error);
112 for (i=0; i<strlen(dl_error); ++i)
113 if (!isprint(dl_error[i]))
115 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
119 h_init_fcn = (char * (*)(void))
121 dlsym(fcn_handle, "Dynamic_Module_Init");
123 dlsym(fcn_handle, "_Dynamic_Module_Init");
126 if (dlerror() != NULL) {
127 fprintf(stderr, "DLoader_Init dlsym failed\n");
130 dl_info = h_init_fcn();
132 lprintf(3, "Loaded module: %s\n", dl_info);
138 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
141 struct LogFunctionHook *newfcn;
143 newfcn = (struct LogFunctionHook *)
144 mallok(sizeof(struct LogFunctionHook));
145 newfcn->next = LogHookTable;
146 newfcn->h_function_pointer = fcn_ptr;
147 newfcn->loglevel = loglevel;
148 LogHookTable = newfcn;
150 lprintf(5, "Registered a new logging function\n");
154 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
157 struct CleanupFunctionHook *newfcn;
159 newfcn = (struct CleanupFunctionHook *)
160 mallok(sizeof(struct CleanupFunctionHook));
161 newfcn->next = CleanupHookTable;
162 newfcn->h_function_pointer = fcn_ptr;
163 CleanupHookTable = newfcn;
165 lprintf(5, "Registered a new cleanup function\n");
169 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
172 struct SessionFunctionHook *newfcn;
174 newfcn = (struct SessionFunctionHook *)
175 mallok(sizeof(struct SessionFunctionHook));
176 newfcn->next = SessionHookTable;
177 newfcn->h_function_pointer = fcn_ptr;
178 newfcn->eventtype = EventType;
179 SessionHookTable = newfcn;
181 lprintf(5, "Registered a new session function (type %d)\n",
186 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
189 struct UserFunctionHook *newfcn;
191 newfcn = (struct UserFunctionHook *)
192 mallok(sizeof(struct UserFunctionHook));
193 newfcn->next = UserHookTable;
194 newfcn->h_function_pointer = fcn_ptr;
195 newfcn->eventtype = EventType;
196 UserHookTable = newfcn;
198 lprintf(5, "Registered a new user function (type %d)\n",
203 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
207 struct MessageFunctionHook *newfcn;
209 newfcn = (struct MessageFunctionHook *)
210 mallok(sizeof(struct MessageFunctionHook));
211 newfcn->next = MessageHookTable;
212 newfcn->h_function_pointer = handler;
213 newfcn->eventtype = EventType;
214 MessageHookTable = newfcn;
216 lprintf(5, "Registered a new message function (type %d)\n",
221 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
224 struct XmsgFunctionHook *newfcn;
226 newfcn = (struct XmsgFunctionHook *)
227 mallok(sizeof(struct XmsgFunctionHook));
228 newfcn->next = XmsgHookTable;
229 newfcn->order = order;
230 newfcn->h_function_pointer = fcn_ptr;
231 XmsgHookTable = newfcn;
232 lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
235 void CtdlRegisterServiceHook(int tcp_port,
237 void (*h_greeting_function) (void),
238 void (*h_command_function) (void) )
240 struct ServiceFunctionHook *newfcn;
243 newfcn = (struct ServiceFunctionHook *)
244 mallok(sizeof(struct ServiceFunctionHook));
245 newfcn->next = ServiceHookTable;
246 newfcn->tcp_port = tcp_port;
247 newfcn->sockpath = sockpath;
248 newfcn->h_greeting_function = h_greeting_function;
249 newfcn->h_command_function = h_command_function;
251 if (sockpath != NULL) {
252 newfcn->msock = ig_uds_server(sockpath, config.c_maxsessions);
253 sprintf(message, "Unix domain socket %s: ", sockpath);
255 else if (tcp_port <= 0) { /* port -1 to disable */
256 lprintf(7, "Service has been manually disabled, skipping\n");
261 newfcn->msock = ig_tcp_server(tcp_port, config.c_maxsessions);
262 sprintf(message, "TCP port %d: ", tcp_port);
265 if (newfcn->msock > 0) {
266 ServiceHookTable = newfcn;
267 strcat(message, "registered.");
268 lprintf(5, "%s\n", message);
271 strcat(message, "FAILED.");
272 lprintf(2, "%s\n", message);
279 void PerformSessionHooks(int EventType)
281 struct SessionFunctionHook *fcn;
283 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
284 if (fcn->eventtype == EventType) {
285 (*fcn->h_function_pointer) ();
290 void PerformLogHooks(int loglevel, char *logmsg)
292 struct LogFunctionHook *fcn;
294 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
295 if (fcn->loglevel >= loglevel) {
296 (*fcn->h_function_pointer) (logmsg);
301 void PerformUserHooks(char *username, long usernum, int EventType)
303 struct UserFunctionHook *fcn;
305 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
306 if (fcn->eventtype == EventType) {
307 (*fcn->h_function_pointer) (username, usernum);
312 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
314 struct MessageFunctionHook *fcn;
315 int total_retval = 0;
317 /* Other code may elect to protect this message from server-side
318 * handlers; if this is the case, don't do anything.
320 lprintf(9, "** Event type is %d, flags are %d\n",
321 EventType, msg->cm_flags);
322 if (msg->cm_flags & CM_SKIP_HOOKS) {
323 lprintf(9, "Skipping hooks\n");
327 /* Otherwise, run all the hooks appropriate to this event type.
329 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
330 if (fcn->eventtype == EventType) {
331 total_retval = total_retval +
332 (*fcn->h_function_pointer) (msg);
336 /* Return the sum of the return codes from the hook functions. If
337 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
338 * the save operation to abort.
345 int PerformXmsgHooks(char *sender, char *recp, char *msg)
347 struct XmsgFunctionHook *fcn;
351 for (p=0; p<MAX_XMSG_PRI; ++p) {
352 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
353 if (fcn->order == p) {
355 (*fcn->h_function_pointer)
359 /* Break out of the loop if a higher-priority function
360 * successfully delivered the message. This prevents duplicate
361 * deliveries to local users simultaneously signed onto
364 if (total_sent) goto DONE;
366 DONE: return total_sent;