1 /*******************************************************
3 * Citadel Dynamic Loading Module
4 * Written by Brian Costello
9 ******************************************************/
16 #include <sys/types.h>
24 #include "dynloader.h"
25 #include "sysdep_decls.h"
35 struct LogFunctionHook *LogHookTable = NULL;
36 struct CleanupFunctionHook *CleanupHookTable = NULL;
37 struct SessionFunctionHook *SessionHookTable = NULL;
38 struct UserFunctionHook *UserHookTable = NULL;
39 struct XmsgFunctionHook *XmsgHookTable = NULL;
40 struct MessageFunctionHook *MessageHookTable = NULL;
41 struct ServiceFunctionHook *ServiceHookTable = NULL;
43 struct ProtoFunctionHook {
44 void (*handler) (char *cmdbuf);
47 struct ProtoFunctionHook *next;
48 } *ProtoHookList = NULL;
50 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
52 struct ProtoFunctionHook *p = mallok(sizeof *p);
55 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
61 p->next = ProtoHookList;
63 lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
66 int DLoader_Exec_Cmd(char *cmdbuf)
68 struct ProtoFunctionHook *p;
70 for (p = ProtoHookList; p; p = p->next) {
71 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
72 p->handler(&cmdbuf[5]);
79 void DLoader_Init(char *pathname)
86 char *(*h_init_fcn) (void);
89 char pathbuf[PATH_MAX];
91 if ((dir = opendir(pathname)) == NULL) {
95 while ((dptr = readdir(dir)) != NULL) {
96 if (strlen(dptr->d_name) < 4)
98 if (strcasecmp(&dptr->d_name[strlen(dptr->d_name)-3], ".so"))
101 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
103 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
105 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
108 safestrncpy(dl_error, dlerror(), sizeof dl_error);
109 for (i=0; i<strlen(dl_error); ++i)
110 if (!isprint(dl_error[i]))
112 fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
116 h_init_fcn = (char * (*)(void))
118 dlsym(fcn_handle, "Dynamic_Module_Init");
120 dlsym(fcn_handle, "_Dynamic_Module_Init");
123 if (dlerror() != NULL) {
124 fprintf(stderr, "DLoader_Init dlsym failed\n");
127 dl_info = h_init_fcn();
129 lprintf(3, "Loaded module: %s\n", dl_info);
135 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
138 struct LogFunctionHook *newfcn;
140 newfcn = (struct LogFunctionHook *)
141 mallok(sizeof(struct LogFunctionHook));
142 newfcn->next = LogHookTable;
143 newfcn->h_function_pointer = fcn_ptr;
144 newfcn->loglevel = loglevel;
145 LogHookTable = newfcn;
147 lprintf(5, "Registered a new logging function\n");
151 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
154 struct CleanupFunctionHook *newfcn;
156 newfcn = (struct CleanupFunctionHook *)
157 mallok(sizeof(struct CleanupFunctionHook));
158 newfcn->next = CleanupHookTable;
159 newfcn->h_function_pointer = fcn_ptr;
160 CleanupHookTable = newfcn;
162 lprintf(5, "Registered a new cleanup function\n");
166 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
169 struct SessionFunctionHook *newfcn;
171 newfcn = (struct SessionFunctionHook *)
172 mallok(sizeof(struct SessionFunctionHook));
173 newfcn->next = SessionHookTable;
174 newfcn->h_function_pointer = fcn_ptr;
175 newfcn->eventtype = EventType;
176 SessionHookTable = newfcn;
178 lprintf(5, "Registered a new session function (type %d)\n",
183 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
186 struct UserFunctionHook *newfcn;
188 newfcn = (struct UserFunctionHook *)
189 mallok(sizeof(struct UserFunctionHook));
190 newfcn->next = UserHookTable;
191 newfcn->h_function_pointer = fcn_ptr;
192 newfcn->eventtype = EventType;
193 UserHookTable = newfcn;
195 lprintf(5, "Registered a new user function (type %d)\n",
200 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
204 struct MessageFunctionHook *newfcn;
206 newfcn = (struct MessageFunctionHook *)
207 mallok(sizeof(struct MessageFunctionHook));
208 newfcn->next = MessageHookTable;
209 newfcn->h_function_pointer = handler;
210 newfcn->eventtype = EventType;
211 MessageHookTable = newfcn;
213 lprintf(5, "Registered a new message function (type %d)\n",
218 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
221 struct XmsgFunctionHook *newfcn;
223 newfcn = (struct XmsgFunctionHook *)
224 mallok(sizeof(struct XmsgFunctionHook));
225 newfcn->next = XmsgHookTable;
226 newfcn->order = order;
227 newfcn->h_function_pointer = fcn_ptr;
228 XmsgHookTable = newfcn;
229 lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
232 void CtdlRegisterServiceHook(int tcp_port,
233 void (*h_greeting_function) (void),
234 void (*h_command_function) (void) )
236 struct ServiceFunctionHook *newfcn;
238 newfcn = (struct ServiceFunctionHook *)
239 mallok(sizeof(struct ServiceFunctionHook));
240 newfcn->next = ServiceHookTable;
241 newfcn->tcp_port = tcp_port;
242 newfcn->h_greeting_function = h_greeting_function;
243 newfcn->h_command_function = h_command_function;
244 newfcn->msock = ig_tcp_server(tcp_port, config.c_maxsessions);
246 if (newfcn->msock >= 0) {
247 ServiceHookTable = newfcn;
248 lprintf(5, "Registered a new service (TCP port %d)\n",
252 lprintf(2, "ERROR: could not bind to TCP port %d.\n",
260 void PerformSessionHooks(int EventType)
262 struct SessionFunctionHook *fcn;
264 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
265 if (fcn->eventtype == EventType) {
266 (*fcn->h_function_pointer) ();
271 void PerformLogHooks(int loglevel, char *logmsg)
273 struct LogFunctionHook *fcn;
275 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
276 if (fcn->loglevel >= loglevel) {
277 (*fcn->h_function_pointer) (logmsg);
282 void PerformUserHooks(char *username, long usernum, int EventType)
284 struct UserFunctionHook *fcn;
286 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
287 if (fcn->eventtype == EventType) {
288 (*fcn->h_function_pointer) (username, usernum);
293 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
295 struct MessageFunctionHook *fcn;
296 int total_retval = 0;
298 /* Other code may elect to protect this message from server-side
299 * handlers; if this is the case, don't do anything.
301 lprintf(9, "** Event type is %d, flags are %d\n",
302 EventType, msg->cm_flags);
303 if (msg->cm_flags & CM_SKIP_HOOKS) {
304 lprintf(9, "Skipping hooks\n");
308 /* Otherwise, run all the hooks appropriate to this event type.
310 for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
311 if (fcn->eventtype == EventType) {
312 total_retval = total_retval +
313 (*fcn->h_function_pointer) (msg);
317 /* Return the sum of the return codes from the hook functions. If
318 * this is an EVT_BEFORESAVE event, a nonzero return code will cause
319 * the save operation to abort.
326 int PerformXmsgHooks(char *sender, char *recp, char *msg)
328 struct XmsgFunctionHook *fcn;
332 for (p=0; p<MAX_XMSG_PRI; ++p) {
333 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
334 if (fcn->order == p) {
336 (*fcn->h_function_pointer)
340 /* Break out of the loop if a higher-priority function
341 * successfully delivered the message. This prevents duplicate
342 * deliveries to local users simultaneously signed onto
345 if (total_sent) goto DONE;
347 DONE: return total_sent;