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"
24 struct CleanupFunctionHook *CleanupHookTable = NULL;
25 struct SessionFunctionHook *SessionHookTable = NULL;
26 struct UserFunctionHook *UserHookTable = NULL;
28 struct ProtoFunctionHook
30 void (*handler)(char *cmdbuf);
33 struct ProtoFunctionHook *next;
34 } *ProtoHookList = NULL;
36 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc)
38 struct ProtoFunctionHook *p = malloc(sizeof *p);
42 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
49 p->next = ProtoHookList;
53 int DLoader_Exec_Cmd(char *cmdbuf)
55 struct ProtoFunctionHook *p;
57 for (p = ProtoHookList; p; p = p->next)
59 if (!strncmp(cmdbuf, p->cmd, 4))
61 p->handler(&cmdbuf[5]);
68 void DLoader_Init(char *pathname)
74 struct DLModule_Info* (*h_init_fcn)(void);
75 struct DLModule_Info *dl_info;
77 char pathbuf[PATH_MAX];
79 if ((dir = opendir(pathname))==NULL)
85 while ((dptr=readdir(dir))!= NULL)
87 if (dptr->d_name[0] == '.')
90 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
91 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
94 fprintf(stderr, "DLoader_Init dlopen failed (%s)\n", dl_error);
98 h_init_fcn = dlsym(fcn_handle, "Dynamic_Module_Init");
99 if ((dl_error = dlerror()) != NULL)
101 fprintf(stderr,"DLoader_Init dlsym failed (%s)\n", dl_error);
105 dl_info = h_init_fcn();
107 printf("Loaded module %s v%d.%d\nBy %s (%s)\n", dl_info->module_name,
108 dl_info->major_version, dl_info->minor_version,
109 dl_info->module_author, dl_info->module_author_email);
115 void CtdlRegisterCleanupHook(void *fcn_ptr) {
117 struct CleanupFunctionHook *newfcn;
119 newfcn = (struct CleanupFunctionHook *)
120 malloc(sizeof(struct CleanupFunctionHook));
121 newfcn->next = CleanupHookTable;
122 newfcn->h_function_pointer = fcn_ptr;
123 CleanupHookTable = newfcn;
125 lprintf(5, "Registered a new cleanup function\n");
129 void CtdlRegisterSessionHook(void *fcn_ptr, int EventType) {
131 struct SessionFunctionHook *newfcn;
133 newfcn = (struct SessionFunctionHook *)
134 malloc(sizeof(struct SessionFunctionHook));
135 newfcn->next = SessionHookTable;
136 newfcn->h_function_pointer = fcn_ptr;
137 newfcn->eventtype = EventType;
138 SessionHookTable = newfcn;
140 lprintf(5, "Registered a new session function (type %d)\n",
145 void CtdlRegisterUserHook(void *fcn_ptr, int EventType) {
147 struct UserFunctionHook *newfcn;
149 newfcn = (struct UserFunctionHook *)
150 malloc(sizeof(struct UserFunctionHook));
151 newfcn->next = UserHookTable;
152 newfcn->h_function_pointer = fcn_ptr;
153 newfcn->eventtype = EventType;
154 UserHookTable = newfcn;
156 lprintf(5, "Registered a new user function (type %d)\n",
161 void PerformSessionHooks(int EventType) {
162 struct SessionFunctionHook *fcn;
164 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
165 if (fcn->eventtype == EventType) {
166 (*fcn->h_function_pointer)();
171 void PerformUserHooks(char *username, long usernum, int EventType) {
172 struct UserFunctionHook *fcn;
174 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
175 if (fcn->eventtype == EventType) {
176 (*fcn->h_function_pointer)(username, usernum);