Re-Sort Cleanup of event io on shutdown
[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 <syslog.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 #define CtdlAideMessage(TEXT, SUBJECT) quickie_message("Citadel",NULL,NULL,AIDEROOM,TEXT,FMT_CITADEL,SUBJECT) 
76 /*
77  * Hook functions available to modules.
78  */
79
80 void CtdlRegisterSessionHook(void (*fcn_ptr)(void), int EventType);
81 void CtdlUnregisterSessionHook(void (*fcn_ptr)(void), int EventType);
82 void CtdlShutdownServiceHooks(void);
83
84 void CtdlRegisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
85 void CtdlUnregisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
86
87 void CtdlRegisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
88 void CtdlUnregisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
89
90 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *),
91                                                         int EventType);
92 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *),
93                                                         int EventType);
94
95 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
96 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
97
98 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *) );
99 void CtdlUnregisterRoomHook(int (*fnc_ptr)(struct ctdlroom *) );
100
101 void CtdlRegisterDeleteHook(void (*handler)(char *, long) );
102 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) );
103
104 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void));
105 void CtdlUnregisterCleanupHook(void (*fcn_ptr)(void));
106
107 void CtdlRegisterEVCleanupHook(void (*fcn_ptr)(void));
108 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr)(void));
109
110 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc);
111
112 void CtdlRegisterServiceHook(int tcp_port,
113                              char *sockpath,
114                              void (*h_greeting_function) (void),
115                              void (*h_command_function) (void),
116                              void (*h_async_function) (void),
117                              const char *ServiceName
118 );
119 void CtdlUnregisterServiceHook(int tcp_port,
120                         char *sockpath,
121                         void (*h_greeting_function) (void),
122                         void (*h_command_function) (void),
123                         void (*h_async_function) (void)
124 );
125
126 void CtdlRegisterFixedOutputHook(char *content_type,
127                         void (*output_function) (char *supplied_data, int len)
128 );
129 void CtdlUnRegisterFixedOutputHook(char *content_type);
130
131 void CtdlRegisterMaintenanceThread(char *name, void *(*thread_proc) (void *arg));
132
133 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name);
134
135
136 /*
137  * Directory services hooks for LDAP etc
138  */
139
140 #define DIRECTORY_USER_DEL 1    // Delete a user entry
141 #define DIRECTORY_CREATE_HOST 2 // Create a host entry if not already there.
142 #define DIRECTORY_CREATE_OBJECT 3       // Create a new object for directory entry
143 #define DIRECTORY_ATTRIB_ADD 4  // Add an attribute to the directory entry object
144 #define DIRECTORY_SAVE_OBJECT 5 // Save the object to the directory service
145 #define DIRECTORY_FREE_OBJECT 6 // Free the object and its attributes
146
147 int CtdlRegisterDirectoryServiceFunc(int (*func)(char *cn, char *ou, void **object), int cmd, char *module);
148 int CtdlDoDirectoryServiceFunc(char *cn, char *ou, void **object, char *module, int cmd);
149
150 /* TODODRW: This needs to be changed into a hook type interface
151  * for now we have this horrible hack
152  */
153 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response);
154
155 /* return the current context list as an array and do it in a safe manner
156  * The returned data is a copy so only reading is useful
157  * The number of contexts is returned in count.
158  * Beware, this does not copy any of the data pointed to by the context.
159  * This means that you can not rely on things like the redirect buffer being valid.
160  * You must free the returned pointer when done.
161  */
162 struct CitContext *CtdlGetContextArray (int *count);
163 void CtdlFillSystemContext(struct CitContext *context, char *name);
164 int CtdlTrySingleUser(void);
165 void CtdlEndSingleUser(void);
166 int CtdlWantSingleUser(void);
167 int CtdlIsSingleUser(void);
168
169
170 int CtdlIsUserLoggedIn (char *user_name);
171 int CtdlIsUserLoggedInByNum (long usernum);
172 void CtdlBumpNewMailCounter(long which_user);
173
174
175 /*
176  * CtdlGetCurrentMessageNumber()  -  Obtain the current highest message number in the system
177  * This provides a quick way to initialise a variable that might be used to indicate
178  * messages that should not be processed. EG. a new Sieve script will use this
179  * to record determine that messages older than this should not be processed.
180  * This function is defined in control.c
181  */
182 long CtdlGetCurrentMessageNumber(void);
183
184
185
186 /*
187  * Expose various room operation functions from room_ops.c to the modules API
188  */
189
190 unsigned CtdlCreateRoom(char *new_room_name,
191                         int new_room_type,
192                         char *new_room_pass,
193                         int new_room_floor,
194                         int really_create,
195                         int avoid_access,
196                         int new_room_view);
197 int CtdlGetRoom(struct ctdlroom *qrbuf, char *room_name);
198 int CtdlGetRoomLock(struct ctdlroom *qrbuf, char *room_name);
199 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr);
200 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf,
201                 int *result, int *view);
202 void CtdlPutRoomLock(struct ctdlroom *qrbuf);
203 void CtdlForEachRoom(void (*CallBack)(struct ctdlroom *EachRoom, void *out_data),
204         void *in_data);
205 void CtdlDeleteRoom(struct ctdlroom *qrbuf);
206 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor);
207 void CtdlUserGoto (char *where, int display_result, int transiently,
208                         int *msgs, int *new);
209 struct floor *CtdlGetCachedFloor(int floor_num);
210 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf);
211 void CtdlGetFloor (struct floor *flbuf, int floor_num);
212 void CtdlPutFloor (struct floor *flbuf, int floor_num);
213 void CtdlPutFloorLock(struct floor *flbuf, int floor_num);
214 int CtdlGetFloorByName(const char *floor_name);
215 int CtdlGetFloorByNameLock(const char *floor_name);
216 int CtdlGetAvailableFloor(void);
217 int CtdlIsNonEditable(struct ctdlroom *qrbuf);
218 void CtdlPutRoom(struct ctdlroom *);
219
220 /*
221  * Possible return values for CtdlRenameRoom()
222  */
223 enum {
224         crr_ok,                         /* success */
225         crr_room_not_found,             /* room not found */
226         crr_already_exists,             /* new name already exists */
227         crr_noneditable,                /* cannot edit this room */
228         crr_invalid_floor,              /* target floor does not exist */
229         crr_access_denied               /* not allowed to edit this room */
230 };
231
232
233
234 /*
235  * API declarations from citserver.h
236  */
237 int CtdlAccessCheck(int);
238 /* 'required access level' values which may be passed to CtdlAccessCheck()
239  */
240 enum {
241         ac_none,
242         ac_logged_in_or_guest,
243         ac_logged_in,
244         ac_room_aide,
245         ac_aide,
246         ac_internal,
247 };
248
249
250
251 /*
252  * API declarations from serv_extensions.h
253  */
254 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name);
255
256 /* 
257  * Global system configuration
258  */
259 struct config {
260         char c_nodename[16];            /* short name of this node on a Citadel network */
261         char c_fqdn[64];                /* this site's fully qualified domain name */
262         char c_humannode[21];           /* human-readable site name */
263         char c_phonenum[16];            /* telephone number */
264         uid_t c_ctdluid;                /* uid of posix account under which Citadel will run */
265         char c_creataide;               /* 1 = creating a room auto-grants room aide privileges */
266         int c_sleeping;                 /* watchdog timer (seconds) */
267         char c_initax;                  /* initial access level for new users */
268         char c_regiscall;               /* after c_regiscall logins user will be asked to register */
269         char c_twitdetect;              /* automatically move messages from problem users to trashcan */
270         char c_twitroom[ROOMNAMELEN];   /* name of trashcan */
271         char c_moreprompt[80];          /* paginator prompt */
272         char c_restrict;                /* require per-user permission to send Internet mail */
273         long c_niu_1;
274         char c_site_location[32];       /* geographic location of this Citadel site */
275         char c_sysadm[26];              /* name of system administrator */
276         char c_niu_2[15];
277         int c_niu_3;
278         int c_maxsessions;              /* maximum number of concurrent sessions allowed */
279         char c_ip_addr[20];             /* bind address for listening sockets */
280         int c_port_number;              /* port number for Citadel protocol (usually 504) */
281         int c_niu_4;
282         struct ExpirePolicy c_ep;       /* default expire policy for the entire site */
283         int c_userpurge;                /* user purge time (in days) */
284         int c_roompurge;                /* room purge time (in days) */
285         char c_logpages[ROOMNAMELEN];
286         char c_createax;
287         long c_maxmsglen;
288         int c_min_workers;
289         int c_max_workers;
290         int c_pop3_port;
291         int c_smtp_port;
292         int c_rfc822_strict_from;
293         int c_aide_zap;
294         int c_imap_port;
295         time_t c_net_freq;
296         char c_disable_newu;
297         char c_enable_fulltext;
298         char c_baseroom[ROOMNAMELEN];
299         char c_aideroom[ROOMNAMELEN];
300         int c_purge_hour;
301         struct ExpirePolicy c_mbxep;
302         char c_ldap_host[128];
303         int c_ldap_port;
304         char c_ldap_base_dn[256];
305         char c_ldap_bind_dn[256];
306         char c_ldap_bind_pw[256];
307         int c_msa_port;
308         int c_imaps_port;
309         int c_pop3s_port;
310         int c_smtps_port;
311         char c_auto_cull;
312         char c_instant_expunge;
313         char c_allow_spoofing;
314         char c_journal_email;
315         char c_journal_pubmsgs;
316         char c_journal_dest[128];
317         char c_default_cal_zone[128];
318         int c_pftcpdict_port;
319         int c_managesieve_port;
320         int c_auth_mode;
321         char c_funambol_host[256];
322         int c_funambol_port;
323         char c_funambol_source[256];
324         char c_funambol_auth[256];
325         char c_rbl_at_greeting;
326         char c_master_user[32];
327         char c_master_pass[32];
328         char c_pager_program[256];
329         char c_imap_keep_from;
330         int c_xmpp_c2s_port;
331         int c_xmpp_s2s_port;
332         time_t c_pop3_fetch;
333         time_t c_pop3_fastest;
334         int c_spam_flag_only;
335         int c_guest_logins;
336 };
337
338 extern struct config config;
339
340
341
342 /*
343  * Expose API calls from user_ops.c
344  */
345 int CtdlGetUser(struct ctdluser *usbuf, char *name);
346 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len);
347 int CtdlGetUserLock(struct ctdluser *usbuf, char *name);
348 void CtdlPutUser(struct ctdluser *usbuf);
349 void CtdlPutUserLock(struct ctdluser *usbuf);
350 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number);
351 void CtdlGetRelationship(visit *vbuf,
352                         struct ctdluser *rel_user,
353                         struct ctdlroom *rel_room);
354 void CtdlSetRelationship(visit *newvisit,
355                         struct ctdluser *rel_user,
356                         struct ctdlroom *rel_room);
357 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix);
358
359 int CtdlLoginExistingUser(char *authname, const char *username);
360
361 /*
362  * Values which may be returned by CtdlLoginExistingUser()
363  */
364 enum {
365         pass_ok,
366         pass_already_logged_in,
367         pass_no_user,
368         pass_internal_error,
369         pass_wrong_password
370 };
371
372 int CtdlTryPassword(const char *password, long len);
373 /*
374  * Values which may be returned by CtdlTryPassword()
375  */
376 enum {
377         login_ok,
378         login_already_logged_in,
379         login_too_many_users,
380         login_not_found
381 };
382
383 void CtdlUserLogout(void);
384
385
386
387
388 /*
389  * Expose API calls from msgbase.c
390  */
391 char *CtdlGetSysConfig(char *sysconfname);
392 void CtdlPutSysConfig(char *sysconfname, char *sysconfdata);
393
394
395
396
397 /*
398  * Expose API calls from euidindex.c
399  */
400 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf);
401
402
403 #endif /* CTDL_MODULE_H */