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)-5], ".html", 5))
360 printf("Content-type: text/html\n");
362 printf("Content-type: application/octet-stream\n");
364 fstat(fileno(fp), &statbuf);
365 bytes = statbuf.st_size;
366 printf("Content-length: %ld\n", (long)bytes);
369 putc(getc(fp), stdout);
376 void output_image() {
381 off_t accomplished = 0L;
384 serv_printf("OIMG %s|%s", bstr("name"), bstr("parm"));
387 bytes = extract_long(&buf[4], 0);
388 printf("HTTP/1.0 200 OK\n");
390 printf("Content-type: image/gif\n");
391 printf("Content-length: %ld\n", bytes);
394 while (bytes > (off_t)0) {
395 thisblock = (off_t)sizeof(xferbuf);
396 if (thisblock > bytes) thisblock = bytes;
397 serv_printf("READ %ld|%ld", accomplished, thisblock);
399 if (buf[0]=='6') thisblock = extract_long(&buf[4],0);
400 serv_read(xferbuf, (int)thisblock);
401 fwrite(xferbuf, thisblock, 1, stdout);
402 bytes = bytes - thisblock;
403 accomplished = accomplished + thisblock;
410 printf("HTTP/1.0 404 %s\n", strerror(errno));
412 printf("Content-Type: text/plain\n");
413 sprintf(buf, "Error retrieving image\n");
414 printf("Content-length: %d\n", strlen(buf));
416 fwrite(buf, strlen(buf), 1, stdout);
421 void extract_action(char *actbuf, char *cmdbuf) {
424 strcpy(actbuf, cmdbuf);
425 if (!strncasecmp(actbuf, "GET /", 5)) strcpy(actbuf, &actbuf[5]);
426 if (!strncasecmp(actbuf, "PUT /", 5)) strcpy(actbuf, &actbuf[5]);
427 if (!strncasecmp(actbuf, "POST /", 6)) strcpy(actbuf, &actbuf[6]);
429 for (i=0; i<strlen(actbuf); ++i) {
430 if (actbuf[i]==' ') { actbuf[i]=0; i=0; }
431 if (actbuf[i]=='/') { actbuf[i]=0; i=0; }
432 if (actbuf[i]=='?') { actbuf[i]=0; i=0; }
433 if (actbuf[i]=='&') { actbuf[i]=0; i=0; }
438 void session_loop(void) {
443 int ContentLength = 0;
447 /* We stuff these with the values coming from the client cookies,
448 * so we can use them to reconnect a timed out session if we have to.
452 char c_username[256];
453 char c_password[256];
454 char c_roomname[256];
456 strcpy(c_host, defaulthost);
457 strcpy(c_port, defaultport);
458 strcpy(c_username, "");
459 strcpy(c_password, "");
460 strcpy(c_roomname, "");
463 extract_action(action, cmd);
468 if (!strncasecmp(buf, "Cookie: wc_host=", 16))
469 strcpy(c_host, &buf[16]);
470 if (!strncasecmp(buf, "Cookie: wc_port=", 16))
471 strcpy(c_port, &buf[16]);
472 if (!strncasecmp(buf, "Cookie: wc_username=", 20))
473 strcpy(c_username, &buf[20]);
474 if (!strncasecmp(buf, "Cookie: wc_password=", 20))
475 strcpy(c_password, &buf[20]);
476 if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
477 strcpy(c_roomname, &buf[20]);
479 if (!strncasecmp(buf, "Content-length: ", 16)) {
480 ContentLength = atoi(&buf[16]);
483 } while(strlen(buf)>0);
487 if (ContentLength > 0) {
488 content = malloc(ContentLength+1);
489 fread(content, ContentLength, 1, stdin);
490 fp = fopen("content", "wb");
491 fwrite(content, ContentLength, 1, fp);
493 content[ContentLength] = 0;
501 * If we're not connected to a Citadel server, try to hook up the
502 * connection now. Preference is given to the host and port specified
503 * by browser cookies, if cookies have been supplied.
506 serv_sock = connectsock(c_host, c_port, "tcp");
508 serv_gets(buf); /* get the server welcome message */
509 strcpy(wc_host, c_host);
510 strcpy(wc_port, c_port);
514 check_for_express_messages();
517 * If we're not logged in, but we have username and password cookies
518 * supplied by the browser, try using them to log in.
520 if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
521 serv_printf("USER %s", c_username);
524 serv_printf("PASS %s", c_password);
527 become_logged_in(c_username, c_password, buf);
533 * If we don't have a current room, but a cookie specifying the
534 * current room is supplied, make an effort to go there.
536 if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
537 serv_printf("GOTO %s", c_roomname);
540 strcpy(wc_roomname, c_roomname);
544 /* If there are variables in the URL, we must grab them now */
545 for (a=0; a<strlen(cmd); ++a) if ((cmd[a]=='?')||(cmd[a]=='&')) {
546 for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
551 if (!strcasecmp(action, "static")) {
552 strcpy(buf, &cmd[12]);
553 for (a=0; a<strlen(buf); ++a) if (isspace(buf[a])) buf[a]=0;
557 else if (!strcasecmp(action, "image")) {
561 else if ((!logged_in)&&(!strcasecmp(action, "login"))) {
565 else if (!logged_in) {
569 /* Various commands... */
571 else if (!strcasecmp(action, "do_welcome")) {
575 else if (!strcasecmp(action, "display_main_menu")) {
579 else if (!strcasecmp(action, "advanced")) {
580 display_advanced_menu();
583 else if (!strcasecmp(action, "whobbs")) {
587 else if (!strcasecmp(action, "knrooms")) {
588 list_all_rooms_by_floor();
591 else if (!strcasecmp(action, "gotonext")) {
596 else if (!strcasecmp(action, "skip")) {
600 else if (!strcasecmp(action, "ungoto")) {
604 else if (!strcasecmp(action, "dotgoto")) {
609 else if (!strcasecmp(action, "termquit")) {
613 else if (!strcasecmp(action, "readnew")) {
617 else if (!strcasecmp(action, "readold")) {
621 else if (!strcasecmp(action, "readfwd")) {
625 else if (!strcasecmp(action, "display_enter")) {
629 else if (!strcasecmp(action, "post")) {
633 else if (!strcasecmp(action, "confirm_delete_msg")) {
634 confirm_delete_msg();
637 else if (!strcasecmp(action, "delete_msg")) {
641 else if (!strcasecmp(action, "confirm_move_msg")) {
645 else if (!strcasecmp(action, "move_msg")) {
649 else if (!strcasecmp(action, "userlist")) {
653 else if (!strcasecmp(action, "showuser")) {
657 /* When all else fails... */
659 printf("HTTP/1.0 200 OK\n");
662 wprintf("TransactionCount is %d<BR>\n", TransactionCount);
663 wprintf("You're in session %d<HR>\n", wc_session);
664 wprintf("Command: <BR><PRE>\n");
666 wprintf("</PRE><HR>\n");
667 wprintf("Variables: <BR><PRE>\n");
669 wprintf("</PRE><HR>\n");
670 wprintf("</BODY></HTML>\n");
675 if (content != NULL) {
682 int main(int argc, char *argv[]) {
684 if (argc < 2 || argc > 4) {
686 "webcit: usage: webcit <session_id> [host [port]]\n");
690 wc_session = atoi(argv[1]);
693 defaulthost = argv[2];
695 defaultport = argv[3];
700 strcpy(wc_username, "");
701 strcpy(wc_password, "");
702 strcpy(wc_roomname, "");