Move back to single threaded structure for rss feed puller
[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 #include <time.h>
30 #include <sys/wait.h>
31 #include <string.h>
32 #ifdef HAVE_STRINGS_H
33 #include <strings.h>
34 #endif
35 #include <limits.h>
36
37 #include <libcitadel.h>
38
39 #include "server.h"
40 #include "sysdep_decls.h"
41 #include "msgbase.h"
42 #include "threads.h"
43 #include "citadel_dirs.h"
44 #include "context.h"
45
46 /*
47  * define macros for module init stuff
48  */
49  
50 #define CTDL_MODULE_INIT(module_name) char *ctdl_module_##module_name##_init (int threading)
51
52 #define CTDL_INIT_CALL(module_name) ctdl_module_##module_name##_init (threading)
53
54 #define CTDL_MODULE_UPGRADE(module_name) char *ctdl_module_##module_name##_upgrade (void)
55
56 #define CTDL_UPGRADE_CALL(module_name) ctdl_module_##module_name##_upgrade ()
57
58 #define CtdlAideMessage(TEXT, SUBJECT)          \
59         quickie_message(                        \
60                 "Citadel",                      \
61                 NULL,                           \
62                 NULL,                           \
63                 AIDEROOM,                       \
64                 TEXT,                           \
65                 FMT_CITADEL,                    \
66                 SUBJECT) 
67
68
69 #define CtdlAideFPMessage(TEXT, SUBJECT, N, STR, STRLEN, ccid, ioid, TIME) \
70         flood_protect_quickie_message(                   \
71                 "Citadel",                               \
72                 NULL,                                    \
73                 NULL,                                    \
74                 AIDEROOM,                                \
75                 TEXT,                                    \
76                 FMT_CITADEL,                             \
77                 SUBJECT,                                 \
78                 N,                                       \
79                 STR,                                     \
80                 STRLEN,                                  \
81                 ccid,                                    \
82                 ioid,                                    \
83                 TIME)
84 /*
85  * Hook functions available to modules.
86  */
87 /* Priorities for  */
88 #define PRIO_QUEUE 500
89 #define PRIO_AGGR 1000
90 #define PRIO_SEND 1500
91 #define PRIO_CLEANUP 2000
92 /* Priorities for EVT_HOUSE */
93 #define PRIO_HOUSE 3000
94 /* Priorities for EVT_LOGIN */
95 #define PRIO_CREATE 10000
96 /* Priorities for EVT_LOGOUT */
97 #define PRIO_LOGOUT 15000
98 /* Priorities for EVT_LOGIN */
99 #define PRIO_LOGIN 20000
100 /* Priorities for EVT_START */
101 #define PRIO_START 25000
102 /* Priorities for EVT_STOP */
103 #define PRIO_STOP 30000
104 /* Priorities for EVT_ASYNC */
105 #define PRIO_ASYNC 35000
106 /* Priorities for EVT_SHUTDOWN */
107 #define PRIO_SHUTDOWN 40000
108 /* Priorities for EVT_UNSTEALTH */
109 #define PRIO_UNSTEALTH 45000
110 /* Priorities for EVT_STEALTH */
111 #define PRIO_STEALTH 50000
112 void CtdlRegisterTDAPVetoHook(int (*fcn_ptr)(StrBuf*), int EventType, int Priority);
113 void CtdlUnregisterTDAPVetoHook(int (*fcn_ptr) (StrBuf*), int EventType);
114
115
116 void CtdlRegisterSessionHook(void (*fcn_ptr)(void), int EventType, int Priority);
117 void CtdlUnregisterSessionHook(void (*fcn_ptr)(void), int EventType);
118 void CtdlShutdownServiceHooks(void);
119
120 void CtdlRegisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
121 void CtdlUnregisterUserHook(void (*fcn_ptr)(struct ctdluser *), int EventType);
122
123 void CtdlRegisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
124 void CtdlUnregisterXmsgHook(int (*fcn_ptr)(char *, char *, char *, char *), int order);
125
126 void CtdlRegisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes*),
127                                                         int EventType);
128 void CtdlUnregisterMessageHook(int (*handler)(struct CtdlMessage *, recptypes *),
129                                int EventType);
130
131 void CtdlRegisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
132 void CtdlUnregisterNetprocHook(int (*handler)(struct CtdlMessage *, char *) );
133
134 void CtdlRegisterRoomHook(int (*fcn_ptr)(struct ctdlroom *) );
135 void CtdlUnregisterRoomHook(int (*fnc_ptr)(struct ctdlroom *) );
136
137 void CtdlRegisterDeleteHook(void (*handler)(char *, long) );
138 void CtdlUnregisterDeleteHook(void (*handler)(char *, long) );
139
140 void CtdlRegisterCleanupHook(void (*fcn_ptr)(void));
141 void CtdlUnregisterCleanupHook(void (*fcn_ptr)(void));
142
143 void CtdlRegisterEVCleanupHook(void (*fcn_ptr)(void));
144 void CtdlUnregisterEVCleanupHook(void (*fcn_ptr)(void));
145
146 void CtdlRegisterProtoHook(void (*handler)(char *), char *cmd, char *desc);
147
148 void CtdlRegisterServiceHook(int tcp_port,
149                              char *sockpath,
150                              void (*h_greeting_function) (void),
151                              void (*h_command_function) (void),
152                              void (*h_async_function) (void),
153                              const char *ServiceName
154 );
155 void CtdlUnregisterServiceHook(int tcp_port,
156                         char *sockpath,
157                         void (*h_greeting_function) (void),
158                         void (*h_command_function) (void),
159                         void (*h_async_function) (void)
160 );
161
162 void CtdlRegisterFixedOutputHook(char *content_type,
163                         void (*output_function) (char *supplied_data, int len)
164 );
165 void CtdlUnRegisterFixedOutputHook(char *content_type);
166
167 void CtdlRegisterMaintenanceThread(char *name, void *(*thread_proc) (void *arg));
168
169 void CtdlRegisterSearchFuncHook(void (*fcn_ptr)(int *, long **, const char *), char *name);
170
171 /*
172  * if you say a) (which may take a while)
173  * don't forget to say b)
174  */
175 void CtdlDisableHouseKeeping(void);
176 void CtdlEnableHouseKeeping(void);
177
178 /*
179  * Directory services hooks for LDAP etc
180  */
181
182 #define DIRECTORY_USER_DEL 1    // Delete a user entry
183 #define DIRECTORY_CREATE_HOST 2 // Create a host entry if not already there.
184 #define DIRECTORY_CREATE_OBJECT 3       // Create a new object for directory entry
185 #define DIRECTORY_ATTRIB_ADD 4  // Add an attribute to the directory entry object
186 #define DIRECTORY_SAVE_OBJECT 5 // Save the object to the directory service
187 #define DIRECTORY_FREE_OBJECT 6 // Free the object and its attributes
188
189 int CtdlRegisterDirectoryServiceFunc(int (*func)(char *cn, char *ou, void **object), int cmd, char *module);
190 int CtdlDoDirectoryServiceFunc(char *cn, char *ou, void **object, char *module, int cmd);
191
192 /* TODODRW: This needs to be changed into a hook type interface
193  * for now we have this horrible hack
194  */
195 void CtdlModuleStartCryptoMsgs(char *ok_response, char *nosup_response, char *error_response);
196
197 /* return the current context list as an array and do it in a safe manner
198  * The returned data is a copy so only reading is useful
199  * The number of contexts is returned in count.
200  * Beware, this does not copy any of the data pointed to by the context.
201  * This means that you can not rely on things like the redirect buffer being valid.
202  * You must free the returned pointer when done.
203  */
204 struct CitContext *CtdlGetContextArray (int *count);
205 void CtdlFillSystemContext(struct CitContext *context, char *name);
206 int CtdlTrySingleUser(void);
207 void CtdlEndSingleUser(void);
208 int CtdlWantSingleUser(void);
209 int CtdlIsSingleUser(void);
210
211
212 int CtdlIsUserLoggedIn (char *user_name);
213 int CtdlIsUserLoggedInByNum (long usernum);
214 void CtdlBumpNewMailCounter(long which_user);
215
216
217 /*
218  * CtdlGetCurrentMessageNumber()  -  Obtain the current highest message number in the system
219  * This provides a quick way to initialise a variable that might be used to indicate
220  * messages that should not be processed. EG. a new Sieve script will use this
221  * to record determine that messages older than this should not be processed.
222  * This function is defined in control.c
223  */
224 long CtdlGetCurrentMessageNumber(void);
225
226
227
228 /*
229  * Expose various room operation functions from room_ops.c to the modules API
230  */
231 typedef struct CfgLineType CfgLineType;
232 typedef struct RoomNetCfgLine RoomNetCfgLine;
233 typedef struct OneRoomNetCfg OneRoomNetCfg;
234
235 unsigned CtdlCreateRoom(char *new_room_name,
236                         int new_room_type,
237                         char *new_room_pass,
238                         int new_room_floor,
239                         int really_create,
240                         int avoid_access,
241                         int new_room_view);
242 int CtdlGetRoom(struct ctdlroom *qrbuf, const char *room_name);
243 int CtdlGetRoomLock(struct ctdlroom *qrbuf, const char *room_name);
244 int CtdlDoIHavePermissionToDeleteThisRoom(struct ctdlroom *qr);
245 void CtdlRoomAccess(struct ctdlroom *roombuf, struct ctdluser *userbuf, int *result, int *view);
246 void CtdlPutRoomLock(struct ctdlroom *qrbuf);
247 typedef void (*ForEachRoomCallBack)(struct ctdlroom *EachRoom, void *out_data);
248 void CtdlForEachRoom(ForEachRoomCallBack CB, void *in_data);
249 typedef void (*ForEachRoomNetCfgCallBack)(struct ctdlroom *EachRoom, void *out_data, OneRoomNetCfg *OneRNCFG);
250 char *LoadRoomNetConfigFile(long roomnum);
251 void CtdlForEachNetCfgRoom(ForEachRoomNetCfgCallBack CB, void *in_data);
252 void SaveChangedConfigs(void);
253 void CtdlDeleteRoom(struct ctdlroom *qrbuf);
254 int CtdlRenameRoom(char *old_name, char *new_name, int new_floor);
255 void CtdlUserGoto (char *where, int display_result, int transiently, int *msgs, int *new, long *oldest, long *newest);
256 struct floor *CtdlGetCachedFloor(int floor_num);
257 void CtdlScheduleRoomForDeletion(struct ctdlroom *qrbuf);
258 void CtdlGetFloor (struct floor *flbuf, int floor_num);
259 void CtdlPutFloor (struct floor *flbuf, int floor_num);
260 void CtdlPutFloorLock(struct floor *flbuf, int floor_num);
261 int CtdlGetFloorByName(const char *floor_name);
262 int CtdlGetFloorByNameLock(const char *floor_name);
263 int CtdlGetAvailableFloor(void);
264 int CtdlIsNonEditable(struct ctdlroom *qrbuf);
265 void CtdlPutRoom(struct ctdlroom *);
266
267 /*
268  * Possible return values for CtdlRenameRoom()
269  */
270 enum {
271         crr_ok,                         /* success */
272         crr_room_not_found,             /* room not found */
273         crr_already_exists,             /* new name already exists */
274         crr_noneditable,                /* cannot edit this room */
275         crr_invalid_floor,              /* target floor does not exist */
276         crr_access_denied               /* not allowed to edit this room */
277 };
278
279
280
281 /*
282  * API declarations from citserver.h
283  */
284 int CtdlAccessCheck(int);
285 /* 'required access level' values which may be passed to CtdlAccessCheck()
286  */
287 enum {
288         ac_none,
289         ac_logged_in_or_guest,
290         ac_logged_in,
291         ac_room_aide,
292         ac_aide,
293         ac_internal,
294 };
295
296
297
298 /*
299  * API declarations from serv_extensions.h
300  */
301 void CtdlModuleDoSearch(int *num_msgs, long **search_msgs, const char *search_string, const char *func_name);
302
303 #define NODENAME                CtdlGetConfigStr("c_nodename")
304 #define FQDN                    CtdlGetConfigStr("c_fqdn")
305 #define CTDLUID                 ctdluid
306 #define CREATAIDE               CtdlGetConfigInt("c_creataide")
307 #define REGISCALL               CtdlGetConfigInt("c_regiscall")
308 #define TWITDETECT              CtdlGetConfigInt("c_twitdetect")
309 #define TWITROOM                CtdlGetConfigStr("c_twitroom")
310 #define RESTRICT_INTERNET       CtdlGetConfigInt("c_restrict")
311
312 typedef void (*CfgLineParser)(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *rncfg);
313 typedef void (*CfgLineSerializer)(const CfgLineType *ThisOne, StrBuf *OuptputBuffer, OneRoomNetCfg *rncfg, RoomNetCfgLine *data);
314 typedef void (*CfgLineDeAllocator)(const CfgLineType *ThisOne, RoomNetCfgLine **data);
315
316 struct CfgLineType {
317         RoomNetCfg C;
318         CfgLineParser Parser;
319         CfgLineSerializer Serializer;
320         CfgLineDeAllocator DeAllocator;
321         ConstStr Str;
322         int IsSingleLine;
323         int nSegments;
324 };
325
326 struct RoomNetCfgLine {
327         RoomNetCfgLine *next;
328         int nValues;
329         StrBuf **Value;
330 };
331
332 struct OneRoomNetCfg {
333         long lastsent;
334         // long changed;
335         StrBuf *Sender;
336         StrBuf *RoomInfo;
337         RoomNetCfgLine *NetConfigs[maxRoomNetCfg];
338         StrBuf *misc;
339 };
340
341
342 #define CtdlREGISTERRoomCfgType(a, p, uniq, nSegs, s, d) RegisterRoomCfgType(#a, sizeof(#a) - 1, a, p, uniq, nSegs, s, d);
343 void RegisterRoomCfgType(const char* Name, long len, RoomNetCfg eCfg, CfgLineParser p, int uniq, int nSegments, CfgLineSerializer s, CfgLineDeAllocator d);
344 void ParseGeneric(const CfgLineType *ThisOne, StrBuf *Line, const char *LinePos, OneRoomNetCfg *sc);
345 void SerializeGeneric(const CfgLineType *ThisOne, StrBuf *OutputBuffer, OneRoomNetCfg *sc, RoomNetCfgLine *data);
346 void DeleteGenericCfgLine(const CfgLineType *ThisOne, RoomNetCfgLine **data);
347 RoomNetCfgLine *DuplicateOneGenericCfgLine(const RoomNetCfgLine *data);
348 void AddRoomCfgLine(OneRoomNetCfg *OneRNCfg, struct ctdlroom *qrbuf, RoomNetCfg LineType, RoomNetCfgLine *Line);
349
350 OneRoomNetCfg *CtdlGetNetCfgForRoom(long QRNumber);
351 void SaveRoomNetConfigFile(OneRoomNetCfg *, long);
352 void FreeRoomNetworkStruct(OneRoomNetCfg **);
353
354 typedef struct _nodeconf {
355         int DeleteMe;
356         StrBuf *NodeName;
357         StrBuf *Secret;
358         StrBuf *Host;
359         StrBuf *Port;
360 }CtdlNodeConf;
361
362 HashList* CtdlLoadIgNetCfg(void);
363
364
365 int CtdlNetconfigCheckRoomaccess(char *errmsgbuf, 
366                                  size_t n,
367                                  const char* RemoteIdentifier);
368
369
370 typedef struct __NetMap {
371         StrBuf *NodeName;
372         time_t lastcontact;
373         StrBuf *NextHop;
374 }CtdlNetMap;
375
376 HashList* CtdlReadNetworkMap(void);
377 StrBuf *CtdlSerializeNetworkMap(HashList *Map);
378 void NetworkLearnTopology(char *node, char *path, HashList *the_netmap, int *netmap_changed);
379 int CtdlIsValidNode(const StrBuf **nexthop,
380                     const StrBuf **secret,
381                     StrBuf *node,
382                     HashList *IgnetCfg,
383                     HashList *the_netmap);
384
385
386
387
388 int CtdlNetworkTalkingTo(const char *nodename, long len, int operation);
389
390 /*
391  * Operations that can be performed by network_talking_to()
392  */
393 enum {
394         NTT_ADD,
395         NTT_REMOVE,
396         NTT_CHECK
397 };
398
399 /*
400  * Expose API calls from user_ops.c
401  */
402 int CtdlGetUser(struct ctdluser *usbuf, char *name);
403 int CtdlGetUserLen(struct ctdluser *usbuf, const char *name, long len);
404 int CtdlGetUserLock(struct ctdluser *usbuf, char *name);
405 void CtdlPutUser(struct ctdluser *usbuf);
406 void CtdlPutUserLock(struct ctdluser *usbuf);
407
408 int CtdlLockGetCurrentUser(void);
409 void CtdlPutCurrentUserLock(void);
410
411 int CtdlGetUserByNumber(struct ctdluser *usbuf, long number);
412 void CtdlGetRelationship(visit *vbuf,
413                         struct ctdluser *rel_user,
414                         struct ctdlroom *rel_room);
415 void CtdlSetRelationship(visit *newvisit,
416                         struct ctdluser *rel_user,
417                         struct ctdlroom *rel_room);
418 void CtdlMailboxName(char *buf, size_t n, const struct ctdluser *who, const char *prefix);
419
420 int CtdlLoginExistingUser(char *authname, const char *username);
421
422 /*
423  * Values which may be returned by CtdlLoginExistingUser()
424  */
425 enum {
426         pass_ok,
427         pass_already_logged_in,
428         pass_no_user,
429         pass_internal_error,
430         pass_wrong_password
431 };
432
433 int CtdlTryPassword(const char *password, long len);
434 /*
435  * Values which may be returned by CtdlTryPassword()
436  */
437 enum {
438         login_ok,
439         login_already_logged_in,
440         login_too_many_users,
441         login_not_found
442 };
443
444 void CtdlUserLogout(void);
445
446
447
448
449 /*
450  * Expose API calls from msgbase.c
451  */
452
453
454
455 /*
456  * Expose API calls from euidindex.c
457  */
458 long CtdlLocateMessageByEuid(char *euid, struct ctdlroom *qrbuf);
459
460
461
462 #endif /* CTDL_MODULE_H */