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;
41 void unescape_input(char *buf)
46 while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
47 buf[strlen(buf)-1] = 0;
49 for (a=0; a<strlen(buf); ++a) {
50 if (buf[a]=='+') buf[a]=' ';
55 sscanf(hex,"%02x",&b);
57 strcpy(&buf[a+1],&buf[a+3]);
64 void addurls(char *url) {
71 while (strlen(up)>0) {
73 /* locate the = sign */
76 for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
80 u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
83 strcpy(u->url_key, buf);
85 /* now chop that part off */
86 for (a=0; a<=b; ++a) ++up;
88 /* locate the & sign */
91 for (a=0; a<strlen(up); ++a) {
92 if (!strncmp(ptr,"&",1)) {
99 for (a=0; a<b; ++a) ++ptr;
102 u->url_data = malloc(strlen(up));
103 strcpy(u->url_data, up);
104 unescape_input(u->url_data);
111 void free_urls(void) {
112 struct urlcontent *u;
114 while (urlstrings != NULL) {
115 free(urlstrings->url_data);
116 u = urlstrings->next;
123 * Diagnostic function to display the contents of all variables
126 struct urlcontent *u;
128 for (u = urlstrings; u != NULL; u = u->next) {
129 wprintf("%38s = %s\n", u->url_key, u->url_data);
133 char *bstr(char *key) {
134 struct urlcontent *u;
136 for (u = urlstrings; u != NULL; u = u->next) {
137 if (!strcasecmp(u->url_key, key)) return(u->url_data);
143 void wprintf(const char *format, ...) {
145 struct webcontent *wptr;
147 wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
158 va_start(arg_ptr, format);
159 vsprintf(wptr->w_data, format, arg_ptr);
164 int wContentLength(void) {
165 struct webcontent *wptr;
168 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
169 len = len + strlen(wptr->w_data);
175 void wDumpContent(void) {
176 struct webcontent *wptr;
178 printf("Content-type: text/html\n");
179 printf("Content-length: %d\n", wContentLength());
182 while (wlist != NULL) {
183 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
192 void escputs1(char *strbuf, int nbsp)
196 for (a=0; a<strlen(strbuf); ++a) {
197 if (strbuf[a]=='<') wprintf("<");
198 else if (strbuf[a]=='>') wprintf(">");
199 else if (strbuf[a]=='&') wprintf("&");
200 else if (strbuf[a]==34) wprintf(""");
201 else if (strbuf[a]==LB) wprintf("<");
202 else if (strbuf[a]==RB) wprintf(">");
203 else if (strbuf[a]==QU) wprintf("\"");
204 else if ((strbuf[a]==32)&&(nbsp==1)) {
208 wprintf("%c", strbuf[a]);
213 void escputs(char *strbuf)
220 char *urlesc(char *strbuf)
223 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
224 static char outbuf[512];
228 for (a=0; a<strlen(strbuf); ++a) {
230 for (b=0; b<strlen(ec); ++b) {
231 if (strbuf[a]==ec[b]) c=1;
234 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
235 else sprintf(&outbuf[b],"%c",strbuf[a]);
240 void urlescputs(char *strbuf)
242 wprintf("%s",urlesc(strbuf));
247 * Look for URL's embedded in a buffer and make them linkable. We use a
248 * target window in order to keep the BBS session in its own window.
263 for (pos=0; pos<strlen(buf); ++pos) {
264 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
265 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
270 if ((start>0)&&(buf[start-1]=='<')) ench = '>';
271 if ((start>0)&&(buf[start-1]=='[')) ench = ']';
272 if ((start>0)&&(buf[start-1]=='(')) ench = ')';
273 if ((start>0)&&(buf[start-1]=='{')) ench = '}';
275 for (pos=strlen(buf); pos>start; --pos) {
276 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
279 strncpy(urlbuf,&buf[start],end-start);
280 urlbuf[end-start] = 0;
283 strncpy(outbuf,buf,start);
284 sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
285 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
286 strcat(outbuf,&buf[end]);
293 void getz(char *buf) {
294 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
296 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
297 buf[strlen(buf)-1] = 0;
302 * Output all that important stuff that the browser will want to see
304 void output_headers(void) {
306 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
308 printf("Server: %s\n", SERVER);
309 printf("Connection: close\n");
310 printf("Set-cookie: wc_session=%d\n", wc_session);
312 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
313 else printf("Set-cookie: wc_host=%s\n", unset);
315 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
316 else printf("Set-cookie: wc_port=%s\n", unset);
318 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
320 else printf("Set-cookie: wc_username=%s\n", unset);
322 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
324 else printf("Set-cookie: wc_password=%s\n", unset);
326 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
328 else printf("Set-cookie: wc_roomname=%s\n", unset);
331 void output_static(char *what) {
337 fprintf(stderr, "output_static(%s)\n", what);
338 sprintf(buf, "static/%s", what);
339 fp = fopen(buf, "rb");
341 printf("HTTP/1.0 404 %s\n", strerror(errno));
343 printf("Content-Type: text/plain\n");
344 sprintf(buf, "%s: %s\n", what, strerror(errno));
345 printf("Content-length: %d\n", strlen(buf));
347 fwrite(buf, strlen(buf), 1, stdout);
350 fprintf(stderr, "opened it ok\n");
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 static const char *defaulthost = DEFAULT_HOST;
374 static const char *defaultport = DEFAULT_PORT;
377 void extract_action(char *actbuf, char *cmdbuf) {
380 strcpy(actbuf, cmdbuf);
381 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
382 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
383 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
385 for (i=0; i<strlen(actbuf); ++i) {
386 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
387 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
388 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
389 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
394 void session_loop(void) {
399 int ContentLength = 0;
402 /* We stuff these with the values coming from the client cookies,
403 * so we can use them to reconnect a timed out session if we have to.
407 char c_username[256];
408 char c_password[256];
409 char c_roomname[256];
411 strcpy(c_host, defaulthost);
412 strcpy(c_port, defaultport);
413 strcpy(c_username, "");
414 strcpy(c_password, "");
415 strcpy(c_roomname, "");
418 extract_action(action, cmd);
423 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
424 strcpy(c_host, &buf[16]);
425 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
426 strcpy(c_port, &buf[16]);
427 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
428 strcpy(c_username, &buf[20]);
429 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
430 strcpy(c_password, &buf[20]);
431 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
432 strcpy(c_roomname, &buf[20]);
434 if (!strncasecmp(buf, "Content-length: ", 16)) {
435 ContentLength = atoi(&buf[16]);
438 } while(strlen(buf)>0);
442 if (ContentLength > 0) {
443 content = malloc(ContentLength+1);
444 fread(content, ContentLength, 1, stdin);
445 content[ContentLength] = 0;
453 * If we're not connected to a Citadel server, try to hook up the
454 * connection now. Preference is given to the host and port specified
455 * by browser cookies, if cookies have been supplied.
458 serv_sock = connectsock(c_host, c_port, "tcp");
460 serv_gets(buf); /* get the server welcome message */
461 strcpy(wc_host, c_host);
462 strcpy(wc_port, c_port);
468 * If we're not logged in, but we have username and password cookies
469 * supplied by the browser, try using them to log in.
471 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
472 serv_printf("USER %s", c_username);
475 serv_printf("PASS %s", c_password);
478 become_logged_in(c_username, c_password, buf);
484 * If we don't have a current room, but a cookie specifying the
485 * current room is supplied, make an effort to go there.
487 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
488 serv_printf("GOTO %s", c_roomname);
491 strcpy(wc_roomname, c_roomname);
495 /* If there are variables in the URL, we must grab them now */
496 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
497 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
502 if (!strcasecmp(action, "static")) {
503 strcpy(buf, &cmd[12]);
504 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
508 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
512 else if (!logged_in) {
516 /* Various commands... */
518 else if (!strcasecmp(action, "do_welcome")) {
522 else if (!strcasecmp(action, "display_main_menu")) {
526 else if (!strcasecmp(action, "advanced")) {
527 display_advanced_menu();
530 else if (!strcasecmp(action, "whobbs")) {
534 else if (!strcasecmp(action, "knrooms")) {
535 list_all_rooms_by_floor();
538 else if (!strcasecmp(action, "gotonext")) {
543 else if (!strcasecmp(action, "skip")) {
547 else if (!strcasecmp(action, "ungoto")) {
551 else if (!strcasecmp(action, "dotgoto")) {
556 else if (!strcasecmp(action, "termquit")) {
560 /* When all else fails... */
562 printf("HTTP/1.0 200 OK\n");
565 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
566 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
567 wprintf("You're in session %d<HR>\n", wc_session);
568 wprintf("Command: <BR><PRE>\n");
570 wprintf("</PRE><HR>\n");
571 wprintf("Variables: <BR><PRE>\n");
573 wprintf("</PRE><HR>\n");
574 wprintf("</BODY></HTML>\n");
579 if (content != NULL) {
586 int main(int argc, char *argv[]) {
588 if (argc < 2 || argc > 4) {
590 "webcit: usage: webcit <session_id> [host [port]]\n");
594 wc_session = atoi(argv[1]);
597 defaulthost = argv[2];
599 defaultport = argv[3];
604 strcpy(wc_username, "");
605 strcpy(wc_password, "");
606 strcpy(wc_roomname, "");