]> code.citadel.org Git - citadel.git/blobdiff - webcit/webcit.c
* converted to autoconf and began port to Digital UNIX
[citadel.git] / webcit / webcit.c
index 3a914ce094ffde31c2a83259c454379cbd15d234..bedccdef5850e0df48582adadd554ddb63602dc4 100644 (file)
@@ -4,6 +4,8 @@
  * This is the actual program called by the webserver.  It maintains a
  * persistent session to the Citadel server, handling HTTP WebCit requests as
  * they arrive and presenting a user interface.
+ *
+ * $Id$
  */
 
 #include <stdlib.h>
 #include <ctype.h>
 #include <string.h>
 #include <errno.h>
+#include <sys/stat.h>
+#include <stdarg.h>
+#include "webcit.h"
+#include "child.h"
 
 int wc_session;
 char wc_host[256];
-int wc_port;
+char wc_port[256];
 char wc_username[256];
 char wc_password[256];
 char wc_roomname[256];
+int TransactionCount = 0;
+int connected = 0;
+int logged_in = 0;
+int axlevel;
+
+struct webcontent *wlist = NULL;
+struct webcontent *wlast = NULL;
+
+struct urlcontent *urlstrings = NULL;
+
+
+void unescape_input(char *buf)
+{
+       int a,b;
+       char hex[3];
+
+       while ((isspace(buf[strlen(buf)-1]))&&(strlen(buf)>0))
+               buf[strlen(buf)-1] = 0;
+
+       for (a=0; a<strlen(buf); ++a) {
+               if (buf[a]=='+') buf[a]=' ';    
+               if (buf[a]=='%') {
+                       hex[0]=buf[a+1];
+                       hex[1]=buf[a+2];
+                       hex[2]=0;
+                       sscanf(hex,"%02x",&b);
+                       buf[a] = (char) b;
+                       strcpy(&buf[a+1],&buf[a+3]);
+                       }
+               }
+
+       }
+
+
+void addurls(char *url) {
+       char *up, *ptr;
+       char buf[256];
+       int a,b;
+       struct urlcontent *u;
+
+       up = url;
+       while (strlen(up)>0) {
+               
+               /* locate the = sign */
+               strncpy(buf,up,255);
+               b = (-1);
+               for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
+               if (b<0) return;
+               buf[b]=0;
+       
+               u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
+               u->next = urlstrings;
+               urlstrings = u;
+               strcpy(u->url_key, buf);
+       
+               /* now chop that part off */
+               for (a=0; a<=b; ++a) ++up;
+       
+               /* locate the & sign */
+               ptr = up;
+               b = strlen(up);
+               for (a=0; a<strlen(up); ++a) {
+                       if (!strncmp(ptr,"&",1)) {
+                               b=a;
+                               break;
+                               }
+                       ++ptr;
+                       }
+               ptr = up;
+               for (a=0; a<b; ++a) ++ptr;
+               strcpy(ptr,"");
+               
+               u->url_data = malloc(strlen(up));
+               strcpy(u->url_data, up);
+               unescape_input(u->url_data);
+
+               up = ptr;
+               ++up;
+               }
+       }
+
+void free_urls(void) {
+       struct urlcontent *u;
+
+       while (urlstrings != NULL) {
+               free(urlstrings->url_data);
+               u = urlstrings->next;
+               free(urlstrings);
+               urlstrings = u;
+               }
+       }
+
+char *bstr(char *key) {
+       struct urlcontent *u;
+
+       for (u = urlstrings; u != NULL; u = u->next) {
+               if (!strcasecmp(u->url_key, key)) return(u->url_data);
+               }
+       return("");
+       }
+
+
+void wprintf(const char *format, ...) {   
+        va_list arg_ptr;   
+       struct webcontent *wptr;
+
+       wptr = (struct webcontent *)malloc(sizeof(struct webcontent));
+       wptr->next = NULL;
+       if (wlist == NULL) {
+               wlist = wptr;
+               wlast = wptr;
+               }
+       else {
+               wlast->next = wptr;
+               wlast = wptr;
+               }
+  
+               va_start(arg_ptr, format);   
+               vsprintf(wptr->w_data, format, arg_ptr);   
+               va_end(arg_ptr);   
+  
+       }
+
+int wContentLength(void) {
+       struct webcontent *wptr;
+       int len = 0;
+
+       for (wptr = wlist; wptr != NULL; wptr = wptr->next) {
+               len = len + strlen(wptr->w_data);
+               }
+
+       return(len);
+       }
+
+void wDumpContent(void) {
+       struct webcontent *wptr;
+
+       printf("Content-type: text/html\n");
+       printf("Content-length: %d\n", wContentLength());
+       printf("\n");
+
+       while (wlist != NULL) {
+               fwrite(wlist->w_data, strlen(wlist->w_data), 1, stdout);
+               wptr = wlist->next;
+               free(wlist);
+               wlist = wptr;
+               }
+       wlast = NULL;
+       }
+
+
+void escputs1(char *strbuf, int nbsp)
+{
+       int a;
+
+       for (a=0; a<strlen(strbuf); ++a) {
+               if (strbuf[a]=='<') wprintf("&lt;");
+               else if (strbuf[a]=='>') wprintf("&gt;");
+               else if (strbuf[a]=='&') wprintf("&amp;");
+               else if (strbuf[a]==34) wprintf("&quot;");
+               else if (strbuf[a]==LB) wprintf("<");
+               else if (strbuf[a]==RB) wprintf(">");
+               else if (strbuf[a]==QU) wprintf("\"");
+               else if ((strbuf[a]==32)&&(nbsp==1)) {
+                       wprintf("&nbsp;");
+                       }
+               else {
+                       wprintf("%c", strbuf[a]);
+                       }
+               }
+       }
+
+void escputs(char *strbuf)
+{
+       escputs1(strbuf,0);
+       }
+
+
+
+char *urlesc(char *strbuf)
+{
+       int a,b,c;
+        char *ec = " #&;`'|*?-~<>^()[]{}$\\";
+       static char outbuf[512];
+       
+       strcpy(outbuf,"");
+
+       for (a=0; a<strlen(strbuf); ++a) {
+               c = 0;
+               for (b=0; b<strlen(ec); ++b) {
+                       if (strbuf[a]==ec[b]) c=1;
+                       }
+               b = strlen(outbuf);
+               if (c==1) sprintf(&outbuf[b],"%%%02x",strbuf[a]);
+               else sprintf(&outbuf[b],"%c",strbuf[a]);
+               }
+       return(outbuf);
+       }
+
+void urlescputs(char *strbuf)
+{
+       wprintf("%s",urlesc(strbuf));
+       }
+
+
+/*
+ * Look for URL's embedded in a buffer and make them linkable.  We use a
+ * target window in order to keep the BBS session in its own window.
+ */
+void url(char *buf)
+{
+
+       int pos;
+       int start,end;
+       char ench;
+       char urlbuf[256];
+       char outbuf[256];
+
+       start = (-1);
+       end = strlen(buf);
+       ench = 0;
+
+       for (pos=0; pos<strlen(buf); ++pos) {
+               if (!strncasecmp(&buf[pos],"http://",7)) start = pos;
+               if (!strncasecmp(&buf[pos],"ftp://",6)) start = pos;
+               }
+
+       if (start<0) return;
+
+       if ((start>0)&&(buf[start-1]=='<')) ench = '>';
+       if ((start>0)&&(buf[start-1]=='[')) ench = ']';
+       if ((start>0)&&(buf[start-1]=='(')) ench = ')';
+       if ((start>0)&&(buf[start-1]=='{')) ench = '}';
+
+       for (pos=strlen(buf); pos>start; --pos) {
+               if ((buf[pos]==' ')||(buf[pos]==ench)) end = pos;
+               }
+
+       strncpy(urlbuf,&buf[start],end-start);
+       urlbuf[end-start] = 0;
+
+
+       strncpy(outbuf,buf,start);
+       sprintf(&outbuf[start],"%cA HREF=%c%s%c TARGET=%c%s%c%c%s%c/A%c", 
+               LB,QU,urlbuf,QU,QU,TARGET,QU,RB,urlbuf,LB,RB);
+       strcat(outbuf,&buf[end]);
+       strcpy(buf,outbuf);
+       }
+
+
+
 
 void getz(char *buf) {
        if (fgets(buf, 256, stdin) == NULL) strcpy(buf, "");
@@ -28,10 +285,15 @@ void getz(char *buf) {
                }
        }
 
-void output_reconnect_cookies() {
+/*
+ * Output all that important stuff that the browser will want to see
+ */
+void output_headers(void) {
+       printf("Server: %s\n", SERVER);
+       printf("Connection: close\n");
        printf("Set-cookie: wc_session=%d\n", wc_session);
        if (strlen(wc_host)>0) printf("Set-cookie: wc_host=%s\n", wc_host);
-       if (wc_port != 0) printf("Set-cookie: wc_port=%d\n", wc_port);
+       if (strlen(wc_port)>0) printf("Set-cookie: wc_port=%s\n", wc_port);
        if (strlen(wc_username)>0) printf("Set-cookie: wc_username=%s\n",
                wc_username);
        if (strlen(wc_password)>0) printf("Set-cookie: wc_password=%s\n",
@@ -43,14 +305,14 @@ void output_reconnect_cookies() {
 void output_static(char *what) {
        char buf[256];
        FILE *fp;
+       struct stat statbuf;
+       off_t bytes;
 
        sprintf(buf, "static/%s", what);
        fp = fopen(buf, "rb");
        if (fp == NULL) {
                printf("HTTP/1.0 404 %s\n", strerror(errno));
-               printf("Server: WebCit v2 (Velma)\n");
-               printf("Connection: close\n");
-               output_reconnect_cookies();
+               output_headers();
                printf("Content-Type: text/plain\n");
                sprintf(buf, "%s: %s\n", what, strerror(errno));
                printf("Content-length: %d\n", strlen(buf));
@@ -59,32 +321,134 @@ void output_static(char *what) {
                }
        else {
                printf("HTTP/1.0 200 OK\n");
-               printf("Server: WebCit v2 (Velma)\n");
-               printf("Connection: close\n");
-               output_reconnect_cookies();
-               printf("Content-Type: text/plain\n");
-               printf("Content-length: 11\n");
+               output_headers();
+
+               if (!strncasecmp(&what[strlen(what)-4], ".gif", 4))
+                       printf("Content-type: image/gif\n");
+               else if (!strncasecmp(&what[strlen(what)-5], ".html", 5))
+                       printf("Content-type: text/html\n");
+               else
+                       printf("Content-type: application/octet-stream\n");
+
+               fstat(fileno(fp), &statbuf);
+               bytes = statbuf.st_size;
+               printf("Content-length: %ld\n", (long)bytes);
                printf("\n");
-               printf("Hi from IG\n");
+               while (bytes--) {
+                       putc(getc(fp), stdout);
+                       }
                fclose(fp);
                }
        }
 
+static const char *defaulthost = DEFAULT_HOST;
+static const char *defaultport = DEFAULT_PORT;
 
-void session_loop() {
+void session_loop(void) {
        char cmd[256];
        char buf[256];
-       char content[4096];
-       static int TransactionCount = 0;
-       int a;
+       int a, b;
+       int ContentLength = 0;
+       char *content;
+
+       /* We stuff these with the values coming from the client cookies,
+        * so we can use them to reconnect a timed out session if we have to.
+        */
+       char c_host[256];
+       char c_port[256];
+       char c_username[256];
+       char c_password[256];
+       char c_roomname[256];
+
+       strcpy(c_host, defaulthost);
+       strcpy(c_port, defaultport);
+       strcpy(c_username, "");
+       strcpy(c_password, "");
+       strcpy(c_roomname, "");
 
        getz(cmd);
+
        do {
                getz(buf);
+
+               if (!strncasecmp(buf, "Cookie: wc_host=", 16))
+                       strcpy(c_host, &buf[16]);
+               if (!strncasecmp(buf, "Cookie: wc_port=", 16))
+                       strcpy(c_port, &buf[16]);
+               if (!strncasecmp(buf, "Cookie: wc_username=", 20))
+                       strcpy(c_username, &buf[20]);
+               if (!strncasecmp(buf, "Cookie: wc_password=", 20))
+                       strcpy(c_password, &buf[20]);
+               if (!strncasecmp(buf, "Cookie: wc_roomname=", 20))
+                       strcpy(c_roomname, &buf[20]);
+
+               if (!strncasecmp(buf, "Content-length: ", 16)) {
+                       ContentLength = atoi(&buf[16]);
+                       }
+
                } while(strlen(buf)>0);
 
-       fprintf(stderr, "Command: %s\n", cmd);
-       fflush(stderr);
+       ++TransactionCount;
+
+       if (ContentLength > 0) {
+               content = malloc(ContentLength+1);
+               fread(content, ContentLength, 1, stdin);
+               content[ContentLength] = 0;
+               addurls(content);
+               }
+       else {
+               content = NULL;
+               }
+
+       /*
+        * If we're not connected to a Citadel server, try to hook up the
+        * connection now.  Preference is given to the host and port specified
+        * by browser cookies, if cookies have been supplied.
+        */
+       if (!connected) {
+               serv_sock = connectsock(c_host, c_port, "tcp");
+               connected = 1;
+               serv_gets(buf); /* get the server welcome message */
+               strcpy(wc_host, c_host);
+               strcpy(wc_port, c_port);
+               get_serv_info();
+               }
+
+
+       /*
+        * If we're not logged in, but we have username and password cookies
+        * supplied by the browser, try using them to log in.
+        */
+       if ((!logged_in)&&(strlen(c_username)>0)&&(strlen(c_password)>0)) {
+               serv_printf("USER %s", c_username);
+               serv_gets(buf);
+               if (buf[0]=='3') {
+                       serv_printf("PASS %s", c_password);
+                       serv_gets(buf);
+                       if (buf[0]=='2') {
+                               become_logged_in(c_username, c_password, buf);
+                               }
+                       }
+               }
+
+       /*
+        * If we don't have a current room, but a cookie specifying the
+        * current room is supplied, make an effort to go there.
+        */
+       if ((strlen(wc_roomname)==0) && (strlen(c_roomname)>0) ) {
+               serv_printf("GOTO %s", c_roomname);
+               serv_gets(buf);
+               if (buf[0]=='2') {
+                       strcpy(wc_roomname, c_roomname);
+                       }
+               }
+
+       /* If there are variables in the URL, we must grab them now */  
+       for (a=0; a<strlen(cmd); ++a) if (cmd[a]=='?') {
+               for (b=a; b<strlen(cmd); ++b) if (isspace(cmd[b])) cmd[b]=0;
+               addurls(&cmd[a+1]);
+               cmd[a] = 0;
+               }
 
        if (!strncasecmp(cmd, "GET /static/", 12)) {
                strcpy(buf, &cmd[12]);
@@ -92,44 +456,97 @@ void session_loop() {
                output_static(buf);
                }
 
+
+       else if ((!logged_in)&&(!strncasecmp(cmd, "POST /login", 11))) {
+               do_login();
+               }
+
+       else if (!logged_in) {
+               display_login();
+               }
+
+       /* Various commands... */
+       
+       else if (!strncasecmp(cmd, "GET /do_welcome", 15)) {
+               do_welcome();
+               }
+
+       else if (!strncasecmp(cmd, "GET /display_main_menu", 22)) {
+               display_main_menu();
+               }
+
+       else if (!strncasecmp(cmd, "GET /advanced", 13)) {
+               display_advanced_menu();
+               }
+
+       else if (!strncasecmp(cmd, "GET /whobbs", 11)) {
+               whobbs();
+               }
+
+       else if (!strncasecmp(cmd, "GET /knrooms", 12)) {
+               list_all_rooms_by_floor();
+               }
+
+       else if (!strncasecmp(cmd, "GET /gotonext", 13)) {
+               slrp_highest();
+               gotonext();
+               }
+
+       else if (!strncasecmp(cmd, "GET /skip", 9)) {
+               gotonext();
+               }
+
+       else if (!strncasecmp(cmd, "GET /ungoto", 11)) {
+               ungoto();
+               }
+
+       else if (!strncasecmp(cmd, "GET /dotgoto", 12)) {
+               slrp_highest();
+               dotgoto();
+               }
+
+       else if (!strncasecmp(cmd, "GET /termquit", 13)) {
+               do_logout();
+               }
+
+       /* When all else fails... */
        else {
                printf("HTTP/1.0 200 OK\n");
-               printf("Server: WebCit v2 (Velma)\n");
-               printf("Connection: close\n");
-               output_reconnect_cookies();
-               printf("Content-Type: text/html\n");
-       
-               strcpy(content, "");
-       
-               sprintf(&content[strlen(content)],
-                       "<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
-               sprintf(&content[strlen(content)],
-                       "TransactionCount is %d<HR>\n", ++TransactionCount);
-               sprintf(&content[strlen(content)],
-                       "You're in session %d<BR>\n", wc_session);
-               sprintf(&content[strlen(content)],
-                       "</BODY></HTML>\n");
+               output_headers();
        
-               printf("Content-length: %d\n", strlen(content));
-               printf("\n");
-               fwrite(content, strlen(content), 1, stdout);
+               wprintf("<HTML><HEAD><TITLE>WebCit</TITLE></HEAD><BODY>\n");
+               wprintf("TransactionCount is %d<HR>\n", TransactionCount);
+               wprintf("You're in session %d<BR>\n", wc_session);
+               wprintf("</BODY></HTML>\n");
+               wDumpContent();
                }
 
        fflush(stdout);
+       if (content != NULL) {
+               free(content);
+               content = NULL;
+               }
+       free_urls();
        }
 
-
-
 int main(int argc, char *argv[]) {
 
-       if (argc != 2) {
-               printf("%s: usage: %s <session_id>\n", argv[0], argv[0]);
-               exit(1);
+       if (argc < 2 || argc > 4) {
+               fprintf(stderr,
+                       "webcit: usage: webcit <session_id> [host [port]]\n");
+               return 1;
                }
 
        wc_session = atoi(argv[1]);
+
+       if (argc > 2) {
+               defaulthost = argv[2];
+               if (argc > 3)
+                       defaultport = argv[3];
+               }
+
        strcpy(wc_host, "");
-       wc_port = 0;
+       strcpy(wc_port, "");
        strcpy(wc_username, "");
        strcpy(wc_password, "");
        strcpy(wc_roomname, "");
@@ -137,6 +554,4 @@ int main(int argc, char *argv[]) {
        while (1) {
                session_loop();
                }
-
-       exit(0);
        }