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