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.
27 char wc_username[256];
28 char wc_password[256];
29 char wc_roomname[256];
30 int TransactionCount = 0;
34 char *ExpressMessages = NULL;
36 struct webcontent *wlist = NULL;
37 struct webcontent *wlast = NULL;
39 struct urlcontent *urlstrings = NULL;
41 static const char *defaulthost = DEFAULT_HOST;
42 static const char *defaultport = DEFAULT_PORT;
44 int upload_length = 0;
48 void unescape_input(char *buf)
53 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
54 buf[strlen(buf)-1] = 0;
56 for (a=0; a<strlen(buf); ++a) {
57 if (buf[a]=='+') buf[a]=' ';
62 sscanf(hex,"%02x",&b);
64 strcpy(&buf[a+1],&buf[a+3]);
71 void addurls(char *url) {
78 while (strlen(up)>0) {
80 /* locate the = sign */
83 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
87 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
90 strcpy(u->url_key, buf);
92 /* now chop that part off */
93 for (a=0; a<=b; ++a) ++up;
95 /* locate the & sign */
98 for (a=0; a<strlen(up); ++a) {
99 if (!strncmp(ptr,"&",1)) {
106 for (a=0; a<b; ++a) ++ptr;
109 u->url_data = malloc(strlen(up)+1);
110 strcpy(u->url_data, up);
112 unescape_input(u->url_data);
118 void free_urls(void) {
119 struct urlcontent *u;
121 while (urlstrings != NULL) {
122 free(urlstrings->url_data);
123 u = urlstrings->next;
130 * Diagnostic function to display the contents of all variables
132 void dump_vars(void) {
133 struct urlcontent *u;
135 for (u = urlstrings; u != NULL; u = u->next) {
136 wprintf("%38s = %s\n", u->url_key, u->url_data);
140 char *bstr(char *key) {
141 struct urlcontent *u;
143 for (u = urlstrings; u != NULL; u = u->next) {
144 if (!strcasecmp(u->url_key, key)) return(u->url_data);
150 void wprintf(const char *format, ...) {
152 struct webcontent *wptr;
154 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
165 va_start(arg_ptr, format);
166 vsprintf(wptr->w_data, format, arg_ptr);
170 int wContentLength(void) {
171 struct webcontent *wptr;
174 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
175 len = len + strlen(wptr->w_data);
181 void wDumpContent(void) {
182 struct webcontent *wptr;
184 printf("Content-type: text/html\n");
185 printf("Content-length: %d\n", wContentLength());
188 while (wlist != NULL) {
189 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
198 void escputs1(char *strbuf, int nbsp)
202 for (a=0; a<strlen(strbuf); ++a) {
203 if (strbuf[a]=='<') wprintf("<");
204 else if (strbuf[a]=='>') wprintf(">");
205 else if (strbuf[a]=='&') wprintf("&");
206 else if (strbuf[a]==34) wprintf(""");
207 else if (strbuf[a]==LB) wprintf("<");
208 else if (strbuf[a]==RB) wprintf(">");
209 else if (strbuf[a]==QU) wprintf("\"");
210 else if ((strbuf[a]==32)&&(nbsp==1)) {
214 wprintf("%c", strbuf[a]);
219 void escputs(char *strbuf)
226 char *urlesc(char *strbuf)
229 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
230 static char outbuf[512];
234 for (a=0; a<strlen(strbuf); ++a) {
236 for (b=0; b<strlen(ec); ++b) {
237 if (strbuf[a]==ec[b]) c=1;
240 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
241 else sprintf(&outbuf[b],"%c",strbuf[a]);
246 void urlescputs(char *strbuf)
248 wprintf("%s",urlesc(strbuf));
252 void getz(char *buf) {
254 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
256 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
257 buf[strlen(buf)-1] = 0;
262 * Output all that important stuff that the browser will want to see
264 void output_headers(int print_standard_html_head) {
266 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
268 printf("Server: %s\n", SERVER);
269 printf("Connection: close\n");
270 printf("Set-cookie: wc_session=%d\n", wc_session);
272 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
273 else printf("Set-cookie: wc_host=%s\n", unset);
275 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
276 else printf("Set-cookie: wc_port=%s\n", unset);
278 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
280 else printf("Set-cookie: wc_username=%s\n", unset);
282 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
284 else printf("Set-cookie: wc_password=%s\n", unset);
286 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
288 else printf("Set-cookie: wc_roomname=%s\n", unset);
290 if (print_standard_html_head) {
291 wprintf("<HTML><HEAD><TITLE>");
292 escputs("WebCit"); /* FIX -- add BBS name here */
293 wprintf("</TITLE></HEAD>");
294 if (ExpressMessages != NULL) {
295 wprintf("<SCRIPT language=\"javascript\">\n");
296 wprintf("function ExpressMessage() {\n");
297 wprintf(" alert(\"");
298 escputs(ExpressMessages);
300 wprintf(" }\n </SCRIPT>\n");
303 if (ExpressMessages != NULL) {
304 wprintf("onload=\"ExpressMessage()\" ");
305 free(ExpressMessages);
306 ExpressMessages = NULL;
308 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
316 void check_for_express_messages() {
322 while (serv_gets(buf), strcmp(buf, "000")) {
323 if (ExpressMessages == NULL) {
324 ExpressMessages = malloc(strlen(buf) + 4);
325 strcpy(ExpressMessages, "");
328 ExpressMessages = realloc(ExpressMessages,
329 (strlen(ExpressMessages) + strlen(buf) + 4) );
331 strcat(ExpressMessages, buf);
332 strcat(ExpressMessages, "\\n");
340 void output_static(char *what) {
346 sprintf(buf, "static/%s", what);
347 fp = fopen(buf, "rb");
349 printf("HTTP/1.0 404 %s\n", strerror(errno));
351 printf("Content-Type: text/plain\n");
352 sprintf(buf, "%s: %s\n", what, strerror(errno));
353 printf("Content-length: %d\n", strlen(buf));
355 fwrite(buf, strlen(buf), 1, stdout);
358 printf("HTTP/1.0 200 OK\n");
361 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
362 printf("Content-type: image/gif\n");
363 else if (!strncasecmp(&what[strlen(what)-4], ".jpg", 4))
364 printf("Content-type: image/jpeg\n");
365 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
366 printf("Content-type: text/html\n");
368 printf("Content-type: application/octet-stream\n");
370 fstat(fileno(fp), &statbuf);
371 bytes = statbuf.st_size;
372 printf("Content-length: %ld\n", (long)bytes);
375 putc(getc(fp), stdout);
382 void output_image() {
387 off_t accomplished = 0L;
390 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
393 bytes = extract_long(&buf[4], 0);
394 printf("HTTP/1.0 200 OK\n");
396 printf("Content-type: image/gif\n");
397 printf("Content-length: %ld\n", bytes);
400 while (bytes > (off_t)0) {
401 thisblock = (off_t)sizeof(xferbuf);
402 if (thisblock > bytes) thisblock = bytes;
403 serv_printf("READ %ld|%ld", accomplished, thisblock);
405 if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
406 serv_read(xferbuf, (int)thisblock);
407 fwrite(xferbuf, thisblock, 1, stdout);
408 bytes = bytes - thisblock;
409 accomplished = accomplished + thisblock;
416 printf("HTTP/1.0 404 %s\n", strerror(errno));
418 printf("Content-Type: text/plain\n");
419 sprintf(buf, "Error retrieving image\n");
420 printf("Content-length: %d\n", strlen(buf));
422 fwrite(buf, strlen(buf), 1, stdout);
429 * Convenience functions to display a page containing only a string
431 void convenience_page(char *titlebarcolor, char *titlebarmsg, char *messagetext) {
432 printf("HTTP/1.0 200 OK\n");
434 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=%s><TR><TD>", titlebarcolor);
435 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
436 wprintf("<B>%s</B>\n", titlebarmsg);
437 wprintf("</FONT></TD></TR></TABLE><BR>\n");
438 escputs(messagetext);
439 wprintf("</BODY></HTML>\n");
443 void display_error(char *errormessage) {
444 convenience_page("770000", "Error", errormessage);
447 void display_success(char *successmessage) {
448 convenience_page("007700", "OK", successmessage);
453 void extract_action(char *actbuf, char *cmdbuf) {
456 strcpy(actbuf, cmdbuf);
457 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
458 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
459 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
461 for (i=0; i<strlen(actbuf); ++i) {
462 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
463 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
464 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
465 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
470 void session_loop(void) {
475 int ContentLength = 0;
476 char ContentType[512];
479 /* We stuff these with the values coming from the client cookies,
480 * so we can use them to reconnect a timed out session if we have to.
484 char c_username[256];
485 char c_password[256];
486 char c_roomname[256];
488 strcpy(c_host, defaulthost);
489 strcpy(c_port, defaultport);
490 strcpy(c_username, "");
491 strcpy(c_password, "");
492 strcpy(c_roomname, "");
498 extract_action(action, cmd);
503 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
504 strcpy(c_host, &buf[16]);
505 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
506 strcpy(c_port, &buf[16]);
507 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
508 strcpy(c_username, &buf[20]);
509 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
510 strcpy(c_password, &buf[20]);
511 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
512 strcpy(c_roomname, &buf[20]);
513 if (!strncasecmp(buf, "Content-length: ", 16)) {
514 ContentLength = atoi(&buf[16]);
516 if (!strncasecmp(buf, "Content-type: ", 14)) {
517 strcpy(ContentType, &buf[14]);
519 } while(strlen(buf)>0);
523 if (ContentLength > 0) {
524 content = malloc(ContentLength+1);
525 fread(content, ContentLength, 1, stdin);
527 content[ContentLength] = 0;
529 if (!strncasecmp(ContentType,
530 "application/x-www-form-urlencoded", 33)) {
533 else if (!strncasecmp(ContentType, "multipart", 9)) {
534 mime_parser(content, ContentLength, ContentType);
541 /* If there are variables in the URL, we must grab them now */
542 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
543 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
549 * If we're not connected to a Citadel server, try to hook up the
550 * connection now. Preference is given to the host and port specified
551 * by browser cookies, if cookies have been supplied.
554 if (strlen(bstr("host"))>0) strcpy(c_host, bstr("host"));
555 if (strlen(bstr("port"))>0) strcpy(c_port, bstr("port"));
556 serv_sock = connectsock(c_host, c_port, "tcp");
558 serv_gets(buf); /* get the server welcome message */
559 strcpy(wc_host, c_host);
560 strcpy(wc_port, c_port);
564 check_for_express_messages();
567 * If we're not logged in, but we have username and password cookies
568 * supplied by the browser, try using them to log in.
570 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
571 serv_printf("USER %s", c_username);
574 serv_printf("PASS %s", c_password);
577 become_logged_in(c_username, c_password, buf);
583 * If we don't have a current room, but a cookie specifying the
584 * current room is supplied, make an effort to go there.
586 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
587 serv_printf("GOTO %s", c_roomname);
590 strcpy(wc_roomname, c_roomname);
594 if (!strcasecmp(action, "static")) {
595 strcpy(buf, &cmd[12]);
596 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
600 else if (!strcasecmp(action, "image")) {
604 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
608 else if (!logged_in) {
612 /* Various commands... */
614 else if (!strcasecmp(action, "do_welcome")) {
618 else if (!strcasecmp(action, "display_main_menu")) {
622 else if (!strcasecmp(action, "advanced")) {
623 display_advanced_menu();
626 else if (!strcasecmp(action, "whobbs")) {
630 else if (!strcasecmp(action, "knrooms")) {
631 list_all_rooms_by_floor();
634 else if (!strcasecmp(action, "gotonext")) {
639 else if (!strcasecmp(action, "skip")) {
643 else if (!strcasecmp(action, "ungoto")) {
647 else if (!strcasecmp(action, "dotgoto")) {
649 gotoroom(bstr("room"), 1);
652 else if (!strcasecmp(action, "termquit")) {
656 else if (!strcasecmp(action, "readnew")) {
660 else if (!strcasecmp(action, "readold")) {
664 else if (!strcasecmp(action, "readfwd")) {
668 else if (!strcasecmp(action, "display_enter")) {
672 else if (!strcasecmp(action, "post")) {
676 else if (!strcasecmp(action, "confirm_delete_msg")) {
677 confirm_delete_msg();
680 else if (!strcasecmp(action, "delete_msg")) {
684 else if (!strcasecmp(action, "confirm_move_msg")) {
688 else if (!strcasecmp(action, "move_msg")) {
692 else if (!strcasecmp(action, "userlist")) {
696 else if (!strcasecmp(action, "showuser")) {
700 else if (!strcasecmp(action, "display_page")) {
704 else if (!strcasecmp(action, "page_user")) {
708 else if (!strcasecmp(action, "chat")) {
712 else if (!strcasecmp(action, "display_private")) {
713 display_private("", 0);
716 else if (!strcasecmp(action, "goto_private")) {
720 else if (!strcasecmp(action, "zapped_list")) {
724 else if (!strcasecmp(action, "display_zap")) {
728 else if (!strcasecmp(action, "zap")) {
732 else if (!strcasecmp(action, "display_entroom")) {
736 else if (!strcasecmp(action, "entroom")) {
740 else if (!strcasecmp(action, "display_editroom")) {
744 else if (!strcasecmp(action, "editroom")) {
748 else if (!strcasecmp(action, "display_editinfo")) {
749 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
752 else if (!strcasecmp(action, "editinfo")) {
753 save_edit("Room info", "EINF 1", 1);
756 else if (!strcasecmp(action, "display_editbio")) {
757 sprintf(buf, "RBIO %s", wc_username);
758 display_edit("Your bio", "NOOP", buf, "editbio");
761 else if (!strcasecmp(action, "editbio")) {
762 save_edit("Your bio", "EBIO", 0);
765 else if (!strcasecmp(action, "confirm_delete_room")) {
766 confirm_delete_room();
769 else if (!strcasecmp(action, "delete_room")) {
773 else if (!strcasecmp(action, "validate")) {
777 else if (!strcasecmp(action, "display_editpic")) {
778 display_graphics_upload("your photo",
783 else if (!strcasecmp(action, "editpic")) {
784 do_graphics_upload("UIMG 1|_userpic_");
787 else if (!strcasecmp(action, "display_editroompic")) {
788 display_graphics_upload("the graphic for this room",
793 else if (!strcasecmp(action, "editroompic")) {
794 do_graphics_upload("UIMG 1|_roompic_");
797 else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
798 select_floor_to_edit_pic();
801 else if (!strcasecmp(action, "display_editfloorpic")) {
802 sprintf(buf, "UIMG 0|_floorpic_|%s",
803 bstr("which_floor"));
804 display_graphics_upload("the graphic for this floor",
809 else if (!strcasecmp(action, "editfloorpic")) {
810 sprintf(buf, "UIMG 1|_floorpic_|%s",
811 bstr("which_floor"));
812 do_graphics_upload(buf);
815 else if (!strcasecmp(action, "display_reg")) {
819 else if (!strcasecmp(action, "register")) {
823 else if (!strcasecmp(action, "display_changepw")) {
827 else if (!strcasecmp(action, "changepw")) {
831 /* When all else fails... */
833 printf("HTTP/1.0 200 OK\n");
836 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
837 wprintf("You're in session %d<HR>\n", wc_session);
838 wprintf("Command: <BR><PRE>\n");
840 wprintf("</PRE><HR>\n");
841 wprintf("Variables: <BR><PRE>\n");
843 wprintf("</PRE><HR>\n");
844 wprintf("</BODY></HTML>\n");
849 if (content != NULL) {
854 if (upload_length > 0) {
860 int main(int argc, char *argv[]) {
862 if (argc < 2 || argc > 4) {
864 "webcit: usage: webcit <session_id> [host [port]]\n");
868 wc_session = atoi(argv[1]);
871 defaulthost = argv[2];
873 defaultport = argv[3];
878 strcpy(wc_username, "");
879 strcpy(wc_password, "");
880 strcpy(wc_roomname, "");