94c9fce4ff9b5b848865e92d37abf32f35cd9e21
[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_MAX
40 };
41
42
43 /*
44  * Here's the big one... the Citadel context structure.
45  *
46  * This structure keeps track of all information relating to a running 
47  * session on the server.  We keep one of these for each session thread.
48  *
49  * Note that the first element is "*next" so that it may be used without
50  * modification in a linked list.
51  */
52 struct CitContext {
53         struct CitContext *next;        /* Link to next session in the list */
54
55         struct usersupp usersupp;       /* Database record buffers */
56         struct quickroom quickroom;
57
58         char curr_user[32];     /* name of current user */
59         int logged_in;          /* logged in */
60         int internal_pgm;       /* authenticated as internal program */
61         char temp[32];          /* temp file name */
62         int nologin;            /* not allowed to log in */
63
64         char net_node[32];
65         THREAD mythread;
66         int n_crit;             /* number of critical sections open */
67         int client_socket;
68         int cs_pid;             /* session ID */
69         char cs_room[ROOMNAMELEN];      /* current room */
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 #define CS_STEALTH      1       /* stealth mode */
107 #define CS_CHAT         2       /* chat mode */
108 #define CS_POSTING      4       /* Posting */
109
110 struct CitContext *MyContext(void);
111 #define CC ((struct CitContext *)MyContext())
112
113 extern struct CitContext *ContextList;
114 extern int ScheduledShutdown;
115 extern struct CitControl CitControl;
116
117
118 struct ExpressMessage {
119         struct ExpressMessage *next;
120         time_t timestamp;       /* When this message was sent */
121         unsigned flags;         /* Special instructions */
122         char sender[64];        /* Name of sending user */
123         char *text;             /* Message text (if applicable) */
124 };
125
126 #define EM_BROADCAST    1       /* Broadcast message */
127 #define EM_GO_AWAY      2       /* Server requests client log off */
128 #define EM_CHAT         4       /* Server requests client enter chat */
129
130 struct ChatLine {
131         struct ChatLine *next;
132         int chat_seq;
133         time_t chat_time;
134         char chat_text[256];
135         char chat_room[20];
136         char chat_username[32];
137 };
138
139 /*
140  * Various things we need to lock and unlock
141  */
142 enum {
143         S_USERSUPP,
144         S_USER_TRANS,
145         S_QUICKROOM,
146         S_MSGMAIN,
147         S_CALLLOG,
148         S_SESSION_TABLE,
149         S_FLOORTAB,
150         S_CHATQUEUE,
151         S_CONTROL,
152         S_HOUSEKEEPING,
153         S_DATABASE,
154         S_NETDB,
155         S_SUPPMSGMAIN,
156         MAX_SEMAPHORES
157 };
158
159
160 /*
161  * Upload types
162  */
163 #define UPL_FILE        0
164 #define UPL_NET         1
165 #define UPL_IMAGE       2
166
167
168 /*
169  * message transfer formats
170  */
171 enum {
172         MT_CITADEL,             /* Citadel proprietary */
173         MT_RFC822,              /* RFC822 */
174         MT_MIME,                /* MIME-formatted message */
175         MT_DOWNLOAD             /* Download a component */
176 };
177
178
179 /*
180  * Citadel DataBases (define one for each cdb we need to open)
181  */
182 enum {
183         CDB_MSGMAIN,            /* message base                  */
184         CDB_USERSUPP,           /* user file                     */
185         CDB_QUICKROOM,          /* room index                    */
186         CDB_FLOORTAB,           /* floor index                   */
187         CDB_MSGLISTS,           /* room message lists            */
188         CDB_VISIT,              /* user/room relationships       */
189         MAXCDB                  /* total number of CDB's defined */
190 };
191
192 struct cdbdata {
193         size_t len;
194         char *ptr;
195 };
196
197
198
199 /* Structures and declarations for function hooks of various types */
200
201 struct LogFunctionHook {
202         struct LogFunctionHook *next;
203         int loglevel;
204         void (*h_function_pointer) (char *);
205 };
206 extern struct LogFunctionHook *LogHookTable;
207
208 struct CleanupFunctionHook {
209         struct CleanupFunctionHook *next;
210         void (*h_function_pointer) (void);
211 };
212 extern struct CleanupFunctionHook *CleanupHookTable;
213
214
215
216
217 /*
218  * SessionFunctionHook extensions are used for any type of hook for which
219  * the context in which it's being called (which is determined by the event
220  * type) will make it obvious for the hook function to know where to look for
221  * pertinent data.
222  */
223 struct SessionFunctionHook {
224         struct SessionFunctionHook *next;
225         void (*h_function_pointer) (void);
226         int eventtype;
227 };
228 extern struct SessionFunctionHook *SessionHookTable;
229
230 /* 
231  * Event types can't be enum'ed, because they must remain consistent between
232  * builds (to allow for binary modules built somewhere else)
233  */
234 #define EVT_STOP        0       /* Session is terminating */
235 #define EVT_START       1       /* Session is starting */
236 #define EVT_LOGIN       2       /* A user is logging in */
237 #define EVT_NEWROOM     3       /* Changing rooms */
238 #define EVT_LOGOUT      4       /* A user is logging out */
239 #define EVT_SETPASS     5       /* Setting or changing password */
240 #define EVT_CMD         6       /* Called after each server command */
241 #define EVT_RWHO        7       /* An RWHO command is being executed */
242
243
244
245
246
247 /*
248  * UserFunctionHook extensions are used for any type of hook which implements
249  * an operation on a user or username (potentially) other than the one
250  * operating the current session.
251  */
252 struct UserFunctionHook {
253         struct UserFunctionHook *next;
254         void (*h_function_pointer) (char *username, long usernum);
255         int eventtype;
256 };
257 extern struct UserFunctionHook *UserHookTable;
258
259 #define EVT_PURGEUSER   100     /* Deleting a user */
260 #define EVT_OUTPUTMSG   101     /* Outputting a message */
261
262
263 /*
264  * MessageFunctionHook extensions are used for hooks which implement handlers
265  * for various types of message operations (save, read, etc.)
266  */
267 struct MessageFunctionHook {
268         struct MessageFunctionHook *next;
269         int (*h_function_pointer) (struct CtdlMessage *msg);
270         int eventtype;
271 };
272 extern struct MessageFunctionHook *MessageHookTable;
273
274 #define EVT_BEFOREREAD  200
275 #define EVT_BEFORESAVE  201
276 #define EVT_AFTERSAVE   202
277
278
279 /*
280  * ExpressMessageFunctionHook extensions are used for hooks which implement
281  * the sending of an express message through various channels.  Any function
282  * registered should return the number of recipients to whom the message was
283  * successfully transmitted.
284  */
285 struct XmsgFunctionHook {
286         struct XmsgFunctionHook *next;
287         int (*h_function_pointer) (char *, char *, char *);
288         int order;
289 };
290 extern struct XmsgFunctionHook *XmsgHookTable;
291
292 /* Priority levels for paging functions (lower is better) */
293 enum {
294         XMSG_PRI_LOCAL,         /* Other users on -this- server */
295         XMSG_PRI_REMOTE,        /* Other users on a Citadel network (future) */
296         XMSG_PRI_FOREIGN,       /* Contacts on foreign instant message hosts */
297         MAX_XMSG_PRI
298 };
299
300
301
302
303
304 /* Defines the relationship of a user to a particular room */
305 struct visit {
306         long v_roomnum;
307         long v_roomgen;
308         long v_usernum;
309         long v_lastseen;
310         unsigned int v_flags;
311 };
312
313 #define V_FORGET        1       /* User has zapped this room        */
314 #define V_LOCKOUT       2       /* User is locked out of this room  */
315 #define V_ACCESS        4       /* Access is granted to this room   */
316
317 #define UA_KNOWN                2
318 #define UA_GOTOALLOWED          4
319 #define UA_HASNEWMSGS           8
320 #define UA_ZAPPED               16
321
322
323 /* Supplementary data for a message on disk
324  * (These are kept separately from the message itself because they are
325  * fields whose values may change at some point after the message is saved.)
326  */
327 struct SuppMsgInfo {
328         long smi_msgnum;        /* Message number in *local* message base */
329         int smi_refcount;       /* Number of rooms which point to this msg */
330         char smi_content_type[64];
331         /* more stuff will be added to this record in the future */
332 };
333
334
335
336 /* Built-in debuggable stuff for checking for memory leaks */
337 #ifdef DEBUG_MEMORY_LEAKS
338
339 #define mallok(howbig)          tracked_malloc(howbig, __FILE__, __LINE__)
340 #define phree(whichptr)                 tracked_free(whichptr)
341 #define reallok(whichptr,howbig)        tracked_realloc(whichptr,howbig)
342 #define strdoop(orig)           tracked_strdup(orig, __FILE__, __LINE__)
343
344 void *tracked_malloc(size_t, char *, int);
345 void tracked_free(void *);
346 void *tracked_realloc(void *, size_t);
347 void dump_tracked(void);
348 char *tracked_strdup(const char *, char *, int);
349
350 struct TheHeap {
351         struct TheHeap *next;
352         char h_file[32];
353         int h_line;
354         void *h_ptr;
355 };
356
357 extern struct TheHeap *heap;
358
359 #else
360
361 #define mallok(howbig)                  malloc(howbig)
362 #define phree(whichptr)                 free(whichptr)
363 #define reallok(whichptr,howbig)        realloc(whichptr,howbig)
364 #define strdoop(orig)                   strdup(orig)
365
366
367 #endif
368
369
370 /* 
371  * Serialization routines use this struct to return a pointer and a length
372  */
373 struct ser_ret {
374         size_t len;
375         char *ser;
376 };
377
378
379 /* Preferred field order */
380 /*               *********                      Important fields */
381 /*                        ****************      Semi-important fields */
382 /*                                        *     Message text (MUST be last) */
383 #define FORDER  "IPTAONHRDBCEFGJKLQSUVWXYZM"