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;
35 struct webcontent *wlist = NULL;
36 struct webcontent *wlast = NULL;
38 struct urlcontent *urlstrings = NULL;
40 static const char *defaulthost = DEFAULT_HOST;
41 static const char *defaultport = DEFAULT_PORT;
44 void unescape_input(char *buf)
49 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
50 buf[strlen(buf)-1] = 0;
52 for (a=0; a<strlen(buf); ++a) {
53 if (buf[a]=='+') buf[a]=' ';
58 sscanf(hex,"%02x",&b);
60 strcpy(&buf[a+1],&buf[a+3]);
67 void addurls(char *url) {
74 while (strlen(up)>0) {
76 /* locate the = sign */
79 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
83 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
86 strcpy(u->url_key, buf);
88 /* now chop that part off */
89 for (a=0; a<=b; ++a) ++up;
91 /* locate the & sign */
94 for (a=0; a<strlen(up); ++a) {
95 if (!strncmp(ptr,"&",1)) {
102 for (a=0; a<b; ++a) ++ptr;
105 u->url_data = malloc(strlen(up));
106 strcpy(u->url_data, up);
107 unescape_input(u->url_data);
114 void free_urls(void) {
115 struct urlcontent *u;
117 while (urlstrings != NULL) {
118 free(urlstrings->url_data);
119 u = urlstrings->next;
126 * Diagnostic function to display the contents of all variables
128 void dump_vars(void) {
129 struct urlcontent *u;
131 for (u = urlstrings; u != NULL; u = u->next) {
132 wprintf("%38s = %s\n", u->url_key, u->url_data);
136 char *bstr(char *key) {
137 struct urlcontent *u;
139 for (u = urlstrings; u != NULL; u = u->next) {
140 if (!strcasecmp(u->url_key, key)) return(u->url_data);
146 void wprintf(const char *format, ...) {
148 struct webcontent *wptr;
150 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
161 va_start(arg_ptr, format);
162 vsprintf(wptr->w_data, format, arg_ptr);
166 int wContentLength(void) {
167 struct webcontent *wptr;
170 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
171 len = len + strlen(wptr->w_data);
177 void wDumpContent(void) {
178 struct webcontent *wptr;
180 printf("Content-type: text/html\n");
181 printf("Content-length: %d\n", wContentLength());
184 while (wlist != NULL) {
185 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
194 void escputs1(char *strbuf, int nbsp)
198 for (a=0; a<strlen(strbuf); ++a) {
199 if (strbuf[a]=='<') wprintf("<");
200 else if (strbuf[a]=='>') wprintf(">");
201 else if (strbuf[a]=='&') wprintf("&");
202 else if (strbuf[a]==34) wprintf(""");
203 else if (strbuf[a]==LB) wprintf("<");
204 else if (strbuf[a]==RB) wprintf(">");
205 else if (strbuf[a]==QU) wprintf("\"");
206 else if ((strbuf[a]==32)&&(nbsp==1)) {
210 wprintf("%c", strbuf[a]);
215 void escputs(char *strbuf)
222 char *urlesc(char *strbuf)
225 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
226 static char outbuf[512];
230 for (a=0; a<strlen(strbuf); ++a) {
232 for (b=0; b<strlen(ec); ++b) {
233 if (strbuf[a]==ec[b]) c=1;
236 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
237 else sprintf(&outbuf[b],"%c",strbuf[a]);
242 void urlescputs(char *strbuf)
244 wprintf("%s",urlesc(strbuf));
249 * Look for URL's embedded in a buffer and make them linkable. We use a
250 * target window in order to keep the BBS session in its own window.
265 for (pos=0; pos<strlen(buf); ++pos) {
266 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
267 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
272 if ((start>0)&&(buf[start-1]=='<')) ench = '>';
273 if ((start>0)&&(buf[start-1]=='[')) ench = ']';
274 if ((start>0)&&(buf[start-1]=='(')) ench = ')';
275 if ((start>0)&&(buf[start-1]=='{')) ench = '}';
277 for (pos=strlen(buf); pos>start; --pos) {
278 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
281 strncpy(urlbuf,&buf[start],end-start);
282 urlbuf[end-start] = 0;
285 strncpy(outbuf,buf,start);
286 sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
287 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
288 strcat(outbuf,&buf[end]);
295 void getz(char *buf) {
296 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
298 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
299 buf[strlen(buf)-1] = 0;
304 * Output all that important stuff that the browser will want to see
306 void output_headers(void) {
308 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
310 printf("Server: %s\n", SERVER);
311 printf("Connection: close\n");
312 printf("Set-cookie: wc_session=%d\n", wc_session);
314 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
315 else printf("Set-cookie: wc_host=%s\n", unset);
317 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
318 else printf("Set-cookie: wc_port=%s\n", unset);
320 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
322 else printf("Set-cookie: wc_username=%s\n", unset);
324 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
326 else printf("Set-cookie: wc_password=%s\n", unset);
328 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
330 else printf("Set-cookie: wc_roomname=%s\n", unset);
333 void output_static(char *what) {
339 sprintf(buf, "static/%s", what);
340 fp = fopen(buf, "rb");
342 printf("HTTP/1.0 404 %s\n", strerror(errno));
344 printf("Content-Type: text/plain\n");
345 sprintf(buf, "%s: %s\n", what, strerror(errno));
346 printf("Content-length: %d\n", strlen(buf));
348 fwrite(buf, strlen(buf), 1, stdout);
351 printf("HTTP/1.0 200 OK\n");
354 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
355 printf("Content-type: image/gif\n");
356 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
357 printf("Content-type: text/html\n");
359 printf("Content-type: application/octet-stream\n");
361 fstat(fileno(fp), &statbuf);
362 bytes = statbuf.st_size;
363 printf("Content-length: %ld\n", (long)bytes);
366 putc(getc(fp), stdout);
373 void output_image() {
378 off_t accomplished = 0L;
381 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
384 bytes = extract_long(&buf[4], 0);
385 printf("HTTP/1.0 200 OK\n");
387 printf("Content-type: image/gif\n");
388 printf("Content-length: %ld\n", bytes);
391 while (bytes > (off_t)0) {
392 thisblock = (off_t)sizeof(xferbuf);
393 if (thisblock > bytes) thisblock = bytes;
394 serv_printf("READ %ld|%ld", accomplished, thisblock);
396 if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
397 serv_read(xferbuf, (int)thisblock);
398 fwrite(xferbuf, thisblock, 1, stdout);
399 bytes = bytes - thisblock;
400 accomplished = accomplished + thisblock;
407 printf("HTTP/1.0 404 %s\n", strerror(errno));
409 printf("Content-Type: text/plain\n");
410 sprintf(buf, "Error retrieving image\n");
411 printf("Content-length: %d\n", strlen(buf));
413 fwrite(buf, strlen(buf), 1, stdout);
418 void extract_action(char *actbuf, char *cmdbuf) {
421 strcpy(actbuf, cmdbuf);
422 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
423 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
424 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
426 for (i=0; i<strlen(actbuf); ++i) {
427 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
428 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
429 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
430 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
435 void session_loop(void) {
440 int ContentLength = 0;
444 /* We stuff these with the values coming from the client cookies,
445 * so we can use them to reconnect a timed out session if we have to.
449 char c_username[256];
450 char c_password[256];
451 char c_roomname[256];
453 strcpy(c_host, defaulthost);
454 strcpy(c_port, defaultport);
455 strcpy(c_username, "");
456 strcpy(c_password, "");
457 strcpy(c_roomname, "");
460 extract_action(action, cmd);
465 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
466 strcpy(c_host, &buf[16]);
467 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
468 strcpy(c_port, &buf[16]);
469 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
470 strcpy(c_username, &buf[20]);
471 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
472 strcpy(c_password, &buf[20]);
473 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
474 strcpy(c_roomname, &buf[20]);
476 if (!strncasecmp(buf, "Content-length: ", 16)) {
477 ContentLength = atoi(&buf[16]);
480 } while(strlen(buf)>0);
484 if (ContentLength > 0) {
485 content = malloc(ContentLength+1);
486 fread(content, ContentLength, 1, stdin);
487 fp = fopen("content", "wb");
488 fwrite(content, ContentLength, 1, fp);
490 content[ContentLength] = 0;
498 * If we're not connected to a Citadel server, try to hook up the
499 * connection now. Preference is given to the host and port specified
500 * by browser cookies, if cookies have been supplied.
503 serv_sock = connectsock(c_host, c_port, "tcp");
505 serv_gets(buf); /* get the server welcome message */
506 strcpy(wc_host, c_host);
507 strcpy(wc_port, c_port);
513 * If we're not logged in, but we have username and password cookies
514 * supplied by the browser, try using them to log in.
516 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
517 serv_printf("USER %s", c_username);
520 serv_printf("PASS %s", c_password);
523 become_logged_in(c_username, c_password, buf);
529 * If we don't have a current room, but a cookie specifying the
530 * current room is supplied, make an effort to go there.
532 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
533 serv_printf("GOTO %s", c_roomname);
536 strcpy(wc_roomname, c_roomname);
540 /* If there are variables in the URL, we must grab them now */
541 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
542 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
547 if (!strcasecmp(action, "static")) {
548 strcpy(buf, &cmd[12]);
549 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
553 else if (!strcasecmp(action, "image")) {
557 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
561 else if (!logged_in) {
565 /* Various commands... */
567 else if (!strcasecmp(action, "do_welcome")) {
571 else if (!strcasecmp(action, "display_main_menu")) {
575 else if (!strcasecmp(action, "advanced")) {
576 display_advanced_menu();
579 else if (!strcasecmp(action, "whobbs")) {
583 else if (!strcasecmp(action, "knrooms")) {
584 list_all_rooms_by_floor();
587 else if (!strcasecmp(action, "gotonext")) {
592 else if (!strcasecmp(action, "skip")) {
596 else if (!strcasecmp(action, "ungoto")) {
600 else if (!strcasecmp(action, "dotgoto")) {
605 else if (!strcasecmp(action, "termquit")) {
609 /* When all else fails... */
611 printf("HTTP/1.0 200 OK\n");
614 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY BACKGROUND=\"/image&name=background\" TEXT=\"#000000\" LINK=\"#004400\">\n");
615 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
616 wprintf("You're in session %d<HR>\n", wc_session);
617 wprintf("Command: <BR><PRE>\n");
619 wprintf("</PRE><HR>\n");
620 wprintf("Variables: <BR><PRE>\n");
622 wprintf("</PRE><HR>\n");
623 wprintf("</BODY></HTML>\n");
628 if (content != NULL) {
635 int main(int argc, char *argv[]) {
637 if (argc < 2 || argc > 4) {
639 "webcit: usage: webcit <session_id> [host [port]]\n");
643 wc_session = atoi(argv[1]);
646 defaulthost = argv[2];
648 defaultport = argv[3];
653 strcpy(wc_username, "");
654 strcpy(wc_password, "");
655 strcpy(wc_roomname, "");