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 (!strncmp(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 = dlsym(fcn_handle, "Dynamic_Module_Init");
104 if ((dl_error = dlerror()) != NULL)
106 fprintf(stderr,"DLoader_Init dlsym failed (%s)\n", dl_error);
110 dl_info = h_init_fcn();
112 printf("Loaded module %s v%d.%d\nBy %s (%s)\n", dl_info->module_name,
113 dl_info->major_version, dl_info->minor_version,
114 dl_info->module_author, dl_info->module_author_email);
120 void CtdlRegisterCleanupHook(void *fcn_ptr) {
122 struct CleanupFunctionHook *newfcn;
124 newfcn = (struct CleanupFunctionHook *)
125 malloc(sizeof(struct CleanupFunctionHook));
126 newfcn->next = CleanupHookTable;
127 newfcn->h_function_pointer = fcn_ptr;
128 CleanupHookTable = newfcn;
130 lprintf(5, "Registered a new cleanup function\n");
134 void CtdlRegisterSessionHook(void *fcn_ptr, int EventType) {
136 struct SessionFunctionHook *newfcn;
138 newfcn = (struct SessionFunctionHook *)
139 malloc(sizeof(struct SessionFunctionHook));
140 newfcn->next = SessionHookTable;
141 newfcn->h_function_pointer = fcn_ptr;
142 newfcn->eventtype = EventType;
143 SessionHookTable = newfcn;
145 lprintf(5, "Registered a new session function (type %d)\n",
150 void CtdlRegisterUserHook(void *fcn_ptr, int EventType) {
152 struct UserFunctionHook *newfcn;
154 newfcn = (struct UserFunctionHook *)
155 malloc(sizeof(struct UserFunctionHook));
156 newfcn->next = UserHookTable;
157 newfcn->h_function_pointer = fcn_ptr;
158 newfcn->eventtype = EventType;
159 UserHookTable = newfcn;
161 lprintf(5, "Registered a new user function (type %d)\n",
166 void PerformSessionHooks(int EventType) {
167 struct SessionFunctionHook *fcn;
169 for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
170 if (fcn->eventtype == EventType) {
171 (*fcn->h_function_pointer)();
176 void PerformUserHooks(char *username, long usernum, int EventType) {
177 struct UserFunctionHook *fcn;
179 for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
180 if (fcn->eventtype == EventType) {
181 (*fcn->h_function_pointer)(username, usernum);