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