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) {
250 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
252 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
253 buf[strlen(buf)-1] = 0;
258 * Output all that important stuff that the browser will want to see
260 void output_headers(int print_standard_html_head) {
262 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
264 printf("Server: %s\n", SERVER);
265 printf("Connection: close\n");
266 printf("Set-cookie: wc_session=%d\n", wc_session);
268 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
269 else printf("Set-cookie: wc_host=%s\n", unset);
271 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
272 else printf("Set-cookie: wc_port=%s\n", unset);
274 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
276 else printf("Set-cookie: wc_username=%s\n", unset);
278 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
280 else printf("Set-cookie: wc_password=%s\n", unset);
282 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
284 else printf("Set-cookie: wc_roomname=%s\n", unset);
286 if (print_standard_html_head) {
287 wprintf("<HTML><HEAD><TITLE>");
289 wprintf("</TITLE></HEAD>");
290 if (ExpressMessages != NULL) {
291 wprintf("<SCRIPT language=\"javascript\">\n");
292 wprintf("function ExpressMessage() {\n");
293 wprintf(" alert(\"");
294 escputs(ExpressMessages);
296 wprintf(" }\n </SCRIPT>\n");
299 if (ExpressMessages != NULL) {
300 wprintf("onload=\"ExpressMessage()\" ");
301 free(ExpressMessages);
302 ExpressMessages = NULL;
304 wprintf("BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
312 void check_for_express_messages() {
318 while (serv_gets(buf), strcmp(buf, "000")) {
319 if (ExpressMessages == NULL) {
320 ExpressMessages = malloc(strlen(buf) + 4);
321 strcpy(ExpressMessages, "");
324 ExpressMessages = realloc(ExpressMessages,
325 (strlen(ExpressMessages) + strlen(buf) + 4) );
327 strcat(ExpressMessages, buf);
328 strcat(ExpressMessages, "\\n");
336 void output_static(char *what) {
342 sprintf(buf, "static/%s", what);
343 fp = fopen(buf, "rb");
345 printf("HTTP/1.0 404 %s\n", strerror(errno));
347 printf("Content-Type: text/plain\n");
348 sprintf(buf, "%s: %s\n", what, strerror(errno));
349 printf("Content-length: %d\n", strlen(buf));
351 fwrite(buf, strlen(buf), 1, stdout);
354 printf("HTTP/1.0 200 OK\n");
357 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
358 printf("Content-type: image/gif\n");
359 else if (!strncasecmp(&what[strlen(what)-4], ".jpg", 4))
360 printf("Content-type: image/jpeg\n");
361 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
362 printf("Content-type: text/html\n");
364 printf("Content-type: application/octet-stream\n");
366 fstat(fileno(fp), &statbuf);
367 bytes = statbuf.st_size;
368 printf("Content-length: %ld\n", (long)bytes);
371 putc(getc(fp), stdout);
378 void output_image() {
383 off_t accomplished = 0L;
386 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
389 bytes = extract_long(&buf[4], 0);
390 printf("HTTP/1.0 200 OK\n");
392 printf("Content-type: image/gif\n");
393 printf("Content-length: %ld\n", bytes);
396 while (bytes > (off_t)0) {
397 thisblock = (off_t)sizeof(xferbuf);
398 if (thisblock > bytes) thisblock = bytes;
399 serv_printf("READ %ld|%ld", accomplished, thisblock);
401 if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
402 serv_read(xferbuf, (int)thisblock);
403 fwrite(xferbuf, thisblock, 1, stdout);
404 bytes = bytes - thisblock;
405 accomplished = accomplished + thisblock;
412 printf("HTTP/1.0 404 %s\n", strerror(errno));
414 printf("Content-Type: text/plain\n");
415 sprintf(buf, "Error retrieving image\n");
416 printf("Content-length: %d\n", strlen(buf));
418 fwrite(buf, strlen(buf), 1, stdout);
423 void extract_action(char *actbuf, char *cmdbuf) {
426 strcpy(actbuf, cmdbuf);
427 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
428 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
429 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
431 for (i=0; i<strlen(actbuf); ++i) {
432 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
433 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
434 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
435 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
440 void session_loop(void) {
445 int ContentLength = 0;
449 /* We stuff these with the values coming from the client cookies,
450 * so we can use them to reconnect a timed out session if we have to.
454 char c_username[256];
455 char c_password[256];
456 char c_roomname[256];
458 strcpy(c_host, defaulthost);
459 strcpy(c_port, defaultport);
460 strcpy(c_username, "");
461 strcpy(c_password, "");
462 strcpy(c_roomname, "");
465 extract_action(action, cmd);
470 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
471 strcpy(c_host, &buf[16]);
472 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
473 strcpy(c_port, &buf[16]);
474 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
475 strcpy(c_username, &buf[20]);
476 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
477 strcpy(c_password, &buf[20]);
478 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
479 strcpy(c_roomname, &buf[20]);
481 if (!strncasecmp(buf, "Content-length: ", 16)) {
482 ContentLength = atoi(&buf[16]);
485 } while(strlen(buf)>0);
489 if (ContentLength > 0) {
490 content = malloc(ContentLength+1);
491 fread(content, ContentLength, 1, stdin);
492 fp = fopen("content", "wb");
493 fwrite(content, ContentLength, 1, fp);
495 content[ContentLength] = 0;
503 * If we're not connected to a Citadel server, try to hook up the
504 * connection now. Preference is given to the host and port specified
505 * by browser cookies, if cookies have been supplied.
508 serv_sock = connectsock(c_host, c_port, "tcp");
510 serv_gets(buf); /* get the server welcome message */
511 strcpy(wc_host, c_host);
512 strcpy(wc_port, c_port);
516 check_for_express_messages();
519 * If we're not logged in, but we have username and password cookies
520 * supplied by the browser, try using them to log in.
522 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
523 serv_printf("USER %s", c_username);
526 serv_printf("PASS %s", c_password);
529 become_logged_in(c_username, c_password, buf);
535 * If we don't have a current room, but a cookie specifying the
536 * current room is supplied, make an effort to go there.
538 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
539 serv_printf("GOTO %s", c_roomname);
542 strcpy(wc_roomname, c_roomname);
546 /* If there are variables in the URL, we must grab them now */
547 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
548 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
553 if (!strcasecmp(action, "static")) {
554 strcpy(buf, &cmd[12]);
555 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
559 else if (!strcasecmp(action, "image")) {
563 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
567 else if (!logged_in) {
571 /* Various commands... */
573 else if (!strcasecmp(action, "do_welcome")) {
577 else if (!strcasecmp(action, "display_main_menu")) {
581 else if (!strcasecmp(action, "advanced")) {
582 display_advanced_menu();
585 else if (!strcasecmp(action, "whobbs")) {
589 else if (!strcasecmp(action, "knrooms")) {
590 list_all_rooms_by_floor();
593 else if (!strcasecmp(action, "gotonext")) {
598 else if (!strcasecmp(action, "skip")) {
602 else if (!strcasecmp(action, "ungoto")) {
606 else if (!strcasecmp(action, "dotgoto")) {
611 else if (!strcasecmp(action, "termquit")) {
615 else if (!strcasecmp(action, "readnew")) {
619 else if (!strcasecmp(action, "readold")) {
623 else if (!strcasecmp(action, "readfwd")) {
627 else if (!strcasecmp(action, "display_enter")) {
631 else if (!strcasecmp(action, "post")) {
635 else if (!strcasecmp(action, "confirm_delete_msg")) {
636 confirm_delete_msg();
639 else if (!strcasecmp(action, "delete_msg")) {
643 else if (!strcasecmp(action, "confirm_move_msg")) {
647 else if (!strcasecmp(action, "move_msg")) {
651 else if (!strcasecmp(action, "userlist")) {
655 else if (!strcasecmp(action, "showuser")) {
659 else if (!strcasecmp(action, "display_page")) {
663 else if (!strcasecmp(action, "page_user")) {
667 /* When all else fails... */
669 printf("HTTP/1.0 200 OK\n");
672 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
673 wprintf("You're in session %d<HR>\n", wc_session);
674 wprintf("Command: <BR><PRE>\n");
676 wprintf("</PRE><HR>\n");
677 wprintf("Variables: <BR><PRE>\n");
679 wprintf("</PRE><HR>\n");
680 wprintf("</BODY></HTML>\n");
685 if (content != NULL) {
692 int main(int argc, char *argv[]) {
694 if (argc < 2 || argc > 4) {
696 "webcit: usage: webcit <session_id> [host [port]]\n");
700 wc_session = atoi(argv[1]);
703 defaulthost = argv[2];
705 defaultport = argv[3];
710 strcpy(wc_username, "");
711 strcpy(wc_password, "");
712 strcpy(wc_roomname, "");