]> 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 16b0df7ec1b9f5ec45b70c8a525fa74ed12c8386..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>
@@ -15,6 +17,7 @@
 #include <sys/stat.h>
 #include <stdarg.h>
 #include "webcit.h"
+#include "child.h"
 
 int wc_session;
 char wc_host[256];
@@ -25,6 +28,7 @@ char wc_roomname[256];
 int TransactionCount = 0;
 int connected = 0;
 int logged_in = 0;
+int axlevel;
 
 struct webcontent *wlist = NULL;
 struct webcontent *wlast = NULL;
@@ -32,8 +36,8 @@ struct webcontent *wlast = NULL;
 struct urlcontent *urlstrings = NULL;
 
 
-void unescape_input(buf)
-char buf[]; {
+void unescape_input(char *buf)
+{
        int a,b;
        char hex[3];
 
@@ -68,7 +72,7 @@ void addurls(char *url) {
                strncpy(buf,up,255);
                b = (-1);
                for (a=255; a>=0; --a) if (buf[a]=='=') b=a;
-               if (b<0) goto DONE;
+               if (b<0) return;
                buf[b]=0;
        
                u = (struct urlcontent *)malloc(sizeof(struct urlcontent));
@@ -85,11 +89,11 @@ void addurls(char *url) {
                for (a=0; a<strlen(up); ++a) {
                        if (!strncmp(ptr,"&",1)) {
                                b=a;
-                               goto FOUNDIT;
+                               break;
                                }
                        ++ptr;
                        }
-FOUNDIT:       ptr = up;
+               ptr = up;
                for (a=0; a<b; ++a) ++ptr;
                strcpy(ptr,"");
                
@@ -99,13 +103,10 @@ FOUNDIT:   ptr = up;
 
                up = ptr;
                ++up;
-
-               fprintf(stderr, "%s=%s\n", u->url_key, u->url_data);
                }
-DONE:
        }
 
-void free_urls() {
+void free_urls(void) {
        struct urlcontent *u;
 
        while (urlstrings != NULL) {
@@ -147,7 +148,7 @@ void wprintf(const char *format, ...) {
   
        }
 
-int wContentLength() {
+int wContentLength(void) {
        struct webcontent *wptr;
        int len = 0;
 
@@ -158,7 +159,7 @@ int wContentLength() {
        return(len);
        }
 
-void wDumpContent() {
+void wDumpContent(void) {
        struct webcontent *wptr;
 
        printf("Content-type: text/html\n");
@@ -174,6 +175,108 @@ void wDumpContent() {
        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, "");
        else {
@@ -185,7 +288,7 @@ void getz(char *buf) {
 /*
  * Output all that important stuff that the browser will want to see
  */
-void output_headers() {
+void output_headers(void) {
        printf("Server: %s\n", SERVER);
        printf("Connection: close\n");
        printf("Set-cookie: wc_session=%d\n", wc_session);
@@ -222,12 +325,14 @@ void output_static(char *what) {
 
                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: %d\n", bytes);
+               printf("Content-length: %ld\n", (long)bytes);
                printf("\n");
                while (bytes--) {
                        putc(getc(fp), stdout);
@@ -236,8 +341,10 @@ void output_static(char *what) {
                }
        }
 
+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];
        int a, b;
@@ -253,20 +360,16 @@ void session_loop() {
        char c_password[256];
        char c_roomname[256];
 
-       strcpy(c_host, DEFAULT_HOST);
-       strcpy(c_port, DEFAULT_PORT);
+       strcpy(c_host, defaulthost);
+       strcpy(c_port, defaultport);
        strcpy(c_username, "");
        strcpy(c_password, "");
        strcpy(c_roomname, "");
 
        getz(cmd);
-       fprintf(stderr, "\nCmd: %s\n", cmd);
-       fflush(stderr);
 
        do {
                getz(buf);
-               fprintf(stderr, "Buf: %s\n", buf);
-               fflush(stderr);
 
                if (!strncasecmp(buf, "Cookie: wc_host=", 16))
                        strcpy(c_host, &buf[16]);
@@ -291,7 +394,6 @@ void session_loop() {
                content = malloc(ContentLength+1);
                fread(content, ContentLength, 1, stdin);
                content[ContentLength] = 0;
-               fprintf(stderr, "CONTENT:\n%s\n", content);
                addurls(content);
                }
        else {
@@ -309,15 +411,7 @@ void session_loop() {
                serv_gets(buf); /* get the server welcome message */
                strcpy(wc_host, c_host);
                strcpy(wc_port, c_port);
-               serv_printf("IDEN %d|%d|%d|%s|%s",
-                       DEVELOPER_ID,
-                       CLIENT_ID,
-                       CLIENT_VERSION,
-                       SERVER,
-                       ""
-                       );
-               serv_gets(buf);
-               /* FIX find out where the user is */
+               get_serv_info();
                }
 
 
@@ -332,9 +426,7 @@ void session_loop() {
                        serv_printf("PASS %s", c_password);
                        serv_gets(buf);
                        if (buf[0]=='2') {
-                               logged_in = 1;
-                               strcpy(wc_username, c_username);
-                               strcpy(wc_password, c_password);
+                               become_logged_in(c_username, c_password, buf);
                                }
                        }
                }
@@ -364,30 +456,69 @@ void session_loop() {
                output_static(buf);
                }
 
-       if (!strncasecmp(cmd, "GET /nothing", 12)) {
-               printf("HTTP/1.0 200 OK\n");
-               output_headers();
-       
-               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();
-               }
 
        else if ((!logged_in)&&(!strncasecmp(cmd, "POST /login", 11))) {
                do_login();
                }
 
        else if (!logged_in) {
-               display_login_page();
+               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();
+               }
 
-       /* When all else fails, display the login page. */
+       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 {
-               display_login_page();
+               printf("HTTP/1.0 200 OK\n");
+               output_headers();
+       
+               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);
@@ -398,16 +529,22 @@ void session_loop() {
        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, "");
        strcpy(wc_port, "");
        strcpy(wc_username, "");
@@ -417,6 +554,4 @@ int main(int argc, char *argv[]) {
        while (1) {
                session_loop();
                }
-
-       exit(0);
        }