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