03041f2f01892b2feac72b64f00f5dfd201920bc
[citadel.git] / webcit / webcit.h
1 /* $Id$ */
2
3 /*
4  * Uncomment to dump an HTTP trace to stderr
5 #define HTTP_TRACING 1
6  */
7
8 #ifdef HTTP_TRACING
9 #undef HAVE_ZLIB_H
10 #undef HAVE_ZLIB
11 #endif
12
13 #ifdef HAVE_ZLIB_H
14 #include <zlib.h>
15 #endif
16
17 #ifdef HAVE_ICAL_H
18 #ifdef HAVE_LIBICAL
19 #define WEBCIT_WITH_CALENDAR_SERVICE 1
20 #endif
21 #endif
22
23 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
24 #include <ical.h>
25 #endif
26
27 #ifdef HAVE_OPENSSL
28 /* Work around RedHat's b0rken OpenSSL includes */
29 #define OPENSSL_NO_KRB5
30 #include <openssl/ssl.h>
31 #include <openssl/err.h>
32 #include <openssl/rand.h>
33 #endif
34
35 #define CALENDAR_ROOM_NAME      "Calendar"
36 #define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
37
38 #define SIZ                     4096            /* generic buffer size */
39
40 #define TRACE fprintf(stderr, "Checkpoint: %s, %d\n", __FILE__, __LINE__)
41
42 #define SLEEPING                180             /* TCP connection timeout */
43 #define WEBCIT_TIMEOUT          900             /* WebCit session timeout */
44 #define PORT_NUM                2000            /* port number to listen on */
45 #define SERVER                  "WebCit v6.21"  /* who's in da house */
46 #define DEVELOPER_ID            0
47 #define CLIENT_ID               4
48 #define CLIENT_VERSION          621             /* This version of WebCit */
49 #define MINIMUM_CIT_VERSION     655             /* min required Citadel vers */
50 #define DEFAULT_HOST            "localhost"     /* Default Citadel server */
51 #define DEFAULT_PORT            "504"
52 #define LB                      (1)             /* Internal escape chars */
53 #define RB                      (2)
54 #define QU                      (3)
55 #define TARGET                  "webcit01"      /* Target for inline URL's */
56 #define HOUSEKEEPING            15              /* Housekeeping frequency */
57 #define MIN_WORKER_THREADS      5
58 #define MAX_WORKER_THREADS      250
59 #define LISTEN_QUEUE_LENGTH     100             /* listen() backlog queue */
60
61 #define USERCONFIGROOM          "My Citadel Config"
62 #define DEFAULT_MAXMSGS         20
63
64
65 /*
66  * Room flags (from Citadel)
67  *
68  * bucket one...
69  */
70 #define QR_PERMANENT    1               /* Room does not purge        */
71 #define QR_INUSE        2               /* Set if in use, clear if avail    */
72 #define QR_PRIVATE      4               /* Set for any type of private room */
73 #define QR_PASSWORDED   8               /* Set if there's a password too    */
74 #define QR_GUESSNAME    16              /* Set if it's a guessname room     */
75 #define QR_DIRECTORY    32              /* Directory room                  */
76 #define QR_UPLOAD       64              /* Allowed to upload            */
77 #define QR_DOWNLOAD     128             /* Allowed to download        */
78 #define QR_VISDIR       256             /* Visible directory            */
79 #define QR_ANONONLY     512             /* Anonymous-Only room        */
80 #define QR_ANONOPT      1024            /* Anonymous-Option room            */
81 #define QR_NETWORK      2048            /* Shared network room        */
82 #define QR_PREFONLY     4096            /* Preferred status needed to enter */
83 #define QR_READONLY     8192            /* Aide status required to post     */
84 #define QR_MAILBOX      16384           /* Set if this is a private mailbox */
85
86 /*
87  * bucket two...
88  */
89 #define QR2_SYSTEM      1               /* System room; hide by default     */
90 #define QR2_SELFLIST    2               /* Self-service mailing list mgmt   */
91
92
93 #define UA_KNOWN                2
94 #define UA_GOTOALLOWED    4
95 #define UA_HASNEWMSGS      8
96 #define UA_ZAPPED              16
97
98
99
100
101
102
103 struct httprequest {
104         struct httprequest *next;
105         char line[SIZ];
106 };
107
108 struct urlcontent {
109         struct urlcontent *next;
110         char url_key[32];
111         char *url_data;
112 };
113
114 struct serv_info {
115         int serv_pid;
116         char serv_nodename[32];
117         char serv_humannode[64];
118         char serv_fqdn[64];
119         char serv_software[64];
120         int serv_rev_level;
121         char serv_bbs_city[64];
122         char serv_sysadm[64];
123         char serv_moreprompt[SIZ];
124         int serv_ok_floors;
125         int serv_supports_ldap;
126 };
127
128
129
130 /*
131  * This struct holds a list of rooms for <G>oto operations.
132  */
133 struct march {
134         struct march *next;
135         char march_name[128];
136         int march_floor;
137         int march_order;
138 };
139
140 /* 
141  * This struct holds a list of rooms for client display.
142  * (oooh, a tree!)
143  */
144 struct roomlisting {
145         struct roomlisting *lnext;
146         struct roomlisting *rnext;
147         char rlname[128];
148         unsigned rlflags;
149         int rlfloor;
150         int rlorder;
151 };
152
153
154
155 /*
156  * Dynamic content for variable substitution in templates
157  */
158 struct wcsubst {
159         struct wcsubst *next;
160         int wcs_type;
161         char wcs_key[32];
162         void *wcs_value;
163         void (*wcs_function)(void);
164 };
165
166 /*
167  * Values for wcs_type
168  */
169 enum {
170         WCS_STRING,
171         WCS_FUNCTION,
172         WCS_SERVCMD
173 };
174
175
176 struct wc_attachment {
177         struct wc_attachment *next;
178         size_t length;
179         char content_type[SIZ];
180         char filename[SIZ];
181         char *data;
182 };
183
184 struct message_summary {
185         time_t date;
186         long msgnum;
187         char from[128];
188         char to[128];
189         char subj[128];
190         int hasattachments;
191         int is_new;
192 };
193
194 /*
195  * One of these is kept for each active Citadel session.
196  * HTTP transactions are bound to one at a time.
197  */
198 struct wcsession {
199         struct wcsession *next;         /* Linked list */
200         int wc_session;                 /* WebCit session ID */
201         char wc_username[SIZ];
202         char wc_password[SIZ];
203         char wc_roomname[SIZ];
204         int connected;
205         int logged_in;
206         int axlevel;
207         int is_aide;
208         int is_room_aide;
209         int http_sock;
210         int serv_sock;
211         int chat_sock;
212         unsigned room_flags;
213         int wc_view;
214         int wc_default_view;
215         int wc_floor;
216         char ugname[128];
217         long uglsn;
218         int upload_length;
219         char *upload;
220         char upload_filename[SIZ];
221         char upload_content_type[SIZ];
222         int new_mail;
223         int remember_new_mail;
224         int need_regi;                  /* This user needs to register. */
225         int need_vali;                  /* New users require validation. */
226         char cs_inet_email[SIZ];        /* User's preferred Internet addr. */
227         pthread_mutex_t SessionMutex;   /* mutex for exclusive access */
228         time_t lastreq;                 /* Timestamp of most recent HTTP */
229         int killthis;                   /* Nonzero == purge this session */
230         struct march *march;            /* march mode room list */
231         char reply_to[SIZ];             /* reply-to address */
232
233         long msgarr[10000];             /* for read operations */
234         int num_summ;
235         struct message_summary *summ;
236
237         int is_wap;                     /* Client is a WAP gateway */
238         struct urlcontent *urlstrings;
239         int HaveInstantMessages;        /* Nonzero if incoming msgs exist */
240         struct wcsubst *vars;
241         char this_page[SIZ];            /* address of current page */
242         char http_host[SIZ];            /* HTTP Host: header */
243         char *preferences;
244 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
245         struct disp_cal {
246                 icalcomponent *cal;             /* cal items for display */
247                 long cal_msgnum;                /* cal msgids for display */
248         } *disp_cal;
249         int num_cal;
250 #endif
251         struct wc_attachment *first_attachment;
252         char ImportantMessage[SIZ];
253         char last_chat_user[SIZ];
254         int ctdl_pid;                   /* Session ID on the Citadel server */
255         char httpauth_user[SIZ];        /* only for GroupDAV sessions */
256         char httpauth_pass[SIZ];        /* only for GroupDAV sessions */
257
258         size_t burst_len;
259         char *burst;
260         int gzip_ok;                    /* Nonzero if Accept-encoding: gzip */
261 };
262
263 #define num_parms(source)               num_tokens(source, '|')
264
265 /* Per-session data */
266 #define WC ((struct wcsession *)pthread_getspecific(MyConKey))
267 extern pthread_key_t MyConKey;
268
269 /* Per-thread SSL context */
270 #ifdef HAVE_OPENSSL
271 #define THREADSSL ((SSL *)pthread_getspecific(ThreadSSL))
272 extern pthread_key_t ThreadSSL;
273 #endif
274
275 struct serv_info serv_info;
276 extern char floorlist[128][SIZ];
277 extern char *axdefs[];
278 extern char *ctdlhost, *ctdlport;
279 extern char *server_cookie;
280 extern int is_https;
281 extern int setup_wizard;
282 extern char wizard_filename[];
283 void do_setup_wizard(void);
284
285 void stuff_to_cookie(char *cookie, int session,
286                         char *user, char *pass, char *room);
287 void cookie_to_stuff(char *cookie, int *session,
288                 char *user, size_t user_len,
289                 char *pass, size_t pass_len,
290                 char *room, size_t room_len);
291 char *bmstrstr(char *text, char *pattern,
292         int (*cmpfunc)(const char *, const char *, size_t) );
293 void locate_host(char *, int);
294 void become_logged_in(char *, char *, char *);
295 void do_login(void);
296 void display_login(char *mesg);
297 void do_welcome(void);
298 void do_logout(void);
299 void display_main_menu(void);
300 void display_aide_menu(void);
301 void display_advanced_menu(void);
302 void slrp_highest(void);
303 void gotonext(void);
304 void ungoto(void);
305 void get_serv_info(char *, char *);
306 int uds_connectsock(char *);
307 int tcp_connectsock(char *, char *);
308 void serv_getln(char *strbuf, int bufsize);
309 void serv_puts(char *string);
310 void who(void);
311 void who_inner_html(void);
312 void fmout(FILE *fp, char *align);
313 void wDumpContent(int);
314 void serv_printf(const char *format,...);
315 char *bstr(char *key);
316 void urlesc(char *, char *);
317 void urlescputs(char *);
318 void jsesc(char *, char *);
319 void jsescputs(char *);
320 void output_headers(    int do_httpheaders,
321                         int do_htmlhead,
322                         int do_room_banner,
323                         int unset_cookies,
324                         int refresh30,
325                         int suppress_check,
326                         int cache);
327 void wprintf(const char *format,...);
328 void output_static(char *what);
329 void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks);
330 void escputs(char *strbuf);
331 void url(char *buf);
332 void escputs1(char *strbuf, int nbsp, int nolinebreaks);
333 void msgesc(char *target, char *strbuf);
334 void msgescputs(char *strbuf);
335 int extract_int(const char *source, int parmnum);
336 long extract_long(const char *source, int parmnum);
337 void stripout(char *str, char leftboundary, char rightboundary);
338 void dump_vars(void);
339 void embed_main_menu(void);
340 void serv_read(char *buf, int bytes);
341 int haschar(char *, char);
342 void readloop(char *oper);
343 void embed_message(void);
344 void text_to_server(char *ptr, int convert_to_html);
345 void display_enter(void);
346 void post_message(void);
347 void confirm_delete_msg(void);
348 void delete_msg(void);
349 void confirm_move_msg(void);
350 void move_msg(void);
351 void userlist(void);
352 void showuser(void);
353 void display_page(void);
354 void page_user(void);
355 void do_chat(void);
356 void display_private(char *rname, int req_pass);
357 void goto_private(void);
358 void zapped_list(void);
359 void display_zap(void);
360 void zap(void);
361 void display_success(char *);
362 void display_entroom(void);
363 void entroom(void);
364 void display_editroom(void);
365 void netedit(void);
366 void editroom(void);
367 void display_whok(void);
368 void do_invt_kick(void);
369 void server_to_text(void);
370 void save_edit(char *description, char *enter_cmd, int regoto);
371 void display_edit(char *description, char *check_cmd,
372                   char *read_cmd, char *save_cmd, int with_room_banner);
373 void gotoroom(char *gname);
374 void confirm_delete_room(void);
375 void delete_room(void);
376 void validate(void);
377 void display_graphics_upload(char *, char *, char *);
378 void do_graphics_upload(char *upl_cmd);
379 void serv_read(char *buf, int bytes);
380 void serv_gets(char *strbuf);
381 void serv_write(char *buf, int nbytes);
382 void serv_puts(char *string);
383 void serv_printf(const char *format,...);
384 void load_floorlist(void);
385 void display_reg(int);
386 void display_changepw(void);
387 void changepw(void);
388 void display_edit_node(void);
389 void edit_node(void);
390 void display_netconf(void);
391 void display_confirm_delete_node(void);
392 void delete_node(void);
393 void display_add_node(void);
394 void add_node(void);
395 void terminate_session(void);
396 void edit_me(void);
397 void display_siteconfig(void);
398 void siteconfig(void);
399 void display_generic(void);
400 void do_generic(void);
401 void display_menubar(int);
402 void smart_goto(char *);
403 void worker_entry(void);
404 void session_loop(struct httprequest *);
405 void fmt_date(char *buf, time_t thetime, int brief);
406 void fmt_time(char *buf, time_t thetime);
407 void httpdate(char *buf, time_t thetime);
408 void end_webcit_session(void);
409 void page_popup(void);
410 void chat_recv(void);
411 void chat_send(void);
412 void http_redirect(char *);
413 void clear_local_substs(void);
414 void svprintf(char *keyname, int keytype, const char *format,...);
415 void svcallback(char *keyname, void (*fcn_ptr)() );
416 void do_template(void *templatename);
417 int lingering_close(int fd);
418 char *memreadline(char *start, char *buf, int maxlen);
419 int num_tokens (char *source, char tok);
420 void extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen);
421 void remove_token(char *source, int parmnum, char separator);
422 char *load_mimepart(long msgnum, char *partnum);
423 int pattern2(char *search, char *patn);
424 void do_edit_vcard(long, char *, char *);
425 void edit_vcard(void);
426 void submit_vcard(void);
427 void striplt(char *);
428 void select_user_to_edit(char *message, char *preselect);
429 void delete_user(char *);
430 void display_edituser(char *who, int is_new);
431 void create_user(void);
432 void edituser(void);
433 void do_change_view(int);
434 void change_view(void);
435 void folders(void);
436 void do_stuff_to_msgs(void);
437 void load_preferences(void);
438 void save_preferences(void);
439 void get_preference(char *key, char *value, size_t value_len);
440 void set_preference(char *key, char *value, int save_to_server);
441 void knrooms(void);
442 int is_msg_in_mset(char *mset, long msgnum);
443 char *safestrncpy(char *dest, const char *src, size_t n);
444 void display_addressbook(long msgnum, char alpha);
445 void offer_start_page(void);
446 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext);
447 void change_start_page(void);
448 void output_html(char *);
449 void display_floorconfig(char *);
450 void delete_floor(void);
451 void create_floor(void);
452 void rename_floor(void);
453 void do_listsub(void);
454 void toggle_self_service(void);
455 void summary(void);
456 ssize_t write(int fd, const void *buf, size_t count);
457 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum);
458 void display_calendar(long msgnum);
459 void display_task(long msgnum);
460 void display_note(long msgnum);
461 void do_calendar_view(void);
462 void do_tasks_view(void);
463 void free_calendar_buffer(void);
464 void calendar_summary_view(void);
465 int load_msg_ptrs(char *servcmd, int with_headers);
466 void CtdlEncodeBase64(char *dest, const char *source, size_t sourcelen);
467 int CtdlDecodeBase64(char *dest, const char *source, size_t length);
468 void free_attachments(struct wcsession *sess);
469 void set_room_policy(void);
470 void display_inetconf(void);
471 void save_inetconf(void);
472 void generate_uuid(char *);
473 void display_preferences(void);
474 void set_preferences(void);
475
476 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
477 void display_edit_task(void);
478 void save_task(void);
479 void display_edit_event(void);
480 void save_event(void);
481 void display_icaltimetype_as_webform(struct icaltimetype *, char *);
482 void icaltime_from_webform(struct icaltimetype *result, char *prefix);
483 void icaltime_from_webform_dateonly(struct icaltimetype *result, char *prefix);
484 void display_edit_individual_event(icalcomponent *supplied_vtodo, long msgnum);
485 void save_individual_event(icalcomponent *supplied_vtodo, long msgnum);
486 void respond_to_request(void);
487 void handle_rsvp(void);
488 void ical_dezonify(icalcomponent *cal);
489 void partstat_as_string(char *buf, icalproperty *attendee);
490 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp);
491 void check_attendee_availability(icalcomponent *supplied_vevent);
492 void do_freebusy(char *req);
493 #endif
494
495 extern char *months[];
496 extern char *days[];
497 void read_server_binary(char *buffer, size_t total_len);
498 char *read_server_text(void);
499 int goto_config_room(void);
500 long locate_user_vcard(char *username, long usernum);
501 void sleeeeeeeeeep(int);
502 void http_transmit_thing(char *thing, size_t length, char *content_type,
503                          int is_static);
504 void unescape_input(char *buf);
505 void do_iconbar(void);
506 void display_customize_iconbar(void);
507 void commit_iconbar(void);
508 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen);
509 void spawn_another_worker_thread(void);
510
511 void embed_room_banner(char *, int);
512 /* navbar types that can be passed to embed_room_banner */
513 enum {
514         navbar_none,
515         navbar_default
516 };
517
518
519 #ifdef HAVE_OPENSSL
520 void init_ssl(void);
521 void endtls(void);
522 void ssl_lock(int mode, int n, const char *file, int line);
523 int starttls(int sock);
524 extern SSL_CTX *ssl_ctx;  
525 int client_read_ssl(char *buf, int bytes, int timeout);
526 void client_write_ssl(char *buf, int nbytes);
527 #endif
528
529 #ifdef HAVE_ZLIB
530 #include <zlib.h>
531 int ZEXPORT compress_gzip(Bytef * dest, uLongf * destLen,
532                           const Bytef * source, uLong sourceLen, int level);
533 #endif
534
535
536 void begin_burst(void);
537 void end_burst(void);
538
539 extern char *ascmonths[];
540 void http_datestring(char *buf, size_t n, time_t xtime);
541
542 /* Views (from citadel.h) */
543 #define VIEW_BBS                0       /* Traditional Citadel BBS view */
544 #define VIEW_MAILBOX            1       /* Mailbox summary */
545 #define VIEW_ADDRESSBOOK        2       /* Address book view */
546 #define VIEW_CALENDAR           3       /* Calendar view */
547 #define VIEW_TASKS              4       /* Tasks view */
548 #define VIEW_NOTES              5       /* Notes view */
549
550
551 /* These should be empty, but we have them for testing */
552 #define DEFAULT_HTTPAUTH_USER   ""
553 #define DEFAULT_HTTPAUTH_PASS   ""