6aedb546393124cce3a72d7e6259eea3740404c5
[citadel.git] / citadel / server.h
1 #ifndef SERVER_H
2 #define SERVER_H
3
4 #ifdef __GNUC__
5 #define INLINE __inline__
6 #else
7 #define INLINE
8 #endif
9
10 #include "citadel.h"
11 #ifdef HAVE_OPENSSL
12 #define OPENSSL_NO_KRB5         /* work around redhat b0rken ssl headers */
13 #include <openssl/ssl.h>
14 #endif
15
16 /*
17  * New format for a message in memory
18  */
19 struct CtdlMessage {
20         int cm_magic;                   /* Self-check (NOT SAVED TO DISK) */
21         char cm_anon_type;              /* Anonymous or author-visible */
22         char cm_format_type;            /* Format type */
23         char *cm_fields[256];           /* Data fields */
24         long cm_lengths[256];           /* size of datafields */
25         unsigned int cm_flags;          /* How to handle (NOT SAVED TO DISK) */
26 };
27
28 #define CTDLMESSAGE_MAGIC               0x159d
29 #define CM_SKIP_HOOKS   0x01            /* Don't run server-side handlers */
30
31
32 /* Data structure returned by validate_recipients() */
33 typedef struct __recptypes {
34         int recptypes_magic;
35         int num_local;
36         int num_internet;
37         int num_ignet;
38         int num_room;
39         int num_error;
40         char *errormsg;
41         char *recp_local;
42         char *recp_internet;
43         char *recp_ignet;
44         char *recp_room;
45         char *recp_orgroom;
46         char *display_recp;
47         char *bounce_to;
48         char *envelope_from;
49         char *sending_room;
50 } recptypes;
51
52 #define RECPTYPES_MAGIC 0xfeeb
53
54
55 #define CTDLEXIT_SHUTDOWN       0       /* Normal shutdown; do NOT auto-restart */
56
57 /*
58  * Exit codes 101 through 109 are used for conditions in which
59  * we deliberately do NOT want the service to automatically
60  * restart.
61  */
62 #define CTDLEXIT_CONFIG         101     /* Could not read citadel.config */
63 #define CTDLEXIT_CONTROL        102     /* Could not acquire lock */
64 #define CTDLEXIT_HOME           103     /* Citadel home directory not found */
65 #define CTDLEXIT_OOD            104     /* Out Of Date config - rerun setup */
66 #define CTDLEXIT_DB             105     /* Unable to initialize database */
67 #define CTDLEXIT_LIBCITADEL     106     /* Incorrect version of libcitadel */
68 #define CTDL_EXIT_UNSUP_AUTH    107     /* Unsupported auth mode configured */
69
70 /*
71  * Reasons why a session would be terminated (set CC->kill_me to these values)
72  */
73 enum {
74         KILLME_NOT,
75         KILLME_UNKNOWN,
76         KILLME_CLIENT_LOGGED_OUT,
77         KILLME_IDLE,
78         KILLME_CLIENT_DISCONNECTED,
79         KILLME_AUTHFAILED,
80         KILLME_SERVER_SHUTTING_DOWN,
81         KILLME_MAX_SESSIONS_EXCEEDED,
82         KILLME_ADMIN_TERMINATE,
83         KILLME_SELECT_INTERRUPTED,
84         KILLME_SELECT_FAILED,
85         KILLME_WRITE_FAILED,
86         KILLME_SIMULATION_WORKER,
87         KILLME_NOLOGIN,
88         KILLME_NO_CRYPTO,
89         KILLME_READSTRING_FAILED,
90         KILLME_MALLOC_FAILED,
91         KILLME_QUOTA,
92         KILLME_READ_FAILED,
93         KILLME_ILLEGAL_MANAGESIEVE_COMMAND,
94         KILLME_SPAMMER,
95         KILLME_XML_PARSER
96 };
97
98
99 #define CS_STEALTH      1       /* stealth mode */
100 #define CS_CHAT         2       /* chat mode */
101 #define CS_POSTING      4       /* Posting */
102
103
104 /*
105  * This is the control record for the message base... 
106  */
107 struct CitControl {
108         long MMhighest;                 /* highest message number in file   */
109         unsigned MMflags;               /* Global system flags              */
110         long MMnextuser;                /* highest user number on system    */
111         long MMnextroom;                /* highest room number on system    */
112         int version;                    /* Server-hosted upgrade level      */
113         int fulltext_wordbreaker;       /* ID of wordbreaker in use         */
114         long MMfulltext;                /* highest message number indexed   */
115         int MMdbversion;                /* Version of Berkeley DB used on previous server run */
116 };
117
118 extern int ScheduledShutdown;
119 extern struct CitControl CitControl;
120
121 struct ExpressMessage {
122         struct ExpressMessage *next;
123         time_t timestamp;       /* When this message was sent */
124         unsigned flags;         /* Special instructions */
125         char sender[256];       /* Name of sending user */
126         char sender_email[256]; /* Email or JID of sending user */
127         char *text;             /* Message text (if applicable) */
128 };
129
130 #define EM_BROADCAST    1       /* Broadcast message */
131 #define EM_GO_AWAY      2       /* Server requests client log off */
132 #define EM_CHAT         4       /* Server requests client enter chat */
133
134 /*
135  * Various things we need to lock and unlock
136  */
137 enum {
138         S_USERS,
139         S_ROOMS,
140         S_SESSION_TABLE,
141         S_FLOORTAB,
142         S_CHATQUEUE,
143         S_CONTROL,
144         S_NETDB,
145         S_SUPPMSGMAIN,
146         S_CONFIG,
147         S_HOUSEKEEPING,
148         S_NTTLIST,
149         S_DIRECTORY,
150         S_NETCONFIGS,
151         S_PUBLIC_CLIENTS,
152         S_FLOORCACHE,
153         S_ATBF,
154         S_JOURNAL_QUEUE,
155         S_RPLIST,
156         S_SIEVELIST,
157         S_CHKPWD,
158         S_LOG,
159         S_NETSPOOL,
160         S_XMPP_QUEUE,
161         S_SCHEDULE_LIST,
162         S_SINGLE_USER,
163         S_LDAP,
164         S_IM_LOGS,
165         MAX_SEMAPHORES
166 };
167
168
169 /*
170  * Upload types
171  */
172 #define UPL_FILE        0
173 #define UPL_NET         1
174 #define UPL_IMAGE       2
175
176
177 /*
178  * message transfer formats
179  */
180 enum {
181         MT_CITADEL,             /* Citadel proprietary */
182         MT_RFC822,              /* RFC822 */
183         MT_MIME,                /* MIME-formatted message */
184         MT_DOWNLOAD,            /* Download a component */
185         MT_SPEW_SECTION         /* Download a component in a single operation */
186 };
187
188 /*
189  * Message format types in the database
190  */
191 #define FMT_CITADEL     0       /* Citadel vari-format (proprietary) */
192 #define FMT_FIXED       1       /* Fixed format (proprietary)        */
193 #define FMT_RFC822      4       /* Standard (headers are in M field) */
194
195
196 /*
197  * Citadel DataBases (define one for each cdb we need to open)
198  */
199 enum {
200         CDB_MSGMAIN,            /* message base                  */
201         CDB_USERS,              /* user file                     */
202         CDB_ROOMS,              /* room index                    */
203         CDB_FLOORTAB,           /* floor index                   */
204         CDB_MSGLISTS,           /* room message lists            */
205         CDB_VISIT,              /* user/room relationships       */
206         CDB_DIRECTORY,          /* address book directory        */
207         CDB_USETABLE,           /* network use table             */
208         CDB_BIGMSGS,            /* larger message bodies         */
209         CDB_FULLTEXT,           /* full text search index        */
210         CDB_EUIDINDEX,          /* locate msgs by EUID           */
211         CDB_USERSBYNUMBER,      /* index of users by number      */
212         CDB_OPENID,             /* associates OpenIDs with users */
213         MAXCDB                  /* total number of CDB's defined */
214 };
215
216 struct cdbdata {
217         size_t len;
218         char *ptr;
219 };
220
221
222 /*
223  * Event types can't be enum'ed, because they must remain consistent between
224  * builds (to allow for binary modules built somewhere else)
225  */
226 #define EVT_STOP        0       /* Session is terminating */
227 #define EVT_START       1       /* Session is starting */
228 #define EVT_LOGIN       2       /* A user is logging in */
229 #define EVT_NEWROOM     3       /* Changing rooms */
230 #define EVT_LOGOUT      4       /* A user is logging out */
231 #define EVT_SETPASS     5       /* Setting or changing password */
232 #define EVT_CMD         6       /* Called after each server command */
233 #define EVT_RWHO        7       /* An RWHO command is being executed */
234 #define EVT_ASYNC       8       /* Doing asynchronous messages */
235 #define EVT_STEALTH     9       /* Entering stealth mode */
236 #define EVT_UNSTEALTH   10      /* Exiting stealth mode */
237
238 #define EVT_TIMER       50      /* Timer events are called once per minute
239                                    and are not tied to any session */
240 #define EVT_HOUSE       51      /* as needed houskeeping stuff */
241 #define EVT_SHUTDOWN    52      /* Server is shutting down */
242
243 #define EVT_PURGEUSER   100     /* Deleting a user */
244 #define EVT_NEWUSER     102     /* Creating a user */
245
246 #define EVT_BEFOREREAD  200
247 #define EVT_BEFORESAVE  201
248 #define EVT_AFTERSAVE   202
249 #define EVT_SMTPSCAN    203     /* called before submitting a msg from SMTP */
250 #define EVT_AFTERUSRMBOXSAVE 204 /* called afte a message was saved into a users inbox */
251 /* Priority levels for paging functions (lower is better) */
252 enum {
253         XMSG_PRI_LOCAL,         /* Other users on -this- server */
254         XMSG_PRI_REMOTE,        /* Other users on a Citadel network (future) */
255         XMSG_PRI_FOREIGN,       /* Contacts on foreign instant message hosts */
256         MAX_XMSG_PRI
257 };
258
259
260 /* Defines the relationship of a user to a particular room */
261 typedef struct __visit {
262         long v_roomnum;
263         long v_roomgen;
264         long v_usernum;
265         long v_lastseen;
266         unsigned int v_flags;
267         char v_seen[SIZ];
268         char v_answered[SIZ];
269         int v_view;
270 } visit;
271
272 #define V_FORGET        1       /* User has zapped this room        */
273 #define V_LOCKOUT       2       /* User is locked out of this room  */
274 #define V_ACCESS        4       /* Access is granted to this room   */
275
276
277 /* Supplementary data for a message on disk
278  * These are kept separate from the message itself for one of two reasons:
279  * 1. Either their values may change at some point after initial save, or
280  * 2. They are merely caches of data which exist somewhere else, for speed.
281  */
282 struct MetaData {
283         long meta_msgnum;               /* Message number in *local* message base */
284         int meta_refcount;              /* Number of rooms pointing to this msg */
285         char meta_content_type[64];     /* Cached MIME content-type */
286         long meta_rfc822_length;        /* Cache of RFC822-translated msg length */
287         char mimetype[64];              /* if we were able to guess the mimetype for the data */ 
288 };
289
290 /* Calls to AdjRefCount() are queued and deferred, so the user doesn't
291  * have to wait for various disk-intensive operations to complete synchronously.
292  * This is the record format.
293  */
294 struct arcq {
295         long arcq_msgnum;               /* Message number being adjusted */
296         int arcq_delta;                 /* Adjustment ( usually 1 or -1 ) */
297 };
298
299
300 /*
301  * Serialization routines use this struct to return a pointer and a length
302  */
303 struct ser_ret {
304         size_t len;
305         unsigned char *ser;
306 };
307
308
309 /*
310  * The S_USETABLE database is used in several modules now, so we define its format here.
311  */
312 struct UseTable {
313         char ut_msgid[SIZ];
314         time_t ut_timestamp;
315 };
316
317
318
319 /* Preferred field order                                                        */
320 /*               **********                     Important fields                */
321 /*                         ***************      Semi-important fields           */
322 /*                                        **    internal only                   */
323 /*                                          *   Message text (MUST be last)     */
324 ///#define FORDER       "IPTAFONHRDBCEWJGKLQSVXZYU12M"
325
326 typedef enum _MsgField {
327         eAuthor       = 'A',
328         eBig_message  = 'B',
329         eRemoteRoom   = 'C',
330         eDestination  = 'D',
331         eExclusiveID  = 'E',
332         erFc822Addr   = 'F',
333         eHumanNode    = 'H',
334         emessageId    = 'I',
335         eJournal      = 'J',
336         eReplyTo      = 'K',
337         eListID       = 'L',
338         eMesageText   = 'M',
339         eNodeName     = 'N',
340         eOriginalRoom = 'O',
341         eMessagePath  = 'P',
342         eRecipient    = 'R',
343         eSpecialField = 'S',
344         eTimestamp    = 'T',
345         eMsgSubject   = 'U',
346         eenVelopeTo   = 'V',
347         eWeferences   = 'W',
348         eCarbonCopY   = 'Y',
349         eErrorMsg     = '0',
350         eSuppressIdx  = '1',
351         eExtnotify    = '2',
352         eVltMsgNum    = '3'
353 }eMsgField;
354
355 #endif /* SERVER_H */