* Initial hack of worker-thread rearchitecture. Right now it is successfully
[citadel.git] / citadel / server.h
1 /* $Id$ */
2 typedef pthread_t THREAD;
3
4 /* Uncomment this if you want to track memory leaks.
5  * This incurs some overhead, so don't use it unless you're debugging the code!
6  */
7 #define DEBUG_MEMORY_LEAKS
8
9 /*
10  * New format for a message in memory
11  */
12 #define CTDLMESSAGE_MAGIC               0x159d
13 struct CtdlMessage {
14         int cm_magic;                   /* Self-check */
15         char cm_anon_type;              /* Anonymous or author-visible */
16         char cm_format_type;            /* Format type */
17         char *cm_fields[256];           /* Data fields */
18         unsigned int cm_flags;          /* How to handle (NOT SAVED TO DISK) */
19 };
20
21 #define CM_SKIP_HOOKS   0x01            /* Don't run server-side handlers */
22
23
24 /*
25  * Generic per-session variable or data structure storage
26  */
27 struct CtdlSessData {
28         struct CtdlSessData *next;
29         unsigned long sym_id;
30         void *sym_data;
31 };
32
33 /*
34  * Static user data symbol types
35  */
36 enum {
37         SYM_DESIRED_SECTION,            /* Used by the MIME parser */
38         SYM_MA_INFO,                    /* Handles multipart/alternative */
39         SYM_REPL,                       /* Used for replication checking */
40         SYM_MAX
41 };
42
43
44 /*
45  * Here's the big one... the Citadel context structure.
46  *
47  * This structure keeps track of all information relating to a running 
48  * session on the server.  We keep one of these for each session thread.
49  *
50  * Note that the first element is "*next" so that it may be used without
51  * modification in a linked list.
52  */
53 struct CitContext {
54         struct CitContext *next;        /* Link to next session in the list */
55
56         struct usersupp usersupp;       /* Database record buffers */
57         struct quickroom quickroom;
58
59         int state;              /* thread state (see CON_ values below) */
60
61         char curr_user[32];     /* name of current user */
62         int logged_in;          /* logged in */
63         int internal_pgm;       /* authenticated as internal program */
64         char temp[32];          /* temp file name */
65         int nologin;            /* not allowed to log in */
66
67         char net_node[32];
68         int client_socket;
69         int cs_pid;             /* session ID */
70         time_t cs_lastupdt;     /* time of last update */
71         time_t lastcmd;         /* time of last command executed */
72         time_t lastidle;        /* For computing idle time */
73         char lastcmdname[5];    /* name of last command executed */
74         unsigned cs_flags;      /* miscellaneous flags */
75
76         /* feeping creaturisms... */
77         int cs_clientdev;       /* client developer ID */
78         int cs_clienttyp;       /* client type code */
79         int cs_clientver;       /* client version number */
80         char cs_clientname[32]; /* name of client software */
81         char cs_host[26];       /* host logged in from */
82
83         FILE *download_fp;      /* Fields relating to file transfer */
84         FILE *upload_fp;
85         char upl_file[256];
86         char upl_path[256];
87         char upl_comment[256];
88         char upl_filedir[256];
89         char chat_room[20];     /* The chat room */
90         char dl_is_net;
91         char upload_type;
92
93         struct ExpressMessage *FirstExpressMessage;
94
95         char fake_username[32]; /* Fake username <bc>                */
96         char fake_postname[32]; /* Fake postname <bc>                */
97         char fake_hostname[25]; /* Name of the fake hostname <bc>    */
98         char fake_roomname[ROOMNAMELEN];        /* Name of the fake room <bc> */
99
100         int FloorBeingSearched; /* This is used by cmd_lrms() etc.   */
101         struct CtdlSessData *FirstSessData;
102 };
103
104 typedef struct CitContext t_context;
105
106 /* Values for CitContext.state */
107 enum {
108         CON_IDLE,               /* This context is doing nothing */
109         CON_EXECUTING,          /* This context is bound to a thread */
110         CON_DYING               /* This context is being terminated */
111 };
112
113
114 #define CS_STEALTH      1       /* stealth mode */
115 #define CS_CHAT         2       /* chat mode */
116 #define CS_POSTING      4       /* Posting */
117
118 struct CitContext *MyContext(void);
119 #define CC ((struct CitContext *)MyContext())
120
121 extern struct CitContext *ContextList;
122 extern int ScheduledShutdown;
123 extern struct CitControl CitControl;
124
125
126 struct ExpressMessage {
127         struct ExpressMessage *next;
128         time_t timestamp;       /* When this message was sent */
129         unsigned flags;         /* Special instructions */
130         char sender[64];        /* Name of sending user */
131         char *text;             /* Message text (if applicable) */
132 };
133
134 #define EM_BROADCAST    1       /* Broadcast message */
135 #define EM_GO_AWAY      2       /* Server requests client log off */
136 #define EM_CHAT         4       /* Server requests client enter chat */
137
138 struct ChatLine {
139         struct ChatLine *next;
140         int chat_seq;
141         time_t chat_time;
142         char chat_text[256];
143         char chat_room[20];
144         char chat_username[32];
145 };
146
147 /*
148  * Various things we need to lock and unlock
149  */
150 enum {
151         S_USERSUPP,
152         S_USER_TRANS,
153         S_QUICKROOM,
154         S_MSGMAIN,
155         S_CALLLOG,
156         S_SESSION_TABLE,
157         S_FLOORTAB,
158         S_CHATQUEUE,
159         S_CONTROL,
160         S_HOUSEKEEPING,
161         S_DATABASE,
162         S_NETDB,
163         S_SUPPMSGMAIN,
164         S_I_WANNA_SELECT,
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 };
186
187
188 /*
189  * Citadel DataBases (define one for each cdb we need to open)
190  */
191 enum {
192         CDB_MSGMAIN,            /* message base                  */
193         CDB_USERSUPP,           /* user file                     */
194         CDB_QUICKROOM,          /* room index                    */
195         CDB_FLOORTAB,           /* floor index                   */
196         CDB_MSGLISTS,           /* room message lists            */
197         CDB_VISIT,              /* user/room relationships       */
198         MAXCDB                  /* total number of CDB's defined */
199 };
200
201 struct cdbdata {
202         size_t len;
203         char *ptr;
204 };
205
206
207
208 /* Structures and declarations for function hooks of various types */
209
210 struct LogFunctionHook {
211         struct LogFunctionHook *next;
212         int loglevel;
213         void (*h_function_pointer) (char *);
214 };
215 extern struct LogFunctionHook *LogHookTable;
216
217 struct CleanupFunctionHook {
218         struct CleanupFunctionHook *next;
219         void (*h_function_pointer) (void);
220 };
221 extern struct CleanupFunctionHook *CleanupHookTable;
222
223
224
225
226 /*
227  * SessionFunctionHook extensions are used for any type of hook for which
228  * the context in which it's being called (which is determined by the event
229  * type) will make it obvious for the hook function to know where to look for
230  * pertinent data.
231  */
232 struct SessionFunctionHook {
233         struct SessionFunctionHook *next;
234         void (*h_function_pointer) (void);
235         int eventtype;
236 };
237 extern struct SessionFunctionHook *SessionHookTable;
238
239 /* 
240  * Event types can't be enum'ed, because they must remain consistent between
241  * builds (to allow for binary modules built somewhere else)
242  */
243 #define EVT_STOP        0       /* Session is terminating */
244 #define EVT_START       1       /* Session is starting */
245 #define EVT_LOGIN       2       /* A user is logging in */
246 #define EVT_NEWROOM     3       /* Changing rooms */
247 #define EVT_LOGOUT      4       /* A user is logging out */
248 #define EVT_SETPASS     5       /* Setting or changing password */
249 #define EVT_CMD         6       /* Called after each server command */
250 #define EVT_RWHO        7       /* An RWHO command is being executed */
251
252
253
254
255
256 /*
257  * UserFunctionHook extensions are used for any type of hook which implements
258  * an operation on a user or username (potentially) other than the one
259  * operating the current session.
260  */
261 struct UserFunctionHook {
262         struct UserFunctionHook *next;
263         void (*h_function_pointer) (char *username, long usernum);
264         int eventtype;
265 };
266 extern struct UserFunctionHook *UserHookTable;
267
268 #define EVT_PURGEUSER   100     /* Deleting a user */
269 #define EVT_OUTPUTMSG   101     /* Outputting a message */
270
271
272 /*
273  * MessageFunctionHook extensions are used for hooks which implement handlers
274  * for various types of message operations (save, read, etc.)
275  */
276 struct MessageFunctionHook {
277         struct MessageFunctionHook *next;
278         int (*h_function_pointer) (struct CtdlMessage *msg);
279         int eventtype;
280 };
281 extern struct MessageFunctionHook *MessageHookTable;
282
283 #define EVT_BEFOREREAD  200
284 #define EVT_BEFORESAVE  201
285 #define EVT_AFTERSAVE   202
286
287
288 /*
289  * ExpressMessageFunctionHook extensions are used for hooks which implement
290  * the sending of an express message through various channels.  Any function
291  * registered should return the number of recipients to whom the message was
292  * successfully transmitted.
293  */
294 struct XmsgFunctionHook {
295         struct XmsgFunctionHook *next;
296         int (*h_function_pointer) (char *, char *, char *);
297         int order;
298 };
299 extern struct XmsgFunctionHook *XmsgHookTable;
300
301 /* Priority levels for paging functions (lower is better) */
302 enum {
303         XMSG_PRI_LOCAL,         /* Other users on -this- server */
304         XMSG_PRI_REMOTE,        /* Other users on a Citadel network (future) */
305         XMSG_PRI_FOREIGN,       /* Contacts on foreign instant message hosts */
306         MAX_XMSG_PRI
307 };
308
309
310
311
312
313 /* Defines the relationship of a user to a particular room */
314 struct visit {
315         long v_roomnum;
316         long v_roomgen;
317         long v_usernum;
318         long v_lastseen;
319         unsigned int v_flags;
320 };
321
322 #define V_FORGET        1       /* User has zapped this room        */
323 #define V_LOCKOUT       2       /* User is locked out of this room  */
324 #define V_ACCESS        4       /* Access is granted to this room   */
325
326 #define UA_KNOWN                2
327 #define UA_GOTOALLOWED          4
328 #define UA_HASNEWMSGS           8
329 #define UA_ZAPPED               16
330
331
332 /* Supplementary data for a message on disk
333  * (These are kept separately from the message itself because they are
334  * fields whose values may change at some point after the message is saved.)
335  */
336 struct SuppMsgInfo {
337         long smi_msgnum;        /* Message number in *local* message base */
338         int smi_refcount;       /* Number of rooms which point to this msg */
339         char smi_content_type[64];
340         /* more stuff will be added to this record in the future */
341 };
342
343
344
345 /* Built-in debuggable stuff for checking for memory leaks */
346 #ifdef DEBUG_MEMORY_LEAKS
347
348 #define mallok(howbig)          tracked_malloc(howbig, __FILE__, __LINE__)
349 #define phree(whichptr)                 tracked_free(whichptr)
350 #define reallok(whichptr,howbig)        tracked_realloc(whichptr,howbig)
351 #define strdoop(orig)           tracked_strdup(orig, __FILE__, __LINE__)
352
353 void *tracked_malloc(size_t, char *, int);
354 void tracked_free(void *);
355 void *tracked_realloc(void *, size_t);
356 void dump_tracked(void);
357 char *tracked_strdup(const char *, char *, int);
358
359 struct TheHeap {
360         struct TheHeap *next;
361         char h_file[32];
362         int h_line;
363         void *h_ptr;
364 };
365
366 extern struct TheHeap *heap;
367
368 #else
369
370 #define mallok(howbig)                  malloc(howbig)
371 #define phree(whichptr)                 free(whichptr)
372 #define reallok(whichptr,howbig)        realloc(whichptr,howbig)
373 #define strdoop(orig)                   strdup(orig)
374
375
376 #endif
377
378
379 /* 
380  * Serialization routines use this struct to return a pointer and a length
381  */
382 struct ser_ret {
383         size_t len;
384         char *ser;
385 };
386
387
388 /* Preferred field order */
389 /*               *********                      Important fields */
390 /*                        ****************      Semi-important fields */
391 /*                                        *     Message text (MUST be last) */
392 #define FORDER  "IPTAONHRDBCEFGJKLQSUVWXYZM"