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