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;
45 void unescape_input(char *buf)
50 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
51 buf[strlen(buf)-1] = 0;
53 for (a=0; a<strlen(buf); ++a) {
54 if (buf[a]=='+') buf[a]=' ';
59 sscanf(hex,"%02x",&b);
61 strcpy(&buf[a+1],&buf[a+3]);
68 void addurls(char *url) {
75 while (strlen(up)>0) {
77 /* locate the = sign */
80 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
84 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
87 strcpy(u->url_key, buf);
89 /* now chop that part off */
90 for (a=0; a<=b; ++a) ++up;
92 /* locate the & sign */
95 for (a=0; a<strlen(up); ++a) {
96 if (!strncmp(ptr,"&",1)) {
103 for (a=0; a<b; ++a) ++ptr;
106 u->url_data = malloc(strlen(up)+1);
107 strcpy(u->url_data, up);
109 unescape_input(u->url_data);
115 void free_urls(void) {
116 struct urlcontent *u;
118 while (urlstrings != NULL) {
119 free(urlstrings->url_data);
120 u = urlstrings->next;
127 * Diagnostic function to display the contents of all variables
129 void dump_vars(void) {
130 struct urlcontent *u;
132 for (u = urlstrings; u != NULL; u = u->next) {
133 wprintf("%38s = %s\n", u->url_key, u->url_data);
137 char *bstr(char *key) {
138 struct urlcontent *u;
140 for (u = urlstrings; u != NULL; u = u->next) {
141 if (!strcasecmp(u->url_key, key)) return(u->url_data);
147 void wprintf(const char *format, ...) {
149 struct webcontent *wptr;
151 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
162 va_start(arg_ptr, format);
163 vsprintf(wptr->w_data, format, arg_ptr);
167 int wContentLength(void) {
168 struct webcontent *wptr;
171 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
172 len = len + strlen(wptr->w_data);
178 void wDumpContent(void) {
179 struct webcontent *wptr;
181 printf("Content-type: text/html\n");
182 printf("Content-length: %d\n", wContentLength());
185 while (wlist != NULL) {
186 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
195 void escputs1(char *strbuf, int nbsp)
199 for (a=0; a<strlen(strbuf); ++a) {
200 if (strbuf[a]=='<') wprintf("<");
201 else if (strbuf[a]=='>') wprintf(">");
202 else if (strbuf[a]=='&') wprintf("&");
203 else if (strbuf[a]==34) wprintf(""");
204 else if (strbuf[a]==LB) wprintf("<");
205 else if (strbuf[a]==RB) wprintf(">");
206 else if (strbuf[a]==QU) wprintf("\"");
207 else if ((strbuf[a]==32)&&(nbsp==1)) {
211 wprintf("%c", strbuf[a]);
216 void escputs(char *strbuf)
223 char *urlesc(char *strbuf)
226 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
227 static char outbuf[512];
231 for (a=0; a<strlen(strbuf); ++a) {
233 for (b=0; b<strlen(ec); ++b) {
234 if (strbuf[a]==ec[b]) c=1;
237 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
238 else sprintf(&outbuf[b],"%c",strbuf[a]);
243 void urlescputs(char *strbuf)
245 wprintf("%s",urlesc(strbuf));
249 void getz(char *buf) {
251 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
253 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
254 buf[strlen(buf)-1] = 0;
259 * Output all that important stuff that the browser will want to see
261 void output_headers(int print_standard_html_head) {
263 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
265 printf("Server: %s\n", SERVER);
266 printf("Connection: close\n");
267 printf("Set-cookie: wc_session=%d\n", wc_session);
269 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
270 else printf("Set-cookie: wc_host=%s\n", unset);
272 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
273 else printf("Set-cookie: wc_port=%s\n", unset);
275 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
277 else printf("Set-cookie: wc_username=%s\n", unset);
279 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
281 else printf("Set-cookie: wc_password=%s\n", unset);
283 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
285 else printf("Set-cookie: wc_roomname=%s\n", unset);
287 if (print_standard_html_head) {
288 wprintf("<HTML><HEAD><TITLE>");
289 escputs("WebCit"); /* FIX -- add BBS name here */
290 wprintf("</TITLE></HEAD>");
291 if (ExpressMessages != NULL) {
292 wprintf("<SCRIPT language=\"javascript\">\n");
293 wprintf("function ExpressMessage() {\n");
294 wprintf(" alert(\"");
295 escputs(ExpressMessages);
297 wprintf(" }\n </SCRIPT>\n");
300 if (ExpressMessages != NULL) {
301 wprintf("onload=\"ExpressMessage()\" ");
302 free(ExpressMessages);
303 ExpressMessages = NULL;
305 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
313 void check_for_express_messages() {
319 while (serv_gets(buf), strcmp(buf, "000")) {
320 if (ExpressMessages == NULL) {
321 ExpressMessages = malloc(strlen(buf) + 4);
322 strcpy(ExpressMessages, "");
325 ExpressMessages = realloc(ExpressMessages,
326 (strlen(ExpressMessages) + strlen(buf) + 4) );
328 strcat(ExpressMessages, buf);
329 strcat(ExpressMessages, "\\n");
337 void output_static(char *what) {
343 sprintf(buf, "static/%s", what);
344 fp = fopen(buf, "rb");
346 printf("HTTP/1.0 404 %s\n", strerror(errno));
348 printf("Content-Type: text/plain\n");
349 sprintf(buf, "%s: %s\n", what, strerror(errno));
350 printf("Content-length: %d\n", strlen(buf));
352 fwrite(buf, strlen(buf), 1, stdout);
355 printf("HTTP/1.0 200 OK\n");
358 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
359 printf("Content-type: image/gif\n");
360 else if (!strncasecmp(&what[strlen(what)-4], ".jpg", 4))
361 printf("Content-type: image/jpeg\n");
362 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
363 printf("Content-type: text/html\n");
365 printf("Content-type: application/octet-stream\n");
367 fstat(fileno(fp), &statbuf);
368 bytes = statbuf.st_size;
369 printf("Content-length: %ld\n", (long)bytes);
372 putc(getc(fp), stdout);
379 void output_image() {
384 off_t accomplished = 0L;
387 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
390 bytes = extract_long(&buf[4], 0);
391 printf("HTTP/1.0 200 OK\n");
393 printf("Content-type: image/gif\n");
394 printf("Content-length: %ld\n", bytes);
397 while (bytes > (off_t)0) {
398 thisblock = (off_t)sizeof(xferbuf);
399 if (thisblock > bytes) thisblock = bytes;
400 serv_printf("READ %ld|%ld", accomplished, thisblock);
402 if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
403 serv_read(xferbuf, (int)thisblock);
404 fwrite(xferbuf, thisblock, 1, stdout);
405 bytes = bytes - thisblock;
406 accomplished = accomplished + thisblock;
413 printf("HTTP/1.0 404 %s\n", strerror(errno));
415 printf("Content-Type: text/plain\n");
416 sprintf(buf, "Error retrieving image\n");
417 printf("Content-length: %d\n", strlen(buf));
419 fwrite(buf, strlen(buf), 1, stdout);
426 * Convenience function to display a page containing only an error string
428 void display_error(char *errormessage) {
429 printf("HTTP/1.0 200 OK\n");
431 wprintf("<TABLE WIDTH=100% BORDER=0 BGCOLOR=770000><TR><TD>");
432 wprintf("<FONT SIZE=+1 COLOR=\"FFFFFF\"");
433 wprintf("<B>Error</B>\n");
434 wprintf("</FONT></TD></TR></TABLE><BR>\n");
435 escputs(errormessage);
436 wprintf("</BODY></HTML>\n");
443 void extract_action(char *actbuf, char *cmdbuf) {
446 strcpy(actbuf, cmdbuf);
447 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
448 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
449 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
451 for (i=0; i<strlen(actbuf); ++i) {
452 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
453 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
454 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
455 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
460 void session_loop(void) {
465 int ContentLength = 0;
466 char ContentType[512];
469 /* We stuff these with the values coming from the client cookies,
470 * so we can use them to reconnect a timed out session if we have to.
474 char c_username[256];
475 char c_password[256];
476 char c_roomname[256];
478 strcpy(c_host, defaulthost);
479 strcpy(c_port, defaultport);
480 strcpy(c_username, "");
481 strcpy(c_password, "");
482 strcpy(c_roomname, "");
485 extract_action(action, cmd);
490 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
491 strcpy(c_host, &buf[16]);
492 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
493 strcpy(c_port, &buf[16]);
494 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
495 strcpy(c_username, &buf[20]);
496 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
497 strcpy(c_password, &buf[20]);
498 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
499 strcpy(c_roomname, &buf[20]);
500 if (!strncasecmp(buf, "Content-length: ", 16)) {
501 ContentLength = atoi(&buf[16]);
503 if (!strncasecmp(buf, "Content-type: ", 14)) {
504 strcpy(ContentType, &buf[14]);
506 } while(strlen(buf)>0);
510 if (ContentLength > 0) {
511 content = malloc(ContentLength+1);
512 fread(content, ContentLength, 1, stdin);
514 content[ContentLength] = 0;
516 if (!strncasecmp(ContentType,
517 "application/x-www-form-urlencoded", 33)) {
520 else if (!strncasecmp(ContentType, "multipart", 9)) {
521 mime_parser(content, ContentLength, ContentType);
529 * If we're not connected to a Citadel server, try to hook up the
530 * connection now. Preference is given to the host and port specified
531 * by browser cookies, if cookies have been supplied.
534 serv_sock = connectsock(c_host, c_port, "tcp");
536 serv_gets(buf); /* get the server welcome message */
537 strcpy(wc_host, c_host);
538 strcpy(wc_port, c_port);
542 check_for_express_messages();
545 * If we're not logged in, but we have username and password cookies
546 * supplied by the browser, try using them to log in.
548 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
549 serv_printf("USER %s", c_username);
552 serv_printf("PASS %s", c_password);
555 become_logged_in(c_username, c_password, buf);
561 * If we don't have a current room, but a cookie specifying the
562 * current room is supplied, make an effort to go there.
564 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
565 serv_printf("GOTO %s", c_roomname);
568 strcpy(wc_roomname, c_roomname);
572 /* If there are variables in the URL, we must grab them now */
573 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
574 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
579 if (!strcasecmp(action, "static")) {
580 strcpy(buf, &cmd[12]);
581 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
585 else if (!strcasecmp(action, "image")) {
589 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
593 else if (!logged_in) {
597 /* Various commands... */
599 else if (!strcasecmp(action, "do_welcome")) {
603 else if (!strcasecmp(action, "display_main_menu")) {
607 else if (!strcasecmp(action, "advanced")) {
608 display_advanced_menu();
611 else if (!strcasecmp(action, "whobbs")) {
615 else if (!strcasecmp(action, "knrooms")) {
616 list_all_rooms_by_floor();
619 else if (!strcasecmp(action, "gotonext")) {
624 else if (!strcasecmp(action, "skip")) {
628 else if (!strcasecmp(action, "ungoto")) {
632 else if (!strcasecmp(action, "dotgoto")) {
634 gotoroom(bstr("room"), 1);
637 else if (!strcasecmp(action, "termquit")) {
641 else if (!strcasecmp(action, "readnew")) {
645 else if (!strcasecmp(action, "readold")) {
649 else if (!strcasecmp(action, "readfwd")) {
653 else if (!strcasecmp(action, "display_enter")) {
657 else if (!strcasecmp(action, "post")) {
661 else if (!strcasecmp(action, "confirm_delete_msg")) {
662 confirm_delete_msg();
665 else if (!strcasecmp(action, "delete_msg")) {
669 else if (!strcasecmp(action, "confirm_move_msg")) {
673 else if (!strcasecmp(action, "move_msg")) {
677 else if (!strcasecmp(action, "userlist")) {
681 else if (!strcasecmp(action, "showuser")) {
685 else if (!strcasecmp(action, "display_page")) {
689 else if (!strcasecmp(action, "page_user")) {
693 else if (!strcasecmp(action, "chat")) {
697 else if (!strcasecmp(action, "display_private")) {
698 display_private("", 0);
701 else if (!strcasecmp(action, "goto_private")) {
705 else if (!strcasecmp(action, "zapped_list")) {
709 else if (!strcasecmp(action, "display_zap")) {
713 else if (!strcasecmp(action, "zap")) {
717 else if (!strcasecmp(action, "display_entroom")) {
721 else if (!strcasecmp(action, "entroom")) {
725 else if (!strcasecmp(action, "display_editroom")) {
729 else if (!strcasecmp(action, "editroom")) {
733 else if (!strcasecmp(action, "display_editinfo")) {
734 display_edit("Room info", "EINF 0", "RINF", "/editinfo");
737 else if (!strcasecmp(action, "editinfo")) {
738 save_edit("Room info", "EINF 1", 1);
741 else if (!strcasecmp(action, "display_editbio")) {
742 sprintf(buf, "RBIO %s", wc_username);
743 display_edit("Your bio", "NOOP", buf, "editbio");
746 else if (!strcasecmp(action, "editbio")) {
747 save_edit("Your bio", "EBIO", 0);
750 else if (!strcasecmp(action, "confirm_delete_room")) {
751 confirm_delete_room();
754 else if (!strcasecmp(action, "delete_room")) {
758 else if (!strcasecmp(action, "validate")) {
762 /* When all else fails... */
764 printf("HTTP/1.0 200 OK\n");
767 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
768 wprintf("You're in session %d<HR>\n", wc_session);
769 wprintf("Command: <BR><PRE>\n");
771 wprintf("</PRE><HR>\n");
772 wprintf("Variables: <BR><PRE>\n");
774 wprintf("</PRE><HR>\n");
775 wprintf("</BODY></HTML>\n");
780 if (content != NULL) {
787 int main(int argc, char *argv[]) {
789 if (argc < 2 || argc > 4) {
791 "webcit: usage: webcit <session_id> [host [port]]\n");
795 wc_session = atoi(argv[1]);
798 defaulthost = argv[2];
800 defaultport = argv[3];
805 strcpy(wc_username, "");
806 strcpy(wc_password, "");
807 strcpy(wc_roomname, "");