Skeleton code for filters.
[citadel.git] / webcit-ng / server / upload.c
index 6854acf8ae726f4fdb6e3a7ab9421e7f1faa2766..fd7a77b250d068d5f87ae061be62cb31e53f6181 100644 (file)
@@ -2,8 +2,7 @@
 //
 // Copyright (c) 1996-2023 by the citadel.org team
 //
-// This program is open source software.  Use, duplication, or
-// disclosure are subject to the GNU General Public License v3.
+// This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License v3.
 
 #include "webcit.h"
 
@@ -24,6 +23,11 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
        }
        u.id[sizeof(u.id)-1] = 0;
 
+       syslog(LOG_DEBUG,
+               "upload_handler: name=%s, filename=%s, partnum=%s, disp=%s, cbtype=%s, cbcharset=%s, length=%ld, encoding=%s, cbid=%s",
+               name, filename, partnum, disp, cbtype, cbcharset, length, encoding, cbid
+       );
+
        safestrncpy(u.filename, filename, sizeof(u.filename));
        safestrncpy(u.content_type, cbtype, sizeof(u.content_type));
        u.length = length;
@@ -65,6 +69,7 @@ void upload_handler(char *name, char *filename, char *partnum, char *disp,
        JsonArrayAppend(j_uploads, j_one_upload);
 }
 
+
 // upload handler
 void upload_files(struct http_transaction *h, struct ctdlsession *c) {
        // FIXME reject uploads if we're not logged in
@@ -146,12 +151,73 @@ struct uploaded_file pop_upload(char *id) {
 }
 
 
+// When reloading attachments already in an existing message, accept only parts that are tagged as attachments.
+void attachment_filter(char *name, char *filename, char *partnum, char *disp,
+                   void *content, char *cbtype, char *cbcharset,
+                   size_t length, char *encoding, char *cbid, void *userdata)
+{
+       struct uploaded_file u;
+
+       if (!strcasecmp(disp, "attachment")) {
+               upload_handler(name, filename, partnum, disp, content, cbtype, cbcharset, length, encoding, cbid, userdata);
+       }
+}
+
+
+// Load the attachments from an existing message.  This is typically used when forwarding a message,
+// so the attachments don't have to be sent out to the browser and back.
+void load_attachments_from_message(struct http_transaction *h, struct ctdlsession *c, char *name) {
+       syslog(LOG_DEBUG, "\033[33mload_attachments_from_message: method is \033[35m%s\033[33m, name is \033[33m%s\033[0m", h->method, name);
+
+       char buf[1024];
+       StrBuf *Body = NULL;
+
+       ctdl_printf(c, "MSG2 %ld", atol(name));
+       ctdl_readline(c, buf, sizeof buf);
+       if (buf[0] != '1') {
+               do_404(h);
+               return;
+       }
+
+       JsonValue *j_uploads = NewJsonArray(HKEY(""));
+       Body = NewStrBuf();
+       while (ctdl_readline(c, buf, sizeof buf), strcmp(buf, "000")) {
+               StrBufAppendPrintf(Body, "%s\n", buf);
+       }
+       char *raw_message = SmashStrBuf(&Body);
+       mime_parser(raw_message, NULL, *attachment_filter, NULL, NULL, j_uploads, 0);
+       free(raw_message);
+
+       // probably do something more clever here
+       h->response_code = 200;
+       h->response_string = strdup("OK");
+
+       // send back a JSON array of all files uploaded
+       StrBuf *sj = NewStrBuf();
+       SerializeJson(sj, j_uploads, 1);        // '1' == free the source object
+       add_response_header(h, strdup("Content-type"), strdup("application/json"));
+       h->response_code = 200;
+       h->response_string = strdup("OK");
+       h->response_body_length = StrLength(sj);
+       h->response_body = SmashStrBuf(&sj);
+       syslog(LOG_DEBUG, "upload: %s", h->response_body);
+}
+
+
 // Handle operations on a specific upload
 void specific_upload(struct http_transaction *h, struct ctdlsession *c, char *name) {
        int i;
        struct uploaded_file *u;
        struct uploaded_file this_one;
 
+       syslog(LOG_DEBUG, "\033[33mspecific_upload: method is \033[35m%s\033[33m, name is \033[33m%s\033[0m", h->method, name);
+
+       // GET of a msgnum is a request to load the attachments from an existing message.
+       if ( (!strcasecmp(h->method, "GET")) && (atol(name) > 0) ) {
+               load_attachments_from_message(h, c, name);
+               return;
+       }
+
        if (upload_list == NULL) {
                do_404(h);
                return;