* Completed SSL support. Still doesn't work with all browsers... gotta
[citadel.git] / webcit / webcit.c
index 0961971818e07bbbe9447be9edc4e8379a572fa4..f5c6fba7c910e1d665336f69028d7ce034fc141b 100644 (file)
@@ -92,11 +92,11 @@ void addurls(char *url)
                for (a = 0; a <= b; ++a)
                        ++up;
 
-               /* locate the & sign */
+               /* locate "&" and "?" delimiters */
                ptr = up;
                b = strlen(up);
                for (a = 0; a < strlen(up); ++a) {
-                       if (!strncmp(ptr, "&", 1)) {
+                       if ( (ptr[0] == '&') || (ptr[0] == '?') ) {
                                b = a;
                                break;
                        }
@@ -161,7 +161,7 @@ void wprintf(const char *format,...)
        vsprintf(wbuf, format, arg_ptr);
        va_end(arg_ptr);
 
-       write(WC->http_sock, wbuf, strlen(wbuf));
+       client_write(wbuf, strlen(wbuf));
 }
 
 
@@ -262,6 +262,40 @@ void urlescputs(char *strbuf)
 }
 
 
+/*
+ * Copy a string, escaping characters for JavaScript strings.
+ */
+void jsesc(char *target, char *strbuf)
+{
+       int a;
+       strcpy(target, "");
+
+       for (a = 0; a < strlen(strbuf); ++a) {
+               if (strbuf[a] == '<')
+                       strcat(target, "[");
+               else if (strbuf[a] == '>')
+                       strcat(target, "]");
+               else if (strbuf[a] == '\"')
+                       strcat(target, "&quot;");
+               else if (strbuf[a] == '&')
+                       strcat(target, "&amp;;");
+               else if (strbuf[a] == '\'') 
+                       strcat(target, "\\'");
+               else {
+                       strncat(target, &strbuf[a], 1);
+               }
+       }
+}
+
+void jsescputs(char *strbuf)
+{
+       char outbuf[SIZ];
+       
+       jsesc(outbuf, strbuf);
+       wprintf("%s", outbuf);
+}
+
+
 
 
 /*
@@ -276,8 +310,9 @@ void urlescputs(char *strbuf)
  * 3 = HTTP and HTML headers, but no room banner
  *
  * Bit 2: Set to 1 to auto-refresh page every 30 seconds
- *
  * Bit 3: suppress check for express messages
+ * Bit 4: Allow browser to cache this document
+ *
  */
 void output_headers(int controlcode)
 {
@@ -285,11 +320,14 @@ void output_headers(int controlcode)
        int print_standard_html_head = 0;
        int refresh30 = 0;
        int suppress_check = 0;
+       int cache = 0;
        char httpnow[SIZ];
+       char onload_fcn[SIZ];
        static int pageseq = 0;
        print_standard_html_head        =       controlcode & 0x03;
        refresh30                       =       ((controlcode & 0x04) >> 2);
        suppress_check                  =       ((controlcode & 0x08) >> 3);
+       cache                           =       ((controlcode & 0x10) >> 4);
 
        wprintf("HTTP/1.0 200 OK\n");
 
@@ -299,13 +337,16 @@ void output_headers(int controlcode)
                wprintf("Content-type: text/html\n"
                        "Server: %s\n", SERVER
                );
-               wprintf("Connection: close\n"
-                       "Pragma: no-cache\n"
-                       "Cache-Control: no-store\n"
-               );
+               if (!cache)
+                       wprintf("Connection: close\n"
+                               "Pragma: no-cache\n"
+                               "Cache-Control: no-store\n"
+                       );
        }
+
        stuff_to_cookie(cookie, WC->wc_session, WC->wc_username,
                        WC->wc_password, WC->wc_roomname);
+
        if (print_standard_html_head == 2) {
                wprintf("Set-cookie: webcit=%s\n", unset);
        } else {
@@ -323,6 +364,17 @@ void output_headers(int controlcode)
                else svprintf("REFRESHTAG", WCS_STRING,
                        "<META HTTP-EQUIV=\"refresh\" CONTENT=\"500363689;\">\n");
                /* script for checking for pages (not always launched) */
+
+               sprintf(onload_fcn, "function onload_fcn() { \n");
+               if (!WC->outside_frameset_allowed) {
+                       strcat(onload_fcn, "  force_frameset();  \n");
+               }
+               if (!suppress_check) if (WC->HaveExpressMessages) {
+                       strcat(onload_fcn, "  launch_page_popup();  \n");
+                       WC->HaveExpressMessages = 0;
+               }
+               strcat(onload_fcn, "} \n");
+
                svprintf("PAGERSCRIPT", WCS_STRING,
                        "<SCRIPT LANGUAGE=\"JavaScript\">\n"
                        "function launch_page_popup() {\n"
@@ -330,30 +382,41 @@ void output_headers(int controlcode)
                        "'toolbar=no,location=no,copyhistory=no,status=no,"
                        "scrollbars=yes,resizable=no,height=250,width=400');\n"
                        "}\n"
+                       "function force_frameset() { \n"
+                       " if (top.frames.length == 0) { \n"
+                       "  top.location.replace('/do_welcome'); \n"
+                       " } \n"
+                       "} \n"
+                       "%s\n"
                        "</SCRIPT>\n",
-                       ++pageseq
+                       ++pageseq,
+                       onload_fcn
                );
                /* end script */
 
+
                do_template("head");
                clear_local_substs();
 
-               if (!suppress_check) if (WC->HaveExpressMessages) {
-                       svprintf("extrabodyparms", WCS_STRING, "%s", 
-                               "onload=\"launch_page_popup()\" ");
-                       WC->HaveExpressMessages = 0;
-               }
+               svprintf("extrabodyparms", WCS_STRING, "%s", 
+                       "onload='onload_fcn();' ");
 
                do_template("background");
                clear_local_substs();
+       }
 
        if (print_standard_html_head == 1) {
                wprintf("<A NAME=\"TheTop\"></A>");
-
                embed_room_banner(NULL);
-
-               }
        }
+
+       if (strlen(WC->ImportantMessage) > 0) {
+               do_template("beginbox_nt");
+               wprintf("<SPAN CLASS=\"errormsg\">"
+                       "%s</SPAN><BR>\n", WC->ImportantMessage);
+               do_template("endbox");
+               strcpy(WC->ImportantMessage, "");
+       }       
 }
 
 
@@ -388,8 +451,9 @@ void check_for_express_messages()
 /* 
  * Output a piece of content to the web browser
  */
-void http_transmit_thing(char *thing, size_t length, char *content_type) {
-       output_headers(0);
+void http_transmit_thing(char *thing, size_t length, char *content_type,
+                        int is_static) {
+       output_headers(is_static ? 0x10 : 0x00);
        wprintf("Content-type: %s\n"
                "Content-length: %ld\n"
                "Server: %s\n"
@@ -399,7 +463,7 @@ void http_transmit_thing(char *thing, size_t length, char *content_type) {
                (long) length,
                SERVER
        );
-       write(WC->http_sock, thing, (size_t)length);
+       client_write(thing, (size_t)length);
 }
 
 
@@ -433,6 +497,8 @@ void output_static(char *what)
                        strcpy(content_type, "image/jpeg");
                else if (!strncasecmp(&what[strlen(what) - 4], ".png", 4))
                        strcpy(content_type, "image/png");
+               else if (!strncasecmp(&what[strlen(what) - 4], ".ico", 4))
+                       strcpy(content_type, "image/x-icon");
                else if (!strncasecmp(&what[strlen(what) - 5], ".html", 5))
                        strcpy(content_type, "text/html");
                else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
@@ -455,7 +521,7 @@ void output_static(char *what)
                fread(bigbuffer, bytes, 1, fp);
                fclose(fp);
 
-               http_transmit_thing(bigbuffer, (size_t)bytes, content_type);
+               http_transmit_thing(bigbuffer, (size_t)bytes, content_type, 1);
                free(bigbuffer);
        }
        if (!strcasecmp(bstr("force_close_session"), "yes")) {
@@ -486,7 +552,7 @@ void output_image()
                serv_gets(buf);
 
                /* Write it to the browser */
-               http_transmit_thing(xferbuf, (size_t)bytes, "image/gif");
+               http_transmit_thing(xferbuf, (size_t)bytes, "image/gif", 0);
                free(xferbuf);
 
        } else {
@@ -531,7 +597,7 @@ void output_mimepart()
                read_server_binary(content, bytes);
                serv_puts("CLOS");
                serv_gets(buf);
-               http_transmit_thing(content, bytes, content_type);
+               http_transmit_thing(content, bytes, content_type, 0);
                free(content);
        } else {
                wprintf("HTTP/1.0 404 %s\n", &buf[4]);
@@ -600,6 +666,13 @@ void blank_page(void) {
 }
 
 
+/*
+ * A template has been requested
+ */
+void url_do_template(void) {
+       do_template(bstr("template"));
+}
+
 
 
 /*
@@ -621,7 +694,9 @@ void offer_start_page(void) {
 void change_start_page(void) {
 
        if (bstr("startpage") == NULL) {
-               display_error("startpage set to null");
+               strcpy(WC->ImportantMessage,
+                       "startpage set to null");
+               display_main_menu();
                return;
        }
 
@@ -635,11 +710,6 @@ void change_start_page(void) {
 
 
 
-void display_error(char *errormessage)
-{
-       convenience_page("770000", "Error", errormessage);
-}
-
 void display_success(char *successmessage)
 {
        convenience_page("007700", "OK", successmessage);
@@ -686,14 +756,6 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
 {
        struct urlcontent *u;
 
-       lprintf(5, "UPLOAD HANDLER CALLED\n");
-       lprintf(5, "       name = %s\n", name);
-       lprintf(5, "   filename = %s\n", filename);
-       lprintf(5, "   encoding = %s\n", encoding);
-       lprintf(5, "       type = %s\n", cbtype);
-       lprintf(5, "     length = %ld\n", (long)length);
-       lprintf(5, "disposition = %s\n", disp);
-
        /* Form fields */
        if ( (length > 0) && (strlen(cbtype) == 0) ) {
                u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
@@ -717,8 +779,7 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
                        memcpy(WC->upload, content, length);
                }
                else {
-                       lprintf(9, "malloc() failed: %s\n",
-                               strerror(errno));
+                       lprintf(3, "malloc() failed: %s\n", strerror(errno));
                }
        }
 
@@ -737,7 +798,7 @@ void session_loop(struct httprequest *req)
        char buf[SIZ];
        int a, b;
        int ContentLength = 0;
-       int BytesRead;
+       int BytesRead = 0;
        char ContentType[512];
        char *content;
        char *content_end;
@@ -798,7 +859,6 @@ void session_loop(struct httprequest *req)
        }
 
        if (ContentLength > 0) {
-               lprintf(5, "Content length: %d\n", ContentLength);
                content = malloc(ContentLength + SIZ);
                memset(content, 0, ContentLength + SIZ);
                sprintf(content, "Content-type: %s\n"
@@ -806,6 +866,7 @@ void session_loop(struct httprequest *req)
                                ContentType, ContentLength);
                body_start = strlen(content);
 
+/***** old version
                BytesRead = 0;
                while (BytesRead < ContentLength) {
                        a=read(WC->http_sock, &content[BytesRead+body_start],
@@ -813,12 +874,16 @@ void session_loop(struct httprequest *req)
                        if (a <= 0) BytesRead = ContentLength;
                        else BytesRead += a;
                }
+*******/
+
+               /* Now we're daring and read it all at once. */
+               client_read(WC->http_sock, &content[BytesRead+body_start], ContentLength);
 
                if (!strncasecmp(ContentType,
                              "application/x-www-form-urlencoded", 33)) {
                        addurls(&content[body_start]);
                } else if (!strncasecmp(ContentType, "multipart", 9)) {
-                       content_end = content + ContentLength;
+                       content_end = content + ContentLength + body_start;
                        mime_parser(content, content_end, *upload_handler,
                                        NULL, NULL, NULL, 0);
                }
@@ -842,6 +907,16 @@ void session_loop(struct httprequest *req)
                }
        }
 
+       /* Static content can be sent without connecting to Citadel. */
+       if (!strcasecmp(action, "static")) {
+               strcpy(buf, &cmd[12]);
+               for (a = 0; a < strlen(buf); ++a)
+                       if (isspace(buf[a]))
+                               buf[a] = 0;
+               output_static(buf);
+               goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
+       }
+
        /*
         * If we're not connected to a Citadel server, try to hook up the
         * connection now.
@@ -890,6 +965,12 @@ void session_loop(struct httprequest *req)
                do_listsub();
                goto SKIP_ALL_THIS_CRAP;
        }
+#ifdef WEBCIT_WITH_CALENDAR_SERVICE
+       if (!strcasecmp(action, "freebusy")) {
+               do_freebusy(cmd);
+               goto SKIP_ALL_THIS_CRAP;
+       }
+#endif
 
        check_for_express_messages();
 
@@ -919,13 +1000,8 @@ void session_loop(struct httprequest *req)
                        strcpy(WC->wc_roomname, c_roomname);
                }
        }
-       if (!strcasecmp(action, "static")) {
-               strcpy(buf, &cmd[12]);
-               for (a = 0; a < strlen(buf); ++a)
-                       if (isspace(buf[a]))
-                               buf[a] = 0;
-               output_static(buf);
-       } else if (!strcasecmp(action, "image")) {
+
+       if (!strcasecmp(action, "image")) {
                output_image();
 
        /*
@@ -946,6 +1022,8 @@ void session_loop(struct httprequest *req)
                do_welcome();
        } else if (!strcasecmp(action, "blank")) {
                blank_page();
+       } else if (!strcasecmp(action, "do_template")) {
+               url_do_template();
        } else if (!strcasecmp(action, "display_main_menu")) {
                display_main_menu();
        } else if (!strcasecmp(action, "whobbs")) {
@@ -1017,12 +1095,12 @@ void session_loop(struct httprequest *req)
         } else if (!strcasecmp(action, "display_whok")) {
                 display_whok();
        } else if (!strcasecmp(action, "display_editinfo")) {
-               display_edit("Room info", "EINF 0", "RINF", "/editinfo");
+               display_edit("Room info", "EINF 0", "RINF", "/editinfo", 1);
        } else if (!strcasecmp(action, "editinfo")) {
                save_edit("Room info", "EINF 1", 1);
        } else if (!strcasecmp(action, "display_editbio")) {
                sprintf(buf, "RBIO %s", WC->wc_username);
-               display_edit("Your bio", "NOOP", buf, "editbio");
+               display_edit("Your bio", "NOOP", buf, "editbio", 3);
        } else if (!strcasecmp(action, "editbio")) {
                save_edit("Your bio", "EBIO", 0);
        } else if (!strcasecmp(action, "confirm_delete_room")) {
@@ -1086,6 +1164,10 @@ void session_loop(struct httprequest *req)
                edit_me();
        } else if (!strcasecmp(action, "display_siteconfig")) {
                display_siteconfig();
+       } else if (!strcasecmp(action, "chat_recv")) {
+               chat_recv();
+       } else if (!strcasecmp(action, "chat_send")) {
+               chat_send();
        } else if (!strcasecmp(action, "page_popup")) {
                page_popup();
        } else if (!strcasecmp(action, "siteconfig")) {
@@ -1105,15 +1187,13 @@ void session_loop(struct httprequest *req)
        } else if (!strcasecmp(action, "select_user_to_edit")) {
                select_user_to_edit(NULL, NULL);
        } else if (!strcasecmp(action, "display_edituser")) {
-               display_edituser(NULL);
+               display_edituser(NULL, 0);
        } else if (!strcasecmp(action, "edituser")) {
                edituser();
        } else if (!strcasecmp(action, "create_user")) {
                create_user();
        } else if (!strcasecmp(action, "changeview")) {
                change_view();
-       } else if (!strcasecmp(action, "folders")) {
-               folders();
        } else if (!strcasecmp(action, "do_stuff_to_msgs")) {
                do_stuff_to_msgs();
        } else if (!strcasecmp(action, "change_start_page")) {
@@ -1138,6 +1218,12 @@ void session_loop(struct httprequest *req)
 #endif
        } else if (!strcasecmp(action, "summary")) {
                summary();
+       } else if (!strcasecmp(action, "iconbar")) {
+               do_iconbar();
+       } else if (!strcasecmp(action, "display_customize_iconbar")) {
+               display_customize_iconbar();
+       } else if (!strcasecmp(action, "commit_iconbar")) {
+               commit_iconbar();
        } else if (!strcasecmp(action, "diagnostics")) {
                output_headers(1);