]> code.citadel.org Git - citadel.git/blob - webcit/webcit.c
* webcit.c: exit Webcit sessions semi-gracefully when the connection
[citadel.git] / webcit / webcit.c
1 /*
2  * webcit.c
3  *
4  * This is the actual program called by the webserver.  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  * $Id$
9  */
10
11 #include <stdlib.h>
12 #ifdef HAVE_UNISTD_H
13 #include <unistd.h>
14 #endif
15 #include <stdio.h>
16 #include <ctype.h>
17 #include <string.h>
18 #include <errno.h>
19 #include <sys/stat.h>
20 #include <stdarg.h>
21 #include "webcit.h"
22 #include "child.h"
23 #include "mime_parser.h"
24
25 int wc_session;
26 char wc_username[256];
27 char wc_password[256];
28 char wc_roomname[256];
29 int TransactionCount = 0;
30 int connected = 0;
31 int logged_in = 0;
32 int axlevel;
33 char *ExpressMessages = NULL;
34 int new_mail = 0;
35 int need_vali = 0;
36
37 /* This variable is set to 1 if the room banner and menubar have been
38  * displayed, and we need to close the <TABLE> tags.
39  */
40 int fake_frames = 0;
41
42 struct webcontent *wlist = NULL;
43 struct webcontent *wlast = NULL;
44
45 struct urlcontent *urlstrings = NULL;
46
47 static const char *defaulthost = DEFAULT_HOST;
48 static const char *defaultport = DEFAULT_PORT;
49
50 int upload_length = 0;
51 char *upload;
52
53
54 void unescape_input(char *buf)
55 {
56         int a, b;
57         char hex[3];
58
59         while ((isspace(buf[strlen(buf) - 1])) && (strlen(buf) > 0))
60                 buf[strlen(buf) - 1] = 0;
61
62         for (a = 0; a < strlen(buf); ++a) {
63                 if (buf[a] == '+')
64                         buf[a] = ' ';
65                 if (buf[a] == '%') {
66                         hex[0] = buf[a + 1];
67                         hex[1] = buf[a + 2];
68                         hex[2] = 0;
69                         sscanf(hex, "%02x", &b);
70                         buf[a] = (char) b;
71                         strcpy(&buf[a + 1], &buf[a + 3]);
72                 }
73         }
74
75 }
76
77
78 void addurls(char *url)
79 {
80         char *up, *ptr;
81         char buf[256];
82         int a, b;
83         struct urlcontent *u;
84
85         up = url;
86         while (strlen(up) > 0) {
87
88                 /* locate the = sign */
89                 strncpy(buf, up, 255);
90                 b = (-1);
91                 for (a = 255; a >= 0; --a)
92                         if (buf[a] == '=')
93                                 b = a;
94                 if (b < 0)
95                         return;
96                 buf[b] = 0;
97
98                 u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
99                 u->next = urlstrings;
100                 urlstrings = u;
101                 strcpy(u->url_key, buf);
102
103                 /* now chop that part off */
104                 for (a = 0; a <= b; ++a)
105                         ++up;
106
107                 /* locate the & sign */
108                 ptr = up;
109                 b = strlen(up);
110                 for (a = 0; a < strlen(up); ++a) {
111                         if (!strncmp(ptr, "&", 1)) {
112                                 b = a;
113                                 break;
114                         }
115                         ++ptr;
116                 }
117                 ptr = up;
118                 for (a = 0; a < b; ++a)
119                         ++ptr;
120                 strcpy(ptr, "");
121
122                 u->url_data = malloc(strlen(up) + 1);
123                 strcpy(u->url_data, up);
124                 u->url_data[b] = 0;
125                 unescape_input(u->url_data);
126                 up = ptr;
127                 ++up;
128         }
129 }
130
131 void free_urls(void)
132 {
133         struct urlcontent *u;
134
135         while (urlstrings != NULL) {
136                 free(urlstrings->url_data);
137                 u = urlstrings->next;
138                 free(urlstrings);
139                 urlstrings = u;
140         }
141 }
142
143 /*
144  * Diagnostic function to display the contents of all variables
145  */
146 void dump_vars(void)
147 {
148         struct urlcontent *u;
149
150         for (u = urlstrings; u != NULL; u = u->next) {
151                 wprintf("%38s = %s\n", u->url_key, u->url_data);
152         }
153 }
154
155 char *bstr(char *key)
156 {
157         struct urlcontent *u;
158
159         for (u = urlstrings; u != NULL; u = u->next) {
160                 if (!strcasecmp(u->url_key, key))
161                         return (u->url_data);
162         }
163         return ("");
164 }
165
166
167 void wprintf(const char *format,...)
168 {
169         va_list arg_ptr;
170         struct webcontent *wptr;
171
172         wptr = (struct webcontent *) malloc(sizeof(struct webcontent));
173         wptr->next = NULL;
174         if (wlist == NULL) {
175                 wlist = wptr;
176                 wlast = wptr;
177         } else {
178                 wlast->next = wptr;
179                 wlast = wptr;
180         }
181
182         va_start(arg_ptr, format);
183         vsprintf(wptr->w_data, format, arg_ptr);
184         va_end(arg_ptr);
185 }
186
187 int wContentLength(void)
188 {
189         struct webcontent *wptr;
190         int len = 0;
191
192         for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
193                 len = len + strlen(wptr->w_data);
194         }
195
196         return (len);
197 }
198
199 /*
200  * wDumpContent() takes all the stuff that's been queued up using
201  * the wprintf() and escputs() functions, and sends it out to the browser.
202  * By queuing instead of transmitting as it's generated, we're able to
203  * calculate a Content-length: header.
204  *
205  * print_standard_html_footer should be set to 0 to transmit only, 1 to
206  * append the main menu and closing tags, or 2 to
207  * append the closing tags only.
208  */
209 void wDumpContent(int print_standard_html_footer)
210 {
211         struct webcontent *wptr;
212
213         if (fake_frames) {
214                 wprintf("<CENTER><FONT SIZE=-1>"
215                         "<A HREF=\"/ungoto\">"
216                         "<IMG SRC=\"/static/back.gif\" BORDER=0>"
217                         "Ungoto</A>&nbsp;&nbsp;&nbsp;");
218                 wprintf("<A HREF=\"#TheTop\">"
219                         "<IMG SRC=\"/static/up.gif\" BORDER=0>"
220                         "Top of page</A>&nbsp;&nbsp;&nbsp;");
221                 wprintf("<A HREF=\"/gotonext\">"
222                         "Goto next room"
223                         "<IMG SRC=\"/static/forward.gif\" BORDER=0></A>"
224                         "</FONT>\n"
225                         "</TD></TR></TABLE></TABLE>\n");
226                 fake_frames = 0;
227         }
228
229         if (print_standard_html_footer) {
230                 if (print_standard_html_footer != 2) {
231                         wprintf("<BR>");
232                 }
233                 wprintf("</BODY></HTML>\n");
234         }
235         printf("Content-type: text/html\n");
236         printf("Content-length: %d\n", wContentLength());
237         printf("\n");
238
239         while (wlist != NULL) {
240                 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
241                 wptr = wlist->next;
242                 free(wlist);
243                 wlist = wptr;
244         }
245         wlast = NULL;
246 }
247
248
249 void escputs1(char *strbuf, int nbsp)
250 {
251         int a;
252
253         for (a = 0; a < strlen(strbuf); ++a) {
254                 if (strbuf[a] == '<')
255                         wprintf("&lt;");
256                 else if (strbuf[a] == '>')
257                         wprintf("&gt;");
258                 else if (strbuf[a] == '&')
259                         wprintf("&amp;");
260                 else if (strbuf[a] == 34)
261                         wprintf("&quot;");
262                 else if (strbuf[a] == LB)
263                         wprintf("<");
264                 else if (strbuf[a] == RB)
265                         wprintf(">");
266                 else if (strbuf[a] == QU)
267                         wprintf("\"");
268                 else if ((strbuf[a] == 32) && (nbsp == 1)) {
269                         wprintf("&nbsp;");
270                 } else {
271                         wprintf("%c", strbuf[a]);
272                 }
273         }
274 }
275
276 void escputs(char *strbuf)
277 {
278         escputs1(strbuf, 0);
279 }
280
281
282
283 char *urlesc(char *strbuf)
284 {
285         int a, b, c;
286         char *ec = " #&;`'|*?-~<>^()[]{}$\\";
287         static char outbuf[512];
288
289         strcpy(outbuf, "");
290
291         for (a = 0; a < strlen(strbuf); ++a) {
292                 c = 0;
293                 for (b = 0; b < strlen(ec); ++b) {
294                         if (strbuf[a] == ec[b])
295                                 c = 1;
296                 }
297                 b = strlen(outbuf);
298                 if (c == 1)
299                         sprintf(&outbuf[b], "%%%02x", strbuf[a]);
300                 else
301                         sprintf(&outbuf[b], "%c", strbuf[a]);
302         }
303         return (outbuf);
304 }
305
306 void urlescputs(char *strbuf)
307 {
308         wprintf("%s", urlesc(strbuf));
309 }
310
311
312
313
314 /*
315  * Get a line of text from the webserver (which originally came from the
316  * user's browser), checking for sanity etc.
317  */
318 char *getz(char *buf)
319 {
320         int e = 0;
321
322         bzero(buf, 256);
323
324         /* If fgets() fails, it's because the webserver crashed, so kill off
325          * the session too.
326          */
327         if (fgets(buf, 256, stdin) == NULL) {
328                 e = errno;
329                 fprintf(stderr, "webcit: exit code %d (%s)\n",
330                         e, strerror(e));
331                 fflush(stderr);
332                 exit(e);
333                 
334         /* Otherwise, strip out nonprintables and resume our happy day.
335          */
336         } else {
337                 while ((strlen(buf) > 0) && (!isprint(buf[strlen(buf) - 1])))
338                         buf[strlen(buf) - 1] = 0;
339                 return buf;
340         }
341 }
342
343
344
345 /*
346  * Output all that important stuff that the browser will want to see
347  *
348  * print_standard_html_head values:
349  * 0 = Nothing.  Do not display any leading HTTP or HTML.
350  * 1 = HTTP headers plus the "fake frames" found in most windows.
351  * 2 = HTTP headers required to terminate the session (unset cookies)
352  * 3 = HTTP headers only.
353  */
354 void output_headers(int print_standard_html_head)
355 {
356
357         static char *unset = "; expires=28-May-1971 18:10:00 GMT";
358         char cookie[256];
359
360         printf("Server: %s\n", SERVER);
361         printf("Connection: close\n");
362
363         if (print_standard_html_head > 0) {
364                 printf("Pragma: no-cache\n");
365                 printf("Cache-Control: no-store\n");
366         }
367         stuff_to_cookie(cookie, wc_session, wc_username,
368                         wc_password, wc_roomname);
369         if (print_standard_html_head == 2) {
370                 printf("X-WebCit-Session: close\n");
371                 printf("Set-cookie: webcit=%s\n", unset);
372         } else {
373                 printf("Set-cookie: webcit=%s\n", cookie);
374         }
375
376         if (print_standard_html_head > 0) {
377                 wprintf("<HTML><HEAD><TITLE>");
378                 escputs(serv_info.serv_humannode);
379                 wprintf("</TITLE></HEAD>");
380                 if (ExpressMessages != NULL) {
381                         wprintf("<SCRIPT language=\"javascript\">\n");
382                         wprintf("function ExpressMessage() {\n");
383                         wprintf(" alert(\"");
384                         escputs(ExpressMessages);
385                         wprintf("\")\n");
386                         wprintf(" }\n </SCRIPT>\n");
387                 }
388                 wprintf("<BODY ");
389                 if (ExpressMessages != NULL) {
390                         wprintf("onload=\"ExpressMessage()\" ");
391                         free(ExpressMessages);
392                         ExpressMessages = NULL;
393                 }
394                 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
395         
396         
397         if (print_standard_html_head == 1) {
398                 wprintf("<A NAME=\"TheTop\"></A>"
399                         "<TABLE border=0 width=100%>"
400                         "<TR VALIGN=TOP><TD>");
401
402                 display_menubar(0);
403
404                 wprintf("</TD><TD VALIGN=TOP>"
405                         "<TABLE border=0 width=100%><TR VALIGN=TOP>"
406                         "<TD>\n");
407
408                 embed_room_banner(NULL);
409
410                 wprintf("</TD></TR><TR VALIGN=TOP><TD>\n");
411                 
412                 fake_frames = 1;
413                 }
414         }
415 }
416
417
418
419 void ExpressMessageCat(char *buf) {
420         if (ExpressMessages == NULL) {
421                 ExpressMessages = malloc(strlen(buf) + 4);
422                 strcpy(ExpressMessages, "");
423         } else {
424                 ExpressMessages = realloc(ExpressMessages,
425                         (strlen(ExpressMessages) + strlen(buf) + 4));
426         }
427         strcat(ExpressMessages, buf);
428         strcat(ExpressMessages, "\\n");
429 }
430
431
432 void check_for_express_messages()
433 {
434         char buf[256];
435
436         serv_puts("PEXP");
437         serv_gets(buf);
438         if (buf[0] == '1') {
439                 while (serv_gets(buf), strcmp(buf, "000")) {
440                         ExpressMessageCat(buf);
441                 }
442         }
443 }
444
445
446
447
448 void output_static(char *what)
449 {
450         char buf[256];
451         FILE *fp;
452         struct stat statbuf;
453         off_t bytes;
454
455         sprintf(buf, "static/%s", what);
456         fp = fopen(buf, "rb");
457         if (fp == NULL) {
458                 printf("HTTP/1.0 404 %s\n", strerror(errno));
459                 output_headers(0);
460                 printf("Content-Type: text/plain\n");
461                 sprintf(buf, "%s: %s\n", what, strerror(errno));
462                 printf("Content-length: %d\n", strlen(buf));
463                 printf("\n");
464                 fwrite(buf, strlen(buf), 1, stdout);
465         } else {
466                 printf("HTTP/1.0 200 OK\n");
467                 output_headers(0);
468
469                 if (!strncasecmp(&what[strlen(what) - 4], ".gif", 4))
470                         printf("Content-type: image/gif\n");
471                 else if (!strncasecmp(&what[strlen(what) - 4], ".txt", 4))
472                         printf("Content-type: text/plain\n");
473                 else if (!strncasecmp(&what[strlen(what) - 4], ".jpg", 4))
474                         printf("Content-type: image/jpeg\n");
475                 else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
476                         printf("Content-type: text/html\n");
477                 else
478                         printf("Content-type: application/octet-stream\n");
479
480                 fstat(fileno(fp), &statbuf);
481                 bytes = statbuf.st_size;
482                 printf("Content-length: %ld\n", (long) bytes);
483                 printf("\n");
484                 while (bytes--) {
485                         putc(getc(fp), stdout);
486                 }
487                 fflush(stdout);
488                 fclose(fp);
489         }
490 }
491
492 void output_image()
493 {
494         char buf[256];
495         char xferbuf[4096];
496         off_t bytes;
497         off_t thisblock;
498         off_t accomplished = 0L;
499
500
501         serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
502         serv_gets(buf);
503         if (buf[0] == '2') {
504                 bytes = extract_long(&buf[4], 0);
505                 printf("HTTP/1.0 200 OK\n");
506                 output_headers(0);
507                 printf("Content-type: image/gif\n");
508                 printf("Content-length: %ld\n", (long) bytes);
509                 printf("\n");
510
511                 while (bytes > (off_t) 0) {
512                         thisblock = (off_t) sizeof(xferbuf);
513                         if (thisblock > bytes)
514                                 thisblock = bytes;
515                         serv_printf("READ %ld|%ld", accomplished, thisblock);
516                         serv_gets(buf);
517                         if (buf[0] == '6')
518                                 thisblock = extract_long(&buf[4], 0);
519                         serv_read(xferbuf, (int) thisblock);
520                         fwrite(xferbuf, thisblock, 1, stdout);
521                         bytes = bytes - thisblock;
522                         accomplished = accomplished + thisblock;
523                 }
524                 fflush(stdout);
525                 serv_puts("CLOS");
526                 serv_gets(buf);
527         } else {
528                 printf("HTTP/1.0 404 %s\n", strerror(errno));
529                 output_headers(0);
530                 printf("Content-Type: text/plain\n");
531                 sprintf(buf, "Error retrieving image\n");
532                 printf("Content-length: %d\n", strlen(buf));
533                 printf("\n");
534                 fwrite(buf, strlen(buf), 1, stdout);
535         }
536
537 }
538
539
540 /*
541  * Convenience functions to display a page containing only a string
542  */
543 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext)
544 {
545         printf("HTTP/1.0 200 OK\n");
546         output_headers(1);
547         wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
548         wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
549         wprintf("<B>%s</B>\n", titlebarmsg);
550         wprintf("</FONT></TD></TR></TABLE><BR>\n");
551         escputs(messagetext);
552
553         wprintf("<HR>\n");
554         embed_main_menu();
555         wDumpContent(1);
556 }
557
558 void display_error(char *errormessage)
559 {
560         convenience_page("770000", "Error", errormessage);
561 }
562
563 void display_success(char *successmessage)
564 {
565         convenience_page("007700", "OK", successmessage);
566 }
567
568
569
570 void extract_action(char *actbuf, char *cmdbuf)
571 {
572         int i;
573
574         strcpy(actbuf, cmdbuf);
575         if (!strncasecmp(actbuf, "GET /", 5))
576                 strcpy(actbuf, &actbuf[5]);
577         if (!strncasecmp(actbuf, "PUT /", 5))
578                 strcpy(actbuf, &actbuf[5]);
579         if (!strncasecmp(actbuf, "POST /", 6))
580                 strcpy(actbuf, &actbuf[6]);
581
582         for (i = 0; i < strlen(actbuf); ++i) {
583                 if (actbuf[i] == ' ') {
584                         actbuf[i] = 0;
585                         i = 0;
586                 }
587                 if (actbuf[i] == '/') {
588                         actbuf[i] = 0;
589                         i = 0;
590                 }
591                 if (actbuf[i] == '?') {
592                         actbuf[i] = 0;
593                         i = 0;
594                 }
595                 if (actbuf[i] == '&') {
596                         actbuf[i] = 0;
597                         i = 0;
598                 }
599         }
600 }
601
602
603 void upload_handler(char *name, char *filename, char *encoding,
604                     void *content, char *cbtype, size_t length)
605 {
606
607         fprintf(stderr, "UPLOAD HANDLER CALLED\n");
608         fprintf(stderr, "    name = %s\n", name);
609         fprintf(stderr, "filename = %s\n", filename);
610         fprintf(stderr, "encoding = %s\n", encoding);
611         fprintf(stderr, "    type = %s\n", cbtype);
612         fprintf(stderr, "  length = %d\n", length);
613
614         if (strlen(name) > 0) {
615                 upload = malloc(length);
616                 if (upload != NULL) {
617                         upload_length = length;
618                         memcpy(upload, content, length);
619                 }
620         }
621 }
622
623
624 void session_loop(char *browser_host, char *user_agent)
625 {
626         char cmd[256];
627         char action[256];
628         char buf[256];
629         int a, b;
630         int ContentLength = 0;
631         char ContentType[512];
632         char *content;
633
634         /* We stuff these with the values coming from the client cookies,
635          * so we can use them to reconnect a timed out session if we have to.
636          */
637         char c_host[256];
638         char c_port[256];
639         char c_username[256];
640         char c_password[256];
641         char c_roomname[256];
642         char cookie[256];
643
644         strcpy(c_host, defaulthost);
645         strcpy(c_port, defaultport);
646         strcpy(c_username, "");
647         strcpy(c_password, "");
648         strcpy(c_roomname, "");
649
650         upload_length = 0;
651         upload = NULL;
652
653         if (getz(cmd) == NULL)
654                 return;
655         extract_action(action, cmd);
656
657         do {
658                 if (getz(buf) == NULL)
659                         return;
660
661                 if (!strncasecmp(buf, "Cookie: webcit=", 15)) {
662                         strcpy(cookie, &buf[15]);
663                         cookie_to_stuff(cookie, NULL,
664                                       c_username, c_password, c_roomname);
665                 }
666                 if (!strncasecmp(buf, "Content-length: ", 16)) {
667                         ContentLength = atoi(&buf[16]);
668                 }
669                 if (!strncasecmp(buf, "Content-type: ", 14)) {
670                         strcpy(ContentType, &buf[14]);
671                 }
672         } while (strlen(buf) > 0);
673
674         ++TransactionCount;
675
676         if (ContentLength > 0) {
677                 content = malloc(ContentLength + 1);
678                 fread(content, ContentLength, 1, stdin);
679
680                 content[ContentLength] = 0;
681
682                 if (!strncasecmp(ContentType,
683                               "application/x-www-form-urlencoded", 33)) {
684                         addurls(content);
685                 } else if (!strncasecmp(ContentType, "multipart", 9)) {
686                         mime_parser(content, ContentLength, ContentType,
687                                     *upload_handler);
688                 }
689         } else {
690                 content = NULL;
691         }
692
693         /* If there are variables in the URL, we must grab them now */
694         for (a = 0; a < strlen(cmd); ++a)
695                 if ((cmd[a] == '?') || (cmd[a] == '&')) {
696                         for (b = a; b < strlen(cmd); ++b)
697                                 if (isspace(cmd[b]))
698                                         cmd[b] = 0;
699                         addurls(&cmd[a + 1]);
700                         cmd[a] = 0;
701                 }
702         /*
703          * If we're not connected to a Citadel server, try to hook up the
704          * connection now.  Preference is given to the host and port specified
705          * by browser cookies, if cookies have been supplied.
706          */
707         if (!connected) {
708                 if (strlen(bstr("host")) > 0)
709                         strcpy(c_host, bstr("host"));
710                 if (strlen(bstr("port")) > 0)
711                         strcpy(c_port, bstr("port"));
712                 serv_sock = connectsock(c_host, c_port, "tcp");
713                 if (serv_sock < 0) {
714                         do_logout();
715                 }
716
717                 connected = 1;
718                 serv_gets(buf); /* get the server welcome message */
719                 get_serv_info(browser_host, user_agent);
720         }
721         check_for_express_messages();
722
723         /*
724          * If we're not logged in, but we have username and password cookies
725          * supplied by the browser, try using them to log in.
726          */
727         if ((!logged_in) && (strlen(c_username) > 0) && (strlen(c_password) > 0)) {
728                 serv_printf("USER %s", c_username);
729                 serv_gets(buf);
730                 if (buf[0] == '3') {
731                         serv_printf("PASS %s", c_password);
732                         serv_gets(buf);
733                         if (buf[0] == '2') {
734                                 become_logged_in(c_username, c_password, buf);
735                         }
736                 }
737         }
738         /*
739          * If we don't have a current room, but a cookie specifying the
740          * current room is supplied, make an effort to go there.
741          */
742         if ((strlen(wc_roomname) == 0) && (strlen(c_roomname) > 0)) {
743                 serv_printf("GOTO %s", c_roomname);
744                 serv_gets(buf);
745                 if (buf[0] == '2') {
746                         strcpy(wc_roomname, c_roomname);
747                 }
748         }
749         if (!strcasecmp(action, "static")) {
750                 strcpy(buf, &cmd[12]);
751                 for (a = 0; a < strlen(buf); ++a)
752                         if (isspace(buf[a]))
753                                 buf[a] = 0;
754                 output_static(buf);
755         } else if (!strcasecmp(action, "image")) {
756                 output_image();
757         } else if ((!logged_in) && (!strcasecmp(action, "login"))) {
758                 do_login();
759         } else if (!logged_in) {
760                 display_login(NULL);
761         }
762         /* Various commands... */
763
764         else if (!strcasecmp(action, "do_welcome")) {
765                 do_welcome();
766         } else if (!strcasecmp(action, "display_main_menu")) {
767                 display_main_menu();
768         } else if (!strcasecmp(action, "advanced")) {
769                 display_advanced_menu();
770         } else if (!strcasecmp(action, "whobbs")) {
771                 whobbs();
772         } else if (!strcasecmp(action, "knrooms")) {
773                 list_all_rooms_by_floor();
774         } else if (!strcasecmp(action, "gotonext")) {
775                 slrp_highest();
776                 gotonext();
777         } else if (!strcasecmp(action, "skip")) {
778                 gotonext();
779         } else if (!strcasecmp(action, "ungoto")) {
780                 ungoto();
781         } else if (!strcasecmp(action, "dotgoto")) {
782                 slrp_highest();
783                 smart_goto(bstr("room"));
784         } else if (!strcasecmp(action, "termquit")) {
785                 do_logout();
786         } else if (!strcasecmp(action, "readnew")) {
787                 readloop("readnew");
788         } else if (!strcasecmp(action, "readold")) {
789                 readloop("readold");
790         } else if (!strcasecmp(action, "readfwd")) {
791                 readloop("readfwd");
792         } else if (!strcasecmp(action, "display_enter")) {
793                 display_enter();
794         } else if (!strcasecmp(action, "post")) {
795                 post_message();
796         } else if (!strcasecmp(action, "confirm_delete_msg")) {
797                 confirm_delete_msg();
798         } else if (!strcasecmp(action, "delete_msg")) {
799                 delete_msg();
800         } else if (!strcasecmp(action, "confirm_move_msg")) {
801                 confirm_move_msg();
802         } else if (!strcasecmp(action, "move_msg")) {
803                 move_msg();
804         } else if (!strcasecmp(action, "userlist")) {
805                 userlist();
806         } else if (!strcasecmp(action, "showuser")) {
807                 showuser();
808         } else if (!strcasecmp(action, "display_page")) {
809                 display_page();
810         } else if (!strcasecmp(action, "page_user")) {
811                 page_user();
812         } else if (!strcasecmp(action, "chat")) {
813                 do_chat();
814         } else if (!strcasecmp(action, "display_private")) {
815                 display_private("", 0);
816         } else if (!strcasecmp(action, "goto_private")) {
817                 goto_private();
818         } else if (!strcasecmp(action, "zapped_list")) {
819                 zapped_list();
820         } else if (!strcasecmp(action, "display_zap")) {
821                 display_zap();
822         } else if (!strcasecmp(action, "zap")) {
823                 zap();
824         } else if (!strcasecmp(action, "display_entroom")) {
825                 display_entroom();
826         } else if (!strcasecmp(action, "entroom")) {
827                 entroom();
828         } else if (!strcasecmp(action, "display_editroom")) {
829                 display_editroom();
830         } else if (!strcasecmp(action, "editroom")) {
831                 editroom();
832         } else if (!strcasecmp(action, "display_editinfo")) {
833                 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
834         } else if (!strcasecmp(action, "editinfo")) {
835                 save_edit("Room info", "EINF 1", 1);
836         } else if (!strcasecmp(action, "display_editbio")) {
837                 sprintf(buf, "RBIO %s", wc_username);
838                 display_edit("Your bio", "NOOP", buf, "editbio");
839         } else if (!strcasecmp(action, "editbio")) {
840                 save_edit("Your bio", "EBIO", 0);
841         } else if (!strcasecmp(action, "confirm_delete_room")) {
842                 confirm_delete_room();
843         } else if (!strcasecmp(action, "delete_room")) {
844                 delete_room();
845         } else if (!strcasecmp(action, "validate")) {
846                 validate();
847         } else if (!strcasecmp(action, "display_editpic")) {
848                 display_graphics_upload("your photo",
849                                         "UIMG 0|_userpic_",
850                                         "/editpic");
851         } else if (!strcasecmp(action, "editpic")) {
852                 do_graphics_upload("UIMG 1|_userpic_");
853         } else if (!strcasecmp(action, "display_editroompic")) {
854                 display_graphics_upload("the graphic for this room",
855                                         "UIMG 0|_roompic_",
856                                         "/editroompic");
857         } else if (!strcasecmp(action, "editroompic")) {
858                 do_graphics_upload("UIMG 1|_roompic_");
859         } else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
860                 select_floor_to_edit_pic();
861         } else if (!strcasecmp(action, "display_editfloorpic")) {
862                 sprintf(buf, "UIMG 0|_floorpic_|%s",
863                         bstr("which_floor"));
864                 display_graphics_upload("the graphic for this floor",
865                                         buf,
866                                         "/editfloorpic");
867         } else if (!strcasecmp(action, "editfloorpic")) {
868                 sprintf(buf, "UIMG 1|_floorpic_|%s",
869                         bstr("which_floor"));
870                 do_graphics_upload(buf);
871         } else if (!strcasecmp(action, "display_reg")) {
872                 display_reg(0);
873         } else if (!strcasecmp(action, "register")) {
874                 register_user();
875         } else if (!strcasecmp(action, "display_changepw")) {
876                 display_changepw();
877         } else if (!strcasecmp(action, "changepw")) {
878                 changepw();
879         } else if (!strcasecmp(action, "display_edit_node")) {
880                 display_edit_node();
881         } else if (!strcasecmp(action, "display_netconf")) {
882                 display_netconf();
883         } else if (!strcasecmp(action, "display_confirm_unshare")) {
884                 display_confirm_unshare();
885         } else if (!strcasecmp(action, "display_confirm_delete_node")) {
886                 display_confirm_delete_node();
887         } else if (!strcasecmp(action, "delete_node")) {
888                 delete_node();
889         } else if (!strcasecmp(action, "unshare")) {
890                 unshare();
891         } else if (!strcasecmp(action, "display_add_node")) {
892                 display_add_node();
893         } else if (!strcasecmp(action, "add_node")) {
894                 add_node();
895         } else if (!strcasecmp(action, "display_share")) {
896                 display_share();
897         } else if (!strcasecmp(action, "share")) {
898                 share();
899         } else if (!strcasecmp(action, "terminate_session")) {
900                 slrp_highest();
901                 terminate_session();
902         } else if (!strcasecmp(action, "edit_me")) {
903                 edit_me();
904         } else if (!strcasecmp(action, "display_siteconfig")) {
905                 display_siteconfig();
906         } else if (!strcasecmp(action, "siteconfig")) {
907                 siteconfig();
908         } else if (!strcasecmp(action, "display_generic")) {
909                 display_generic();
910         } else if (!strcasecmp(action, "do_generic")) {
911                 do_generic();
912         } else if (!strcasecmp(action, "display_menubar")) {
913                 display_menubar(1);
914         } else if (!strcasecmp(action, "diagnostics")) {
915                 printf("HTTP/1.0 200 OK\n");
916                 output_headers(1);
917
918                 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
919                 wprintf("You're in session %d<HR>\n", wc_session);
920                 wprintf("Command: <BR><PRE>\n");
921                 escputs(cmd);
922                 wprintf("</PRE><HR>\n");
923                 wprintf("Variables: <BR><PRE>\n");
924                 dump_vars();
925                 wprintf("</PRE><HR>\n");
926                 wDumpContent(1);
927         }
928         /* When all else fais, display the main menu. */
929         else {
930                 display_main_menu();
931         }
932
933         fflush(stdout);
934         if (content != NULL) {
935                 free(content);
936                 content = NULL;
937         }
938         free_urls();
939         if (upload_length > 0) {
940                 free(upload);
941                 upload_length = 0;
942         }
943 }
944
945 int main(int argc, char *argv[])
946 {
947
948         char browser[256];
949         int bd;
950
951         if (argc != 6) {
952                 fprintf(stderr,
953                         "webcit: usage error (argc must be 6, not %d)\n",
954                         argc);
955                 return 1;
956         }
957
958         wc_session = atoi(argv[1]);
959         defaulthost = argv[2];
960         defaultport = argv[3];
961
962         strcpy(wc_username, "");
963         strcpy(wc_password, "");
964         strcpy(wc_roomname, "");
965
966         /* Clear out serv_info and temporarily set the value of serv_humannode
967          * to a default value, because it'll be used in HTML page titles
968          */
969         memset(&serv_info, 0, sizeof(serv_info));
970         strcpy(serv_info.serv_humannode, "WebCit");
971
972         strcpy(browser, argv[5]);
973         bd = browser_braindamage_check(browser);
974
975         while (1) {
976                 session_loop(argv[4], browser);
977         }
978 }