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