21550ed872be44eaa9f0487e28b72a4ffbb17cbc
[citadel.git] / citadel / include / ctdl_module.h
1 /* $Id$ */
2
3 #ifndef CTDL_MODULE_H
4 #define CTDL_MODULE_H
5
6 #include "sysdep.h"
7
8 #ifdef HAVE_GC
9 #define GC_THREADS
10 #define GC_REDIRECT_TO_LOCAL
11 #include <gc/gc_local_alloc.h>
12 #else
13 #define GC_MALLOC malloc
14 #define GC_MALLOC_ATOMIC malloc
15 #define GC_FREE free
16 #define GC_REALLOC realloc
17 #endif
18
19
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <ctype.h>
25 #include <signal.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <sys/types.h>
29
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
32 # include <time.h>
33 #else
34 # if HAVE_SYS_TIME_H
35 #  include <sys/time.h>
36 # else
37 #  include <time.h>
38 # endif
39 #endif
40
41 #include <sys/wait.h>
42 #include <string.h>
43 #ifdef HAVE_STRINGS_H
44 #include <strings.h>
45 #endif
46 #include <limits.h>
47
48
49 #ifndef HAVE_SNPRINTF
50 #include "snprintf.h"
51 #endif
52
53
54 #include <libcitadel.h>
55
56 #include "server.h"
57 #include "sysdep_decls.h"
58 #include "msgbase.h"
59 #include "threads.h"
60 #include "citadel_dirs.h"
61 #include "context.h"
62
63 /*
64  * define macros for module init stuff
65  */
66  
67 #define CTDL_MODULE_INIT(module_name) char *ctdl_module_##module_name##_init (int threading)
68
69 #define CTDL_INIT_CALL(module_name) ctdl_module_##module_name##_init (threading)
70
71 #define CTDL_MODULE_UPGRADE(module_name) char *ctdl_module_##module_name##_upgrade (void)
72
73 #define CTDL_UPGRADE_CALL(module_name) ctdl_module_##module_name##_upgrade ()
74
75
76 /*
77  * Prototype for making log entries in Citadel.
78  */
79
80 void CtdlLogPrintf(enum LogLevel loglevel, const char *format, ...);
81
82 #define CtdlAideMessage(TEXT, SUBJECT) quickie_message("Citadel",NULL,NULL,AIDEROOM,TEXT,FMT_CITADEL,SUBJECT) 
83
84 /*
85  * Hook functions available to modules.
86  */
87
88 void CtdlRegisterSessionHook(void (*fcn_ptr)(void), int EventType);
89 void CtdlUnregisterSessionHook(void (*fcn_ptr)(void), int EventType);
90
91 void CtdlRegisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
92 void CtdlUnregisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
93
94 void CtdlRegisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
95 void CtdlUnregisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
96
97 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
98                                                         int EventType);
99 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
100                                                         int EventType);
101
102 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
103 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
104
105 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *) );
106 void CtdlUnregisterRoomHook(int (*fnc_ptr)(struct ctdlroom *) );
107
108 void CtdlRegisterDeleteHook(void (*handler)(char *, long) );
109 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) );
110
111 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void));
112 void CtdlUnregisterCleanupHook(void (*fcn_ptr)(void));
113
114 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc);
115
116 void CtdlRegisterServiceHook(int tcp_port,
117                              char *sockpath,
118                              void (*h_greeting_function) (void),
119                              void (*h_command_function) (void),
120                              void (*h_async_function) (void),
121                              const char *ServiceName
122 );
123 void CtdlUnregisterServiceHook(int tcp_port,
124                         char *sockpath,
125                         void (*h_greeting_function) (void),
126                         void (*h_command_function) (void),
127                         void (*h_async_function) (void)
128 );
129
130 void CtdlRegisterFixedOutputHook(char *content_type,
131                         void (*output_function) (char *supplied_data, int len)
132 );
133 void CtdlUnRegisterFixedOutputHook(char *content_type);
134
135 void CtdlRegisterMaintenanceThread(char *name, void *(*thread_proc) (void *arg));
136
137 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name);
138
139
140 /*
141  * Directory services hooks for LDAP etc
142  */
143
144 #define DIRECTORY_USER_DEL 1    // Delete a user entry
145 #define DIRECTORY_CREATE_HOST 2 // Create a host entry if not already there.
146 #define DIRECTORY_CREATE_OBJECT 3       // Create a new object for directory entry
147 #define DIRECTORY_ATTRIB_ADD 4  // Add an attribute to the directory entry object
148 #define DIRECTORY_SAVE_OBJECT 5 // Save the object to the directory service
149 #define DIRECTORY_FREE_OBJECT 6 // Free the object and its attributes
150
151 int CtdlRegisterDirectoryServiceFunc(int (*func)(char *cn, char *ou, void **object), int cmd, char *module);
152 int CtdlDoDirectoryServiceFunc(char *cn, char *ou, void **object, char *module, int cmd);
153
154 /* TODODRW: This needs to be changed into a hook type interface
155  * for now we have this horrible hack
156  */
157 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response);
158
159
160 /*
161  * Citadel Threads API
162  */
163 struct CtdlThreadNode *CtdlThreadCreate(char *name, long flags, void *(*thread_func) (void *arg), void *args);
164 struct CtdlThreadNode *CtdlThreadSchedule(char *name, long flags, void *(*thread_func) (void *arg), void *args, time_t when);
165 void CtdlThreadSleep(int secs);
166 void CtdlThreadStop(struct CtdlThreadNode *thread);
167 int CtdlThreadCheckStop(void);
168 /* void CtdlThreadCancel2(struct CtdlThreadNode *thread); Leave this out, it should never be needed */
169 const char *CtdlThreadName(const char *name);
170 struct CtdlThreadNode *CtdlThreadSelf(void);
171 int CtdlThreadGetCount(void);
172 int CtdlThreadGetWorkers(void);
173 double CtdlThreadGetWorkerAvg(void);
174 double CtdlThreadGetLoadAvg(void);
175 void CtdlThreadGC(void);
176 void CtdlThreadStopAll(void);
177 int CtdlThreadSelect(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout);
178 void CtdlThreadAllocTSD(void);
179
180 #define CTDLTHREAD_BIGSTACK     0x0001
181 #define CTDLTHREAD_WORKER       0x0002
182
183 /* Macros to speed up getting outr thread */
184
185 #define MYCURSORS       (((ThreadTSD*)pthread_getspecific(ThreadKey))->cursors)
186 #define MYTID           (((ThreadTSD*)pthread_getspecific(ThreadKey))->tid)
187 #define CT              (((ThreadTSD*)pthread_getspecific(ThreadKey))->self)
188 #define CTP             ((ThreadTSD*)pthread_getspecific(ThreadKey))
189
190 /** return the current context list as an array and do it in a safe manner
191  * The returned data is a copy so only reading is useful
192  * The number of contexts is returned in count.
193  * Beware, this does not copy any of the data pointed to by the context.
194  * This means that you can not rely on things like the redirect buffer being valid.
195  * You must free the returned pointer when done.
196  */
197 struct CitContext *CtdlGetContextArray (int *count);
198 void CtdlFillSystemContext(struct CitContext *context, char *name);
199 void CtdlClearSystemContext(void);
200 int CtdlTrySingleUser(void);
201 void CtdlEndSingleUser(void);
202 int CtdlWantSingleUser(void);
203 int CtdlIsSingleUser(void);
204
205
206 int CtdlIsUserLoggedIn (char *user_name);
207 int CtdlIsUserLoggedInByNum (long usernum);
208 void CtdlBumpNewMailCounter(long which_user);
209
210
211 /*
212  * CtdlGetCurrentMessageNumber()  -  Obtain the current highest message number in the system
213  * This provides a quick way to initialise a variable that might be used to indicate
214  * messages that should not be processed. EG. a new Sieve script will use this
215  * to record determine that messages older than this should not be processed.
216  * This function is defined in control.c
217  */
218 long CtdlGetCurrentMessageNumber(void);
219
220
221
222 /*
223  * Expose various room operation functions from room_ops.c to the modules API
224  */
225
226 unsigned CtdlCreateRoom(char *new_room_name,
227                         int new_room_type,
228                         char *new_room_pass,
229                         int new_room_floor,
230                         int really_create,
231                         int avoid_access,
232                         int new_room_view);
233 int CtdlGetRoom(struct ctdlroom *qrbuf, char *room_name);
234 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name);
235 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr);
236 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
237                 int *result, int *view);
238 void CtdlPutRoomLock(struct ctdlroom *qrbuf);
239 void CtdlForEachRoom(void (*CallBack)(struct ctdlroom *EachRoom, void *out_data),
240         void *in_data);
241 void CtdlDeleteRoom(struct ctdlroom *qrbuf);
242 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor);
243 void CtdlUserGoto (char *where, int display_result, int transiently,
244                         int *msgs, int *new);
245 struct floor *CtdlGetCachedFloor(int floor_num);
246 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf);
247 void CtdlGetFloor (struct floor *flbuf, int floor_num);
248 void CtdlPutFloor (struct floor *flbuf, int floor_num);
249 void CtdlPutFloorLock(struct floor *flbuf, int floor_num);
250 int CtdlGetFloorByName(const char *floor_name);
251 int CtdlGetFloorByNameLock(const char *floor_name);
252 int CtdlGetAvailableFloor(void);
253 int CtdlIsNonEditable(struct ctdlroom *qrbuf);
254 void CtdlPutRoom(struct ctdlroom *);
255
256 /*
257  * Possible return values for CtdlRenameRoom()
258  */
259 enum {
260         crr_ok,                         /* success */
261         crr_room_not_found,             /* room not found */
262         crr_already_exists,             /* new name already exists */
263         crr_noneditable,                /* cannot edit this room */
264         crr_invalid_floor,              /* target floor does not exist */
265         crr_access_denied               /* not allowed to edit this room */
266 };
267
268
269
270 /*
271  * API declarations from citserver.h
272  */
273 int CtdlAccessCheck(int);
274 /* 'required access level' values which may be passed to CtdlAccessCheck()
275  */
276 enum {
277         ac_none,
278         ac_logged_in,
279         ac_room_aide,
280         ac_aide,
281         ac_internal
282 };
283
284
285
286 /*
287  * API declarations from serv_extensions.h
288  */
289 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name);
290 /* 
291  * Global system configuration.  Don't change anything here.  It's all in dtds/config-defs.h now.
292  */
293 struct config {
294 #include "datadefinitions.h"
295 #include "dtds/config-defs.h"
296 #include "undef_data.h"
297 };
298
299 extern struct config config;
300
301
302
303 /*
304  * Expose API calls from user_ops.c
305  */
306 int CtdlGetUser(struct ctdluser *usbuf, char *name);
307 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len);
308 int CtdlGetUserLock(struct ctdluser *usbuf, char *name);
309 void CtdlPutUser(struct ctdluser *usbuf);
310 void CtdlPutUserLock(struct ctdluser *usbuf);
311 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number);
312 void CtdlGetRelationship(struct visit *vbuf,
313                         struct ctdluser *rel_user,
314                         struct ctdlroom *rel_room);
315 void CtdlSetRelationship(struct visit *newvisit,
316                         struct ctdluser *rel_user,
317                         struct ctdlroom *rel_room);
318 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix);
319
320 int CtdlLoginExistingUser(char *authname, const char *username);
321
322 /*
323  * Values which may be returned by CtdlLoginExistingUser()
324  */
325 enum {
326         pass_ok,
327         pass_already_logged_in,
328         pass_no_user,
329         pass_internal_error,
330         pass_wrong_password
331 };
332
333 int CtdlTryPassword(const char *password, long len);
334 /*
335  * Values which may be returned by CtdlTryPassword()
336  */
337 enum {
338         login_ok,
339         login_already_logged_in,
340         login_too_many_users,
341         login_not_found
342 };
343
344 void CtdlUserLogout(void);
345
346
347
348
349 /*
350  * Expose API calls from msgbase.c
351  */
352 char *CtdlGetSysConfig(char *sysconfname);
353 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata);
354
355
356
357
358 /*
359  * Expose API calls from euidindex.c
360  */
361 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf);
362
363
364 #endif /* CTDL_MODULE_H */