]> code.citadel.org Git - citadel.git/blob - citadel/dynloader.c
* Finished off the message architecture stuff with a new class of hooks to
[citadel.git] / citadel / dynloader.c
1 /*******************************************************
2  *
3  * Citadel Dynamic Loading Module
4  * Written by Brian Costello
5  * btx@calyx.net
6  *
7  * $Id$
8  *
9  ******************************************************/
10
11
12 #include "sysdep.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <dlfcn.h>
16 #include <sys/types.h>
17 #include <dirent.h>
18 #include <strings.h>
19 #include <syslog.h>
20 #ifdef HAVE_PTHREAD_H
21 #include <pthread.h>
22 #endif
23 #include <limits.h>
24 #include <ctype.h>
25 #include "citadel.h"
26 #include "server.h"
27 #include "dynloader.h"
28 #include "sysdep_decls.h"
29 #include "msgbase.h"
30 #include "tools.h"
31
32 #ifndef HAVE_SNPRINTF
33 #include <stdarg.h>
34 #include "snprintf.h"
35 #endif
36
37 struct LogFunctionHook *LogHookTable = NULL;
38 struct CleanupFunctionHook *CleanupHookTable = NULL;
39 struct SessionFunctionHook *SessionHookTable = NULL;
40 struct UserFunctionHook *UserHookTable = NULL;
41 struct XmsgFunctionHook *XmsgHookTable = NULL;
42 struct MessageFunctionHook *MessageHookTable = NULL;
43
44 struct ProtoFunctionHook {
45         void (*handler) (char *cmdbuf);
46         char *cmd;
47         char *desc;
48         struct ProtoFunctionHook *next;
49 } *ProtoHookList = NULL;
50
51 void CtdlRegisterProtoHook(void (*handler) (char *), char *cmd, char *desc)
52 {
53         struct ProtoFunctionHook *p = mallok(sizeof *p);
54
55         if (p == NULL) {
56                 fprintf(stderr, "can't malloc new ProtoFunctionHook\n");
57                 exit(EXIT_FAILURE);
58         }
59         p->handler = handler;
60         p->cmd = cmd;
61         p->desc = desc;
62         p->next = ProtoHookList;
63         ProtoHookList = p;
64         lprintf(5, "Registered server command %s (%s)\n", cmd, desc);
65 }
66
67 int DLoader_Exec_Cmd(char *cmdbuf)
68 {
69         struct ProtoFunctionHook *p;
70
71         for (p = ProtoHookList; p; p = p->next) {
72                 if (!strncasecmp(cmdbuf, p->cmd, 4)) {
73                         p->handler(&cmdbuf[5]);
74                         return 1;
75                 }
76         }
77         return 0;
78 }
79
80 void DLoader_Init(char *pathname)
81 {
82         void *fcn_handle;
83         char dl_error[256];
84         DIR *dir;
85         int i;
86         struct dirent *dptr;
87         char *(*h_init_fcn) (void);
88         char *dl_info;
89
90         char pathbuf[PATH_MAX];
91
92         if ((dir = opendir(pathname)) == NULL) {
93                 perror("opendir");
94                 exit(1);
95         }
96         while ((dptr = readdir(dir)) != NULL) {
97                 if (dptr->d_name[0] == '.')
98                         continue;
99
100                 snprintf(pathbuf, PATH_MAX, "%s/%s", pathname, dptr->d_name);
101 #ifdef RTLD_NOW
102                 if (!(fcn_handle = dlopen(pathbuf, RTLD_NOW)))
103 #else                           /* OpenBSD */
104                 if (!(fcn_handle = dlopen(pathbuf, DL_LAZY)))
105 #endif
106                 {
107                         safestrncpy(dl_error, dlerror(), sizeof dl_error);
108                         for (i=0; i<strlen(dl_error); ++i)
109                                 if (!isprint(dl_error[i]))
110                                         dl_error[i]='.';
111                         fprintf(stderr, "DLoader_Init dlopen failed: %s\n",
112                                 dl_error);
113                         continue;
114                 }
115                 h_init_fcn = (char * (*)(void))
116 #ifndef __OpenBSD__
117                     dlsym(fcn_handle, "Dynamic_Module_Init");
118 #else
119                     dlsym(fcn_handle, "_Dynamic_Module_Init");
120 #endif
121
122                 if (dlerror() != NULL) {
123                         fprintf(stderr, "DLoader_Init dlsym failed\n");
124                         continue;
125                 }
126                 dl_info = h_init_fcn();
127
128                 lprintf(3, "Loaded module: %s\n", dl_info);
129         }       /* While */
130 }
131
132
133
134 void CtdlRegisterLogHook(void (*fcn_ptr) (char *), int loglevel)
135 {
136
137         struct LogFunctionHook *newfcn;
138
139         newfcn = (struct LogFunctionHook *)
140             mallok(sizeof(struct LogFunctionHook));
141         newfcn->next = LogHookTable;
142         newfcn->h_function_pointer = fcn_ptr;
143         newfcn->loglevel = loglevel;
144         LogHookTable = newfcn;
145
146         lprintf(5, "Registered a new logging function\n");
147 }
148
149
150 void CtdlRegisterCleanupHook(void (*fcn_ptr) (void))
151 {
152
153         struct CleanupFunctionHook *newfcn;
154
155         newfcn = (struct CleanupFunctionHook *)
156             mallok(sizeof(struct CleanupFunctionHook));
157         newfcn->next = CleanupHookTable;
158         newfcn->h_function_pointer = fcn_ptr;
159         CleanupHookTable = newfcn;
160
161         lprintf(5, "Registered a new cleanup function\n");
162 }
163
164
165 void CtdlRegisterSessionHook(void (*fcn_ptr) (void), int EventType)
166 {
167
168         struct SessionFunctionHook *newfcn;
169
170         newfcn = (struct SessionFunctionHook *)
171             mallok(sizeof(struct SessionFunctionHook));
172         newfcn->next = SessionHookTable;
173         newfcn->h_function_pointer = fcn_ptr;
174         newfcn->eventtype = EventType;
175         SessionHookTable = newfcn;
176
177         lprintf(5, "Registered a new session function (type %d)\n",
178                 EventType);
179 }
180
181
182 void CtdlRegisterUserHook(void (*fcn_ptr) (char *, long), int EventType)
183 {
184
185         struct UserFunctionHook *newfcn;
186
187         newfcn = (struct UserFunctionHook *)
188             mallok(sizeof(struct UserFunctionHook));
189         newfcn->next = UserHookTable;
190         newfcn->h_function_pointer = fcn_ptr;
191         newfcn->eventtype = EventType;
192         UserHookTable = newfcn;
193
194         lprintf(5, "Registered a new user function (type %d)\n",
195                 EventType);
196 }
197
198
199 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
200                                 int EventType)
201 {
202
203         struct MessageFunctionHook *newfcn;
204
205         newfcn = (struct MessageFunctionHook *)
206             mallok(sizeof(struct MessageFunctionHook));
207         newfcn->next = MessageHookTable;
208         newfcn->h_function_pointer = handler;
209         newfcn->eventtype = EventType;
210         MessageHookTable = newfcn;
211
212         lprintf(5, "Registered a new message function (type %d)\n",
213                 EventType);
214 }
215
216
217 void CtdlRegisterXmsgHook(int (*fcn_ptr) (char *, char *, char *), int order)
218 {
219
220         struct XmsgFunctionHook *newfcn;
221
222         newfcn = (struct XmsgFunctionHook *)
223             mallok(sizeof(struct XmsgFunctionHook));
224         newfcn->next = XmsgHookTable;
225         newfcn->order = order;
226         newfcn->h_function_pointer = fcn_ptr;
227         XmsgHookTable = newfcn;
228         lprintf(5, "Registered a new x-msg function (priority %d)\n", order);
229 }
230
231
232 void PerformSessionHooks(int EventType)
233 {
234         struct SessionFunctionHook *fcn;
235
236         for (fcn = SessionHookTable; fcn != NULL; fcn = fcn->next) {
237                 if (fcn->eventtype == EventType) {
238                         (*fcn->h_function_pointer) ();
239                 }
240         }
241 }
242
243 void PerformLogHooks(int loglevel, char *logmsg)
244 {
245         struct LogFunctionHook *fcn;
246
247         for (fcn = LogHookTable; fcn != NULL; fcn = fcn->next) {
248                 if (fcn->loglevel >= loglevel) {
249                         (*fcn->h_function_pointer) (logmsg);
250                 }
251         }
252 }
253
254 void PerformUserHooks(char *username, long usernum, int EventType)
255 {
256         struct UserFunctionHook *fcn;
257
258         for (fcn = UserHookTable; fcn != NULL; fcn = fcn->next) {
259                 if (fcn->eventtype == EventType) {
260                         (*fcn->h_function_pointer) (username, usernum);
261                 }
262         }
263 }
264
265 int PerformMessageHooks(struct CtdlMessage *msg, int EventType)
266 {
267         struct MessageFunctionHook *fcn;
268         int total_retval = 0;
269
270         for (fcn = MessageHookTable; fcn != NULL; fcn = fcn->next) {
271                 if (fcn->eventtype == EventType) {
272                         total_retval = total_retval +
273                                 (*fcn->h_function_pointer) (msg);
274                 }
275         }
276
277         return total_retval;
278 }
279
280 int PerformXmsgHooks(char *sender, char *recp, char *msg)
281 {
282         struct XmsgFunctionHook *fcn;
283         int total_sent = 0;
284         int p;
285
286         for (p=0; p<MAX_XMSG_PRI; ++p) {
287                 for (fcn = XmsgHookTable; fcn != NULL; fcn = fcn->next) {
288                         if (fcn->order == p) {
289                                 total_sent +=
290                                         (*fcn->h_function_pointer)
291                                                 (sender, recp, msg);
292                         }
293                 }
294                 /* Break out of the loop if a higher-priority function
295                  * successfully delivered the message.  This prevents duplicate
296                  * deliveries to local users simultaneously signed onto
297                  * remote services.
298                  */
299                 if (total_sent) goto DONE;
300         }
301 DONE:   return total_sent;
302 }