1 /*******************************************************
3 * Citadel Dynamic Loading Module
4 * Written by Brian Costello
9 ******************************************************/
15 #include <sys/types.h>
21 #include "dynloader.h"
24 #include "sysdep_decls.h"
31 struct CleanupFunctionHook *CleanupHookTable = NULL;
32 struct SessionFunctionHook *SessionHookTable = NULL;
33 struct UserFunctionHook *UserHookTable = NULL;
35 struct ProtoFunctionHook
37 void (*handler)(char *cmdbuf);
40 struct ProtoFunctionHook *next;
41 } *ProtoHookList = NULL;
43 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc)
45 struct ProtoFunctionHook *p = malloc(sizeof *p);
49 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
56 p->next = ProtoHookList;
60 int DLoader_Exec_Cmd(char *cmdbuf)
62 struct ProtoFunctionHook *p;
64 for (p = ProtoHookList; p; p = p->next)
66 if (!strncasecmp(cmdbuf, p->cmd, 4))
68 p->handler(&cmdbuf[5]);
75 void DLoader_Init(char *pathname)
81 struct DLModule_Info* (*h_init_fcn)(void);
82 struct DLModule_Info *dl_info;
84 char pathbuf[PATH_MAX];
86 if ((dir = opendir(pathname))==NULL)
92 while ((dptr=readdir(dir))!= NULL)
94 if (dptr->d_name[0] == '.')
97 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
98 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
100 dl_error = dlerror();
101 fprintf(stderr, "DLoader_Init dlopen failed (%s)\n", dl_error);
105 h_init_fcn = (struct DLModule_Info * (*)(void))
106 dlsym(fcn_handle, "Dynamic_Module_Init");
108 if ((dl_error = dlerror()) != NULL)
110 fprintf(stderr,"DLoader_Init dlsym failed (%s)\n", dl_error);
114 dl_info = h_init_fcn();
116 printf("Loaded module %s v%d.%d\nBy %s (%s)\n", dl_info->module_name,
117 dl_info->major_version, dl_info->minor_version,
118 dl_info->module_author, dl_info->module_author_email);
124 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void)) {
126 struct CleanupFunctionHook *newfcn;
128 newfcn = (struct CleanupFunctionHook *)
129 malloc(sizeof(struct CleanupFunctionHook));
130 newfcn->next = CleanupHookTable;
131 newfcn->h_function_pointer = fcn_ptr;
132 CleanupHookTable = newfcn;
134 lprintf(5, "Registered a new cleanup function\n");
138 void CtdlRegisterSessionHook(void (*fcn_ptr)(void), int EventType) {
140 struct SessionFunctionHook *newfcn;
142 newfcn = (struct SessionFunctionHook *)
143 malloc(sizeof(struct SessionFunctionHook));
144 newfcn->next = SessionHookTable;
145 newfcn->h_function_pointer = fcn_ptr;
146 newfcn->eventtype = EventType;
147 SessionHookTable = newfcn;
149 lprintf(5, "Registered a new session function (type %d)\n",
154 void CtdlRegisterUserHook(void (*fcn_ptr)(char*, long), int EventType) {
156 struct UserFunctionHook *newfcn;
158 newfcn = (struct UserFunctionHook *)
159 malloc(sizeof(struct UserFunctionHook));
160 newfcn->next = UserHookTable;
161 newfcn->h_function_pointer = fcn_ptr;
162 newfcn->eventtype = EventType;
163 UserHookTable = newfcn;
165 lprintf(5, "Registered a new user function (type %d)\n",
170 void PerformSessionHooks(int EventType) {
171 struct SessionFunctionHook *fcn;
173 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
174 if (fcn->eventtype == EventType) {
175 (*fcn->h_function_pointer)();
180 void PerformUserHooks(char *username, long usernum, int EventType) {
181 struct UserFunctionHook *fcn;
183 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
184 if (fcn->eventtype == EventType) {
185 (*fcn->h_function_pointer)(username, usernum);