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 function to display a page containing only an error string
431 void display_error(char *errormessage) {
432 printf("HTTP/1.0 200 OK\n");
434 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=770000><TR><TD>");
435 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
436 wprintf("<B>Error</B>\n");
437 wprintf("</FONT></TD></TR></TABLE><BR>\n");
438 escputs(errormessage);
439 wprintf("</BODY></HTML>\n");
446 void extract_action(char *actbuf, char *cmdbuf) {
449 strcpy(actbuf, cmdbuf);
450 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
451 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
452 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
454 for (i=0; i<strlen(actbuf); ++i) {
455 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
456 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
457 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
458 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
463 void session_loop(void) {
468 int ContentLength = 0;
469 char ContentType[512];
472 /* We stuff these with the values coming from the client cookies,
473 * so we can use them to reconnect a timed out session if we have to.
477 char c_username[256];
478 char c_password[256];
479 char c_roomname[256];
481 strcpy(c_host, defaulthost);
482 strcpy(c_port, defaultport);
483 strcpy(c_username, "");
484 strcpy(c_password, "");
485 strcpy(c_roomname, "");
491 extract_action(action, cmd);
496 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
497 strcpy(c_host, &buf[16]);
498 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
499 strcpy(c_port, &buf[16]);
500 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
501 strcpy(c_username, &buf[20]);
502 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
503 strcpy(c_password, &buf[20]);
504 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
505 strcpy(c_roomname, &buf[20]);
506 if (!strncasecmp(buf, "Content-length: ", 16)) {
507 ContentLength = atoi(&buf[16]);
509 if (!strncasecmp(buf, "Content-type: ", 14)) {
510 strcpy(ContentType, &buf[14]);
512 } while(strlen(buf)>0);
516 if (ContentLength > 0) {
517 content = malloc(ContentLength+1);
518 fread(content, ContentLength, 1, stdin);
520 content[ContentLength] = 0;
522 if (!strncasecmp(ContentType,
523 "application/x-www-form-urlencoded", 33)) {
526 else if (!strncasecmp(ContentType, "multipart", 9)) {
527 mime_parser(content, ContentLength, ContentType);
534 /* If there are variables in the URL, we must grab them now */
535 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
536 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
542 * If we're not connected to a Citadel server, try to hook up the
543 * connection now. Preference is given to the host and port specified
544 * by browser cookies, if cookies have been supplied.
547 if (strlen(bstr("host"))>0) strcpy(c_host, bstr("host"));
548 if (strlen(bstr("port"))>0) strcpy(c_port, bstr("port"));
549 serv_sock = connectsock(c_host, c_port, "tcp");
551 serv_gets(buf); /* get the server welcome message */
552 strcpy(wc_host, c_host);
553 strcpy(wc_port, c_port);
557 check_for_express_messages();
560 * If we're not logged in, but we have username and password cookies
561 * supplied by the browser, try using them to log in.
563 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
564 serv_printf("USER %s", c_username);
567 serv_printf("PASS %s", c_password);
570 become_logged_in(c_username, c_password, buf);
576 * If we don't have a current room, but a cookie specifying the
577 * current room is supplied, make an effort to go there.
579 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
580 serv_printf("GOTO %s", c_roomname);
583 strcpy(wc_roomname, c_roomname);
587 /* FIX unfix etc. here's where the addurls() WAS... */
588 if (!strcasecmp(action, "static")) {
589 strcpy(buf, &cmd[12]);
590 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
594 else if (!strcasecmp(action, "image")) {
598 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
602 else if (!logged_in) {
606 /* Various commands... */
608 else if (!strcasecmp(action, "do_welcome")) {
612 else if (!strcasecmp(action, "display_main_menu")) {
616 else if (!strcasecmp(action, "advanced")) {
617 display_advanced_menu();
620 else if (!strcasecmp(action, "whobbs")) {
624 else if (!strcasecmp(action, "knrooms")) {
625 list_all_rooms_by_floor();
628 else if (!strcasecmp(action, "gotonext")) {
633 else if (!strcasecmp(action, "skip")) {
637 else if (!strcasecmp(action, "ungoto")) {
641 else if (!strcasecmp(action, "dotgoto")) {
643 gotoroom(bstr("room"), 1);
646 else if (!strcasecmp(action, "termquit")) {
650 else if (!strcasecmp(action, "readnew")) {
654 else if (!strcasecmp(action, "readold")) {
658 else if (!strcasecmp(action, "readfwd")) {
662 else if (!strcasecmp(action, "display_enter")) {
666 else if (!strcasecmp(action, "post")) {
670 else if (!strcasecmp(action, "confirm_delete_msg")) {
671 confirm_delete_msg();
674 else if (!strcasecmp(action, "delete_msg")) {
678 else if (!strcasecmp(action, "confirm_move_msg")) {
682 else if (!strcasecmp(action, "move_msg")) {
686 else if (!strcasecmp(action, "userlist")) {
690 else if (!strcasecmp(action, "showuser")) {
694 else if (!strcasecmp(action, "display_page")) {
698 else if (!strcasecmp(action, "page_user")) {
702 else if (!strcasecmp(action, "chat")) {
706 else if (!strcasecmp(action, "display_private")) {
707 display_private("", 0);
710 else if (!strcasecmp(action, "goto_private")) {
714 else if (!strcasecmp(action, "zapped_list")) {
718 else if (!strcasecmp(action, "display_zap")) {
722 else if (!strcasecmp(action, "zap")) {
726 else if (!strcasecmp(action, "display_entroom")) {
730 else if (!strcasecmp(action, "entroom")) {
734 else if (!strcasecmp(action, "display_editroom")) {
738 else if (!strcasecmp(action, "editroom")) {
742 else if (!strcasecmp(action, "display_editinfo")) {
743 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
746 else if (!strcasecmp(action, "editinfo")) {
747 save_edit("Room info", "EINF 1", 1);
750 else if (!strcasecmp(action, "display_editbio")) {
751 sprintf(buf, "RBIO %s", wc_username);
752 display_edit("Your bio", "NOOP", buf, "editbio");
755 else if (!strcasecmp(action, "editbio")) {
756 save_edit("Your bio", "EBIO", 0);
759 else if (!strcasecmp(action, "confirm_delete_room")) {
760 confirm_delete_room();
763 else if (!strcasecmp(action, "delete_room")) {
767 else if (!strcasecmp(action, "validate")) {
771 else if (!strcasecmp(action, "display_editpic")) {
772 display_graphics_upload("your photo",
777 else if (!strcasecmp(action, "editpic")) {
778 do_graphics_upload("UIMG 1|_userpic_");
781 else if (!strcasecmp(action, "display_editroompic")) {
782 display_graphics_upload("the graphic for this room",
787 else if (!strcasecmp(action, "editroompic")) {
788 do_graphics_upload("UIMG 1|_roompic_");
791 else if (!strcasecmp(action, "select_floor_to_edit_pic")) {
792 select_floor_to_edit_pic();
795 else if (!strcasecmp(action, "display_editfloorpic")) {
796 sprintf(buf, "UIMG 0|_floorpic_|%s",
797 bstr("which_floor"));
798 display_graphics_upload("the graphic for this floor",
803 else if (!strcasecmp(action, "editfloorpic")) {
804 sprintf(buf, "UIMG 1|_floorpic_|%s",
805 bstr("which_floor"));
806 do_graphics_upload(buf);
809 else if (!strcasecmp(action, "display_reg")) {
813 else if (!strcasecmp(action, "register")) {
817 /* When all else fails... */
819 printf("HTTP/1.0 200 OK\n");
822 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
823 wprintf("You're in session %d<HR>\n", wc_session);
824 wprintf("Command: <BR><PRE>\n");
826 wprintf("</PRE><HR>\n");
827 wprintf("Variables: <BR><PRE>\n");
829 wprintf("</PRE><HR>\n");
830 wprintf("</BODY></HTML>\n");
835 if (content != NULL) {
840 if (upload_length > 0) {
846 int main(int argc, char *argv[]) {
848 if (argc < 2 || argc > 4) {
850 "webcit: usage: webcit <session_id> [host [port]]\n");
854 wc_session = atoi(argv[1]);
857 defaulthost = argv[2];
859 defaultport = argv[3];
864 strcpy(wc_username, "");
865 strcpy(wc_password, "");
866 strcpy(wc_roomname, "");