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
125 void dump_vars(void) {
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);
163 int wContentLength(void) {
164 struct webcontent *wptr;
167 for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
168 len = len + strlen(wptr->w_data);
174 void wDumpContent(void) {
175 struct webcontent *wptr;
177 printf("Content-type: text/html\n");
178 printf("Content-length: %d\n", wContentLength());
181 while (wlist != NULL) {
182 fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
191 void escputs1(char *strbuf, int nbsp)
195 for (a=0; a<strlen(strbuf); ++a) {
196 if (strbuf[a]=='<') wprintf("<");
197 else if (strbuf[a]=='>') wprintf(">");
198 else if (strbuf[a]=='&') wprintf("&");
199 else if (strbuf[a]==34) wprintf(""");
200 else if (strbuf[a]==LB) wprintf("<");
201 else if (strbuf[a]==RB) wprintf(">");
202 else if (strbuf[a]==QU) wprintf("\"");
203 else if ((strbuf[a]==32)&&(nbsp==1)) {
207 wprintf("%c", strbuf[a]);
212 void escputs(char *strbuf)
219 char *urlesc(char *strbuf)
222 char *ec = " #&;`'|*?-~<>^()[]{}$\\";
223 static char outbuf[512];
227 for (a=0; a<strlen(strbuf); ++a) {
229 for (b=0; b<strlen(ec); ++b) {
230 if (strbuf[a]==ec[b]) c=1;
233 if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
234 else sprintf(&outbuf[b],"%c",strbuf[a]);
239 void urlescputs(char *strbuf)
241 wprintf("%s",urlesc(strbuf));
246 * Look for URL's embedded in a buffer and make them linkable. We use a
247 * target window in order to keep the BBS session in its own window.
262 for (pos=0; pos<strlen(buf); ++pos) {
263 if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
264 if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
269 if ((start>0)&&(buf[start-1]=='<')) ench = '>';
270 if ((start>0)&&(buf[start-1]=='[')) ench = ']';
271 if ((start>0)&&(buf[start-1]=='(')) ench = ')';
272 if ((start>0)&&(buf[start-1]=='{')) ench = '}';
274 for (pos=strlen(buf); pos>start; --pos) {
275 if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
278 strncpy(urlbuf,&buf[start],end-start);
279 urlbuf[end-start] = 0;
282 strncpy(outbuf,buf,start);
283 sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c",
284 LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
285 strcat(outbuf,&buf[end]);
292 void getz(char *buf) {
293 if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
295 while ((strlen(buf)>0)&&(!isprint(buf[strlen(buf)-1])))
296 buf[strlen(buf)-1] = 0;
301 * Output all that important stuff that the browser will want to see
303 void output_headers(void) {
305 static char *unset = "; expires=28-May-1971 18:10:00 GMT";
307 printf("Server: %s\n", SERVER);
308 printf("Connection: close\n");
309 printf("Set-cookie: wc_session=%d\n", wc_session);
311 if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
312 else printf("Set-cookie: wc_host=%s\n", unset);
314 if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
315 else printf("Set-cookie: wc_port=%s\n", unset);
317 if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
319 else printf("Set-cookie: wc_username=%s\n", unset);
321 if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
323 else printf("Set-cookie: wc_password=%s\n", unset);
325 if (strlen(wc_roomname)>0) printf("Set-cookie: wc_roomname=%s\n",
327 else printf("Set-cookie: wc_roomname=%s\n", unset);
330 void output_static(char *what) {
336 fprintf(stderr, "output_static(%s)\n", what);
337 sprintf(buf, "static/%s", what);
338 fp = fopen(buf, "rb");
340 printf("HTTP/1.0 404 %s\n", strerror(errno));
342 printf("Content-Type: text/plain\n");
343 sprintf(buf, "%s: %s\n", what, strerror(errno));
344 printf("Content-length: %d\n", strlen(buf));
346 fwrite(buf, strlen(buf), 1, stdout);
349 fprintf(stderr, "opened it ok\n");
350 printf("HTTP/1.0 200 OK\n");
353 if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
354 printf("Content-type: image/gif\n");
355 else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
356 printf("Content-type: text/html\n");
358 printf("Content-type: application/octet-stream\n");
360 fstat(fileno(fp), &statbuf);
361 bytes = statbuf.st_size;
362 printf("Content-length: %ld\n", (long)bytes);
365 putc(getc(fp), stdout);
372 static const char *defaulthost = DEFAULT_HOST;
373 static const char *defaultport = DEFAULT_PORT;
376 void extract_action(char *actbuf, char *cmdbuf) {
379 strcpy(actbuf, cmdbuf);
380 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
381 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
382 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
384 for (i=0; i<strlen(actbuf); ++i) {
385 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
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; }
393 void session_loop(void) {
398 int ContentLength = 0;
401 /* We stuff these with the values coming from the client cookies,
402 * so we can use them to reconnect a timed out session if we have to.
406 char c_username[256];
407 char c_password[256];
408 char c_roomname[256];
410 strcpy(c_host, defaulthost);
411 strcpy(c_port, defaultport);
412 strcpy(c_username, "");
413 strcpy(c_password, "");
414 strcpy(c_roomname, "");
417 extract_action(action, cmd);
422 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
423 strcpy(c_host, &buf[16]);
424 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
425 strcpy(c_port, &buf[16]);
426 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
427 strcpy(c_username, &buf[20]);
428 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
429 strcpy(c_password, &buf[20]);
430 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
431 strcpy(c_roomname, &buf[20]);
433 if (!strncasecmp(buf, "Content-length: ", 16)) {
434 ContentLength = atoi(&buf[16]);
437 } while(strlen(buf)>0);
441 if (ContentLength > 0) {
442 content = malloc(ContentLength+1);
443 fread(content, ContentLength, 1, stdin);
444 content[ContentLength] = 0;
452 * If we're not connected to a Citadel server, try to hook up the
453 * connection now. Preference is given to the host and port specified
454 * by browser cookies, if cookies have been supplied.
457 serv_sock = connectsock(c_host, c_port, "tcp");
459 serv_gets(buf); /* get the server welcome message */
460 strcpy(wc_host, c_host);
461 strcpy(wc_port, c_port);
467 * If we're not logged in, but we have username and password cookies
468 * supplied by the browser, try using them to log in.
470 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
471 serv_printf("USER %s", c_username);
474 serv_printf("PASS %s", c_password);
477 become_logged_in(c_username, c_password, buf);
483 * If we don't have a current room, but a cookie specifying the
484 * current room is supplied, make an effort to go there.
486 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
487 serv_printf("GOTO %s", c_roomname);
490 strcpy(wc_roomname, c_roomname);
494 /* If there are variables in the URL, we must grab them now */
495 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
496 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
501 if (!strcasecmp(action, "static")) {
502 strcpy(buf, &cmd[12]);
503 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
507 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
511 else if (!logged_in) {
515 /* Various commands... */
517 else if (!strcasecmp(action, "do_welcome")) {
521 else if (!strcasecmp(action, "display_main_menu")) {
525 else if (!strcasecmp(action, "advanced")) {
526 display_advanced_menu();
529 else if (!strcasecmp(action, "whobbs")) {
533 else if (!strcasecmp(action, "knrooms")) {
534 list_all_rooms_by_floor();
537 else if (!strcasecmp(action, "gotonext")) {
542 else if (!strcasecmp(action, "skip")) {
546 else if (!strcasecmp(action, "ungoto")) {
550 else if (!strcasecmp(action, "dotgoto")) {
555 else if (!strcasecmp(action, "termquit")) {
559 /* When all else fails... */
561 printf("HTTP/1.0 200 OK\n");
564 wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
565 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
566 wprintf("You're in session %d<HR>\n", wc_session);
567 wprintf("Command: <BR><PRE>\n");
569 wprintf("</PRE><HR>\n");
570 wprintf("Variables: <BR><PRE>\n");
572 wprintf("</PRE><HR>\n");
573 wprintf("</BODY></HTML>\n");
578 if (content != NULL) {
585 int main(int argc, char *argv[]) {
587 if (argc < 2 || argc > 4) {
589 "webcit: usage: webcit <session_id> [host [port]]\n");
593 wc_session = atoi(argv[1]);
596 defaulthost = argv[2];
598 defaultport = argv[3];
603 strcpy(wc_username, "");
604 strcpy(wc_password, "");
605 strcpy(wc_roomname, "");