* Finished the "arbitrary service" registration.
[citadel.git] / citadel / server.h
1 /* $Id$ */
2
3 /* Uncomment this if you want to track memory leaks.
4  * This incurs some overhead, so don't use it unless you're debugging the code!
5  */
6 /* #define DEBUG_MEMORY_LEAKS */
7
8 /*
9  * New format for a message in memory
10  */
11 #define CTDLMESSAGE_MAGIC               0x159d
12 struct CtdlMessage {
13         int cm_magic;                   /* Self-check */
14         char cm_anon_type;              /* Anonymous or author-visible */
15         char cm_format_type;            /* Format type */
16         char *cm_fields[256];           /* Data fields */
17         unsigned int cm_flags;          /* How to handle (NOT SAVED TO DISK) */
18 };
19
20 #define CM_SKIP_HOOKS   0x01            /* Don't run server-side handlers */
21
22
23 /*
24  * Generic per-session variable or data structure storage
25  */
26 struct CtdlSessData {
27         struct CtdlSessData *next;
28         unsigned long sym_id;
29         void *sym_data;
30 };
31
32 /*
33  * Static user data symbol types
34  */
35 enum {
36         SYM_DESIRED_SECTION,            /* Used by the MIME parser */
37         SYM_MA_INFO,                    /* Handles multipart/alternative */
38         SYM_REPL,                       /* Used for replication checking */
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         int state;              /* thread state (see CON_ values below) */
59         int kill_me;            /* Set to nonzero to flag for termination */
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         void (*h_command_function) (void) ;     /* proto command function */
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 /* Values for CitContext.state */
108 enum {
109         CON_IDLE,               /* This context is doing nothing */
110         CON_EXECUTING           /* This context is bound to a thread */
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  * ServiceFunctionHook extensions are used for hooks which implement various
313  * non-Citadel services (on TCP protocols) directly in the Citadel server.
314  */
315 struct ServiceFunctionHook {
316         struct ServiceFunctionHook *next;
317         int tcp_port;
318         void (*h_greeting_function) (void) ;
319         void (*h_command_function) (void) ;
320         int msock;
321 };
322 extern struct ServiceFunctionHook *ServiceHookTable;
323
324
325
326 /* Defines the relationship of a user to a particular room */
327 struct visit {
328         long v_roomnum;
329         long v_roomgen;
330         long v_usernum;
331         long v_lastseen;
332         unsigned int v_flags;
333 };
334
335 #define V_FORGET        1       /* User has zapped this room        */
336 #define V_LOCKOUT       2       /* User is locked out of this room  */
337 #define V_ACCESS        4       /* Access is granted to this room   */
338
339 #define UA_KNOWN                2
340 #define UA_GOTOALLOWED          4
341 #define UA_HASNEWMSGS           8
342 #define UA_ZAPPED               16
343
344
345 /* Supplementary data for a message on disk
346  * (These are kept separately from the message itself because they are
347  * fields whose values may change at some point after the message is saved.)
348  */
349 struct SuppMsgInfo {
350         long smi_msgnum;        /* Message number in *local* message base */
351         int smi_refcount;       /* Number of rooms which point to this msg */
352         char smi_content_type[64];
353         /* more stuff will be added to this record in the future */
354 };
355
356
357
358 /* Built-in debuggable stuff for checking for memory leaks */
359 #ifdef DEBUG_MEMORY_LEAKS
360
361 #define mallok(howbig)          tracked_malloc(howbig, __FILE__, __LINE__)
362 #define phree(whichptr)                 tracked_free(whichptr)
363 #define reallok(whichptr,howbig)        tracked_realloc(whichptr,howbig)
364 #define strdoop(orig)           tracked_strdup(orig, __FILE__, __LINE__)
365
366 void *tracked_malloc(size_t, char *, int);
367 void tracked_free(void *);
368 void *tracked_realloc(void *, size_t);
369 void dump_tracked(void);
370 char *tracked_strdup(const char *, char *, int);
371
372 struct TheHeap {
373         struct TheHeap *next;
374         char h_file[32];
375         int h_line;
376         void *h_ptr;
377 };
378
379 extern struct TheHeap *heap;
380
381 #else
382
383 #define mallok(howbig)                  malloc(howbig)
384 #define phree(whichptr)                 free(whichptr)
385 #define reallok(whichptr,howbig)        realloc(whichptr,howbig)
386 #define strdoop(orig)                   strdup(orig)
387
388
389 #endif
390
391
392 /* 
393  * Serialization routines use this struct to return a pointer and a length
394  */
395 struct ser_ret {
396         size_t len;
397         char *ser;
398 };
399
400
401 /* Preferred field order */
402 /*               *********                      Important fields */
403 /*                        ****************      Semi-important fields */
404 /*                                        *     Message text (MUST be last) */
405 #define FORDER  "IPTAONHRDBCEFGJKLQSUVWXYZM"