* we have now several bstrs:
[citadel.git] / webcit / webcit.c
1 /*
2  * $Id$
3  *
4  * This is the main transaction loop of the web service.  It maintains a
5  * persistent session to the Citadel server, handling HTTP WebCit requests as
6  * they arrive and presenting a user interface.
7  */
8
9 #include "webcit.h"
10 #include "groupdav.h"
11 #include "webserver.h"
12
13 #include <stdio.h>
14 #include <stdarg.h>
15
16 /*
17  * String to unset the cookie.
18  * Any date "in the past" will work, so I chose my birthday, right down to
19  * the exact minute.  :)
20  */
21 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
22
23 /**   
24  * \brief remove escaped strings from i.e. the url string (like %20 for blanks)
25  * \param buf the buffer to examine
26  */
27 long unescape_input(char *buf)
28 {
29         int a, b;
30         char hex[3];
31         long buflen;
32         long len;
33
34         buflen = strlen(buf);
35
36         while ((buflen > 0) && (isspace(buf[buflen - 1]))){
37                 buf[buflen - 1] = 0;
38                 buflen --;
39         }
40
41         a = 0; 
42         while (a < buflen) {
43                 if (buf[a] == '+')
44                         buf[a] = ' ';
45                 if (buf[a] == '%') {
46                         /* don't let % chars through, rather truncate the input. */
47                         if (a + 2 > buflen) {
48                                 buf[a] = '\0';
49                                 buflen = a;
50                         }
51                         else {                  
52                                 hex[0] = buf[a + 1];
53                                 hex[1] = buf[a + 2];
54                                 hex[2] = 0;
55                                 b = 0;
56                                 sscanf(hex, "%02x", &b);
57                                 buf[a] = (char) b;
58                                 len = buflen - a - 2;
59                                 if (len > 0)
60                                         memmove(&buf[a + 1], &buf[a + 3], len);
61                         
62                                 buflen -=2;
63                         }
64                 }
65                 a++;
66         }
67         return a;
68 }
69
70 void free_url(void *U)
71 {
72         urlcontent *u = (urlcontent*) U;
73         free(u->url_data);
74         free(u);
75 }
76
77 /**
78  * \brief Extract variables from the URL.
79  * \param url URL supplied by the HTTP parser
80  */
81 void addurls(char *url)
82 {
83         char *aptr, *bptr, *eptr;
84         char *up;
85         char buf[SIZ] = "";
86         int len, n, keylen;
87         urlcontent *u;
88         struct wcsession *WCC = WC;
89
90         if (WCC->urlstrings == NULL)
91                 WCC->urlstrings = NewHash();
92         eptr = buf + sizeof (buf);
93         up = url;
94         /** locate the = sign */
95         n = safestrncpy(buf, up, sizeof buf);
96         if (n < 0) /** hm, we exceeded the buffer... hmmm what todo now? */
97                 n = -n;
98         up = buf;
99 //      while ((up < eptr) && (*up != '?') && (*up != '&'))
100 //              up++;
101         while (!IsEmptyStr(up)) {
102                 aptr = up;
103                 while ((aptr < eptr) && (*aptr != '\0') && (*aptr != '='))
104                         aptr++;
105                 if (*aptr != '=')
106                         return;
107                 *aptr = '\0';
108                 aptr++;
109                 bptr = aptr;
110                 while ((bptr < eptr) && (*bptr != '\0')
111                       && (*bptr != '&') && (*bptr != '?') && (*bptr != ' ')) {
112                         bptr++;
113                 }
114                 *bptr = '\0';
115                 u = (urlcontent *) malloc(sizeof(urlcontent));
116
117                 keylen = safestrncpy(u->url_key, up, sizeof u->url_key);
118                 if (keylen < 0){
119                         lprintf(1, "URLkey to long! [%s]", up);
120                         continue;
121                 }
122
123                 Put(WCC->urlstrings, u->url_key, keylen, u, free_url);
124                 len = bptr - aptr;
125                 u->url_data = malloc(len + 2);
126                 safestrncpy(u->url_data, aptr, len + 2);
127                 u->url_data_size = unescape_input(u->url_data);
128                 u->url_data[u->url_data_size] = '\0';
129                 up = bptr;
130                 ++up;
131
132                 lprintf(9, "%s = [%ld]  %s\n", u->url_key, u->url_data_size, u->url_data); 
133         }
134 }
135
136 /**
137  * \brief free urlstring memory
138  */
139 void free_urls(void)
140 {
141         DeleteHash(&WC->urlstrings);
142 }
143
144 /**
145  * \brief Diagnostic function to display the contents of all variables
146  */
147
148 void dump_vars(void)
149 {
150         struct wcsession *WCC = WC;
151         urlcontent *u;
152         void *U;
153         long HKLen;
154         char *HKey;
155         HashPos *Cursor;
156         
157         Cursor = GetNewHashPos ();
158         while (GetNextHashPos(WCC->urlstrings, Cursor, &HKLen, &HKey, &U)) {
159                 u = (urlcontent*) U;
160                 wprintf("%38s = %s\n", u->url_key, u->url_data);
161         }
162 }
163
164 /**
165  * \brief Return the value of a variable supplied to the current web page (from the url or a form)
166  * \param key The name of the variable we want
167  */
168
169 const char *XBstr(char *key, size_t keylen, size_t *len)
170 {
171         void *U;
172
173         if ((WC->urlstrings != NULL) && 
174             GetHash(WC->urlstrings, key, keylen, &U)) {
175                 *len = ((urlcontent *)U)->url_data_size;
176                 return ((urlcontent *)U)->url_data;
177         }
178         else {
179                 *len = 0;
180                 return ("");
181         }
182 }
183
184 const char *XBSTR(char *key, size_t *len)
185 {
186         void *U;
187
188         if ((WC->urlstrings != NULL) &&
189             GetHash(WC->urlstrings, key, strlen (key), &U)){
190                 *len = ((urlcontent *)U)->url_data_size;
191                 return ((urlcontent *)U)->url_data;
192         }
193         else {
194                 *len = 0;
195                 return ("");
196         }
197 }
198
199
200 const char *BSTR(char *key)
201 {
202         void *U;
203
204         if ((WC->urlstrings != NULL) &&
205             GetHash(WC->urlstrings, key, strlen (key), &U))
206                 return ((urlcontent *)U)->url_data;
207         else    
208                 return ("");
209 }
210
211 const char *Bstr(char *key, size_t keylen)
212 {
213         void *U;
214
215         if ((WC->urlstrings != NULL) && 
216             GetHash(WC->urlstrings, key, keylen, &U))
217                 return ((urlcontent *)U)->url_data;
218         else    
219                 return ("");
220 }
221
222 long LBstr(char *key, size_t keylen)
223 {
224         void *U;
225
226         if ((WC->urlstrings != NULL) && 
227             GetHash(WC->urlstrings, key, keylen, &U))
228                 return atol(((urlcontent *)U)->url_data);
229         else    
230                 return (0);
231 }
232
233 long LBSTR(char *key)
234 {
235         void *U;
236
237         if ((WC->urlstrings != NULL) && 
238             GetHash(WC->urlstrings, key, strlen(key), &U))
239                 return atol(((urlcontent *)U)->url_data);
240         else    
241                 return (0);
242 }
243
244 int IBstr(char *key, size_t keylen)
245 {
246         void *U;
247
248         if ((WC->urlstrings != NULL) && 
249             GetHash(WC->urlstrings, key, keylen, &U))
250                 return atoi(((urlcontent *)U)->url_data);
251         else    
252                 return (0);
253 }
254
255 int IBSTR(char *key)
256 {
257         void *U;
258
259         if ((WC->urlstrings != NULL) && 
260             GetHash(WC->urlstrings, key, strlen(key), &U))
261                 return atoi(((urlcontent *)U)->url_data);
262         else    
263                 return (0);
264 }
265
266 int HaveBstr(char *key, size_t keylen)
267 {
268         void *U;
269
270         if ((WC->urlstrings != NULL) && 
271             GetHash(WC->urlstrings, key, keylen, &U))
272                 return ((urlcontent *)U)->url_data_size != 0;
273         else    
274                 return (0);
275 }
276
277 int HAVEBSTR(char *key)
278 {
279         void *U;
280
281         if ((WC->urlstrings != NULL) && 
282             GetHash(WC->urlstrings, key, strlen(key), &U))
283                 return ((urlcontent *)U)->url_data_size != 0;
284         else    
285                 return (0);
286 }
287
288
289 int YesBstr(char *key, size_t keylen)
290 {
291         void *U;
292
293         if ((WC->urlstrings != NULL) && 
294             GetHash(WC->urlstrings, key, keylen, &U))
295                 return strcmp( ((urlcontent *)U)->url_data, "yes") == 0;
296         else    
297                 return (0);
298 }
299
300 int YESBSTR(char *key)
301 {
302         void *U;
303
304         if ((WC->urlstrings != NULL) && 
305             GetHash(WC->urlstrings, key, strlen(key), &U))
306                 return strcmp( ((urlcontent *)U)->url_data, "yes") == 0;
307         else    
308                 return (0);
309 }
310
311 /**
312  * \brief web-printing funcion. uses our vsnprintf wrapper
313  * \param format printf format string 
314  * \param ... the varargs to put into formatstring
315  */
316 void wprintf(const char *format,...)
317 {
318         va_list arg_ptr;
319         char wbuf[4096];
320
321         va_start(arg_ptr, format);
322         vsnprintf(wbuf, sizeof wbuf, format, arg_ptr);
323         va_end(arg_ptr);
324
325         client_write(wbuf, strlen(wbuf));
326 }
327
328
329 /**
330  * \brief wrap up an HTTP session, closes tags, etc.
331  * \todo multiline params?
332  * \param print_standard_html_footer should be set to 0 to transmit only, 1 to
333  * append the main menu and closing tags, or 2 to
334  * append the closing tags only.
335  */
336 void wDumpContent(int print_standard_html_footer)
337 {
338         if (print_standard_html_footer) {
339                 wprintf("</div>\n");    /* end of "text" div */
340                 do_template("trailing");
341         }
342
343         /* If we've been saving it all up for one big output burst,
344          * go ahead and do that now.
345          */
346         end_burst();
347 }
348
349
350 /**
351  * \brief Copy a string, escaping characters which have meaning in HTML.  
352  * \param target target buffer
353  * \param strbuf source buffer
354  * \param nbsp If nonzero, spaces are converted to non-breaking spaces.
355  * \param nolinebreaks if set, linebreaks are removed from the string.
356  */
357 long stresc(char *target, long tSize, char *strbuf, int nbsp, int nolinebreaks)
358 {
359         char *aptr, *bptr, *eptr;
360
361         *target = '\0';
362         aptr = strbuf;
363         bptr = target;
364         eptr = target + tSize - 6; // our biggest unit to put in... 
365
366         while ((bptr < eptr) && !IsEmptyStr(aptr) ){
367                 if (*aptr == '<') {
368                         memcpy(bptr, "&lt;", 4);
369                         bptr += 4;
370                 }
371                 else if (*aptr == '>') {
372                         memcpy(bptr, "&gt;", 4);
373                         bptr += 4;
374                 }
375                 else if (*aptr == '&') {
376                         memcpy(bptr, "&amp;", 5);
377                         bptr += 5;
378                 }
379                 else if (*aptr == '\"') {
380                         memcpy(bptr, "&quot;", 6);
381                         bptr += 6;
382                 }
383                 else if (*aptr == '\'') {
384                         memcpy(bptr, "&#39;", 5);
385                         bptr += 5;
386                 }
387                 else if (*aptr == LB) {
388                         *bptr = '<';
389                         bptr ++;
390                 }
391                 else if (*aptr == RB) {
392                         *bptr = '>';
393                         bptr ++;
394                 }
395                 else if (*aptr == QU) {
396                         *bptr ='"';
397                         bptr ++;
398                 }
399                 else if ((*aptr == 32) && (nbsp == 1)) {
400                         memcpy(bptr, "&nbsp;", 6);
401                         bptr += 6;
402                 }
403                 else if ((*aptr == '\n') && (nolinebreaks)) {
404                         *bptr='\0';     /* nothing */
405                 }
406                 else if ((*aptr == '\r') && (nolinebreaks)) {
407                         *bptr='\0';     /* nothing */
408                 }
409                 else{
410                         *bptr = *aptr;
411                         bptr++;
412                 }
413                 aptr ++;
414         }
415         *bptr = '\0';
416         if ((bptr = eptr - 1 ) && !IsEmptyStr(aptr) )
417                 return -1;
418         return (bptr - target);
419 }
420
421 /**
422  * \brief WHAT???
423  * \param strbuf what???
424  * \param nbsp If nonzero, spaces are converted to non-breaking spaces.
425  * \param nolinebreaks if set, linebreaks are removed from the string.
426  */ 
427 void escputs1(char *strbuf, int nbsp, int nolinebreaks)
428 {
429         char *buf;
430         long Siz;
431
432         if (strbuf == NULL) return;
433         Siz = (3 * strlen(strbuf)) + SIZ ;
434         buf = malloc(Siz);
435         stresc(buf, Siz, strbuf, nbsp, nolinebreaks);
436         wprintf("%s", buf);
437         free(buf);
438 }
439
440 /** 
441  * \brief static wrapper for ecsputs1
442  * \param strbuf buffer to print escaped to client
443  */
444 void escputs(char *strbuf)
445 {
446         escputs1(strbuf, 0, 0);
447 }
448
449
450 /**
451  * \brief urlescape buffer and print it to the client
452  * \param strbuf buffer to urlescape
453  */
454 void urlescputs(char *strbuf)
455 {
456         char outbuf[SIZ];
457         
458         urlesc(outbuf, SIZ, strbuf);
459         wprintf("%s", outbuf);
460 }
461
462
463 /**
464  * \brief Copy a string, escaping characters for JavaScript strings.
465  * \param target output string
466  * \param strbuf input string
467  */
468 void jsesc(char *target, size_t tlen, char *strbuf)
469 {
470         int len;
471         char *tend;
472         char *send;
473         char *tptr;
474         char *sptr;
475
476         target[0]='\0';
477         len = strlen (strbuf);
478         send = strbuf + len;
479         tend = target + tlen;
480         sptr = strbuf;
481         tptr = target;
482         
483         while (!IsEmptyStr(sptr) && 
484                (sptr < send) &&
485                (tptr < tend)) {
486                
487                 if (*sptr == '<')
488                         *tptr = '[';
489                 else if (*sptr == '>')
490                         *tptr = ']';
491                 else if (*sptr == '\'') {
492                         if (tend - tptr < 3)
493                                 return;
494                         *(tptr++) = '\\';
495                         *tptr = '\'';
496                 }
497                 else if (*sptr == '"') {
498                         if (tend - tptr < 8)
499                                 return;
500                         *(tptr++) = '&';
501                         *(tptr++) = 'q';
502                         *(tptr++) = 'u';
503                         *(tptr++) = 'o';
504                         *(tptr++) = 't';
505                         *tptr = ';';
506                 }
507                 else if (*sptr == '&') {
508                         if (tend - tptr < 7)
509                                 return;
510                         *(tptr++) = '&';
511                         *(tptr++) = 'a';
512                         *(tptr++) = 'm';
513                         *(tptr++) = 'p';
514                         *tptr = ';';
515                 } else {
516                         *tptr = *sptr;
517                 }
518                 tptr++; sptr++;
519         }
520         *tptr = '\0';
521 }
522
523 /**
524  * \brief escape and print java script
525  * \param strbuf the js code
526  */
527 void jsescputs(char *strbuf)
528 {
529         char outbuf[SIZ];
530         
531         jsesc(outbuf, SIZ, strbuf);
532         wprintf("%s", outbuf);
533 }
534
535 /**
536  * \brief Copy a string, escaping characters for message text hold
537  * \param target target buffer
538  * \param strbuf source buffer
539  */
540 void msgesc(char *target, size_t tlen, char *strbuf)
541 {
542         int len;
543         char *tend;
544         char *send;
545         char *tptr;
546         char *sptr;
547
548         target[0]='\0';
549         len = strlen (strbuf);
550         send = strbuf + len;
551         tend = target + tlen;
552         sptr = strbuf;
553         tptr = target;
554
555         while (!IsEmptyStr(sptr) && 
556                (sptr < send) &&
557                (tptr < tend)) {
558                
559                 if (*sptr == '\n')
560                         *tptr = ' ';
561                 else if (*sptr == '\r')
562                         *tptr = ' ';
563                 else if (*sptr == '\'') {
564                         if (tend - tptr < 8)
565                                 return;
566                         *(tptr++) = '&';
567                         *(tptr++) = '#';
568                         *(tptr++) = '3';
569                         *(tptr++) = '9';
570                         *tptr = ';';
571                 } else {
572                         *tptr = *sptr;
573                 }
574                 tptr++; sptr++;
575         }
576         *tptr = '\0';
577 }
578
579 /**
580  * \brief print a string to the client after cleaning it with msgesc() and stresc()
581  * \param strbuf string to be printed
582  */
583 void msgescputs1( char *strbuf)
584 {
585         char *outbuf;
586         char *outbuf2;
587         int buflen;
588
589         if (strbuf == NULL) return;
590         buflen = 3 * strlen(strbuf) + SIZ;
591         outbuf = malloc( buflen);
592         outbuf2 = malloc( buflen);
593         msgesc(outbuf, buflen, strbuf);
594         stresc(outbuf2, buflen, outbuf, 0, 0);
595         wprintf("%s", outbuf2);
596         free(outbuf);
597         free(outbuf2);
598 }
599
600 /**
601  * \brief print a string to the client after cleaning it with msgesc()
602  * \param strbuf string to be printed
603  */
604 void msgescputs(char *strbuf) {
605         char *outbuf;
606         size_t len;
607
608         if (strbuf == NULL) return;
609         len =  (3 * strlen(strbuf)) + SIZ;
610         outbuf = malloc(len);
611         msgesc(outbuf, len, strbuf);
612         wprintf("%s", outbuf);
613         free(outbuf);
614 }
615
616
617
618
619 /**
620  * \brief Output all that important stuff that the browser will want to see
621  */
622 void output_headers(    int do_httpheaders,     /**< 1 = output HTTP headers                          */
623                         int do_htmlhead,        /**< 1 = output HTML <head> section and <body> opener */
624
625                         int do_room_banner,     /**< 0=no, 1=yes,                                     
626                                                                  * 2 = I'm going to embed my own, so don't open the 
627                                                                  *     <div id="content"> either.                   
628                                                                  */
629
630                         int unset_cookies,      /**< 1 = session is terminating, so unset the cookies */
631                         int suppress_check,     /**< 1 = suppress check for instant messages          */
632                         int cache               /**< 1 = allow browser to cache this page             */
633 ) {
634         char cookie[1024];
635         char httpnow[128];
636
637         wprintf("HTTP/1.1 200 OK\n");
638         http_datestring(httpnow, sizeof httpnow, time(NULL));
639
640         if (do_httpheaders) {
641                 wprintf("Content-type: text/html; charset=utf-8\r\n"
642                         "Server: %s / %s\n"
643                         "Connection: close\r\n",
644                         PACKAGE_STRING, serv_info.serv_software
645                 );
646         }
647
648         if (cache) {
649                 wprintf("Pragma: public\r\n"
650                         "Cache-Control: max-age=3600, must-revalidate\r\n"
651                         "Last-modified: %s\r\n",
652                         httpnow
653                 );
654         }
655         else {
656                 wprintf("Pragma: no-cache\r\n"
657                         "Cache-Control: no-store\r\n"
658                         "Expires: -1\r\n"
659                 );
660         }
661
662         stuff_to_cookie(cookie, 1024, WC->wc_session, WC->wc_username,
663                         WC->wc_password, WC->wc_roomname);
664
665         if (unset_cookies) {
666                 wprintf("Set-cookie: webcit=%s; path=/\r\n", unset);
667         } else {
668                 wprintf("Set-cookie: webcit=%s; path=/\r\n", cookie);
669                 if (server_cookie != NULL) {
670                         wprintf("%s\n", server_cookie);
671                 }
672         }
673
674         if (do_htmlhead) {
675                 begin_burst();
676                 if (!access("static.local/webcit.css", R_OK)) {
677                         svprintf("CSSLOCAL", WCS_STRING,
678                            "<link href=\"static.local/webcit.css\" rel=\"stylesheet\" type=\"text/css\">"
679                         );
680                 }
681                 do_template("head");
682         }
683
684         /** ICONBAR */
685         if (do_htmlhead) {
686
687
688                 /** check for ImportantMessages (these display in a div overlaying the main screen) */
689                 if (!IsEmptyStr(WC->ImportantMessage)) {
690                         wprintf("<div id=\"important_message\">\n"
691                                 "<span class=\"imsg\">");
692                         escputs(WC->ImportantMessage);
693                         wprintf("</span><br />\n"
694                                 "</div>\n"
695                                 "<script type=\"text/javascript\">\n"
696                                 "        setTimeout('hide_imsg_popup()', 5000); \n"
697                                 "</script>\n");
698                         WC->ImportantMessage[0] = 0;
699                 }
700
701                 if ( (WC->logged_in) && (!unset_cookies) ) {
702                         wprintf("<div id=\"iconbar\">");
703                         do_selected_iconbar();
704                         /** check for instant messages (these display in a new window) */
705                         page_popup();
706                         wprintf("</div>");
707                 }
708
709                 if (do_room_banner == 1) {
710                         wprintf("<div id=\"banner\">\n");
711                         embed_room_banner(NULL, navbar_default);
712                         wprintf("</div>\n");
713                 }
714         }
715
716         if (do_room_banner == 1) {
717                 wprintf("<div id=\"content\">\n");
718         }
719 }
720
721
722 /**
723  * \brief Generic function to do an HTTP redirect.  Easy and fun.
724  * \param whichpage target url to 302 to
725  */
726 void http_redirect(char *whichpage) {
727         wprintf("HTTP/1.1 302 Moved Temporarily\n");
728         wprintf("Location: %s\r\n", whichpage);
729         wprintf("URI: %s\r\n", whichpage);
730         wprintf("Content-type: text/html; charset=utf-8\r\n\r\n");
731         wprintf("<html><body>");
732         wprintf("Go <a href=\"%s\">here</A>.", whichpage);
733         wprintf("</body></html>\n");
734 }
735
736
737
738 /**
739  * \brief Output a piece of content to the web browser
740  */
741 void http_transmit_thing(char *thing, size_t length, const char *content_type,
742                          int is_static) {
743
744         output_headers(0, 0, 0, 0, 0, is_static);
745
746         wprintf("Content-type: %s\r\n"
747                 "Server: %s\r\n"
748                 "Connection: close\r\n",
749                 content_type,
750                 PACKAGE_STRING);
751
752 #ifdef HAVE_ZLIB
753         /** If we can send the data out compressed, please do so. */
754         if (WC->gzip_ok) {
755                 char *compressed_data = NULL;
756                 size_t compressed_len;
757
758                 compressed_len =  ((length * 101) / 100) + 100;
759                 compressed_data = malloc(compressed_len);
760
761                 if (compress_gzip((Bytef *) compressed_data,
762                                   &compressed_len,
763                                   (Bytef *) thing,
764                                   (uLongf) length, Z_BEST_SPEED) == Z_OK) {
765                         wprintf("Content-encoding: gzip\r\n"
766                                 "Content-length: %ld\r\n"
767                                 "\r\n",
768                                 (long) compressed_len
769                         );
770                         client_write(compressed_data, (size_t)compressed_len);
771                         free(compressed_data);
772                         return;
773                 }
774         }
775 #endif
776
777         /** No compression ... just send it out as-is */
778         wprintf("Content-length: %ld\r\n"
779                 "\r\n",
780                 (long) length
781         );
782         client_write(thing, (size_t)length);
783 }
784
785 /**
786  * \brief print menu box like used in the floor view or admin interface.
787  * This function takes pair of strings as va_args, 
788  * \param Title Title string of the box
789  * \param Class CSS Class for the box
790  * \param nLines How many string pairs should we print? (URL, UrlText)
791  * \param ... Pairs of URL Strings and their Names
792  */
793 void print_menu_box(char* Title, char *Class, int nLines, ...)
794 {
795         va_list arg_list;
796         long i;
797         
798         svprintf("BOXTITLE", WCS_STRING, Title);
799         do_template("beginbox");
800         
801         wprintf("<ul class=\"%s\">", Class);
802         
803         va_start(arg_list, nLines);
804         for (i = 0; i < nLines; ++i)
805         { 
806                 wprintf("<li><a href=\"%s\">", va_arg(arg_list, char *));
807                 wprintf((char *) va_arg(arg_list, char *));
808                 wprintf("</a></li>\n");
809         }
810         va_end (arg_list);
811         
812         wprintf("</a></li>\n");
813         
814         wprintf("</ul>");
815         
816         do_template("endbox");
817 }
818
819
820 /**
821  * \brief dump out static pages from disk
822  * \param what the file urs to print
823  */
824 void output_static(char *what)
825 {
826         FILE *fp;
827         struct stat statbuf;
828         off_t bytes;
829         off_t count = 0;
830         size_t res;
831         char *bigbuffer;
832         const char *content_type;
833         int len;
834
835         fp = fopen(what, "rb");
836         if (fp == NULL) {
837                 lprintf(9, "output_static('%s')  -- NOT FOUND --\n", what);
838                 wprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
839                 wprintf("Content-Type: text/plain\r\n");
840                 wprintf("\r\n");
841                 wprintf("Cannot open %s: %s\r\n", what, strerror(errno));
842         } else {
843                 len = strlen (what);
844                 content_type = GuessMimeByFilename(what, len);
845
846                 if (fstat(fileno(fp), &statbuf) == -1) {
847                         lprintf(9, "output_static('%s')  -- FSTAT FAILED --\n", what);
848                         wprintf("HTTP/1.1 404 %s\r\n", strerror(errno));
849                         wprintf("Content-Type: text/plain\r\n");
850                         wprintf("\r\n");
851                         wprintf("Cannot fstat %s: %s\n", what, strerror(errno));
852                         return;
853                 }
854
855                 count = 0;
856                 bytes = statbuf.st_size;
857                 if ((bigbuffer = malloc(bytes + 2)) == NULL) {
858                         lprintf(9, "output_static('%s')  -- MALLOC FAILED (%s) --\n", what, strerror(errno));
859                         wprintf("HTTP/1.1 500 internal server error\r\n");
860                         wprintf("Content-Type: text/plain\r\n");
861                         wprintf("\r\n");
862                         return;
863                 }
864                 while (count < bytes) {
865                         if ((res = fread(bigbuffer + count, 1, bytes - count, fp)) == 0) {
866                                 lprintf(9, "output_static('%s')  -- FREAD FAILED (%s) %zu bytes of %zu --\n", what, strerror(errno), bytes - count, bytes);
867                                 wprintf("HTTP/1.1 500 internal server error \r\n");
868                                 wprintf("Content-Type: text/plain\r\n");
869                                 wprintf("\r\n");
870                                 return;
871                         }
872                         count += res;
873                 }
874
875                 fclose(fp);
876
877                 lprintf(9, "output_static('%s')  %s\n", what, content_type);
878                 http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
879                 free(bigbuffer);
880         }
881         if (yesbstr("force_close_session")) {
882                 end_webcit_session();
883         }
884 }
885
886 /**
887  * \brief When the browser requests an image file from the Citadel server,
888  * this function is called to transmit it.
889  */
890 void output_image()
891 {
892         char buf[SIZ];
893         char *xferbuf = NULL;
894         off_t bytes;
895         const char *MimeType;
896
897         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
898         serv_getln(buf, sizeof buf);
899         if (buf[0] == '2') {
900                 bytes = extract_long(&buf[4], 0);
901                 xferbuf = malloc(bytes + 2);
902
903                 /** Read it from the server */
904                 read_server_binary(xferbuf, bytes);
905                 serv_puts("CLOS");
906                 serv_getln(buf, sizeof buf);
907
908                 MimeType = GuessMimeType (xferbuf, bytes);
909                 /** Write it to the browser */
910                 if (!IsEmptyStr(MimeType))
911                 {
912                         http_transmit_thing(xferbuf, 
913                                             (size_t)bytes, 
914                                             MimeType, 
915                                             0);
916                         free(xferbuf);
917                         return;
918                 }
919                 /* hm... unknown mimetype? fallback to blank gif */
920                 free(xferbuf);
921         } 
922
923         
924         /**
925          * Instead of an ugly 404, send a 1x1 transparent GIF
926          * when there's no such image on the server.
927          */
928         char blank_gif[SIZ];
929         snprintf (blank_gif, SIZ, "%s%s", static_dirs[0], "/blank.gif");
930         output_static(blank_gif);
931 }
932
933 /**
934  * \brief Generic function to output an arbitrary MIME part from an arbitrary
935  *        message number on the server.
936  *
937  * \param msgnum                Number of the item on the citadel server
938  * \param partnum               The MIME part to be output
939  * \param force_download        Nonzero to force set the Content-Type: header
940  *                              to "application/octet-stream"
941  */
942 void mimepart(char *msgnum, char *partnum, int force_download)
943 {
944         char buf[256];
945         off_t bytes;
946         char content_type[256];
947         char *content = NULL;
948         
949         serv_printf("OPNA %s|%s", msgnum, partnum);
950         serv_getln(buf, sizeof buf);
951         if (buf[0] == '2') {
952                 bytes = extract_long(&buf[4], 0);
953                 content = malloc(bytes + 2);
954                 if (force_download) {
955                         strcpy(content_type, "application/octet-stream");
956                 }
957                 else {
958                         extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
959                 }
960                 output_headers(0, 0, 0, 0, 0, 0);
961                 read_server_binary(content, bytes);
962                 serv_puts("CLOS");
963                 serv_getln(buf, sizeof buf);
964                 http_transmit_thing(content, bytes, content_type, 0);
965                 free(content);
966         } else {
967                 wprintf("HTTP/1.1 404 %s\n", &buf[4]);
968                 output_headers(0, 0, 0, 0, 0, 0);
969                 wprintf("Content-Type: text/plain\r\n");
970                 wprintf("\r\n");
971                 wprintf(_("An error occurred while retrieving this part: %s\n"), &buf[4]);
972         }
973
974 }
975
976
977 /**
978  * \brief Read any MIME part of a message, from the server, into memory.
979  * \param msgnum number of the message on the citadel server
980  * \param partnum the MIME part to be loaded
981  */
982 char *load_mimepart(long msgnum, char *partnum)
983 {
984         char buf[SIZ];
985         off_t bytes;
986         char content_type[SIZ];
987         char *content;
988         
989         serv_printf("DLAT %ld|%s", msgnum, partnum);
990         serv_getln(buf, sizeof buf);
991         if (buf[0] == '6') {
992                 bytes = extract_long(&buf[4], 0);
993                 extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
994
995                 content = malloc(bytes + 2);
996                 serv_read(content, bytes);
997
998                 content[bytes] = 0;     /* null terminate for good measure */
999                 return(content);
1000         }
1001         else {
1002                 return(NULL);
1003         }
1004
1005 }
1006
1007
1008 /**
1009  * \brief Convenience functions to display a page containing only a string
1010  * \param titlebarcolor color of the titlebar of the frame
1011  * \param titlebarmsg text to display in the title bar
1012  * \param messagetext body of the box
1013  */
1014 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
1015 {
1016         wprintf("HTTP/1.1 200 OK\n");
1017         output_headers(1, 1, 2, 0, 0, 0);
1018         wprintf("<div id=\"banner\">\n");
1019         wprintf("<table width=100%% border=0 bgcolor=\"#%s\"><tr><td>", titlebarcolor);
1020         wprintf("<span class=\"titlebar\">%s</span>\n", titlebarmsg);
1021         wprintf("</td></tr></table>\n");
1022         wprintf("</div>\n<div id=\"content\">\n");
1023         escputs(messagetext);
1024
1025         wprintf("<hr />\n");
1026         wDumpContent(1);
1027 }
1028
1029
1030 /**
1031  * \brief Display a blank page.
1032  */
1033 void blank_page(void) {
1034         output_headers(1, 1, 0, 0, 0, 0);
1035         wDumpContent(2);
1036 }
1037
1038
1039 /**
1040  * \brief A template has been requested
1041  */
1042 void url_do_template(void) {
1043         do_template(bstr("template"));
1044 }
1045
1046
1047
1048 /**
1049  * \brief Offer to make any page the user's "start page."
1050  */
1051 void offer_start_page(void) {
1052         wprintf("<a href=\"change_start_page?startpage=");
1053         urlescputs(WC->this_page);
1054         wprintf("\">");
1055         wprintf(_("Make this my start page"));
1056         wprintf("</a>");
1057 #ifdef TECH_PREVIEW
1058         wprintf("<br/><a href=\"rss?room=");
1059         urlescputs(WC->wc_roomname);
1060         wprintf("\" title=\"RSS 2.0 feed for ");
1061         escputs(WC->wc_roomname);
1062         wprintf("\"><img alt=\"RSS\" border=\"0\" src=\"static/xml_button.gif\"/></a>\n");
1063 #endif
1064 }
1065
1066
1067 /**
1068  * \brief Change the user's start page
1069  */
1070 void change_start_page(void) {
1071
1072         if (bstr("startpage") == NULL) {
1073                 safestrncpy(WC->ImportantMessage,
1074                         _("You no longer have a start page selected."),
1075                         sizeof WC->ImportantMessage);
1076                 display_main_menu();
1077                 return;
1078         }
1079
1080         set_preference("startpage", bstr("startpage"), 1);
1081
1082         output_headers(1, 1, 0, 0, 0, 0);
1083         do_template("newstartpage");
1084         wDumpContent(1);
1085 }
1086
1087
1088
1089 /**
1090  * \brief convenience function to indicate success
1091  * \param successmessage the mesage itself
1092  */
1093 void display_success(char *successmessage)
1094 {
1095         convenience_page("007700", "OK", successmessage);
1096 }
1097
1098
1099 /**
1100  * \brief Authorization required page 
1101  * This is probably temporary and should be revisited 
1102  * \param message message to put in header
1103 */
1104 void authorization_required(const char *message)
1105 {
1106         wprintf("HTTP/1.1 401 Authorization Required\r\n");
1107         wprintf("WWW-Authenticate: Basic realm=\"\"\r\n", serv_info.serv_humannode);
1108         wprintf("Content-Type: text/html\r\n\r\n");
1109         wprintf("<h1>");
1110         wprintf(_("Authorization Required"));
1111         wprintf("</h1>\r\n");
1112         wprintf(_("The resource you requested requires a valid username and password. "
1113                 "You could not be logged in: %s\n"), message);
1114         wDumpContent(0);
1115 }
1116
1117 /**
1118  * \brief This function is called by the MIME parser to handle data uploaded by
1119  *        the browser.  Form data, uploaded files, and the data from HTTP PUT
1120  *        operations (such as those found in GroupDAV) all arrive this way.
1121  *
1122  * \param name Name of the item being uploaded
1123  * \param filename Filename of the item being uploaded
1124  * \param partnum MIME part identifier (not needed)
1125  * \param disp MIME content disposition (not needed)
1126  * \param content The actual data
1127  * \param cbtype MIME content-type
1128  * \param cbcharset Character set
1129  * \param length Content length
1130  * \param encoding MIME encoding type (not needed)
1131  * \param userdata Not used here
1132  */
1133 void upload_handler(char *name, char *filename, char *partnum, char *disp,
1134                         void *content, char *cbtype, char *cbcharset,
1135                         size_t length, char *encoding, void *userdata)
1136 {
1137         urlcontent *u;
1138
1139         lprintf(9, "upload_handler() name=%s, type=%s, len=%d\n", name, cbtype, length);
1140
1141         if (WC->urlstrings == NULL)
1142                 WC->urlstrings = NewHash();
1143
1144         /* Form fields */
1145         if ( (length > 0) && (IsEmptyStr(cbtype)) ) {
1146                 u = (urlcontent *) malloc(sizeof(urlcontent));
1147                 
1148                 safestrncpy(u->url_key, name, sizeof(u->url_key));
1149                 u->url_data = malloc(length + 1);
1150                 u->url_data_size = length;
1151                 memcpy(u->url_data, content, length);
1152                 u->url_data[length] = 0;
1153                 Put(WC->urlstrings, u->url_key, strlen(u->url_key), u, free_url);
1154
1155                 /* lprintf(9, "Key: <%s>  Data: <%s>\n", u->url_key, u->url_data); */
1156         }
1157
1158         /** Uploaded files */
1159         if ( (length > 0) && (!IsEmptyStr(cbtype)) ) {
1160                 WC->upload = malloc(length);
1161                 if (WC->upload != NULL) {
1162                         WC->upload_length = length;
1163                         safestrncpy(WC->upload_filename, filename,
1164                                         sizeof(WC->upload_filename));
1165                         safestrncpy(WC->upload_content_type, cbtype,
1166                                         sizeof(WC->upload_content_type));
1167                         memcpy(WC->upload, content, length);
1168                 }
1169                 else {
1170                         lprintf(3, "malloc() failed: %s\n", strerror(errno));
1171                 }
1172         }
1173
1174 }
1175
1176 /**
1177  * \brief Convenience functions to wrap around asynchronous ajax responses
1178  */
1179 void begin_ajax_response(void) {
1180         output_headers(0, 0, 0, 0, 0, 0);
1181
1182         wprintf("Content-type: text/html; charset=UTF-8\r\n"
1183                 "Server: %s\r\n"
1184                 "Connection: close\r\n"
1185                 "Pragma: no-cache\r\n"
1186                 "Cache-Control: no-cache\r\n"
1187                 "Expires: -1\r\n"
1188                 ,
1189                 PACKAGE_STRING);
1190         begin_burst();
1191 }
1192
1193 /**
1194  * \brief print ajax response footer 
1195  */
1196 void end_ajax_response(void) {
1197         wprintf("\r\n");
1198         wDumpContent(0);
1199 }
1200
1201 /**
1202  * \brief Wraps a Citadel server command in an AJAX transaction.
1203  */
1204 void ajax_servcmd(void)
1205 {
1206         char buf[1024];
1207         char gcontent[1024];
1208         char *junk;
1209         size_t len;
1210
1211         begin_ajax_response();
1212
1213         serv_printf("%s", bstr("g_cmd"));
1214         serv_getln(buf, sizeof buf);
1215         wprintf("%s\n", buf);
1216
1217         if (buf[0] == '8') {
1218                 serv_printf("\n\n000");
1219         }
1220         if ((buf[0] == '1') || (buf[0] == '8')) {
1221                 while (serv_getln(gcontent, sizeof gcontent), strcmp(gcontent, "000")) {
1222                         wprintf("%s\n", gcontent);
1223                 }
1224                 wprintf("000");
1225         }
1226         if (buf[0] == '4') {
1227                 text_to_server(bstr("g_input"));
1228                 serv_puts("000");
1229         }
1230         if (buf[0] == '6') {
1231                 len = atol(&buf[4]);
1232                 junk = malloc(len);
1233                 serv_read(junk, len);
1234                 free(junk);
1235         }
1236         if (buf[0] == '7') {
1237                 len = atol(&buf[4]);
1238                 junk = malloc(len);
1239                 memset(junk, 0, len);
1240                 serv_write(junk, len);
1241                 free(junk);
1242         }
1243
1244         end_ajax_response();
1245         
1246         /**
1247          * This is kind of an ugly hack, but this is the only place it can go.
1248          * If the command was GEXP, then the instant messenger window must be
1249          * running, so reset the "last_pager_check" watchdog timer so
1250          * that page_popup() doesn't try to open it a second time.
1251          */
1252         if (!strncasecmp(bstr("g_cmd"), "GEXP", 4)) {
1253                 WC->last_pager_check = time(NULL);
1254         }
1255 }
1256
1257
1258 /**
1259  * \brief Helper function for the asynchronous check to see if we need
1260  * to open the instant messenger window.
1261  */
1262 void seconds_since_last_gexp(void)
1263 {
1264         char buf[256];
1265
1266         begin_ajax_response();
1267         if ( (time(NULL) - WC->last_pager_check) < 30) {
1268                 wprintf("NO\n");
1269         }
1270         else {
1271                 serv_puts("NOOP");
1272                 serv_getln(buf, sizeof buf);
1273                 if (buf[3] == '*') {
1274                         wprintf("YES");
1275                 }
1276                 else {
1277                         wprintf("NO");
1278                 }
1279         }
1280         end_ajax_response();
1281 }
1282
1283
1284
1285
1286 /**
1287  * \brief Entry point for WebCit transaction
1288  */
1289 void session_loop(struct httprequest *req)
1290 {
1291         char cmd[1024];
1292         char action[1024];
1293         char arg[8][128];
1294         size_t sizes[10];
1295         char *index[10];
1296         char buf[SIZ];
1297         char request_method[128];
1298         char pathname[1024];
1299         int a, b, nBackDots, nEmpty;
1300         int ContentLength = 0;
1301         int BytesRead = 0;
1302         char ContentType[512];
1303         char *content = NULL;
1304         char *content_end = NULL;
1305         struct httprequest *hptr;
1306         char browser_host[256];
1307         char user_agent[256];
1308         int body_start = 0;
1309         int is_static = 0;
1310         int n_static = 0;
1311         int len = 0;
1312         /**
1313          * We stuff these with the values coming from the client cookies,
1314          * so we can use them to reconnect a timed out session if we have to.
1315          */
1316         char c_username[SIZ];
1317         char c_password[SIZ];
1318         char c_roomname[SIZ];
1319         char c_httpauth_string[SIZ];
1320         char c_httpauth_user[SIZ];
1321         char c_httpauth_pass[SIZ];
1322         char cookie[SIZ];
1323
1324         safestrncpy(c_username, "", sizeof c_username);
1325         safestrncpy(c_password, "", sizeof c_password);
1326         safestrncpy(c_roomname, "", sizeof c_roomname);
1327         safestrncpy(c_httpauth_string, "", sizeof c_httpauth_string);
1328         safestrncpy(c_httpauth_user, DEFAULT_HTTPAUTH_USER, sizeof c_httpauth_user);
1329         safestrncpy(c_httpauth_pass, DEFAULT_HTTPAUTH_PASS, sizeof c_httpauth_pass);
1330         strcpy(browser_host, "");
1331
1332         WC->upload_length = 0;
1333         WC->upload = NULL;
1334         WC->vars = NULL;
1335         WC->is_wap = 0;
1336
1337         hptr = req;
1338         if (hptr == NULL) return;
1339
1340         safestrncpy(cmd, hptr->line, sizeof cmd);
1341         hptr = hptr->next;
1342         extract_token(request_method, cmd, 0, ' ', sizeof request_method);
1343         extract_token(pathname, cmd, 1, ' ', sizeof pathname);
1344
1345         /** Figure out the action */
1346         index[0] = action;
1347         sizes[0] = sizeof action;
1348         for (a=1; a<9; a++)
1349         {
1350                 index[a] = arg[a-1];
1351                 sizes[a] = sizeof arg[a-1];
1352         }
1353 ////    index[9] = &foo; todo
1354         nBackDots = 0;
1355         nEmpty = 0;
1356         for ( a = 0; a < 9; ++a)
1357         {
1358                 extract_token(index[a], pathname, a + 1, '/', sizes[a]);
1359                 if (strstr(index[a], "?")) *strstr(index[a], "?") = 0;
1360                 if (strstr(index[a], "&")) *strstr(index[a], "&") = 0;
1361                 if (strstr(index[a], " ")) *strstr(index[a], " ") = 0;
1362                 if ((index[a][0] == '.') && (index[a][1] == '.'))
1363                         nBackDots++;
1364                 if (index[a][0] == '\0')
1365                         nEmpty++;
1366         }
1367
1368         while (hptr != NULL) {
1369                 safestrncpy(buf, hptr->line, sizeof buf);
1370                 /* lprintf(9, "HTTP HEADER: %s\n", buf); */
1371                 hptr = hptr->next;
1372
1373                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
1374                         safestrncpy(cookie, &buf[15], sizeof cookie);
1375                         cookie_to_stuff(cookie, NULL,
1376                                         c_username, sizeof c_username,
1377                                         c_password, sizeof c_password,
1378                                         c_roomname, sizeof c_roomname);
1379                 }
1380                 else if (!strncasecmp(buf, "Authorization: Basic ", 21)) {
1381                         CtdlDecodeBase64(c_httpauth_string, &buf[21], strlen(&buf[21]));
1382                         extract_token(c_httpauth_user, c_httpauth_string, 0, ':', sizeof c_httpauth_user);
1383                         extract_token(c_httpauth_pass, c_httpauth_string, 1, ':', sizeof c_httpauth_pass);
1384                 }
1385                 else if (!strncasecmp(buf, "Content-length: ", 16)) {
1386                         ContentLength = atoi(&buf[16]);
1387                 }
1388                 else if (!strncasecmp(buf, "Content-type: ", 14)) {
1389                         safestrncpy(ContentType, &buf[14], sizeof ContentType);
1390                 }
1391                 else if (!strncasecmp(buf, "User-agent: ", 12)) {
1392                         safestrncpy(user_agent, &buf[12], sizeof user_agent);
1393                 }
1394                 else if (!strncasecmp(buf, "X-Forwarded-Host: ", 18)) {
1395                         if (follow_xff) {
1396                                 safestrncpy(WC->http_host, &buf[18], sizeof WC->http_host);
1397                         }
1398                 }
1399                 else if (!strncasecmp(buf, "Host: ", 6)) {
1400                         if (IsEmptyStr(WC->http_host)) {
1401                                 safestrncpy(WC->http_host, &buf[6], sizeof WC->http_host);
1402                         }
1403                 }
1404                 else if (!strncasecmp(buf, "X-Forwarded-For: ", 17)) {
1405                         safestrncpy(browser_host, &buf[17], sizeof browser_host);
1406                         while (num_tokens(browser_host, ',') > 1) {
1407                                 remove_token(browser_host, 0, ',');
1408                         }
1409                         striplt(browser_host);
1410                 }
1411                 /** Only WAP gateways explicitly name this content-type */
1412                 else if (strstr(buf, "text/vnd.wap.wml")) {
1413                         WC->is_wap = 1;
1414                 }
1415         }
1416
1417         if (ContentLength > 0) {
1418                 content = malloc(ContentLength + SIZ);
1419                 memset(content, 0, ContentLength + SIZ);
1420                 snprintf(content,  ContentLength + SIZ, "Content-type: %s\n"
1421                                 "Content-length: %d\n\n",
1422                                 ContentType, ContentLength);
1423                 body_start = strlen(content);
1424
1425                 /** Read the entire input data at once. */
1426                 client_read(WC->http_sock, &content[BytesRead+body_start], ContentLength);
1427
1428                 if (!strncasecmp(ContentType, "application/x-www-form-urlencoded", 33)) {
1429                         addurls(&content[body_start]);
1430                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
1431                         content_end = content + ContentLength + body_start;
1432                         mime_parser(content, content_end, *upload_handler, NULL, NULL, NULL, 0);
1433                 }
1434         } else {
1435                 content = NULL;
1436         }
1437
1438         /** make a note of where we are in case the user wants to save it */
1439         safestrncpy(WC->this_page, cmd, sizeof(WC->this_page));
1440         remove_token(WC->this_page, 2, ' ');
1441         remove_token(WC->this_page, 0, ' ');
1442
1443         /** If there are variables in the URL, we must grab them now */
1444         len = strlen(cmd);
1445         for (a = 0; a < len; ++a) {
1446                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
1447                         for (b = a; b < len; ++b) {
1448                                 if (isspace(cmd[b])){
1449                                         cmd[b] = 0;
1450                                         len = b - 1;
1451                                 }
1452                         }
1453                         addurls(&cmd[a + 1]);
1454                         cmd[a] = 0;
1455                         len = a - 1;
1456                 }
1457         }
1458
1459         /** If it's a "force 404" situation then display the error and bail. */
1460         if (!strcmp(action, "404")) {
1461                 wprintf("HTTP/1.1 404 Not found\r\n");
1462                 wprintf("Content-Type: text/plain\r\n");
1463                 wprintf("\r\n");
1464                 wprintf("Not found\r\n");
1465                 goto SKIP_ALL_THIS_CRAP;
1466         }
1467
1468         /** Static content can be sent without connecting to Citadel. */
1469         is_static = 0;
1470         for (a=0; a<ndirs; ++a) {
1471                 if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
1472                         is_static = 1;
1473                         n_static = a;
1474                 }
1475         }
1476         if (is_static) {
1477                 if (nBackDots < 2)
1478                 {
1479                         snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
1480                                  static_dirs[n_static], 
1481                                  index[1], index[2], index[3], index[4], index[5], index[6], index[7]);
1482                         for (a=0; a<8; ++a) {
1483                                 if (buf[strlen(buf)-1] == '/') {
1484                                         buf[strlen(buf)-1] = 0;
1485                                 }
1486                         }
1487                         for (a = 0; a < strlen(buf); ++a) {
1488                                 if (isspace(buf[a])) {
1489                                         buf[a] = 0;
1490                                 }
1491                         }
1492                         output_static(buf);
1493                 }
1494                 else 
1495                 {
1496                         lprintf(9, "Suspicious request. Ignoring.");
1497                         wprintf("HTTP/1.1 404 Security check failed\r\n");
1498                         wprintf("Content-Type: text/plain\r\n");
1499                         wprintf("\r\n");
1500                         wprintf("You have sent a malformed or invalid request.\r\n");
1501                 }
1502                 goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
1503         }
1504
1505         /* If the client sent a nonce that is incorrect, kill the request. */
1506         if (strlen(bstr("nonce")) > 0) {
1507                 lprintf(9, "Comparing supplied nonce %s to session nonce %ld\n", 
1508                         bstr("nonce"), WC->nonce);
1509                 if (ibstr("nonce") != WC->nonce) {
1510                         lprintf(9, "Ignoring request with mismatched nonce.\n");
1511                         wprintf("HTTP/1.1 404 Security check failed\r\n");
1512                         wprintf("Content-Type: text/plain\r\n");
1513                         wprintf("\r\n");
1514                         wprintf("Security check failed.\r\n");
1515                         goto SKIP_ALL_THIS_CRAP;
1516                 }
1517         }
1518
1519         /**
1520          * If we're not connected to a Citadel server, try to hook up the
1521          * connection now.
1522          */
1523         if (!WC->connected) {
1524                 if (!strcasecmp(ctdlhost, "uds")) {
1525                         /* unix domain socket */
1526                         snprintf(buf, SIZ, "%s/citadel.socket", ctdlport);
1527                         WC->serv_sock = uds_connectsock(buf);
1528                 }
1529                 else {
1530                         /* tcp socket */
1531                         WC->serv_sock = tcp_connectsock(ctdlhost, ctdlport);
1532                 }
1533
1534                 if (WC->serv_sock < 0) {
1535                         do_logout();
1536                         goto SKIP_ALL_THIS_CRAP;
1537                 }
1538                 else {
1539                         WC->connected = 1;
1540                         serv_getln(buf, sizeof buf);    /** get the server welcome message */
1541
1542                         /**
1543                          * From what host is our user connecting?  Go with
1544                          * the host at the other end of the HTTP socket,
1545                          * unless we are following X-Forwarded-For: headers
1546                          * and such a header has already turned up something.
1547                          */
1548                         if ( (!follow_xff) || (strlen(browser_host) == 0) ) {
1549                                 locate_host(browser_host, WC->http_sock);
1550                         }
1551
1552                         get_serv_info(browser_host, user_agent);
1553                         if (serv_info.serv_rev_level < MINIMUM_CIT_VERSION) {
1554                                 wprintf(_("You are connected to a Citadel "
1555                                         "server running Citadel %d.%02d. \n"
1556                                         "In order to run this version of WebCit "
1557                                         "you must also have Citadel %d.%02d or"
1558                                         " newer.\n\n\n"),
1559                                                 serv_info.serv_rev_level / 100,
1560                                                 serv_info.serv_rev_level % 100,
1561                                                 MINIMUM_CIT_VERSION / 100,
1562                                                 MINIMUM_CIT_VERSION % 100
1563                                         );
1564                                 end_webcit_session();
1565                                 goto SKIP_ALL_THIS_CRAP;
1566                         }
1567                 }
1568         }
1569
1570         /**
1571          * Functions which can be performed without logging in
1572          */
1573         if (!strcasecmp(action, "listsub")) {
1574                 do_listsub();
1575                 goto SKIP_ALL_THIS_CRAP;
1576         }
1577         if (!strcasecmp(action, "freebusy")) {
1578                 do_freebusy(cmd);
1579                 goto SKIP_ALL_THIS_CRAP;
1580         }
1581
1582         /**
1583          * If we're not logged in, but we have HTTP Authentication data,
1584          * try logging in to Citadel using that.
1585          */
1586         if ((!WC->logged_in)
1587            && (strlen(c_httpauth_user) > 0)
1588            && (strlen(c_httpauth_pass) > 0)) {
1589                 serv_printf("USER %s", c_httpauth_user);
1590                 serv_getln(buf, sizeof buf);
1591                 if (buf[0] == '3') {
1592                         serv_printf("PASS %s", c_httpauth_pass);
1593                         serv_getln(buf, sizeof buf);
1594                         if (buf[0] == '2') {
1595                                 become_logged_in(c_httpauth_user,
1596                                                 c_httpauth_pass, buf);
1597                                 safestrncpy(WC->httpauth_user, c_httpauth_user, sizeof WC->httpauth_user);
1598                                 safestrncpy(WC->httpauth_pass, c_httpauth_pass, sizeof WC->httpauth_pass);
1599                         } else {
1600                                 /** Should only display when password is wrong */
1601                                 authorization_required(&buf[4]);
1602                                 goto SKIP_ALL_THIS_CRAP;
1603                         }
1604                 }
1605         }
1606
1607         /** This needs to run early */
1608 #ifdef TECH_PREVIEW
1609         if (!strcasecmp(action, "rss")) {
1610                 display_rss(bstr("room"), request_method);
1611                 goto SKIP_ALL_THIS_CRAP;
1612         }
1613 #endif
1614
1615         /** 
1616          * The GroupDAV stuff relies on HTTP authentication instead of
1617          * our session's authentication.
1618          */
1619         if (!strncasecmp(action, "groupdav", 8)) {
1620                 groupdav_main(req, ContentType, /* do GroupDAV methods */
1621                         ContentLength, content+body_start);
1622                 if (!WC->logged_in) {
1623                         WC->killthis = 1;       /* If not logged in, don't */
1624                 }                               /* keep the session active */
1625                 goto SKIP_ALL_THIS_CRAP;
1626         }
1627
1628
1629         /**
1630          * Automatically send requests with any method other than GET or
1631          * POST to the GroupDAV code as well.
1632          */
1633         if ((strcasecmp(request_method, "GET")) && (strcasecmp(request_method, "POST"))) {
1634                 groupdav_main(req, ContentType, /** do GroupDAV methods */
1635                         ContentLength, content+body_start);
1636                 if (!WC->logged_in) {
1637                         WC->killthis = 1;       /** If not logged in, don't */
1638                 }                               /** keep the session active */
1639                 goto SKIP_ALL_THIS_CRAP;
1640         }
1641
1642         /**
1643          * If we're not logged in, but we have username and password cookies
1644          * supplied by the browser, try using them to log in.
1645          */
1646         if ((!WC->logged_in)
1647            && (!IsEmptyStr(c_username))
1648            && (!IsEmptyStr(c_password))) {
1649                 serv_printf("USER %s", c_username);
1650                 serv_getln(buf, sizeof buf);
1651                 if (buf[0] == '3') {
1652                         serv_printf("PASS %s", c_password);
1653                         serv_getln(buf, sizeof buf);
1654                         if (buf[0] == '2') {
1655                                 become_logged_in(c_username, c_password, buf);
1656                         }
1657                 }
1658         }
1659         /**
1660          * If we don't have a current room, but a cookie specifying the
1661          * current room is supplied, make an effort to go there.
1662          */
1663         if ((IsEmptyStr(WC->wc_roomname)) && (!IsEmptyStr(c_roomname))) {
1664                 serv_printf("GOTO %s", c_roomname);
1665                 serv_getln(buf, sizeof buf);
1666                 if (buf[0] == '2') {
1667                         safestrncpy(WC->wc_roomname, c_roomname, sizeof WC->wc_roomname);
1668                 }
1669         }
1670
1671         if (!strcasecmp(action, "image")) {
1672                 output_image();
1673         } else if (!strcasecmp(action, "display_mime_icon")) {
1674                 display_mime_icon();
1675
1676                 /**
1677                  * All functions handled below this point ... make sure we log in
1678                  * before doing anything else!
1679                  */
1680         } else if ((!WC->logged_in) && (!strcasecmp(action, "login"))) {
1681                 do_login();
1682         } else if (!WC->logged_in) {
1683                 display_login(NULL);
1684         }
1685
1686         /**
1687          * Various commands...
1688          */
1689
1690         else if (!strcasecmp(action, "do_welcome")) {
1691                 do_welcome();
1692         } else if (!strcasecmp(action, "blank")) {
1693                 blank_page();
1694         } else if (!strcasecmp(action, "do_template")) {
1695                 url_do_template();
1696         } else if (!strcasecmp(action, "display_aide_menu")) {
1697                 display_aide_menu();
1698         } else if (!strcasecmp(action, "server_shutdown")) {
1699                 display_shutdown();
1700         } else if (!strcasecmp(action, "display_main_menu")) {
1701                 display_main_menu();
1702         } else if (!strcasecmp(action, "who")) {
1703                 who();
1704         } else if (!strcasecmp(action, "sslg")) {
1705                 seconds_since_last_gexp();
1706         } else if (!strcasecmp(action, "who_inner_html")) {
1707                 begin_ajax_response();
1708                 who_inner_div();
1709                 end_ajax_response();
1710         } else if (!strcasecmp(action, "wholist_section")) {
1711                 begin_ajax_response();
1712                 wholist_section();
1713                 end_ajax_response();
1714         } else if (!strcasecmp(action, "new_messages_html")) {
1715                 begin_ajax_response();
1716                 new_messages_section();
1717                 end_ajax_response();
1718         } else if (!strcasecmp(action, "tasks_inner_html")) {
1719                 begin_ajax_response();
1720                 tasks_section();
1721                 end_ajax_response();
1722         } else if (!strcasecmp(action, "calendar_inner_html")) {
1723                 begin_ajax_response();
1724                 calendar_section();
1725                 end_ajax_response();
1726         } else if (!strcasecmp(action, "mini_calendar")) {
1727                 begin_ajax_response();
1728                 ajax_mini_calendar();
1729                 end_ajax_response();
1730         } else if (!strcasecmp(action, "iconbar_ajax_menu")) {
1731                 begin_ajax_response();
1732                 do_iconbar();
1733                 end_ajax_response();
1734         } else if (!strcasecmp(action, "iconbar_ajax_rooms")) {
1735                 begin_ajax_response();
1736                 do_iconbar_roomlist();
1737                 end_ajax_response();
1738         } else if (!strcasecmp(action, "knrooms")) {
1739                 knrooms();
1740         } else if (!strcasecmp(action, "gotonext")) {
1741                 slrp_highest();
1742                 gotonext();
1743         } else if (!strcasecmp(action, "skip")) {
1744                 gotonext();
1745         } else if (!strcasecmp(action, "ungoto")) {
1746                 ungoto();
1747         } else if (!strcasecmp(action, "dotgoto")) {
1748                 if (WC->wc_view != VIEW_MAILBOX) {      /* dotgoto acts like dotskip when we're in a mailbox view */
1749                         slrp_highest();
1750                 }
1751                 smart_goto(bstr("room"));
1752         } else if (!strcasecmp(action, "dotskip")) {
1753                 smart_goto(bstr("room"));
1754         } else if (!strcasecmp(action, "termquit")) {
1755                 do_logout();
1756         } else if (!strcasecmp(action, "readnew")) {
1757                 readloop("readnew");
1758         } else if (!strcasecmp(action, "readold")) {
1759                 readloop("readold");
1760         } else if (!strcasecmp(action, "readfwd")) {
1761                 readloop("readfwd");
1762         } else if (!strcasecmp(action, "headers")) {
1763                 readloop("headers");
1764         } else if (!strcasecmp(action, "do_search")) {
1765                 readloop("do_search");
1766         } else if (!strcasecmp(action, "msg")) {
1767                 embed_message(index[1]);
1768         } else if (!strcasecmp(action, "printmsg")) {
1769                 print_message(index[1]);
1770         } else if (!strcasecmp(action, "msgheaders")) {
1771                 display_headers(index[1]);
1772         } else if (!strcasecmp(action, "wiki")) {
1773                 display_wiki_page();
1774         } else if (!strcasecmp(action, "display_enter")) {
1775                 display_enter();
1776         } else if (!strcasecmp(action, "post")) {
1777                 post_message();
1778         } else if (!strcasecmp(action, "move_msg")) {
1779                 move_msg();
1780         } else if (!strcasecmp(action, "delete_msg")) {
1781                 delete_msg();
1782         } else if (!strcasecmp(action, "userlist")) {
1783                 userlist();
1784         } else if (!strcasecmp(action, "showuser")) {
1785                 showuser();
1786         } else if (!strcasecmp(action, "display_page")) {
1787                 display_page();
1788         } else if (!strcasecmp(action, "page_user")) {
1789                 page_user();
1790         } else if (!strcasecmp(action, "chat")) {
1791                 do_chat();
1792         } else if (!strcasecmp(action, "display_private")) {
1793                 display_private("", 0);
1794         } else if (!strcasecmp(action, "goto_private")) {
1795                 goto_private();
1796         } else if (!strcasecmp(action, "zapped_list")) {
1797                 zapped_list();
1798         } else if (!strcasecmp(action, "display_zap")) {
1799                 display_zap();
1800         } else if (!strcasecmp(action, "zap")) {
1801                 zap();
1802         } else if (!strcasecmp(action, "display_entroom")) {
1803                 display_entroom();
1804         } else if (!strcasecmp(action, "entroom")) {
1805                 entroom();
1806         } else if (!strcasecmp(action, "display_whok")) {
1807                 display_whok();
1808         } else if (!strcasecmp(action, "do_invt_kick")) {
1809                 do_invt_kick();
1810         } else if (!strcasecmp(action, "display_editroom")) {
1811                 display_editroom();
1812         } else if (!strcasecmp(action, "netedit")) {
1813                 netedit();
1814         } else if (!strcasecmp(action, "editroom")) {
1815                 editroom();
1816         } else if (!strcasecmp(action, "display_editinfo")) {
1817                 display_edit(_("Room info"), "EINF 0", "RINF", "editinfo", 1);
1818         } else if (!strcasecmp(action, "editinfo")) {
1819                 save_edit(_("Room info"), "EINF 1", 1);
1820         } else if (!strcasecmp(action, "display_editbio")) {
1821                 snprintf(buf, SIZ, "RBIO %s", WC->wc_fullname);
1822                 display_edit(_("Your bio"), "NOOP", buf, "editbio", 3);
1823         } else if (!strcasecmp(action, "editbio")) {
1824                 save_edit(_("Your bio"), "EBIO", 0);
1825         } else if (!strcasecmp(action, "confirm_move_msg")) {
1826                 confirm_move_msg();
1827         } else if (!strcasecmp(action, "delete_room")) {
1828                 delete_room();
1829         } else if (!strcasecmp(action, "validate")) {
1830                 validate();
1831                 /* The users photo display / upload facility */
1832         } else if (!strcasecmp(action, "display_editpic")) {
1833                 display_graphics_upload(_("your photo"),
1834                                         "_userpic_",
1835                                         "editpic");
1836         } else if (!strcasecmp(action, "editpic")) {
1837                 do_graphics_upload("_userpic_");
1838                 /* room picture dispay / upload facility */
1839         } else if (!strcasecmp(action, "display_editroompic")) {
1840                 display_graphics_upload(_("the icon for this room"),
1841                                         "_roompic_",
1842                                         "editroompic");
1843         } else if (!strcasecmp(action, "editroompic")) {
1844                 do_graphics_upload("_roompic_");
1845                 /* the greetingpage hello pic */
1846         } else if (!strcasecmp(action, "display_edithello")) {
1847                 display_graphics_upload(_("the Greetingpicture for the login prompt"),
1848                                         "hello",
1849                                         "edithellopic");
1850         } else if (!strcasecmp(action, "edithellopic")) {
1851                 do_graphics_upload("hello");
1852                 /* the logoff banner */
1853         } else if (!strcasecmp(action, "display_editgoodbyepic")) {
1854                 display_graphics_upload(_("the Logoff banner picture"),
1855                                         "UIMG 0|%s|goodbuye",
1856                                         "editgoodbuyepic");
1857         } else if (!strcasecmp(action, "editgoodbuyepic")) {
1858                 do_graphics_upload("UIMG 1|%s|goodbuye");
1859
1860         } else if (!strcasecmp(action, "delete_floor")) {
1861                 delete_floor();
1862         } else if (!strcasecmp(action, "rename_floor")) {
1863                 rename_floor();
1864         } else if (!strcasecmp(action, "create_floor")) {
1865                 create_floor();
1866         } else if (!strcasecmp(action, "display_editfloorpic")) {
1867                 snprintf(buf, SIZ, "UIMG 0|_floorpic_|%s",
1868                         bstr("which_floor"));
1869                 display_graphics_upload(_("the icon for this floor"),
1870                                         buf,
1871                                         "editfloorpic");
1872         } else if (!strcasecmp(action, "editfloorpic")) {
1873                 snprintf(buf, SIZ, "UIMG 1|_floorpic_|%s",
1874                         bstr("which_floor"));
1875                 do_graphics_upload(buf);
1876         } else if (!strcasecmp(action, "display_reg")) {
1877                 display_reg(0);
1878         } else if (!strcasecmp(action, "display_changepw")) {
1879                 display_changepw();
1880         } else if (!strcasecmp(action, "changepw")) {
1881                 changepw();
1882         } else if (!strcasecmp(action, "display_edit_node")) {
1883                 display_edit_node();
1884         } else if (!strcasecmp(action, "edit_node")) {
1885                 edit_node();
1886         } else if (!strcasecmp(action, "display_netconf")) {
1887                 display_netconf();
1888         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
1889                 display_confirm_delete_node();
1890         } else if (!strcasecmp(action, "delete_node")) {
1891                 delete_node();
1892         } else if (!strcasecmp(action, "display_add_node")) {
1893                 display_add_node();
1894         } else if (!strcasecmp(action, "terminate_session")) {
1895                 slrp_highest();
1896                 terminate_session();
1897         } else if (!strcasecmp(action, "edit_me")) {
1898                 edit_me();
1899         } else if (!strcasecmp(action, "display_siteconfig")) {
1900                 display_siteconfig();
1901         } else if (!strcasecmp(action, "chat_recv")) {
1902                 chat_recv();
1903         } else if (!strcasecmp(action, "chat_send")) {
1904                 chat_send();
1905         } else if (!strcasecmp(action, "siteconfig")) {
1906                 siteconfig();
1907         } else if (!strcasecmp(action, "display_generic")) {
1908                 display_generic();
1909         } else if (!strcasecmp(action, "do_generic")) {
1910                 do_generic();
1911         } else if (!strcasecmp(action, "ajax_servcmd")) {
1912                 ajax_servcmd();
1913         } else if (!strcasecmp(action, "display_menubar")) {
1914                 display_menubar(1);
1915         } else if (!strcasecmp(action, "mimepart")) {
1916                 mimepart(index[1], index[2], 0);
1917         } else if (!strcasecmp(action, "mimepart_download")) {
1918                 mimepart(index[1], index[2], 1);
1919         } else if (!strcasecmp(action, "edit_vcard")) {
1920                 edit_vcard();
1921         } else if (!strcasecmp(action, "submit_vcard")) {
1922                 submit_vcard();
1923         } else if (!strcasecmp(action, "select_user_to_edit")) {
1924                 select_user_to_edit(NULL, NULL);
1925         } else if (!strcasecmp(action, "display_edituser")) {
1926                 display_edituser(NULL, 0);
1927         } else if (!strcasecmp(action, "edituser")) {
1928                 edituser();
1929         } else if (!strcasecmp(action, "create_user")) {
1930                 create_user();
1931         } else if (!strcasecmp(action, "changeview")) {
1932                 change_view();
1933         } else if (!strcasecmp(action, "change_start_page")) {
1934                 change_start_page();
1935         } else if (!strcasecmp(action, "display_floorconfig")) {
1936                 display_floorconfig(NULL);
1937         } else if (!strcasecmp(action, "toggle_self_service")) {
1938                 toggle_self_service();
1939         } else if (!strcasecmp(action, "display_edit_task")) {
1940                 display_edit_task();
1941         } else if (!strcasecmp(action, "save_task")) {
1942                 save_task();
1943         } else if (!strcasecmp(action, "display_edit_event")) {
1944                 display_edit_event();
1945         } else if (!strcasecmp(action, "save_event")) {
1946                 save_event();
1947         } else if (!strcasecmp(action, "respond_to_request")) {
1948                 respond_to_request();
1949         } else if (!strcasecmp(action, "handle_rsvp")) {
1950                 handle_rsvp();
1951         } else if (!strcasecmp(action, "summary")) {
1952                 summary();
1953         } else if (!strcasecmp(action, "summary_inner_div")) {
1954                 begin_ajax_response();
1955                 summary_inner_div();
1956                 end_ajax_response();
1957         } else if (!strcasecmp(action, "display_customize_iconbar")) {
1958                 display_customize_iconbar();
1959         } else if (!strcasecmp(action, "commit_iconbar")) {
1960                 commit_iconbar();
1961         } else if (!strcasecmp(action, "set_room_policy")) {
1962                 set_room_policy();
1963         } else if (!strcasecmp(action, "display_inetconf")) {
1964                 display_inetconf();
1965         } else if (!strcasecmp(action, "save_inetconf")) {
1966                 save_inetconf();
1967         } else if (!strcasecmp(action, "display_smtpqueue")) {
1968                 display_smtpqueue();
1969         } else if (!strcasecmp(action, "display_smtpqueue_inner_div")) {
1970                 display_smtpqueue_inner_div();
1971         } else if (!strcasecmp(action, "display_sieve")) {
1972                 display_sieve();
1973         } else if (!strcasecmp(action, "save_sieve")) {
1974                 save_sieve();
1975         } else if (!strcasecmp(action, "display_pushemail")) {
1976                 display_pushemail();
1977         } else if (!strcasecmp(action, "save_pushemail")) {
1978                 save_pushemail();
1979         } else if (!strcasecmp(action, "display_add_remove_scripts")) {
1980                 display_add_remove_scripts(NULL);
1981         } else if (!strcasecmp(action, "create_script")) {
1982                 create_script();
1983         } else if (!strcasecmp(action, "delete_script")) {
1984                 delete_script();
1985         } else if (!strcasecmp(action, "setup_wizard")) {
1986                 do_setup_wizard();
1987         } else if (!strcasecmp(action, "display_preferences")) {
1988                 display_preferences();
1989         } else if (!strcasecmp(action, "set_preferences")) {
1990                 set_preferences();
1991         } else if (!strcasecmp(action, "recp_autocomplete")) {
1992                 recp_autocomplete(bstr("recp"));
1993         } else if (!strcasecmp(action, "cc_autocomplete")) {
1994                 recp_autocomplete(bstr("cc"));
1995         } else if (!strcasecmp(action, "bcc_autocomplete")) {
1996                 recp_autocomplete(bstr("bcc"));
1997         } else if (!strcasecmp(action, "display_address_book_middle_div")) {
1998                 display_address_book_middle_div();
1999         } else if (!strcasecmp(action, "display_address_book_inner_div")) {
2000                 display_address_book_inner_div();
2001         } else if (!strcasecmp(action, "set_floordiv_expanded")) {
2002                 set_floordiv_expanded(index[1]);
2003         } else if (!strcasecmp(action, "diagnostics")) {
2004                 output_headers(1, 1, 1, 0, 0, 0);
2005                 wprintf("Session: %d<hr />\n", WC->wc_session);
2006                 wprintf("Command: <br /><PRE>\n");
2007                 escputs(cmd);
2008                 wprintf("</PRE><hr />\n");
2009                 wprintf("Variables: <br /><PRE>\n");
2010                 dump_vars();
2011                 wprintf("</PRE><hr />\n");
2012                 wDumpContent(1);
2013         } else if (!strcasecmp(action, "updatenote")) {
2014                 updatenote();
2015         } else if (!strcasecmp(action, "display_room_directory")) {
2016                 display_room_directory();
2017         } else if (!strcasecmp(action, "display_pictureview")) {
2018                 display_pictureview();
2019         } else if (!strcasecmp(action, "download_file")) {
2020                 download_file(index[1]);
2021         } else if (!strcasecmp(action, "upload_file")) {
2022                 upload_file();
2023         }
2024
2025         /** When all else fais, display the main menu. */
2026         else {
2027                 display_main_menu();
2028         }
2029
2030 SKIP_ALL_THIS_CRAP:
2031         fflush(stdout);
2032         if (content != NULL) {
2033                 free(content);
2034                 content = NULL;
2035         }
2036         free_urls();
2037         if (WC->upload_length > 0) {
2038                 free(WC->upload);
2039                 WC->upload_length = 0;
2040         }
2041 }
2042
2043 /**
2044  * \brief Replacement for sleep() that uses select() in order to avoid SIGALRM
2045  * \param seconds how many seconds should we sleep?
2046  */
2047 void sleeeeeeeeeep(int seconds)
2048 {
2049         struct timeval tv;
2050
2051         tv.tv_sec = seconds;
2052         tv.tv_usec = 0;
2053         select(0, NULL, NULL, NULL, &tv);
2054 }
2055
2056
2057 /*@}*/