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 NewRoomFunctionHook *NewRoomHookTable = NULL;
26 struct SessionFunctionHook *SessionHookTable = NULL;
27 struct LoginFunctionHook *LoginHookTable = NULL;
29 struct ProtoFunctionHook
31 void (*handler)(char *cmdbuf);
34 struct ProtoFunctionHook *next;
35 } *ProtoHookList = NULL;
37 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc)
39 struct ProtoFunctionHook *p = malloc(sizeof *p);
43 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
50 p->next = ProtoHookList;
54 int DLoader_Exec_Cmd(char *cmdbuf)
56 struct ProtoFunctionHook *p;
58 for (p = ProtoHookList; p; p = p->next)
60 if (!strncmp(cmdbuf, p->cmd, 4))
62 p->handler(&cmdbuf[5]);
69 void DLoader_Init(char *pathname)
75 struct DLModule_Info* (*h_init_fcn)(void);
76 struct DLModule_Info *dl_info;
78 char pathbuf[PATH_MAX];
80 if ((dir = opendir(pathname))==NULL)
86 while ((dptr=readdir(dir))!= NULL)
88 if (dptr->d_name[0] == '.')
91 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
92 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
95 fprintf(stderr, "DLoader_Init dlopen failed (%s)\n", dl_error);
99 h_init_fcn = dlsym(fcn_handle, "Dynamic_Module_Init");
100 if ((dl_error = dlerror()) != NULL)
102 fprintf(stderr,"DLoader_Init dlsym failed (%s)\n", dl_error);
106 dl_info = h_init_fcn();
108 printf("Loaded module %s v%d.%d\nBy %s (%s)\n", dl_info->module_name,
109 dl_info->major_version, dl_info->minor_version,
110 dl_info->module_author, dl_info->module_author_email);
116 void CtdlRegisterCleanupHook(void *fcn_ptr) {
118 struct CleanupFunctionHook *newfcn;
120 newfcn = (struct CleanupFunctionHook *)
121 malloc(sizeof(struct CleanupFunctionHook));
122 newfcn->next = CleanupHookTable;
123 newfcn->h_function_pointer = fcn_ptr;
124 CleanupHookTable = newfcn;
126 lprintf(5, "Registered a new cleanup function\n");
129 void CtdlRegisterNewRoomHook(void *fcn_ptr) {
131 struct NewRoomFunctionHook *newfcn;
133 newfcn = (struct NewRoomFunctionHook *)
134 malloc(sizeof(struct NewRoomFunctionHook));
135 newfcn->next = NewRoomHookTable;
136 newfcn->h_function_pointer = fcn_ptr;
137 NewRoomHookTable = newfcn;
139 lprintf(5, "Registered a new NewRoom function\n");
142 void CtdlRegisterSessionHook(void *fcn_ptr, int StartStop) {
144 struct SessionFunctionHook *newfcn;
146 newfcn = (struct SessionFunctionHook *)
147 malloc(sizeof(struct SessionFunctionHook));
148 newfcn->next = SessionHookTable;
149 newfcn->h_function_pointer = fcn_ptr;
150 newfcn->startstop = StartStop;
151 SessionHookTable = newfcn;
153 lprintf(5, "Registered a new session %s function\n",
154 (StartStop ? "start" : "stop") );
157 void CtdlRegisterLoginHook(void *fcn_ptr) {
159 struct LoginFunctionHook *newfcn;
161 newfcn = (struct LoginFunctionHook *)
162 malloc(sizeof(struct LoginFunctionHook));
163 newfcn->next = LoginHookTable;
164 newfcn->h_function_pointer = fcn_ptr;
165 LoginHookTable = newfcn;
167 lprintf(5, "Registered a new login function\n");