add a few #defines to fix OS X
[citadel.git] / webcit / webcit.h
1 /* $Id$ */
2
3 #include "sysdep.h"
4
5
6 #include <sys/select.h>
7
8 #include <ctype.h>
9 #include <stdlib.h>
10 #ifdef HAVE_UNISTD_H
11 #include <unistd.h>
12 #endif
13 #include <stdio.h>
14 #ifdef HAVE_FCNTL_H
15 #include <fcntl.h>
16 #endif
17 #include <signal.h>
18 #include <sys/types.h>
19 #include <sys/wait.h>
20 #include <sys/socket.h>
21 #ifdef HAVE_SYS_TIME_H
22 #include <sys/time.h>
23 #endif
24 #include <sys/stat.h>
25 #ifdef HAVE_LIMITS_H
26 #include <limits.h>
27 #endif
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
30 #include <sys/un.h>
31 #include <netdb.h>
32 #include <sys/poll.h>
33 #include <string.h>
34 #include <pwd.h>
35 #include <errno.h>
36 #include <stdarg.h>
37 #include <pthread.h>
38 #include <signal.h>
39 #include <sys/utsname.h>
40
41 #include <libcitadel.h>
42
43 #ifndef INADDR_NONE
44 #define INADDR_NONE 0xffffffff
45 #endif
46
47 #ifdef HAVE_ICONV
48 #include <iconv.h>
49 #endif
50
51 #ifdef ENABLE_NLS
52 #ifdef __APPLE__
53 #include <xlocale.h>
54 #endif
55 #include <libintl.h>
56 #include <locale.h>
57 #ifdef HAVE_USELOCALE
58 extern locale_t wc_locales[];
59 #endif
60 #define _(string)       gettext(string)
61 #else
62 #define _(string)       (string)
63 #endif
64
65 #define IsEmptyStr(a) ((a)[0] == '\0')
66 /*
67  * Uncomment to dump an HTTP trace to stderr
68 #define HTTP_TRACING 1
69  */
70
71 #ifdef HTTP_TRACING
72 #undef HAVE_ZLIB_H
73 #undef HAVE_ZLIB
74 #endif
75
76 #ifdef HAVE_ZLIB_H
77 #include <zlib.h>
78 #endif
79
80 #include <libical/ical.h>
81
82 #undef PACKAGE
83 #undef VERSION
84 #undef PACKAGE_NAME
85 #undef PACKAGE_STRING
86 #undef PACKAGE_TARNAME
87 #undef PACKAGE_VERSION
88 #undef PACKAGE_BUGREPORT
89 #include "sysdep.h"
90
91 ////////#include "hash.h"
92
93 #ifdef HAVE_OPENSSL
94 /* Work around RedHat's b0rken OpenSSL includes */
95 #define OPENSSL_NO_KRB5
96 #include <openssl/ssl.h>
97 #include <openssl/err.h>
98 #include <openssl/rand.h>
99 #endif
100
101 #define CALENDAR_ROOM_NAME      "Calendar"
102 #define PRODID "-//Citadel//NONSGML Citadel Calendar//EN"
103
104 #define SIZ                     4096            /* generic buffer size */
105
106 #define TRACE fprintf(stderr, "Checkpoint: %s, %d\n", __FILE__, __LINE__)
107
108 #define SLEEPING                180             /* TCP connection timeout */
109 #define WEBCIT_TIMEOUT          900             /* WebCit session timeout */
110 #define PORT_NUM                2000            /* port number to listen on */
111 #define DEVELOPER_ID            0
112 #define CLIENT_ID               4
113 #define CLIENT_VERSION          739             /* This version of WebCit */
114 #define MINIMUM_CIT_VERSION     739             /* min required Citadel ver */
115 #define LIBCITADEL_MIN          739             /* min required libcitadel ver */
116 #define DEFAULT_HOST            "localhost"     /* Default Citadel server */
117 #define DEFAULT_PORT            "504"
118 #define TARGET                  "webcit01"      /* Target for inline URL's */
119 #define HOUSEKEEPING            15              /* Housekeeping frequency */
120 #define MIN_WORKER_THREADS      5
121 #define MAX_WORKER_THREADS      250
122 #define LISTEN_QUEUE_LENGTH     100             /* listen() backlog queue */
123
124 #define USERCONFIGROOM          "My Citadel Config"
125 #define DEFAULT_MAXMSGS         20
126
127
128 #ifdef LIBCITADEL_VERSION_NUMBER
129 #if LIBCITADEL_VERSION_NUMBER < LIBCITADEL_MIN
130 #error libcitadel is too old.  Please upgrade it before continuing.
131 #endif
132 #endif
133
134
135 /*
136  * Room flags (from Citadel)
137  *
138  * bucket one...
139  */
140 #define QR_PERMANENT    1               /* Room does not purge          */
141 #define QR_INUSE        2               /* Set if in use, clear if avail        */
142 #define QR_PRIVATE      4               /* Set for any type of private room     */
143 #define QR_PASSWORDED   8               /* Set if there's a password too        */
144 #define QR_GUESSNAME    16              /* Set if it's a guessname room */
145 #define QR_DIRECTORY    32              /* Directory room                       */
146 #define QR_UPLOAD       64              /* Allowed to upload                    */
147 #define QR_DOWNLOAD     128             /* Allowed to download          */
148 #define QR_VISDIR       256             /* Visible directory                    */
149 #define QR_ANONONLY     512             /* Anonymous-Only room          */
150 #define QR_ANONOPT      1024            /* Anonymous-Option room                */
151 #define QR_NETWORK      2048            /* Shared network room          */
152 #define QR_PREFONLY     4096            /* Preferred status needed to enter     */
153 #define QR_READONLY     8192            /* Aide status required to post */
154 #define QR_MAILBOX      16384           /* Set if this is a private mailbox     */
155
156 /*
157  * bucket two...
158  */
159 #define QR2_SYSTEM      1               /* System room; hide by default */
160 #define QR2_SELFLIST    2               /* Self-service mailing list mgmt       */
161 #define QR2_COLLABDEL   4               /* Anyone who can post can also delete*/
162 #define QR2_SUBJECTREQ  8               /* Subject strongly recommended */
163 #define QR2_SMTP_PUBLIC 16              /* smtp public postable room */
164 #define QR2_MODERATED   32              /* Listservice aide has to permit posts  */
165
166 /*
167  * user/room access
168  */
169 #define UA_KNOWN        2
170 #define UA_GOTOALLOWED  4
171 #define UA_HASNEWMSGS   8
172 #define UA_ZAPPED       16
173
174
175 /*
176  * User flags (from Citadel)
177  */
178 #define US_NEEDVALID    1               /* User needs to be validated           */
179 #define US_PERM         4               /* Permanent user                       */
180 #define US_LASTOLD      16              /* Print last old message with new      */
181 #define US_EXPERT       32              /* Experienced user                     */
182 #define US_UNLISTED     64              /* Unlisted userlog entry               */
183 #define US_NOPROMPT     128             /* Don't prompt after each message      */
184 #define US_PROMPTCTL    256             /* <N>ext & <S>top work at prompt       */
185 #define US_DISAPPEAR    512             /* Use "disappearing msg prompts"       */
186 #define US_REGIS        1024            /* Registered user                      */
187 #define US_PAGINATOR    2048            /* Pause after each screen of text      */
188 #define US_INTERNET     4096            /* Internet mail privileges             */
189 #define US_FLOORS       8192            /* User wants to see floors             */
190 #define US_COLOR        16384           /* User wants ANSI color support        */
191 #define US_USER_SET     (US_LASTOLD | US_EXPERT | US_UNLISTED | \
192                         US_NOPROMPT | US_DISAPPEAR | US_PAGINATOR | \
193                         US_FLOORS | US_COLOR | US_PROMPTCTL )
194
195 /*
196  * NLI is the string that shows up in a who's online listing for sessions
197  * that are active, but for which no user has yet authenticated.
198  */
199 #define NLI     "(not logged in)"
200
201
202 /*
203  * \brief       Linked list of session variables encoded in an x-www-urlencoded content type
204  */
205 typedef struct urlcontent urlcontent;
206 struct urlcontent {
207         char url_key[32];          /* the variable name */
208         StrBuf *url_data;            /* its value */
209 };
210
211 /*
212  * \brief information about us ???
213  */ 
214 struct serv_info {
215         int serv_pid;                   /* Process ID of the Citadel server */
216         char serv_nodename[32];         /* Node name of the Citadel server */
217         char serv_humannode[64];        /* human readable node name of the Citadel server */
218         char serv_fqdn[64];             /* fully quallified Domain Name (such as uncensored.citadel.org) */
219         char serv_software[64];         /* What version does our connected citadel server use */
220         int serv_rev_level;             /* Whats the citadel server revision */
221         char serv_bbs_city[64];         /* Geographic location of the Citadel server */
222         char serv_sysadm[64];           /* Name of system administrator */
223         char serv_moreprompt[256];      /* Whats the commandline textprompt */
224         int serv_ok_floors;             /* nonzero == server supports floors */
225         int serv_supports_ldap;         /* is the server linked against an ldap tree for adresses? */
226         int serv_newuser_disabled;      /* Has the server disabled self-service new user creation? */
227         char serv_default_cal_zone[128];/* Default timezone for unspecified calendar items */
228         int serv_supports_sieve;        /* Does the server support Sieve mail filtering? */
229         int serv_fulltext_enabled;      /* Does the server have the full text index enabled? */
230         char serv_svn_revision[256];    /* SVN revision of the server */
231         int serv_supports_openid;       /* Does the server support authentication via OpenID? */
232 };
233
234
235
236 /*
237  * \brief This struct holds a list of rooms for \\\<G\\\>oto operations.
238  */
239 struct march {
240         struct march *next;       /* pointer to next in linked list */
241         char march_name[128];     /* name of room */
242         int march_floor;          /* floor number of room */
243         int march_order;          /* sequence in which we are to visit this room */
244 };
245
246 /* *
247  * \brief       This struct holds a list of rooms for client display.
248  *              It is a binary tree.
249  */
250 struct roomlisting {
251         struct roomlisting *lnext;      /* pointer to 'left' tree node */
252         struct roomlisting *rnext;      /* pointer to 'right' tree node */
253         char rlname[128];               /* name of room */
254         unsigned rlflags;               /* room flags */
255         int rlfloor;                    /* the floor it resides on */
256         int rlorder;                    /* room listing order */
257 };
258
259
260 #define TYPE_STR   1
261 #define TYPE_LONG  2
262 #define MAXPARAM  20
263 typedef struct _TemplateParam {
264         const char *Start;
265         int Type;
266         long len;
267         long lvalue;
268 } TemplateParam;
269
270 typedef struct _TemplateToken {
271         StrBuf *FlatToken;
272         long Line;
273         const char *pTokenStart;
274         size_t TokenStart;
275         size_t TokenEnd;
276         const char *pTokenEnd;
277         int Flags;
278         void *PreEval;
279
280         const char *pName;
281         size_t NameEnd;
282
283         int HaveParameters;
284         int nParameters;
285         TemplateParam *Params[MAXPARAM];
286 } WCTemplateToken;
287
288 typedef void (*WCHandlerFunc)();
289
290
291 /*
292  * \brief Dynamic content for variable substitution in templates
293  */
294 typedef struct _wcsubst {
295         int wcs_type;                       /* which type of Substitution are we */
296         char wcs_key[32];                   /* copy of our hashkey for debugging */
297         StrBuf *wcs_value;                  /* if we're a string, keep it here */
298         long lvalue;                        /* type long? keep data here */
299         int ContextRequired;                /* do we require a context type? */
300         WCHandlerFunc wcs_function; /* funcion hook ???*/
301 } wcsubst;
302
303 #define CTX_NONE 0
304 #define CTX_SITECFG 1
305 #define CTX_SESSION 2
306 #define CTX_INETCFG 3
307 #define CTX_VNOTE 4
308 #define CTX_WHO 5
309 #define CTX_PREF 6
310 #define CTX_NODECONF 7
311 #define CTX_USERLIST 8
312
313 void RegisterNS(const char *NSName, long len, 
314                 int nMinArgs, 
315                 int nMaxArgs, 
316                 WCHandlerFunc HandlerFunc,
317                 int ContextRequired);
318 #define RegisterNamespace(a, b, c, d, e) RegisterNS(a, sizeof(a)-1, b, c, d, e)
319
320 typedef int (*WCConditionalFunc)(WCTemplateToken *Token, void *Context, int ContextType);
321 typedef struct _ConditionalStruct {
322         int nParams;
323         int ContextRequired;
324         WCConditionalFunc CondF;
325 } ConditionalStruct;
326 void RegisterConditional(const char *Name, long len, 
327                          int nParams,
328                          WCConditionalFunc CondF, 
329                          int ContextRequired);
330
331
332
333 typedef void (*SubTemplFunc)(StrBuf *TemplBuffer, void *Context, WCTemplateToken *Token);
334 typedef HashList *(*RetrieveHashlistFunc)(WCTemplateToken *Token);
335 typedef void (*HashDestructorFunc) (HashList **KillMe);
336 void RegisterITERATOR(const char *Name, long len,
337                       int AdditionalParams, 
338                       HashList *StaticList, 
339                       RetrieveHashlistFunc GetHash, 
340                       SubTemplFunc DoSubTempl,
341                       HashDestructorFunc Destructor,
342                       int ContextType);
343 #define RegisterIterator(a, b, c, d, e, f, g) RegisterITERATOR(a, sizeof(a)-1, b, c, d, e, f, g)
344
345 void SVPut(char *keyname, size_t keylen, int keytype, char *Data);
346 #define svput(a, b, c) SVPut(a, sizeof(a) - 1, b, c)
347 void SVPutLong(char *keyname, size_t keylen, long Data);
348 #define svputlong(a, b) SVPutLong(a, sizeof(a) - 1, b)
349 void svprintf(char *keyname, size_t keylen, int keytype, const char *format,...) __attribute__((__format__(__printf__,4,5)));
350 void SVPRINTF(char *keyname, int keytype, const char *format,...) __attribute__((__format__(__printf__,3,4)));
351 void SVCALLBACK(char *keyname, WCHandlerFunc fcn_ptr);
352 void SVCallback(char *keyname, size_t keylen,  WCHandlerFunc fcn_ptr);
353 #define svcallback(a, b) SVCallback(a, sizeof(a) - 1, b)
354
355 void SVPUTBuf(const char *keyname, int keylen, const StrBuf *Buf, int ref);
356 #define SVPutBuf(a, b, c); SVPUTBuf(a, sizeof(a) - 1, b, c)
357
358 void DoTemplate(const char *templatename, long len, StrBuf *Target, void *Context, int ContextType);
359 #define do_template(a, b) DoTemplate(a, sizeof(a) -1, NULL, b, 0);
360 void url_do_template(void);
361
362 int CompareSubstToToken(TemplateParam *ParamToCompare, TemplateParam *ParamToLookup);
363 int CompareSubstToStrBuf(StrBuf *Compare, TemplateParam *ParamToLookup);
364
365
366
367
368 /*
369  * \brief Values for wcs_type
370  */
371 enum {
372         WCS_STRING,       /* its a string */
373         WCS_FUNCTION,     /* its a function callback */
374         WCS_SERVCMD,      /* its a command to send to the citadel server */
375         WCS_STRBUF,       /* its a strbuf we own */
376         WCS_STRBUF_REF,   /* its a strbuf we mustn't free */
377         WCS_LONG          /* its an integer */
378 };
379
380 /*
381  * \brief mail attachment ???
382  */
383 struct wc_attachment {
384         struct wc_attachment *next;/* pointer to next in list */
385         size_t length;                     /* length of the contenttype */
386         char content_type[SIZ];    /* the content itself ???*/
387         char filename[SIZ];                /* the filename hooked to this content ??? */
388         char *data;                /* the data pool; aka this content */
389         long lvalue;               /* if we put a long... */
390 };
391
392 /*
393  * \brief message summary structure. ???
394  */
395 struct message_summary {
396         time_t date;        /* its creation date */
397         long msgnum;            /* the message number on the citadel server */
398         char from[128];         /* the author */
399         char to[128];           /* the recipient */
400         char subj[256];         /* the title / subject */
401         int hasattachments;     /* does it have atachments? */
402         int is_new;         /* is it yet read? */
403 };
404
405 /*
406  * \brief  Data structure for roomlist-to-folderlist conversion 
407  */
408 struct folder {
409         int floor;      /* which floor is it on */
410         char room[SIZ]; /* which roomname ??? */
411         char name[SIZ]; /* which is its own name??? */
412         int hasnewmsgs; /* are there unread messages inside */
413         int is_mailbox; /* is it a mailbox?  */
414         int selectable; /* can we select it ??? */
415         int view;       /* whats its default view? inbox/calendar.... */
416         int num_rooms;  /* If this is a floor, how many rooms does it have */
417 };
418
419 typedef struct _disp_cal {                                      
420         icalcomponent *cal;             /* cal items for display */
421         long cal_msgnum;                /* cal msgids for display */
422         char *from;                     /* owner of this component */
423         int unread;                     /* already seen by the user? */
424
425         time_t event_start;
426         time_t event_end;
427
428         int multi_day_event;
429         int is_repeat;
430 } disp_cal;                                             
431
432
433 /*
434  * One of these is kept for each active Citadel session.
435  * HTTP transactions are bound to one at a time.
436  */
437 struct wcsession {
438         struct wcsession *next;                 /* Linked list */
439         int wc_session;                         /* WebCit session ID */
440         char wc_username[128];                  /* login name of current user */
441         char wc_fullname[128];                  /* Screen name of current user */
442         char wc_password[128];                  /* Password of current user */
443         char wc_roomname[256];                  /* Room we are currently in */
444         int connected;                          /* nonzero == we are connected to Citadel */
445         int logged_in;                          /* nonzero == we are logged in  */
446         int axlevel;                            /* this user's access level */
447         int is_aide;                            /* nonzero == this user is an Aide */
448         int is_room_aide;                       /* nonzero == this user is a Room Aide in this room */
449         int http_sock;                          /* HTTP server socket */
450         int serv_sock;                          /* Client socket to Citadel server */
451         int chat_sock;                          /* Client socket to Citadel server - for chat */
452         unsigned room_flags;                    /* flags associated with the current room */
453         unsigned room_flags2;                   /* flags associated with the current room */
454         int wc_view;                            /* view for the current room */
455         int wc_default_view;                    /* default view for the current room */
456         int wc_is_trash;                        /* nonzero == current room is a Trash folder */
457         int wc_floor;                           /* floor number of current room */
458         char ugname[128];                       /* where does 'ungoto' take us */
459         long uglsn;                             /* last seen message number for ungoto */
460         int upload_length;                      /* content length of http-uploaded data */
461         char *upload;                           /* pointer to http-uploaded data */
462         char upload_filename[PATH_MAX];         /* filename of http-uploaded data */
463         char upload_content_type[256];          /* content type of http-uploaded data */
464         int new_mail;                           /* user has new mail waiting */
465         int remember_new_mail;                  /* last count of new mail messages */
466         int need_regi;                          /* This user needs to register. */
467         int need_vali;                          /* New users require validation. */
468         char cs_inet_email[256];                /* User's preferred Internet addr. */
469         pthread_mutex_t SessionMutex;           /* mutex for exclusive access */
470         time_t lastreq;                         /* Timestamp of most recent HTTP */
471         int killthis;                           /* Nonzero == purge this session */
472         struct march *march;                    /* march mode room list */
473         char reply_to[512];                     /* reply-to address */
474         long msgarr[10000];                     /* for read operations */
475         int num_summ;                           /* number of messages in mailbox summary view */
476         struct message_summary *summ;           /* array of messages for mailbox summary view */
477         int is_mobile;                          /* Client is a handheld browser */
478         HashList *urlstrings;                   /* variables passed to webcit in a URL */
479         HashList *vars;                         /* HTTP variable substitutions for this page */
480         char this_page[512];                    /* URL of current page */
481         char http_host[512];                    /* HTTP Host: header */
482         HashList *hash_prefs;                   /* WebCit preferences for this user */
483         HashList *disp_cal_items;               /* sorted list of calendar items; startdate is the sort criteria. */
484         struct wc_attachment *first_attachment; /* linked list of attachments for 'enter message' */
485         char last_chat_user[256];
486         char ImportantMessage[SIZ];
487         int ctdl_pid;                           /* Session ID on the Citadel server */
488         char httpauth_user[256];                /* only for GroupDAV sessions */
489         char httpauth_pass[256];                /* only for GroupDAV sessions */
490         int gzip_ok;                            /* Nonzero if Accept-encoding: gzip */
491         int is_mailbox;                         /* the current room is a private mailbox */
492         struct folder *cache_fold;              /* cache the iconbar room list */
493         int cache_max_folders;
494         int cache_num_floors;
495         time_t cache_timestamp;
496         HashList *IconBarSetttings;             /* which icons should be shown / not shown? */
497         long current_iconbar;                   /* What is currently in the iconbar? */
498         const StrBuf *floordiv_expanded;        /* which floordiv currently expanded */
499         int selected_language;                  /* Language selected by user */
500         time_t last_pager_check;                /* last time we polled for instant msgs */
501         int nonce;                              /* session nonce (to prevent session riding) */
502         int time_format_cache;                  /* which timeformat does our user like? */
503         StrBuf *UrlFragment1;                   /* first urlfragment, if NEED_URL is specified by handler*/
504         StrBuf *UrlFragment2;                   /* second urlfragment, if NEED_URL is specified by handler*/
505         StrBuf *WBuf;                           /* Our output buffer */
506         StrBuf *HBuf;                           /* Our HeaderBuffer */
507         StrBuf *CLineBuf;                       /* linebuffering client stuff */
508         HashList *ServCfg;                      /* cache our server config for editing */
509         HashList *InetCfg;                      /* Our inet server config for editing */
510         StrBuf *trailing_javascript;            /* extra javascript to be appended to page */
511 };
512
513 /* values for WC->current_iconbar */
514 enum {
515         current_iconbar_menu,     /* view the icon menue */
516         current_iconbar_roomlist  /* view the roomtree */
517 };
518 enum {
519         S_SELECT,
520         S_SHUTDOWN,
521         MAX_SEMAPHORES
522 };
523
524
525 /*
526  * calview contains data passed back and forth between the message fetching loop
527  * and the calendar view renderer.
528  */
529 enum {
530         calview_month,
531         calview_day,
532         calview_week,
533         calview_brief
534 };
535
536 struct calview {
537         int view;
538         int year;
539         int month;
540         int day;
541         time_t lower_bound;
542         time_t upper_bound;
543 };
544
545 #ifndef num_parms
546 #define num_parms(source)               num_tokens(source, '|') 
547 #endif
548
549 /* Per-session data */
550 #define WC ((struct wcsession *)pthread_getspecific(MyConKey))
551 extern pthread_key_t MyConKey;
552
553 /* Per-thread SSL context */
554 #ifdef HAVE_OPENSSL
555 #define THREADSSL ((SSL *)pthread_getspecific(ThreadSSL))
556 extern pthread_key_t ThreadSSL;
557 extern char ctdl_key_dir[PATH_MAX];
558 extern char file_crpt_file_key[PATH_MAX];
559 extern char file_crpt_file_csr[PATH_MAX];
560 extern char file_crpt_file_cer[PATH_MAX];
561 #endif
562
563 struct serv_info serv_info;
564 extern char floorlist[128][SIZ];
565 extern char *axdefs[];
566 extern char *ctdlhost, *ctdlport;
567 extern int http_port;
568 extern char *server_cookie;
569 extern int is_https;
570 extern int setup_wizard;
571 extern char wizard_filename[];
572 extern time_t if_modified_since;
573 extern int follow_xff;
574 extern HashList *HandlerHash;
575 extern HashList *PreferenceHooks;
576 extern HashList *WirelessTemplateCache;
577 extern HashList *WirelessLocalTemplateCache;
578 extern HashList *TemplateCache;
579 extern HashList *LocalTemplateCache;
580 extern HashList *GlobalNS;
581 extern HashList *Iterators;
582 extern HashList *ZoneHash;
583 extern HashList *Contitionals;
584
585
586 void InitialiseSemaphores(void);
587 void begin_critical_section(int which_one);
588 void end_critical_section(int which_one);
589
590
591 void stuff_to_cookie(char *cookie, size_t clen, int session,
592                         char *user, char *pass, char *room);
593 void cookie_to_stuff(StrBuf *cookie, int *session,
594                 char *user, size_t user_len,
595                 char *pass, size_t pass_len,
596                 char *room, size_t room_len);
597 void locate_host(char *, int);
598 void become_logged_in(char *, char *, char *);
599 void openid_manual_create(void);
600 void display_login(char *mesg);
601 void display_openids(void);
602 void do_welcome(void);
603 void do_logout(void);
604 void display_main_menu(void);
605 void display_aide_menu(void);
606 void display_advanced_menu(void);
607 void slrp_highest(void);
608 void get_serv_info(char *, char *);
609 int uds_connectsock(char *);
610 int tcp_connectsock(char *, char *);
611 int serv_getln(char *strbuf, int bufsize);
612 int StrBuf_ServGetln(StrBuf *buf);
613 void serv_puts(const char *string);
614 void who(void);
615 void who_inner_div(void);
616 void ajax_mini_calendar(void);
617 void fmout(char *align);
618 void _fmout(StrBuf *Targt, char *align);
619 void pullquote_fmout(void);
620 void wDumpContent(int);
621
622 /* These may return NULL if not foud */
623 #define sbstr(a) SBstr(a, sizeof(a) - 1)
624 const StrBuf *SBSTR(const char *key);
625 const StrBuf *SBstr(const char *key, size_t keylen);
626
627 #define xbstr(a, b) (char*) XBstr(a, sizeof(a) - 1, b)
628 const char *XBstr(const char *key, size_t keylen, size_t *len);
629 const char *XBSTR(const char *key, size_t *len);
630
631 #define lbstr(a) LBstr(a, sizeof(a) - 1)
632 long LBstr(const char *key, size_t keylen);
633 long LBSTR(const char *key);
634
635 #define ibstr(a) IBstr(a, sizeof(a) - 1)
636 int IBstr(const char *key, size_t keylen);
637 int IBSTR(const char *key);
638
639 #define havebstr(a) HaveBstr(a, sizeof(a) - 1)
640 int HaveBstr(const char *key, size_t keylen);
641 int HAVEBSTR(const char *key);
642
643 #define yesbstr(a) YesBstr(a, sizeof(a) - 1)
644 int YesBstr(const char *key, size_t keylen);
645 int YESBSTR(const char *key);
646
647 /* TODO: get rid of the non-const-typecast */
648 #define bstr(a) (char*) Bstr(a, sizeof(a) - 1)
649 const char *BSTR(const char *key);
650 const char *Bstr(const char *key, size_t keylen);
651
652
653
654 void UrlescPutStrBuf(const StrBuf *strbuf);
655 void StrEscPuts(const StrBuf *strbuf);
656 void StrEscputs1(const StrBuf *strbuf, int nbsp, int nolinebreaks);
657
658 void urlescputs(const char *);
659 void hurlescputs(const char *);
660 void jsesc(char *, size_t, char *);
661 void jsescputs(char *);
662 void output_headers(    int do_httpheaders,
663                         int do_htmlhead,
664                         int do_room_banner,
665                         int unset_cookies,
666                         int suppress_check,
667                         int cache);
668 void wprintf(const char *format,...)__attribute__((__format__(__printf__,1,2)));
669 void hprintf(const char *format,...)__attribute__((__format__(__printf__,1,2)));
670 void output_static(char *what);
671 void display_mime_icon(void);
672 void print_menu_box(char* Title, char *Class, int nLines, ...);
673 long stresc(char *target, long tSize, char *strbuf, int nbsp, int nolinebreaks);
674 void escputs(char *strbuf);
675 void url(char *buf, size_t bufsize);
676 void escputs1(char *strbuf, int nbsp, int nolinebreaks);
677 void msgesc(char *target, size_t tlen, char *strbuf);
678 void msgescputs(char *strbuf);
679 void msgescputs1(char *strbuf);
680 void stripout(char *str, char leftboundary, char rightboundary);
681 void dump_vars(void);
682 void embed_main_menu(void);
683 void serv_read(char *buf, int bytes);
684 void readloop(char *oper);
685 void read_message(long msgnum, int printable_view, char *section);
686 void text_to_server(char *ptr);
687 void text_to_server_qp(char *ptr);
688 void confirm_delete_msg(void);
689 void display_success(char *);
690 void authorization_required(const char *message);
691 void server_to_text(void);
692 void save_edit(char *description, char *enter_cmd, int regoto);
693 void display_edit(char *description, char *check_cmd,
694                   char *read_cmd, char *save_cmd, int with_room_banner);
695 int gotoroom(char *gname);
696 void confirm_delete_room(void);
697 void validate(void);
698 void display_graphics_upload(char *, char *, char *);
699 void do_graphics_upload(char *upl_cmd);
700 void serv_read(char *buf, int bytes);
701 void serv_gets(char *strbuf);
702 void serv_write(const char *buf, int nbytes);
703 void serv_puts(const char *string);
704 void serv_putbuf(const StrBuf *string);
705 void serv_printf(const char *format,...)__attribute__((__format__(__printf__,1,2)));
706 void load_floorlist(void);
707 void shutdown_sessions(void);
708 void do_housekeeping(void);
709 void smart_goto(char *);
710 void worker_entry(void);
711 void session_loop(HashList *HTTPHeaders, StrBuf *ReqLine, StrBuf *ReqType, StrBuf *ReadBuf);
712 size_t wc_strftime(char *s, size_t max, const char *format, const struct tm *tm);
713 void fmt_time(char *buf, time_t thetime);
714 void httpdate(char *buf, time_t thetime);
715 time_t httpdate_to_timestamp(StrBuf *buf);
716 void end_webcit_session(void);
717 void page_popup(void);
718 void http_redirect(const char *);
719 void clear_substs(struct wcsession *wc);
720 void clear_local_substs(void);
721
722
723
724 int lingering_close(int fd);
725 char *memreadline(char *start, char *buf, int maxlen);
726 char *memreadlinelen(char *start, char *buf, int maxlen, int *retlen);
727 long extract_token(char *dest, const char *source, int parmnum, char separator, int maxlen);
728 void remove_token(char *source, int parmnum, char separator);
729 char *load_mimepart(long msgnum, char *partnum);
730 int pattern2(char *search, char *patn);
731 void do_edit_vcard(long, char *, char *, char *);
732 void striplt(char *);
733 void stripltlen(char *, int *);
734 void select_user_to_edit(char *message, char *preselect);
735 void delete_user(char *);
736 void do_change_view(int);
737 void folders(void);
738
739
740 void load_preferences(void);
741 void save_preferences(void);
742 #define get_preference(a, b) get_PREFERENCE(a, sizeof(a) - 1, b)
743 #define get_pref(a, b)       get_PREFERENCE(ChrPtr(a), StrLength(a), b)
744 int get_PREFERENCE(const char *key, size_t keylen, StrBuf **value);
745 #define set_preference(a, b, c) set_PREFERENCE(a, sizeof(a) - 1, b, c)
746 #define set_pref(a, b, c)       set_PREFERENCE(ChrPtr(a), StrLength(a), b, c)
747 void set_PREFERENCE(const char *key, size_t keylen, StrBuf *value, int save_to_server);
748
749 #define get_pref_long(a, b, c) get_PREF_LONG(a, sizeof(a) - 1, b, c)
750 int get_PREF_LONG(const char *key, size_t keylen, long *value, long Default);
751 #define set_pref_long(a, b, c) set_PREF_LONG(a, sizeof(a) - 1, b, c)
752 void set_PREF_LONG(const char *key, size_t keylen, long value, int save_to_server);
753
754 #define get_pref_yesno(a, b, c) get_PREF_YESNO(a, sizeof(a) - 1, b, c)
755 int get_PREF_YESNO(const char *key, size_t keylen, int *value, int Default);
756 #define set_pref_yesno(a, b, c) set_PREF_YESNO(a, sizeof(a) - 1, b, c)
757 void set_PREF_YESNO(const char *key, size_t keylen, int value, int save_to_server);
758
759 #define get_room_pref(a) get_ROOM_PREFS(a, sizeof(a) - 1)
760 StrBuf *get_ROOM_PREFS(const char *key, size_t keylen);
761
762 #define set_room_pref(a, b, c) set_ROOM_PREFS(a, sizeof(a) - 1, b, c)
763 void set_ROOM_PREFS(const char *key, size_t keylen, StrBuf *value, int save_to_server);
764
765 int is_msg_in_mset(char *mset, long msgnum);
766 void display_addressbook(long msgnum, char alpha);
767 void offer_start_page(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType);
768 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext);
769 void output_html(char *, int, int);
770 void do_listsub(void);
771 void toggle_self_service(void);
772 ssize_t write(int fd, const void *buf, size_t count);
773 void cal_process_attachment(char *part_source, long msgnum, char *cal_partnum);
774 void load_calendar_item(long msgnum, int unread, struct calview *c);
775 void display_task(long msgnum, int unread);
776 void display_note(long msgnum, int unread);
777 void updatenote(void);
778 void parse_calendar_view_request(struct calview *c);
779 void render_calendar_view(struct calview *c);
780 void do_tasks_view(void);
781 void calendar_summary_view(void);
782 int load_msg_ptrs(char *servcmd, int with_headers);
783 void free_attachments(struct wcsession *sess);
784 void free_march_list(struct wcsession *wcf);
785 void display_rules_editor_inner_div(void);
786 void generate_uuid(char *);
787 void CtdlMakeTempFileName(char *, int);
788 void address_book_popup(void);
789 void begin_ajax_response(void);
790 void end_ajax_response(void);
791 void initialize_viewdefs(void);
792 void initialize_axdefs(void);
793 void burn_folder_cache(time_t age);
794 void list_all_rooms_by_floor(const char *viewpref);
795 void display_pictureview(void);
796
797 void display_edit_task(void);
798 void display_edit_event(void);
799 icaltimezone *get_default_icaltimezone(void);
800 void display_icaltimetype_as_webform(struct icaltimetype *, char *, int);
801 void icaltime_from_webform(struct icaltimetype *result, char *prefix);
802 void icaltime_from_webform_dateonly(struct icaltimetype *result, char *prefix);
803 void display_edit_individual_event(icalcomponent *supplied_vtodo, long msgnum, char *from,
804         int unread, struct calview *calv);
805 void save_individual_event(icalcomponent *supplied_vtodo, long msgnum, char *from,
806         int unread, struct calview *calv);
807 void ical_dezonify(icalcomponent *cal);
808 void partstat_as_string(char *buf, icalproperty *attendee);
809 icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp);
810 void check_attendee_availability(icalcomponent *supplied_vevent);
811 void do_freebusy(const char *req);
812 int ical_ctdl_is_overlap(
813                         struct icaltimetype t1start,
814                         struct icaltimetype t1end,
815                         struct icaltimetype t2start,
816                         struct icaltimetype t2end
817 );
818
819 #ifdef ENABLE_NLS
820 void initialize_locales(void);
821 void ShutdownLocale(void);
822 #endif
823 void TmplGettext(StrBuf *Target, int nTokens, WCTemplateToken *Token);
824
825 extern char *months[];
826 extern char *days[];
827 int read_server_binary(StrBuf *Ret, size_t total_len);
828 int StrBuf_ServGetBLOB(StrBuf *buf, long BlobSize);
829 int read_server_text(StrBuf *Buf, long *nLines);;
830 int goto_config_room(void);
831 long locate_user_vcard(char *username, long usernum);
832 void sleeeeeeeeeep(int);
833 void http_transmit_thing(const char *content_type, int is_static);
834 long unescape_input(char *buf);
835 void do_selected_iconbar(void);
836 int CtdlDecodeQuotedPrintable(char *decoded, char *encoded, int sourcelen);
837 void spawn_another_worker_thread(void);
838 void display_rss(char *roomname, StrBuf *request_method);
839 void offer_languages(StrBuf *Target, int nArgs, WCTemplateToken *Token, void *Context, int ContextType);
840 void set_selected_language(const char *);
841 void go_selected_language(void);
842 void stop_selected_language(void);
843 void preset_locale(void);
844 void httplang_to_locale(StrBuf *LocaleString);
845 void StrEndTab(StrBuf *Target, int tabnum, int num_tabs);
846 void StrBeginTab(StrBuf *Target, int tabnum, int num_tabs);
847 void StrTabbedDialog(StrBuf *Target, int num_tabs, StrBuf *tabnames[]);
848 void tabbed_dialog(int num_tabs, char *tabnames[]);
849 void begin_tab(int tabnum, int num_tabs);
850 void end_tab(int tabnum, int num_tabs);
851 void str_wiki_index(char *s);
852 int get_time_format_cached (void);
853 int xtoi(const char *in, size_t len);
854 const char *get_selected_language(void);
855 void webcit_fmt_date(char *buf, time_t thetime, int brief);
856 int fetch_http(char *url, char *target_buf, int maxbytes);
857
858 int is_mobile_ua(char *user_agent);
859
860 #ifdef HAVE_ICONV
861 iconv_t ctdl_iconv_open(const char *tocode, const char *fromcode);
862 #endif
863
864 void embed_room_banner(char *, int);
865
866 /* navbar types that can be passed to embed_room_banner */
867 enum {
868         navbar_none,
869         navbar_default
870 };
871
872
873 #ifdef HAVE_OPENSSL
874 void init_ssl(void);
875 void endtls(void);
876 void ssl_lock(int mode, int n, const char *file, int line);
877 int starttls(int sock);
878 extern SSL_CTX *ssl_ctx;  
879 int client_read_sslbuffer(StrBuf *buf, int timeout);
880 void client_write_ssl(const StrBuf *Buf);
881 #endif
882
883 #ifdef HAVE_ZLIB
884 #include <zlib.h>
885 int ZEXPORT compress_gzip(Bytef * dest, size_t * destLen,
886                           const Bytef * source, uLong sourceLen, int level);
887 #endif
888
889 void utf8ify_rfc822_string(char *buf);
890
891 void begin_burst(void);
892 long end_burst(void);
893
894 extern char *hourname[];        /* Names of hours (12am, 1am, etc.) */
895
896 void http_datestring(char *buf, size_t n, time_t xtime);
897
898
899 typedef void (*WebcitHandlerFunc)(void);
900 typedef struct  _WebcitHandler{
901         WebcitHandlerFunc F;
902         long Flags;
903 } WebcitHandler;
904 void WebcitAddUrlHandler(const char * UrlString, long UrlSLen, WebcitHandlerFunc F, long Flags);
905
906 #define AJAX (1<<0)
907 #define ANONYMOUS (1<<1)
908 #define NEED_URL (1<<2)
909
910
911 /* These should be empty, but we have them for testing */
912 #define DEFAULT_HTTPAUTH_USER   ""
913 #define DEFAULT_HTTPAUTH_PASS   ""
914
915
916 /* Exit codes 101 through 109 are initialization failures so we don't want to
917  * just keep respawning indefinitely.
918  */
919 #define WC_EXIT_BIND            101     /* Can't bind to the port */
920 #define WC_EXIT_SSL             102     /* Can't initialize SSL */
921
922
923 #define WC_TIMEFORMAT_NONE 0
924 #define WC_TIMEFORMAT_AMPM 1
925 #define WC_TIMEFORMAT_24 2
926
927