Skeleton code for filters.
[citadel.git] / webcit-ng / server / messages.c
index 4152413684128738dcec1b63557d651dc650f713..841580405514ae6ec1324f4bde26676aa6b08c10 100644 (file)
@@ -1,9 +1,8 @@
 // Message base functions
 //
-// Copyright (c) 1996-2022 by the citadel.org team
+// 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"
 
@@ -163,10 +162,12 @@ void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msg
 void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *euid, long old_msgnum) {
        char buf[1024];
        char *content_type = NULL;
+       char *content_transfer_encoding = NULL;
        int n;
        long new_msgnum;
        char new_euid[1024];
        char response_string[1024];
+       char mime_boundary[80];
 
        if ((h->request_body == NULL) || (h->request_body_length < 1)) {
                do_404(h);                              // Refuse to post a null message
@@ -200,17 +201,116 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
        // Remember, ctdl_printf() appends \n on its own, so when adding a CRLF newline, only use \r
        // Or for a blank line, use ctdl_write() with \r\n
 
+       // If there are attachments, open up a multipart/mixed MIME container.
+       char *att = get_url_param(h, "att");
+       if (att) {
+               snprintf(mime_boundary, sizeof(mime_boundary), "citadel-multipart-%x-%x", (unsigned int)time(NULL), rand());
+               ctdl_printf(c, "MIME-Version: 1.0\r");
+               ctdl_printf(c, "Content-Type: multipart/mixed; boundary=\"%s\"\r", mime_boundary);
+               ctdl_write(c, HKEY("\r\n"));
+               ctdl_printf(c, "--%s\r", mime_boundary);        // start of message body
+       }
+
+       // This section
        content_type = header_val(h, "Content-type");
-       ctdl_printf(c, "Content-type: %s\r", (content_type ? content_type : "application/octet-stream"));
-       ctdl_write(c, HKEY("\r\n"));
-       ctdl_write(c, h->request_body, h->request_body_length);
+       content_transfer_encoding = header_val(h, "Content-transfer-encoding");
+
+       // If the content is already encoded, pass it along as-is
+       if (!IsEmptyStr(content_transfer_encoding)) {
+               ctdl_printf(c, "Content-type: %s\r", (content_type ? content_type : "application/octet-stream"));
+               ctdl_write(c, HKEY("\r\n"));
+               ctdl_write(c, h->request_body, h->request_body_length);
+       }
+
+       // But if it's raw, we ought to encode it so it's MIME-friendly.
+       else {
+               ctdl_printf(c, "Content-type: %s\r", (content_type ? content_type : "application/octet-stream"));
+               ctdl_printf(c, "Content-transfer-encoding: quoted-printable\r");
+               ctdl_write(c, HKEY("\r\n"));
+               h->request_body[h->request_body_length] = 0;            // make doubly sure it's null terminated.
+
+               // Adapted from https://www.w3.org/Tools/Mail-Transcode/mail-transcode.c
+               char *s = h->request_body;
+               char strconv[8];
+               int strconv_len;
+               int n;
+               for (n = 0; *s; s++) {
+                       if (n >= 73 && *s != 10 && *s != 13) {
+                               ctdl_write(c, HKEY("=\r\n"));
+                               n = 0;
+                       }
+                       if (*s == 10 || *s == 13) {
+                               ctdl_write(c, s, 1);
+                               n = 0;
+                       }
+                       else if (*s<32 || *s==61 || *s>126) {
+                               strconv_len = sprintf(strconv, "=%02X", (unsigned char)*s);
+                               ctdl_write(c, strconv, strconv_len);
+                               n += strconv_len;
+                       }
+                       else if (*s != 32 || (*(s+1) != 10 && *(s+1) != 13)) {
+                               ctdl_write(c, s, 1);
+                               n++;
+                       }
+                       else {
+                               ctdl_write(c, "=20", 3);
+                               n += 3;
+                       }
+               }
+       }
+
        if (h->request_body[h->request_body_length] != '\n') {
                ctdl_write(c, HKEY("\r\n"));
        }
-       ctdl_printf(c, "000");
 
-       // Now handle the response from the Citadel server.
+       // If there are attachments, add them now.
+       if (att) {
+               int i;
+               char attid[10];
+               struct uploaded_file one_att;
+               int num_attachments = num_tokens(att, ',');
+
+               for (i=0; i<num_attachments; ++i) {
+                       extract_token(attid, att, i, ',', sizeof(attid));
+                       one_att = pop_upload(attid);
+
+                       // After calling pop_upload(), the attachment is no longer in the global list.
+                       // The file descriptor has zero links, so when we close it, the filesystem will remove it from disk.
+                       ctdl_printf(c, "--%s\r", mime_boundary);
+                       ctdl_printf(c, "Content-Type: %s; name=\"%s\"\r", one_att.content_type, one_att.filename);
+                       ctdl_printf(c, "Content-Disposition: attachment; filename=\"%s\"\r", one_att.filename);
+                       ctdl_printf(c, "Content-Transfer-Encoding: base64\r");
+                       ctdl_write(c, HKEY("\r\n"));
+
+                       char *raw_att = malloc(one_att.length);
+                       if (raw_att) {
+                               rewind(one_att.fp);
+                               if (fread(raw_att, one_att.length, 1, one_att.fp) != 1) {
+                                       syslog(LOG_ERR, "messages: %m");
+                               }
+
+                               // now encode it
+                               char *encoded_att = malloc((one_att.length * 150) / 100);
+                               if (encoded_att) {
+                                       size_t encoded_length = CtdlEncodeBase64(encoded_att, raw_att, one_att.length, BASE64_YES_LINEBREAKS);
+                                       ctdl_write(c, encoded_att, encoded_length);
+                                       syslog(LOG_DEBUG, "Encoded attachment: len=%ld", encoded_length);
+                                       free(encoded_att);
+                               }
+                               free(raw_att);
+                       }
+
+                       fclose(one_att.fp);
+               }
+
+               // Close the multipart/mixed MIME container.
+               ctdl_printf(c, "--%s--\r", mime_boundary);
+       }
+
+       // Done writing to the Citadel Server.
+       ctdl_printf(c, "000");
 
+       // Now handle the response from the Citadel Server.
        n = 0;
        new_msgnum = 0;
        strcpy(new_euid, "");