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 LogFunctionHook *LogHookTable = NULL;
32 struct CleanupFunctionHook *CleanupHookTable = NULL;
33 struct SessionFunctionHook *SessionHookTable = NULL;
34 struct UserFunctionHook *UserHookTable = NULL;
36 struct ProtoFunctionHook
38 void (*handler)(char *cmdbuf);
41 struct ProtoFunctionHook *next;
42 } *ProtoHookList = NULL;
44 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc)
46 struct ProtoFunctionHook *p = malloc(sizeof *p);
50 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
57 p->next = ProtoHookList;
61 int DLoader_Exec_Cmd(char *cmdbuf)
63 struct ProtoFunctionHook *p;
65 for (p = ProtoHookList; p; p = p->next)
67 if (!strncasecmp(cmdbuf, p->cmd, 4))
69 p->handler(&cmdbuf[5]);
76 void DLoader_Init(char *pathname)
82 struct DLModule_Info* (*h_init_fcn)(void);
83 struct DLModule_Info *dl_info;
85 char pathbuf[PATH_MAX];
87 if ((dir = opendir(pathname))==NULL)
93 while ((dptr=readdir(dir))!= NULL)
95 if (dptr->d_name[0] == '.')
98 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
99 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
101 dl_error = dlerror();
102 fprintf(stderr, "DLoader_Init dlopen failed (%s)\n", dl_error);
106 h_init_fcn = (struct DLModule_Info * (*)(void))
107 dlsym(fcn_handle, "Dynamic_Module_Init");
109 if ((dl_error = dlerror()) != NULL)
111 fprintf(stderr,"DLoader_Init dlsym failed (%s)\n", dl_error);
115 dl_info = h_init_fcn();
117 printf("Loaded module %s v%d.%d\nBy %s (%s)\n", dl_info->module_name,
118 dl_info->major_version, dl_info->minor_version,
119 dl_info->module_author, dl_info->module_author_email);
125 void CtdlRegisterLogHook(void (*fcn_ptr)(char *), int loglevel) {
127 struct LogFunctionHook *newfcn;
129 newfcn = (struct LogFunctionHook *)
130 malloc(sizeof(struct LogFunctionHook));
131 newfcn->next = LogHookTable;
132 newfcn->h_function_pointer = fcn_ptr;
133 newfcn->loglevel = loglevel;
134 LogHookTable = newfcn;
136 lprintf(5, "Registered a new logging function\n");
140 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void)) {
142 struct CleanupFunctionHook *newfcn;
144 newfcn = (struct CleanupFunctionHook *)
145 malloc(sizeof(struct CleanupFunctionHook));
146 newfcn->next = CleanupHookTable;
147 newfcn->h_function_pointer = fcn_ptr;
148 CleanupHookTable = newfcn;
150 lprintf(5, "Registered a new cleanup function\n");
154 void CtdlRegisterSessionHook(void (*fcn_ptr)(void), int EventType) {
156 struct SessionFunctionHook *newfcn;
158 newfcn = (struct SessionFunctionHook *)
159 malloc(sizeof(struct SessionFunctionHook));
160 newfcn->next = SessionHookTable;
161 newfcn->h_function_pointer = fcn_ptr;
162 newfcn->eventtype = EventType;
163 SessionHookTable = newfcn;
165 lprintf(5, "Registered a new session function (type %d)\n",
170 void CtdlRegisterUserHook(void (*fcn_ptr)(char*, long), int EventType) {
172 struct UserFunctionHook *newfcn;
174 newfcn = (struct UserFunctionHook *)
175 malloc(sizeof(struct UserFunctionHook));
176 newfcn->next = UserHookTable;
177 newfcn->h_function_pointer = fcn_ptr;
178 newfcn->eventtype = EventType;
179 UserHookTable = newfcn;
181 lprintf(5, "Registered a new user function (type %d)\n",
186 void PerformSessionHooks(int EventType) {
187 struct SessionFunctionHook *fcn;
189 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
190 if (fcn->eventtype == EventType) {
191 (*fcn->h_function_pointer)();
196 void PerformLogHooks(int loglevel, char *logmsg) {
197 struct LogFunctionHook *fcn;
199 for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
200 if (fcn->loglevel >= loglevel) {
201 (*fcn->h_function_pointer)(logmsg);
206 void PerformUserHooks(char *username, long usernum, int EventType) {
207 struct UserFunctionHook *fcn;
209 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
210 if (fcn->eventtype == EventType) {
211 (*fcn->h_function_pointer)(username, usernum);