Added commands to view a room's file directory and to
[citadel.git] / webcit / webcit.c
index 85ee1ca4ba9e28af897fcee0279dcaf1dc18074d..2d71873103ec54593a602ed73f64c083fd8f0f9c 100644 (file)
 #include "webserver.h"
 #include "mime_parser.h"
 
-/**
- * Subdirectories from which the client may request static content
- */
-char *static_content_dirs[] = {
-       "static",                     /** static templates */
-       "tiny_mce"                    /** the JS editor */
-};
-
 /**
  * String to unset the cookie.
  * Any date "in the past" will work, so I chose my birthday, right down to
@@ -110,6 +102,8 @@ void addurls(char *url)
                unescape_input(u->url_data);
                up = ptr;
                ++up;
+
+               /* lprintf(9, "%s = %s\n", u->url_key, u->url_data); */
        }
 }
 
@@ -435,6 +429,11 @@ void output_headers(       int do_httpheaders,     /**< 1 = output HTTP headers
 
        if (do_htmlhead) {
                begin_burst();
+               if (!access("static.local/webcit.css", R_OK)) {
+                       svprintf("CSSLOCAL", WCS_STRING,
+                          "<link href=\"static.local/webcit.css\" rel=\"stylesheet\" type=\"text/css\">"
+                       );
+               }
                do_template("head");
        }
 
@@ -700,17 +699,15 @@ char *load_mimepart(long msgnum, char *partnum)
        char content_type[SIZ];
        char *content;
        
-       serv_printf("OPNA %ld|%s", msgnum, partnum);
+       serv_printf("DLAT %ld|%s", msgnum, partnum);
        serv_getln(buf, sizeof buf);
-       if (buf[0] == '2') {
+       if (buf[0] == '6') {
                bytes = extract_long(&buf[4], 0);
                extract_token(content_type, &buf[4], 3, '|', sizeof content_type);
 
                content = malloc(bytes + 2);
-               read_server_binary(content, bytes);
+               serv_read(content, bytes);
 
-               serv_puts("CLOS");
-               serv_getln(buf, sizeof buf);
                content[bytes] = 0;     /* null terminate for good measure */
                return(content);
        }
@@ -999,17 +996,13 @@ void session_loop(struct httprequest *req)
 {
        char cmd[1024];
        char action[1024];
-       char arg1[128];
-       char arg2[128];
-       char arg3[128];
-       char arg4[128];
-       char arg5[128];
-       char arg6[128];
-       char arg7[128];
+       char arg[8][128];
+       size_t sizes[10];
+       char *index[10];
        char buf[SIZ];
        char request_method[128];
        char pathname[1024];
-       int a, b;
+       int a, b, nBackDots, nEmpty;
        int ContentLength = 0;
        int BytesRead = 0;
        char ContentType[512];
@@ -1020,7 +1013,7 @@ void session_loop(struct httprequest *req)
        char user_agent[256];
        int body_start = 0;
        int is_static = 0;
-
+       int n_static = 0;
        /**
         * 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.
@@ -1055,45 +1048,27 @@ void session_loop(struct httprequest *req)
        extract_token(pathname, cmd, 1, ' ', sizeof pathname);
 
        /** Figure out the action */
-       extract_token(action, pathname, 1, '/', sizeof action);
-       if (strstr(action, "?")) *strstr(action, "?") = 0;
-       if (strstr(action, "&")) *strstr(action, "&") = 0;
-       if (strstr(action, " ")) *strstr(action, " ") = 0;
-
-       extract_token(arg1, pathname, 2, '/', sizeof arg1);
-       if (strstr(arg1, "?")) *strstr(arg1, "?") = 0;
-       if (strstr(arg1, "&")) *strstr(arg1, "&") = 0;
-       if (strstr(arg1, " ")) *strstr(arg1, " ") = 0;
-
-       extract_token(arg2, pathname, 3, '/', sizeof arg2);
-       if (strstr(arg2, "?")) *strstr(arg2, "?") = 0;
-       if (strstr(arg2, "&")) *strstr(arg2, "&") = 0;
-       if (strstr(arg2, " ")) *strstr(arg2, " ") = 0;
-
-       extract_token(arg3, pathname, 4, '/', sizeof arg3);
-       if (strstr(arg3, "?")) *strstr(arg3, "?") = 0;
-       if (strstr(arg3, "&")) *strstr(arg3, "&") = 0;
-       if (strstr(arg3, " ")) *strstr(arg3, " ") = 0;
-
-       extract_token(arg4, pathname, 5, '/', sizeof arg4);
-       if (strstr(arg4, "?")) *strstr(arg4, "?") = 0;
-       if (strstr(arg4, "&")) *strstr(arg4, "&") = 0;
-       if (strstr(arg4, " ")) *strstr(arg4, " ") = 0;
-
-       extract_token(arg5, pathname, 6, '/', sizeof arg5);
-       if (strstr(arg5, "?")) *strstr(arg5, "?") = 0;
-       if (strstr(arg5, "&")) *strstr(arg5, "&") = 0;
-       if (strstr(arg5, " ")) *strstr(arg5, " ") = 0;
-
-       extract_token(arg6, pathname, 7, '/', sizeof arg6);
-       if (strstr(arg6, "?")) *strstr(arg6, "?") = 0;
-       if (strstr(arg6, "&")) *strstr(arg6, "&") = 0;
-       if (strstr(arg6, " ")) *strstr(arg6, " ") = 0;
-
-       extract_token(arg7, pathname, 8, '/', sizeof arg7);
-       if (strstr(arg7, "?")) *strstr(arg7, "?") = 0;
-       if (strstr(arg7, "&")) *strstr(arg7, "&") = 0;
-       if (strstr(arg7, " ")) *strstr(arg7, " ") = 0;
+       index[0] = action;
+       sizes[0] = sizeof action;
+       for (a=1; a<9; a++)
+       {
+               index[a] = arg[a-1];
+               sizes[a] = sizeof arg[a-1];
+       }
+////   index[9] = &foo; todo
+       nBackDots = 0;
+       nEmpty = 0;
+       for ( a = 0; a < 9; ++a)
+       {
+               extract_token(index[a], pathname, a + 1, '/', sizes[a]);
+               if (strstr(index[a], "?")) *strstr(index[a], "?") = 0;
+               if (strstr(index[a], "&")) *strstr(index[a], "&") = 0;
+               if (strstr(index[a], " ")) *strstr(index[a], " ") = 0;
+               if ((index[a][0] == '.') && (index[a][1] == '.'))
+                       nBackDots++;
+               if (index[a][0] == '\0')
+                       nEmpty++;
+       }
 
        while (hptr != NULL) {
                safestrncpy(buf, hptr->line, sizeof buf);
@@ -1195,25 +1170,38 @@ void session_loop(struct httprequest *req)
 
        /** Static content can be sent without connecting to Citadel. */
        is_static = 0;
-       for (a=0; a<(sizeof(static_content_dirs) / sizeof(char *)); ++a) {
-               if (!strcasecmp(action, static_content_dirs[a])) {
+       for (a=0; a<ndirs; ++a) {
+               if (!strcasecmp(action, (char*)static_content_dirs[a])) { /* map web to disk location */
                        is_static = 1;
+                       n_static = a;
                }
        }
        if (is_static) {
-               snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
-                       action, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
-               for (a=0; a<8; ++a) {
-                       if (buf[strlen(buf)-1] == '/') {
-                               buf[strlen(buf)-1] = 0;
+               if (nBackDots < 2)
+               {
+                       snprintf(buf, sizeof buf, "%s/%s/%s/%s/%s/%s/%s/%s",
+                                static_dirs[n_static], 
+                                index[1], index[2], index[3], index[4], index[5], index[6], index[7]);
+                       for (a=0; a<8; ++a) {
+                               if (buf[strlen(buf)-1] == '/') {
+                                       buf[strlen(buf)-1] = 0;
+                               }
                        }
-               }
-               for (a = 0; a < strlen(buf); ++a) {
-                       if (isspace(buf[a])) {
-                               buf[a] = 0;
+                       for (a = 0; a < strlen(buf); ++a) {
+                               if (isspace(buf[a])) {
+                                       buf[a] = 0;
+                               }
                        }
+                       output_static(buf);
+               }
+               else 
+               {
+                       lprintf(9, "Suspicious request. Ignoring.");
+                       wprintf("HTTP/1.1 404 Not found. Don't try to Trick me DUDE!\r\n");
+                       wprintf("Content-Type: text/plain\r\n");
+                       wprintf("\r\n");
+                       wprintf("Not found. Don't play games on me!\r\n");
                }
-               output_static(buf);
                goto SKIP_ALL_THIS_CRAP;        /* Don't try to connect */
        }
 
@@ -1438,12 +1426,14 @@ void session_loop(struct httprequest *req)
                readloop("readfwd");
        } else if (!strcasecmp(action, "headers")) {
                readloop("headers");
+       } else if (!strcasecmp(action, "do_search")) {
+               readloop("do_search");
        } else if (!strcasecmp(action, "msg")) {
-               embed_message(arg1);
+               embed_message(index[1]);
        } else if (!strcasecmp(action, "printmsg")) {
-               print_message(arg1);
+               print_message(index[1]);
        } else if (!strcasecmp(action, "msgheaders")) {
-               display_headers(arg1);
+               display_headers(index[1]);
        } else if (!strcasecmp(action, "wiki")) {
                display_wiki_page();
        } else if (!strcasecmp(action, "display_enter")) {
@@ -1573,9 +1563,9 @@ void session_loop(struct httprequest *req)
        } else if (!strcasecmp(action, "display_menubar")) {
                display_menubar(1);
        } else if (!strcasecmp(action, "mimepart")) {
-               mimepart(arg1, arg2, 0);
+               mimepart(index[1], index[2], 0);
        } else if (!strcasecmp(action, "mimepart_download")) {
-               mimepart(arg1, arg2, 1);
+               mimepart(index[1], index[2], 1);
        } else if (!strcasecmp(action, "edit_vcard")) {
                edit_vcard();
        } else if (!strcasecmp(action, "submit_vcard")) {
@@ -1626,6 +1616,20 @@ void session_loop(struct httprequest *req)
                display_inetconf();
        } else if (!strcasecmp(action, "save_inetconf")) {
                save_inetconf();
+       } else if (!strcasecmp(action, "display_smtpqueue")) {
+               display_smtpqueue();
+       } else if (!strcasecmp(action, "display_smtpqueue_inner_div")) {
+               display_smtpqueue_inner_div();
+       } else if (!strcasecmp(action, "display_sieve")) {
+               display_sieve();
+       } else if (!strcasecmp(action, "save_sieve")) {
+               save_sieve();
+       } else if (!strcasecmp(action, "display_add_remove_scripts")) {
+               display_add_remove_scripts(NULL);
+       } else if (!strcasecmp(action, "create_script")) {
+               create_script();
+       } else if (!strcasecmp(action, "delete_script")) {
+               delete_script();
        } else if (!strcasecmp(action, "setup_wizard")) {
                do_setup_wizard();
        } else if (!strcasecmp(action, "display_preferences")) {
@@ -1639,7 +1643,7 @@ void session_loop(struct httprequest *req)
        } else if (!strcasecmp(action, "bcc_autocomplete")) {
                recp_autocomplete(bstr("bcc"));
        } else if (!strcasecmp(action, "set_floordiv_expanded")) {
-               set_floordiv_expanded(arg1);
+               set_floordiv_expanded(index[1]);
        } else if (!strcasecmp(action, "diagnostics")) {
                output_headers(1, 1, 1, 0, 0, 0);
                wprintf("Session: %d<hr />\n", WC->wc_session);
@@ -1652,6 +1656,10 @@ void session_loop(struct httprequest *req)
                wDumpContent(1);
        } else if (!strcasecmp(action, "updatenote")) {
                updatenote();
+       } else if (!strcasecmp(action, "display_room_directory")) {
+               display_room_directory();
+       } else if (!strcasecmp(action, "download_file")) {
+               download_file(index[1]);
        }
 
        /** When all else fais, display the main menu. */