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