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