1 /*******************************************************
3 * Citadel Dynamic Loading Module
4 * Written by Brian Costello
7 ******************************************************/
13 #include <sys/types.h>
19 #include "dynloader.h"
22 #include "sysdep_decls.h"
29 struct CleanupFunctionHook *CleanupHookTable = NULL;
30 struct SessionFunctionHook *SessionHookTable = NULL;
31 struct UserFunctionHook *UserHookTable = NULL;
33 struct ProtoFunctionHook
35 void (*handler)(char *cmdbuf);
38 struct ProtoFunctionHook *next;
39 } *ProtoHookList = NULL;
41 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc)
43 struct ProtoFunctionHook *p = malloc(sizeof *p);
47 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
54 p->next = ProtoHookList;
58 int DLoader_Exec_Cmd(char *cmdbuf)
60 struct ProtoFunctionHook *p;
62 for (p = ProtoHookList; p; p = p->next)
64 if (!strncasecmp(cmdbuf, p->cmd, 4))
66 p->handler(&cmdbuf[5]);
73 void DLoader_Init(char *pathname)
79 struct DLModule_Info* (*h_init_fcn)(void);
80 struct DLModule_Info *dl_info;
82 char pathbuf[PATH_MAX];
84 if ((dir = opendir(pathname))==NULL)
90 while ((dptr=readdir(dir))!= NULL)
92 if (dptr->d_name[0] == '.')
95 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
96 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
99 fprintf(stderr, "DLoader_Init dlopen failed (%s)\n", dl_error);
103 h_init_fcn = (struct DLModule_Info * (*)(void))
104 dlsym(fcn_handle, "Dynamic_Module_Init");
106 if ((dl_error = dlerror()) != NULL)
108 fprintf(stderr,"DLoader_Init dlsym failed (%s)\n", dl_error);
112 dl_info = h_init_fcn();
114 printf("Loaded module %s v%d.%d\nBy %s (%s)\n", dl_info->module_name,
115 dl_info->major_version, dl_info->minor_version,
116 dl_info->module_author, dl_info->module_author_email);
122 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void)) {
124 struct CleanupFunctionHook *newfcn;
126 newfcn = (struct CleanupFunctionHook *)
127 malloc(sizeof(struct CleanupFunctionHook));
128 newfcn->next = CleanupHookTable;
129 newfcn->h_function_pointer = fcn_ptr;
130 CleanupHookTable = newfcn;
132 lprintf(5, "Registered a new cleanup function\n");
136 void CtdlRegisterSessionHook(void (*fcn_ptr)(void), int EventType) {
138 struct SessionFunctionHook *newfcn;
140 newfcn = (struct SessionFunctionHook *)
141 malloc(sizeof(struct SessionFunctionHook));
142 newfcn->next = SessionHookTable;
143 newfcn->h_function_pointer = fcn_ptr;
144 newfcn->eventtype = EventType;
145 SessionHookTable = newfcn;
147 lprintf(5, "Registered a new session function (type %d)\n",
152 void CtdlRegisterUserHook(void (*fcn_ptr)(char*, long), int EventType) {
154 struct UserFunctionHook *newfcn;
156 newfcn = (struct UserFunctionHook *)
157 malloc(sizeof(struct UserFunctionHook));
158 newfcn->next = UserHookTable;
159 newfcn->h_function_pointer = fcn_ptr;
160 newfcn->eventtype = EventType;
161 UserHookTable = newfcn;
163 lprintf(5, "Registered a new user function (type %d)\n",
168 void PerformSessionHooks(int EventType) {
169 struct SessionFunctionHook *fcn;
171 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
172 if (fcn->eventtype == EventType) {
173 (*fcn->h_function_pointer)();
178 void PerformUserHooks(char *username, long usernum, int EventType) {
179 struct UserFunctionHook *fcn;
181 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
182 if (fcn->eventtype == EventType) {
183 (*fcn->h_function_pointer)(username, usernum);