* Removed the CtdlRedirectOutput() API, as we are no longer using it.
[citadel.git] / citadel / server.h
1 /* $Id$ */
2
3
4 #ifndef SERVER_H
5 #define SERVER_H
6
7 #ifdef __GNUC__
8 #define INLINE __inline__
9 #else
10 #define INLINE
11 #endif
12
13 #include "citadel.h"
14 #ifdef HAVE_OPENSSL
15 #define OPENSSL_NO_KRB5         /* work around redhat b0rken ssl headers */
16 #include <openssl/ssl.h>
17 #endif
18
19 /*
20  * New format for a message in memory
21  */
22 struct CtdlMessage {
23         int cm_magic;                   /* Self-check (NOT SAVED TO DISK) */
24         char cm_anon_type;              /* Anonymous or author-visible */
25         char cm_format_type;            /* Format type */
26         char *cm_fields[256];           /* Data fields */
27         unsigned int cm_flags;          /* How to handle (NOT SAVED TO DISK) */
28 };
29
30 #define CTDLMESSAGE_MAGIC               0x159d
31 #define CM_SKIP_HOOKS   0x01            /* Don't run server-side handlers */
32
33
34 /*
35  * Here's the big one... the Citadel context structure.
36  *
37  * This structure keeps track of all information relating to a running 
38  * session on the server.  We keep one of these for each session thread.
39  *
40  * Note that the first element is "*next" so that it may be used without
41  * modification in a linked list.
42  */
43 struct CitContext {
44         struct CitContext *prev;        /* Link to previous session in list */
45         struct CitContext *next;        /* Link to next session in the list */
46
47         int state;              /* thread state (see CON_ values below) */
48         int kill_me;            /* Set to nonzero to flag for termination */
49         int client_socket;
50         int cs_pid;             /* session ID */
51         time_t lastcmd;         /* time of last command executed */
52         time_t lastidle;        /* For computing idle time */
53
54         char curr_user[USERNAME_SIZE];  /* name of current user */
55         int logged_in;          /* logged in */
56         int internal_pgm;       /* authenticated as internal program */
57         char temp[PATH_MAX];    /* temp file name */
58         int nologin;            /* not allowed to log in */
59         int is_local_socket;    /* set to 1 if client is on unix domain sock */
60         int curr_view;          /* The view type for the current user/room */
61
62         char net_node[PATH_MAX];/* Is the client another Citadel server? */
63         time_t previous_login;  /* Date/time of previous login */
64         char lastcmdname[5];    /* name of last command executed */
65         unsigned cs_flags;      /* miscellaneous flags */
66         void (*h_command_function) (void) ;     /* service command function */
67         void (*h_async_function) (void) ;       /* do async msgs function */
68         int is_async;           /* Nonzero if client accepts async msgs */
69         int async_waiting;      /* Nonzero if there are async msgs waiting */
70         int input_waiting;      /* Nonzero if there is client input waiting */
71
72         /* feeping creaturisms... */
73         int cs_clientdev;       /* client developer ID */
74         int cs_clienttyp;       /* client type code */
75         int cs_clientver;       /* client version number */
76         char cs_clientname[32]; /* name of client software */
77         char cs_host[64];       /* host logged in from */
78         char cs_addr[64];       /* address logged in from */
79
80         /* The Internet type of thing */
81         char cs_inet_email[SIZ];/* Return address of outbound Internet mail */
82
83         FILE *download_fp;      /* Fields relating to file transfer */
84         char download_desired_section[128];
85         FILE *upload_fp;
86         char upl_file[PATH_MAX];
87         char upl_path[PATH_MAX];
88         char upl_comment[SIZ];
89         char upl_filedir[PATH_MAX];
90         char dl_is_net;
91         char upload_type;
92
93         struct ctdluser user;   /* Database record buffers */
94         struct ctdlroom room;
95
96         /* Beginning of cryptography - session nonce */
97         char cs_nonce[NONCE_SIZE];      /* The nonce for this session's next auth transaction */
98
99         /* Redirect this session's output to a memory buffer? */
100         char *redirect_buffer;          /* the buffer */
101         size_t redirect_len;            /* length of data in buffer */
102         size_t redirect_alloc;          /* length of allocated buffer */
103 #ifdef HAVE_OPENSSL
104         SSL *ssl;
105         int redirect_ssl;
106 #endif
107
108         int buffering;
109         char *output_buffer;    /* hold output for one big dump */
110         int buffer_len;
111
112         /* A linked list of all instant messages sent to us. */
113         struct ExpressMessage *FirstExpressMessage;
114         int disable_exp;        /* Set to 1 to disable incoming pages */
115         int newmail;            /* Other sessions increment this */
116
117         /* Masquerade... */
118         char fake_username[USERNAME_SIZE];      /* Fake username <bc> */ 
119         char fake_postname[USERNAME_SIZE];      /* Fake postname <bc> */
120         char fake_hostname[64];                 /* Fake hostname <bc> */
121         char fake_roomname[ROOMNAMELEN];        /* Fake roomname <bc> */
122
123         char preferred_formats[SIZ];            /* Preferred MIME formats */
124
125         /* Dynamically allocated session data */
126         struct citimap *IMAP;
127         struct citpop3 *POP3;
128         struct citsmtp *SMTP;
129         char *SMTP_RECPS;
130         char *SMTP_ROOMS;
131         struct cit_ical *CIT_ICAL;              /* calendaring data */
132         struct ma_info *ma;                     /* multipart/alternative data */
133 };
134
135 typedef struct CitContext t_context;
136
137 /*
138  * Values for CitContext.state
139  * 
140  * A session that is doing nothing is in CON_IDLE state.  When activity
141  * is detected on the socket, it goes to CON_READY, indicating that it
142  * needs to have a worker thread bound to it.  When a thread binds to
143  * the session, it goes to CON_EXECUTING and does its thing.  When the
144  * transaction is finished, the thread sets it back to CON_IDLE and lets
145  * it go.
146  */
147 enum {
148         CON_IDLE,               /* This context is doing nothing */
149         CON_READY,              /* This context needs attention */
150         CON_EXECUTING           /* This context is bound to a thread */
151 };
152
153
154 #define CS_STEALTH      1       /* stealth mode */
155 #define CS_CHAT         2       /* chat mode */
156 #define CS_POSTING      4       /* Posting */
157
158 struct CitContext *MyContext(void);
159 #define CC MyContext()
160
161 extern struct CitContext *ContextList;
162 extern int ScheduledShutdown;
163 extern struct CitControl CitControl;
164
165
166 struct ExpressMessage {
167         struct ExpressMessage *next;
168         time_t timestamp;       /* When this message was sent */
169         unsigned flags;         /* Special instructions */
170         char sender[64];        /* Name of sending user */
171         char *text;             /* Message text (if applicable) */
172 };
173
174 #define EM_BROADCAST    1       /* Broadcast message */
175 #define EM_GO_AWAY      2       /* Server requests client log off */
176 #define EM_CHAT         4       /* Server requests client enter chat */
177
178 struct ChatLine {
179         struct ChatLine *next;
180         int chat_seq;
181         time_t chat_time;
182         char chat_text[SIZ];
183         char chat_username[USERNAME_SIZE];
184         char chat_room[ROOMNAMELEN];
185 };
186
187 /*
188  * Various things we need to lock and unlock
189  */
190 enum {
191         S_USERS,
192         S_ROOMS,
193         S_SESSION_TABLE,
194         S_FLOORTAB,
195         S_CHATQUEUE,
196         S_CONTROL,
197         S_NETDB,
198         S_SUPPMSGMAIN,
199         S_CONFIG,
200         S_WORKER_LIST,
201         S_HOUSEKEEPING,
202         S_NTTLIST,
203         S_DIRECTORY,
204         S_NETCONFIGS,
205         S_PUBLIC_CLIENTS,
206         S_LDAP,
207         S_FLOORCACHE,
208         S_DEBUGMEMLEAKS,
209         MAX_SEMAPHORES
210 };
211
212
213 /*
214  * Upload types
215  */
216 #define UPL_FILE        0
217 #define UPL_NET         1
218 #define UPL_IMAGE       2
219
220
221 /*
222  * message transfer formats
223  */
224 enum {
225         MT_CITADEL,             /* Citadel proprietary */
226         MT_RFC822,              /* RFC822 */
227         MT_MIME,                /* MIME-formatted message */
228         MT_DOWNLOAD             /* Download a component */
229 };
230
231 /*
232  * Message format types in the database
233  */
234 #define FMT_CITADEL     0       /* Citadel vari-format (proprietary) */
235 #define FMT_FIXED       1       /* Fixed format (proprietary)        */
236 #define FMT_RFC822      4       /* Standard (headers are in M field) */
237
238
239 /*
240  * Citadel DataBases (define one for each cdb we need to open)
241  */
242 enum {
243         CDB_MSGMAIN,            /* message base                  */
244         CDB_USERS,              /* user file                     */
245         CDB_ROOMS,              /* room index                    */
246         CDB_FLOORTAB,           /* floor index                   */
247         CDB_MSGLISTS,           /* room message lists            */
248         CDB_VISIT,              /* user/room relationships       */
249         CDB_DIRECTORY,          /* address book directory        */
250         CDB_USETABLE,           /* network use table             */
251         CDB_BIGMSGS,            /* larger message bodies         */
252         MAXCDB                  /* total number of CDB's defined */
253 };
254
255 struct cdbdata {
256         size_t len;
257         char *ptr;
258 };
259
260
261
262 /* Structures and declarations for function hooks of various types */
263
264 struct LogFunctionHook {
265         struct LogFunctionHook *next;
266         int loglevel;
267         void (*h_function_pointer) (char *);
268 };
269 extern struct LogFunctionHook *LogHookTable;
270
271 struct CleanupFunctionHook {
272         struct CleanupFunctionHook *next;
273         void (*h_function_pointer) (void);
274 };
275 extern struct CleanupFunctionHook *CleanupHookTable;
276
277
278
279
280 /*
281  * SessionFunctionHook extensions are used for any type of hook for which
282  * the context in which it's being called (which is determined by the event
283  * type) will make it obvious for the hook function to know where to look for
284  * pertinent data.
285  */
286 struct SessionFunctionHook {
287         struct SessionFunctionHook *next;
288         void (*h_function_pointer) (void);
289         int eventtype;
290 };
291 extern struct SessionFunctionHook *SessionHookTable;
292
293 /* 
294  * Event types can't be enum'ed, because they must remain consistent between
295  * builds (to allow for binary modules built somewhere else)
296  */
297 #define EVT_STOP        0       /* Session is terminating */
298 #define EVT_START       1       /* Session is starting */
299 #define EVT_LOGIN       2       /* A user is logging in */
300 #define EVT_NEWROOM     3       /* Changing rooms */
301 #define EVT_LOGOUT      4       /* A user is logging out */
302 #define EVT_SETPASS     5       /* Setting or changing password */
303 #define EVT_CMD         6       /* Called after each server command */
304 #define EVT_RWHO        7       /* An RWHO command is being executed */
305 #define EVT_ASYNC       8       /* Doing asynchronous messages */
306
307 #define EVT_TIMER       50      /* Timer events are called once per minute
308                                    and are not tied to any session */
309
310 /*
311  * UserFunctionHook extensions are used for any type of hook which implements
312  * an operation on a user or username (potentially) other than the one
313  * operating the current session.
314  */
315 struct UserFunctionHook {
316         struct UserFunctionHook *next;
317         void (*h_function_pointer) (struct ctdluser *usbuf);
318         int eventtype;
319 };
320 extern struct UserFunctionHook *UserHookTable;
321
322 #define EVT_PURGEUSER   100     /* Deleting a user */
323 #define EVT_NEWUSER     102     /* Creating a user */
324
325 /*
326  * MessageFunctionHook extensions are used for hooks which implement handlers
327  * for various types of message operations (save, read, etc.)
328  */
329 struct MessageFunctionHook {
330         struct MessageFunctionHook *next;
331         int (*h_function_pointer) (struct CtdlMessage *msg);
332         int eventtype;
333 };
334 extern struct MessageFunctionHook *MessageHookTable;
335
336 #define EVT_BEFOREREAD  200
337 #define EVT_BEFORESAVE  201
338 #define EVT_AFTERSAVE   202
339 #define EVT_SMTPSCAN    203     /* called before submitting a msg from SMTP */
340
341
342
343 /*
344  * NetprocFunctionHook extensions are used for hooks which implement handlers
345  * for incoming network messages.
346  */
347 struct NetprocFunctionHook {
348         struct NetprocFunctionHook *next;
349         int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
350 };
351 extern struct NetprocFunctionHook *NetprocHookTable;
352
353
354 /*
355  * DeleteFunctionHook extensions are used for hooks which get called when a
356  * message is about to be deleted.
357  */
358 struct DeleteFunctionHook {
359         struct DeleteFunctionHook *next;
360         void (*h_function_pointer) (char *target_room, long msgnum);
361 };
362 extern struct DeleteFunctionHook *DeleteHookTable;
363
364
365 /*
366  * ExpressMessageFunctionHook extensions are used for hooks which implement
367  * the sending of an instant message through various channels.  Any function
368  * registered should return the number of recipients to whom the message was
369  * successfully transmitted.
370  */
371 struct XmsgFunctionHook {
372         struct XmsgFunctionHook *next;
373         int (*h_function_pointer) (char *, char *, char *);
374         int order;
375 };
376 extern struct XmsgFunctionHook *XmsgHookTable;
377
378 /* Priority levels for paging functions (lower is better) */
379 enum {
380         XMSG_PRI_LOCAL,         /* Other users on -this- server */
381         XMSG_PRI_REMOTE,        /* Other users on a Citadel network (future) */
382         XMSG_PRI_FOREIGN,       /* Contacts on foreign instant message hosts */
383         MAX_XMSG_PRI
384 };
385
386
387
388 /*
389  * ServiceFunctionHook extensions are used for hooks which implement various
390  * non-Citadel services (on TCP protocols) directly in the Citadel server.
391  */
392 struct ServiceFunctionHook {
393         struct ServiceFunctionHook *next;
394         int tcp_port;
395         char *sockpath;
396         void (*h_greeting_function) (void) ;
397         void (*h_command_function) (void) ;
398         void (*h_async_function) (void) ;
399         int msock;
400 };
401 extern struct ServiceFunctionHook *ServiceHookTable;
402
403
404
405 /* Defines the relationship of a user to a particular room */
406 struct visit {
407         long v_roomnum;
408         long v_roomgen;
409         long v_usernum;
410         long v_lastseen;
411         unsigned int v_flags;
412         char v_seen[SIZ];
413         char v_answered[SIZ];
414         int v_view;
415 };
416
417 #define V_FORGET        1       /* User has zapped this room        */
418 #define V_LOCKOUT       2       /* User is locked out of this room  */
419 #define V_ACCESS        4       /* Access is granted to this room   */
420
421
422 /* Supplementary data for a message on disk
423  * These are kept separate from the message itself for one of two reasons:
424  * 1. Either their values may change at some point after initial save, or
425  * 2. They are merely caches of data which exist somewhere else, for speed.
426  */
427 struct MetaData {
428         long meta_msgnum;               /* Message number in *local* message base */
429         int meta_refcount;              /* Number of rooms pointing to this msg */
430         char meta_content_type[64];     /* Cached MIME content-type */
431         long meta_rfc822_length;        /* Cache of RFC822-translated msg length */
432 };
433
434
435 /* 
436  * Serialization routines use this struct to return a pointer and a length
437  */
438 struct ser_ret {
439         size_t len;
440         unsigned char *ser;
441 };
442
443
444 /* Preferred field order */
445 /*               **********                     Important fields */
446 /*                         ***************      Semi-important fields */
447 /*                                        *     Message text (MUST be last) */
448 #define FORDER  "IPTAFONHRDBCEGJKLQSVWXYZUM"
449
450 #endif /* SERVER_H */