]> code.citadel.org Git - citadel.git/blob - citadel/dynloader.c
* Added a UserFunctionHook category to implement hooks which perform
[citadel.git] / citadel / dynloader.c
1 /*******************************************************
2  *
3  * Citadel Dynamic Loading Module
4  * Written by Brian Costello
5  * btx@calyx.net
6  *
7  ******************************************************/
8
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <dlfcn.h>
13 #include <sys/types.h>
14 #include <dirent.h>
15 #include <strings.h>
16 #include <syslog.h>
17 #include <pthread.h>
18 #include <limits.h>
19 #include "dynloader.h"
20 #include "citadel.h"
21 #include "server.h"
22 #include "sysdep_decls.h"
23
24 struct CleanupFunctionHook *CleanupHookTable = NULL;
25 struct SessionFunctionHook *SessionHookTable = NULL;
26 struct UserFunctionHook *UserHookTable = NULL;
27
28 struct ProtoFunctionHook
29 {
30   void (*handler)(char *cmdbuf);
31   char *cmd;
32   char *desc;
33   struct ProtoFunctionHook *next;
34 } *ProtoHookList = NULL;
35
36 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc)
37 {
38   struct ProtoFunctionHook *p = malloc(sizeof *p);
39   
40   if (p == NULL)
41     {
42       fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
43       exit(EXIT_FAILURE);
44     }
45
46   p->handler = handler;
47   p->cmd = cmd;
48   p->desc = desc;
49   p->next = ProtoHookList;
50   ProtoHookList = p;
51 }
52
53 int DLoader_Exec_Cmd(char *cmdbuf)
54 {
55   struct ProtoFunctionHook *p;
56
57   for (p = ProtoHookList; p; p = p->next)
58     {
59       if (!strncmp(cmdbuf, p->cmd, 4))
60         {
61           p->handler(&cmdbuf[5]);
62           return 1;
63         }
64     }
65   return 0;
66 }
67
68 void DLoader_Init(char *pathname)
69 {
70    void *fcn_handle;
71    char *dl_error;
72    DIR *dir;
73    struct dirent *dptr;
74    struct DLModule_Info* (*h_init_fcn)(void);
75    struct DLModule_Info *dl_info;
76
77    char pathbuf[PATH_MAX];
78    
79    if ((dir = opendir(pathname))==NULL)
80    {
81       perror("opendir");
82       exit(1);
83    }
84    
85    while ((dptr=readdir(dir))!= NULL)
86    {
87       if (dptr->d_name[0] == '.')
88          continue;
89    
90       snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
91       if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
92       {
93          dl_error = dlerror();
94          fprintf(stderr, "DLoader_Init dlopen failed (%s)\n", dl_error);
95          continue;
96       }
97       
98       h_init_fcn = dlsym(fcn_handle, "Dynamic_Module_Init");
99       if ((dl_error = dlerror()) != NULL)
100       {
101          fprintf(stderr,"DLoader_Init dlsym failed (%s)\n", dl_error);
102          continue;
103       }
104       
105       dl_info = h_init_fcn();
106
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);
110    }    /* While */
111 }
112
113
114
115 void CtdlRegisterCleanupHook(void *fcn_ptr) {
116
117         struct CleanupFunctionHook *newfcn;
118
119         newfcn = (struct CleanupFunctionHook *)
120                 malloc(sizeof(struct CleanupFunctionHook));
121         newfcn->next = CleanupHookTable;
122         newfcn->h_function_pointer = fcn_ptr;
123         CleanupHookTable = newfcn;
124
125         lprintf(5, "Registered a new cleanup function\n");
126         }
127
128
129 void CtdlRegisterSessionHook(void *fcn_ptr, int EventType) {
130
131         struct SessionFunctionHook *newfcn;
132
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;
139
140         lprintf(5, "Registered a new session function (type %d)\n", 
141                 EventType);
142         }
143
144
145 void CtdlRegisterUserHook(void *fcn_ptr, int EventType) {
146
147         struct UserFunctionHook *newfcn;
148
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;
155
156         lprintf(5, "Registered a new user function (type %d)\n", 
157                 EventType);
158         }
159
160
161 void PerformSessionHooks(int EventType) {
162         struct SessionFunctionHook *fcn;
163
164         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
165                 if (fcn->eventtype == EventType) {
166                         (*fcn->h_function_pointer)();
167                         }
168                 }
169         }
170
171 void PerformUserHooks(char *username, long usernum, int EventType) {
172         struct UserFunctionHook *fcn;
173
174         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
175                 if (fcn->eventtype == EventType) {
176                         (*fcn->h_function_pointer)(username, usernum);
177                         }
178                 }
179         }
180