* inetconf.c: added. Not finished yet.
[citadel.git] / webcit / webcit.c
index 30374d37b6bd3263785ad7ffccd7c7c6c22fecb7..fa577a5be5e12ad0f0a17fb5ac17a34d9422aabd 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));
 }
 
 
@@ -186,7 +186,7 @@ void wDumpContent(int print_standard_html_footer)
  * Copy a string, escaping characters which have meaning in HTML.  If
  * nbsp is nonzero, spaces are converted to non-breaking spaces.
  */
-void stresc(char *target, char *strbuf, int nbsp)
+void stresc(char *target, char *strbuf, int nbsp, int nolinebreaks)
 {
        int a;
        strcpy(target, "");
@@ -208,24 +208,31 @@ void stresc(char *target, char *strbuf, int nbsp)
                        strcat(target, ">");
                else if (strbuf[a] == QU)
                        strcat(target, "\"");
-               else if ((strbuf[a] == 32) && (nbsp == 1)) {
+               else if ((strbuf[a] == 32) && (nbsp == 1))
                        strcat(target, "&nbsp;");
-               } else {
+               else if ((strbuf[a] == '\n') && (nolinebreaks))
+                       strcat(target, "");     /* nothing */
+               else if ((strbuf[a] == '\r') && (nolinebreaks))
+                       strcat(target, "");     /* nothing */
+               else
                        strncat(target, &strbuf[a], 1);
-               }
        }
 }
 
-void escputs1(char *strbuf, int nbsp)
+void escputs1(char *strbuf, int nbsp, int nolinebreaks)
 {
-       char buf[1024];
-       stresc(buf, strbuf, nbsp);
+       char *buf;
+
+       if (strbuf == NULL) return;
+       buf = malloc(2 * strlen(strbuf));
+       stresc(buf, strbuf, nbsp, nolinebreaks);
        wprintf("%s", buf);
+       free(buf);
 }
 
 void escputs(char *strbuf)
 {
-       escputs1(strbuf, 0);
+       escputs1(strbuf, 0, 0);
 }
 
 /*
@@ -262,6 +269,71 @@ 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);
+}
+
+/*
+ * Copy a string, escaping characters for message text hold
+ */
+void msgesc(char *target, char *strbuf)
+{
+       int a;
+       strcpy(target, "");
+
+       for (a = 0; a < strlen(strbuf); ++a) {
+               if (strbuf[a] == '\'') 
+                       strcat(target, "\\'");
+               else if (strbuf[a] == '\n')
+                       strcat(target, " ");
+               else if (strbuf[a] == '\r')
+                       strcat(target, " ");
+               else {
+                       strncat(target, &strbuf[a], 1);
+               }
+       }
+}
+
+void msgescputs(char *strbuf) {
+       char *outbuf;
+
+       if (strbuf == NULL) return;
+       outbuf = malloc(2 * strlen(strbuf));
+       msgesc(outbuf, strbuf);
+       wprintf("%s", outbuf);
+       free(outbuf);
+}
+
+
 
 
 /*
@@ -276,8 +348,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 +358,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 +375,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 +402,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 +420,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 +489,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 +501,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,8 +535,12 @@ 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], ".htm", 4))
+                       strcpy(content_type, "text/html");
                else if (!strncasecmp(&what[strlen(what) - 4], ".wml", 4))
                        strcpy(content_type, "text/vnd.wap.wml");
                else if (!strncasecmp(&what[strlen(what) - 5], ".wmls", 5))
@@ -444,9 +550,11 @@ void output_static(char *what)
                else if (!strncasecmp(&what[strlen(what) - 6], ".wmlsc", 6))
                        strcpy(content_type, "application/vnd.wap.wmlscriptc");
                else if (!strncasecmp(&what[strlen(what) - 5], ".wbmp", 5))
-                       wprintf("Content-type: image/vnd.wap.wbmp");
+                       strcpy(content_type, "image/vnd.wap.wbmp");
+               else if (!strncasecmp(&what[strlen(what) - 3], ".js", 3))
+                       strcpy(content_type, "text/javascript");
                else
-                       wprintf("Content-type: application/octet-stream");
+                       strcpy(content_type, "application/octet-stream");
 
                fstat(fileno(fp), &statbuf);
                bytes = statbuf.st_size;
@@ -455,7 +563,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 +594,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 +639,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 +708,13 @@ void blank_page(void) {
 }
 
 
+/*
+ * A template has been requested
+ */
+void url_do_template(void) {
+       do_template(bstr("template"));
+}
+
 
 
 /*
@@ -621,7 +736,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 +752,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,9 +798,6 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
 {
        struct urlcontent *u;
 
-       lprintf(5, "Upload handler called: %s, %ld bytes\n",
-               cbtype, (long)length);
-
        /* Form fields */
        if ( (length > 0) && (strlen(cbtype) == 0) ) {
                u = (struct urlcontent *) malloc(sizeof(struct urlcontent));
@@ -712,8 +821,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));
                }
        }
 
@@ -732,7 +840,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;
@@ -793,7 +901,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"
@@ -801,6 +908,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],
@@ -808,12 +916,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);
                }
@@ -895,6 +1007,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();
 
@@ -946,6 +1064,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 +1137,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 +1206,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,7 +1229,7 @@ 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")) {
@@ -1136,6 +1260,18 @@ 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, "set_room_policy")) {
+               set_room_policy();
+       } else if (!strcasecmp(action, "display_inetconf")) {
+               display_inetconf();
+       } else if (!strcasecmp(action, "save_inetconf")) {
+               save_inetconf();
        } else if (!strcasecmp(action, "diagnostics")) {
                output_headers(1);