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