Amended the MSGP command to provide a way for the client to
[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 /*
36  * Exit codes 101 through 109 are used for conditions in which
37  * we deliberately do NOT want the service to automatically
38  * restart.
39  */
40 #define CTDLEXIT_CONFIG         101     /* Could not read citadel.config */
41 #define CTDLEXIT_CONTROL        102     /* Could not acquire lock */
42 #define CTDLEXIT_HOME           103     /* Citadel home directory not found */
43 #define CTDLEXIT_OOD            104     /* Out Of Date config - rerun setup */
44 #define CTDLEXIT_DB             105     /* Unable to initialize database */
45
46
47
48
49 /*
50  * Here's the big one... the Citadel context structure.
51  *
52  * This structure keeps track of all information relating to a running 
53  * session on the server.  We keep one of these for each session thread.
54  *
55  */
56 struct CitContext {
57         struct CitContext *prev;        /* Link to previous session in list */
58         struct CitContext *next;        /* Link to next session in the list */
59
60         int state;              /* thread state (see CON_ values below) */
61         int kill_me;            /* Set to nonzero to flag for termination */
62         int client_socket;
63         int cs_pid;             /* session ID */
64         time_t lastcmd;         /* time of last command executed */
65         time_t lastidle;        /* For computing idle time */
66
67         char curr_user[USERNAME_SIZE];  /* name of current user */
68         int logged_in;          /* logged in */
69         int internal_pgm;       /* authenticated as internal program */
70         int nologin;            /* not allowed to log in */
71         int is_local_socket;    /* set to 1 if client is on unix domain sock */
72         int curr_view;          /* The view type for the current user/room */
73         int is_master;          /* Is this session logged in using the master user? */
74
75         char net_node[32]       ;/* Is the client another Citadel server? */
76         time_t previous_login;  /* Date/time of previous login */
77         char lastcmdname[5];    /* name of last command executed */
78         unsigned cs_flags;      /* miscellaneous flags */
79         void (*h_command_function) (void) ;     /* service command function */
80         void (*h_async_function) (void) ;       /* do async msgs function */
81         int is_async;           /* Nonzero if client accepts async msgs */
82         int async_waiting;      /* Nonzero if there are async msgs waiting */
83         int input_waiting;      /* Nonzero if there is client input waiting */
84
85         /* Client information */
86         int cs_clientdev;       /* client developer ID */
87         int cs_clienttyp;       /* client type code */
88         int cs_clientver;       /* client version number */
89         char cs_clientname[32]; /* name of client software */
90         char cs_host[64];       /* host logged in from */
91         char cs_addr[64];       /* address logged in from */
92
93         /* The Internet type of thing */
94         char cs_inet_email[128];                /* Return address of outbound Internet mail */
95         char cs_inet_other_emails[1024];        /* User's other valid Internet email addresses */
96         char cs_inet_fn[128];                   /* Friendly-name of outbound Internet mail */
97
98         FILE *download_fp;      /* Fields relating to file transfer */
99         char download_desired_section[128];
100         FILE *upload_fp;
101         char upl_file[256];
102         char upl_path[PATH_MAX];
103         char upl_comment[256];
104         char upl_filedir[PATH_MAX];
105         char dl_is_net;
106         char upload_type;
107
108         struct ctdluser user;   /* Database record buffers */
109         struct ctdlroom room;
110
111         /* Beginning of cryptography - session nonce */
112         char cs_nonce[NONCE_SIZE];      /* The nonce for this session's next auth transaction */
113
114         /* Redirect this session's output to a memory buffer? */
115         char *redirect_buffer;          /* the buffer */
116         size_t redirect_len;            /* length of data in buffer */
117         size_t redirect_alloc;          /* length of allocated buffer */
118 #ifdef HAVE_OPENSSL
119         SSL *ssl;
120         int redirect_ssl;
121 #endif
122
123         int buffering;
124         char *output_buffer;    /* hold output for one big dump */
125         int buffer_len;
126
127         /* A linked list of all instant messages sent to us. */
128         struct ExpressMessage *FirstExpressMessage;
129         int disable_exp;        /* Set to 1 to disable incoming pages */
130         int newmail;            /* Other sessions increment this */
131
132         /* Masqueraded values in the 'who is online' list */
133         char fake_username[USERNAME_SIZE];
134         char fake_hostname[64];
135         char fake_roomname[ROOMNAMELEN];
136
137         /* Preferred MIME formats */
138         char preferred_formats[256];
139         int msg4_dont_decode;
140
141         /* Dynamically allocated session data */
142         struct citimap *IMAP;
143         struct citpop3 *POP3;
144         struct citsmtp *SMTP;
145         struct citmgsve *MGSVE;                 /**< Managesieve Session struct */
146         struct cit_ical *CIT_ICAL;              /* calendaring data */
147         struct ma_info *ma;                     /* multipart/alternative data */
148         const char* ServiceName;                /**< whats our actual purpose? */
149 };
150
151 typedef struct CitContext t_context;
152
153 /*
154  * Values for CitContext.state
155  * 
156  * A session that is doing nothing is in CON_IDLE state.  When activity
157  * is detected on the socket, it goes to CON_READY, indicating that it
158  * needs to have a worker thread bound to it.  When a thread binds to
159  * the session, it goes to CON_EXECUTING and does its thing.  When the
160  * transaction is finished, the thread sets it back to CON_IDLE and lets
161  * it go.
162  */
163 enum {
164         CON_IDLE,               /* This context is doing nothing */
165         CON_READY,              /* This context needs attention */
166         CON_EXECUTING           /* This context is bound to a thread */
167 };
168
169
170 #define CS_STEALTH      1       /* stealth mode */
171 #define CS_CHAT         2       /* chat mode */
172 #define CS_POSTING      4       /* Posting */
173
174 struct CitContext *MyContext(void);
175 #define CC MyContext()
176
177 /*
178  * This is the control record for the message base... 
179  */
180 struct CitControl {
181         long MMhighest;                 /* highest message number in file   */
182         unsigned MMflags;               /* Global system flags              */
183         long MMnextuser;                /* highest user number on system    */
184         long MMnextroom;                /* highest room number on system    */
185         int version;                    /* Server-hosted upgrade level      */
186         int fulltext_wordbreaker;       /* ID of wordbreaker in use         */
187         long MMfulltext;                /* highest message number indexed   */
188 };
189
190 extern struct CitContext *ContextList;
191 extern int ScheduledShutdown;
192 extern struct CitControl CitControl;
193
194
195 struct ExpressMessage {
196         struct ExpressMessage *next;
197         time_t timestamp;       /* When this message was sent */
198         unsigned flags;         /* Special instructions */
199         char sender[64];        /* Name of sending user */
200         char *text;             /* Message text (if applicable) */
201 };
202
203 #define EM_BROADCAST    1       /* Broadcast message */
204 #define EM_GO_AWAY      2       /* Server requests client log off */
205 #define EM_CHAT         4       /* Server requests client enter chat */
206
207 struct ChatLine {
208         struct ChatLine *next;
209         int chat_seq;
210         time_t chat_time;
211         char chat_text[SIZ];
212         char chat_username[USERNAME_SIZE];
213         char chat_room[ROOMNAMELEN];
214 };
215
216 /*
217  * Various things we need to lock and unlock
218  */
219 enum {
220         S_USERS,
221         S_ROOMS,
222         S_SESSION_TABLE,
223         S_FLOORTAB,
224         S_CHATQUEUE,
225         S_CONTROL,
226         S_NETDB,
227         S_SUPPMSGMAIN,
228         S_CONFIG,
229         S_WORKER_LIST,
230         S_HOUSEKEEPING,
231         S_NTTLIST,
232         S_DIRECTORY,
233         S_NETCONFIGS,
234         S_PUBLIC_CLIENTS,
235         S_LDAP,
236         S_FLOORCACHE,
237         S_DEBUGMEMLEAKS,
238         S_ATBF,
239         S_JOURNAL_QUEUE,
240         S_RPLIST,
241         S_SIEVELIST,
242         S_CHKPWD,
243         S_LOG,
244         S_NETSPOOL,
245         MAX_SEMAPHORES
246 };
247
248
249 /*
250  * Upload types
251  */
252 #define UPL_FILE        0
253 #define UPL_NET         1
254 #define UPL_IMAGE       2
255
256
257 /*
258  * message transfer formats
259  */
260 enum {
261         MT_CITADEL,             /* Citadel proprietary */
262         MT_RFC822,              /* RFC822 */
263         MT_MIME,                /* MIME-formatted message */
264         MT_DOWNLOAD,            /* Download a component */
265         MT_SPEW_SECTION         /* Download a component in a single operation */
266 };
267
268 /*
269  * Message format types in the database
270  */
271 #define FMT_CITADEL     0       /* Citadel vari-format (proprietary) */
272 #define FMT_FIXED       1       /* Fixed format (proprietary)        */
273 #define FMT_RFC822      4       /* Standard (headers are in M field) */
274
275
276 /*
277  * Citadel DataBases (define one for each cdb we need to open)
278  */
279 enum {
280         CDB_MSGMAIN,            /* message base                  */
281         CDB_USERS,              /* user file                     */
282         CDB_ROOMS,              /* room index                    */
283         CDB_FLOORTAB,           /* floor index                   */
284         CDB_MSGLISTS,           /* room message lists            */
285         CDB_VISIT,              /* user/room relationships       */
286         CDB_DIRECTORY,          /* address book directory        */
287         CDB_USETABLE,           /* network use table             */
288         CDB_BIGMSGS,            /* larger message bodies         */
289         CDB_FULLTEXT,           /* full text search index        */
290         CDB_EUIDINDEX,          /* locate msgs by EUID           */
291         MAXCDB                  /* total number of CDB's defined */
292 };
293
294 struct cdbdata {
295         size_t len;
296         char *ptr;
297 };
298
299
300
301 /* Structures and declarations for function hooks of various types */
302
303 struct LogFunctionHook {
304         struct LogFunctionHook *next;
305         int loglevel;
306         void (*h_function_pointer) (char *);
307 };
308 extern struct LogFunctionHook *LogHookTable;
309
310 struct CleanupFunctionHook {
311         struct CleanupFunctionHook *next;
312         void (*h_function_pointer) (void);
313 };
314 extern struct CleanupFunctionHook *CleanupHookTable;
315
316 struct FixedOutputHook {
317         struct FixedOutputHook *next;
318         char content_type[64];
319         void (*h_function_pointer) (char *, int);
320 };
321 extern struct FixedOutputHook *FixedOutputTable;
322
323
324
325 /*
326  * SessionFunctionHook extensions are used for any type of hook for which
327  * the context in which it's being called (which is determined by the event
328  * type) will make it obvious for the hook function to know where to look for
329  * pertinent data.
330  */
331 struct SessionFunctionHook {
332         struct SessionFunctionHook *next;
333         void (*h_function_pointer) (void);
334         int eventtype;
335 };
336 extern struct SessionFunctionHook *SessionHookTable;
337
338 /* 
339  * Event types can't be enum'ed, because they must remain consistent between
340  * builds (to allow for binary modules built somewhere else)
341  */
342 #define EVT_STOP        0       /* Session is terminating */
343 #define EVT_START       1       /* Session is starting */
344 #define EVT_LOGIN       2       /* A user is logging in */
345 #define EVT_NEWROOM     3       /* Changing rooms */
346 #define EVT_LOGOUT      4       /* A user is logging out */
347 #define EVT_SETPASS     5       /* Setting or changing password */
348 #define EVT_CMD         6       /* Called after each server command */
349 #define EVT_RWHO        7       /* An RWHO command is being executed */
350 #define EVT_ASYNC       8       /* Doing asynchronous messages */
351
352 #define EVT_TIMER       50      /* Timer events are called once per minute
353                                    and are not tied to any session */
354 #define EVT_HOUSE       51      /* as needed houskeeping stuff */
355 /*
356  * UserFunctionHook extensions are used for any type of hook which implements
357  * an operation on a user or username (potentially) other than the one
358  * operating the current session.
359  */
360 struct UserFunctionHook {
361         struct UserFunctionHook *next;
362         void (*h_function_pointer) (struct ctdluser *usbuf);
363         int eventtype;
364 };
365 extern struct UserFunctionHook *UserHookTable;
366
367 #define EVT_PURGEUSER   100     /* Deleting a user */
368 #define EVT_NEWUSER     102     /* Creating a user */
369
370 /*
371  * MessageFunctionHook extensions are used for hooks which implement handlers
372  * for various types of message operations (save, read, etc.)
373  */
374 struct MessageFunctionHook {
375         struct MessageFunctionHook *next;
376         int (*h_function_pointer) (struct CtdlMessage *msg);
377         int eventtype;
378 };
379 extern struct MessageFunctionHook *MessageHookTable;
380
381 #define EVT_BEFOREREAD  200
382 #define EVT_BEFORESAVE  201
383 #define EVT_AFTERSAVE   202
384 #define EVT_SMTPSCAN    203     /* called before submitting a msg from SMTP */
385
386
387 /*
388  * NetprocFunctionHook extensions are used for hooks which implement handlers
389  * for incoming network messages.
390  */
391 struct NetprocFunctionHook {
392         struct NetprocFunctionHook *next;
393         int (*h_function_pointer) (struct CtdlMessage *msg, char *target_room);
394 };
395 extern struct NetprocFunctionHook *NetprocHookTable;
396
397
398 /*
399  * DeleteFunctionHook extensions are used for hooks which get called when a
400  * message is about to be deleted.
401  */
402 struct DeleteFunctionHook {
403         struct DeleteFunctionHook *next;
404         void (*h_function_pointer) (char *target_room, long msgnum);
405 };
406 extern struct DeleteFunctionHook *DeleteHookTable;
407
408
409 /*
410  * ExpressMessageFunctionHook extensions are used for hooks which implement
411  * the sending of an instant message through various channels.  Any function
412  * registered should return the number of recipients to whom the message was
413  * successfully transmitted.
414  */
415 struct XmsgFunctionHook {
416         struct XmsgFunctionHook *next;
417         int (*h_function_pointer) (char *, char *, char *);
418         int order;
419 };
420 extern struct XmsgFunctionHook *XmsgHookTable;
421
422 /* Priority levels for paging functions (lower is better) */
423 enum {
424         XMSG_PRI_LOCAL,         /* Other users on -this- server */
425         XMSG_PRI_REMOTE,        /* Other users on a Citadel network (future) */
426         XMSG_PRI_FOREIGN,       /* Contacts on foreign instant message hosts */
427         MAX_XMSG_PRI
428 };
429
430
431
432 /*
433  * ServiceFunctionHook extensions are used for hooks which implement various
434  * protocols (either on TCP or on unix domain sockets) directly in the Citadel server.
435  */
436 struct ServiceFunctionHook {
437         struct ServiceFunctionHook *next;
438         int tcp_port;
439         char *sockpath;
440         void (*h_greeting_function) (void) ;
441         void (*h_command_function) (void) ;
442         void (*h_async_function) (void) ;
443         int msock;
444         const char* ServiceName; /* this is just for debugging and logging purposes. */
445 };
446 extern struct ServiceFunctionHook *ServiceHookTable;
447
448
449 /*
450  * RoomFunctionHook extensions are used for hooks which impliment room
451  * processing functions when new messages are added EG. SIEVE.
452  */
453 struct RoomFunctionHook {
454         struct RoomFunctionHook *next;
455         int (*fcn_ptr) (struct ctdlroom *);
456 };
457 extern struct RoomFunctionHook *RoomHookTable;
458
459
460 struct MaintenanceThreadHook {
461         struct MaintenanceThreadHook *next;
462         char *name;
463         void *(*fcn_ptr) (void *arg);
464         pthread_t MaintenanceThread_tid;
465 };
466 extern struct MaintenanceThreadHook *MaintenanceThreadHookTable;
467
468
469 struct SearchFunctionHook {
470         struct SearchFunctionHook *next;
471         void (*fcn_ptr) (int *, long **, char *);
472         char *name;
473 };
474 extern struct SearchFunctionHook *SearchFunctionHookTable;
475
476
477 /* Defines the relationship of a user to a particular room */
478 struct visit {
479         long v_roomnum;
480         long v_roomgen;
481         long v_usernum;
482         long v_lastseen;
483         unsigned int v_flags;
484         char v_seen[SIZ];
485         char v_answered[SIZ];
486         int v_view;
487 };
488
489 #define V_FORGET        1       /* User has zapped this room        */
490 #define V_LOCKOUT       2       /* User is locked out of this room  */
491 #define V_ACCESS        4       /* Access is granted to this room   */
492
493
494 /* Supplementary data for a message on disk
495  * These are kept separate from the message itself for one of two reasons:
496  * 1. Either their values may change at some point after initial save, or
497  * 2. They are merely caches of data which exist somewhere else, for speed.
498  */
499 struct MetaData {
500         long meta_msgnum;               /* Message number in *local* message base */
501         int meta_refcount;              /* Number of rooms pointing to this msg */
502         char meta_content_type[64];     /* Cached MIME content-type */
503         long meta_rfc822_length;        /* Cache of RFC822-translated msg length */
504 };
505
506 /* Calls to AdjRefCount() are queued and deferred, so the user doesn't
507  * have to wait for various disk-intensive operations to complete synchronously.
508  * This is the record format.
509  */
510 struct arcq {
511         long arcq_msgnum;               /* Message number being adjusted */
512         int arcq_delta;                 /* Adjustment ( usually 1 or -1 ) */
513 };
514
515
516 /* 
517  * Serialization routines use this struct to return a pointer and a length
518  */
519 struct ser_ret {
520         size_t len;
521         unsigned char *ser;
522 };
523
524
525 /*
526  * The S_USETABLE database is used in several modules now, so we define its format here.
527  */
528 struct UseTable {
529         char ut_msgid[SIZ];
530         time_t ut_timestamp;
531 };
532
533
534
535 /* Preferred field order                                                        */
536 /*               **********                     Important fields                */
537 /*                         ***************      Semi-important fields           */
538 /*                                        *     Message text (MUST be last)     */
539 #define FORDER  "IPTAFONHRDBCEJGKLQSVWXZYUM"
540
541 #endif /* SERVER_H */