]> code.citadel.org Git - citadel.git/blobdiff - webcit-ng/server/messages.c
Progress on upload
[citadel.git] / webcit-ng / server / messages.c
index 1ef039f8246c3e2bfecfe009c5495e1df4ca001f..f176156a51d4ccdda7728acf21464348adb80098 100644 (file)
@@ -1,4 +1,3 @@
-//
 // Message base functions
 //
 // Copyright (c) 1996-2022 by the citadel.org team
@@ -41,8 +40,43 @@ long locate_message_by_uid(struct ctdlsession *c, char *uid) {
 // DAV delete an object in a room.
 void dav_delete_message(struct http_transaction *h, struct ctdlsession *c, long msgnum) {
        ctdl_delete_msgs(c, &msgnum, 1);
-       h->response_code = 204;
-       h->response_string = strdup("no content");
+       do_204(h);
+}
+
+
+// DAV move or copy an object in a room.
+void dav_move_or_copy_message(struct http_transaction *h, struct ctdlsession *c, long msgnum, int move_or_copy) {
+       char target_room[ROOMNAMELEN];
+       char buf[1024];
+
+       // HTTP "Destination" header will tell us the target collection
+       char *target_collection = header_val(h, "Destination");
+       syslog(LOG_DEBUG, "dest coll: \"%s\"", target_collection);
+
+       // Translate the target WebDAV Collection name to a Citadel Room name.
+       // Note that some clients will supply a fully-qualified URL such as "http://example.com/ctdl/r/roomname/999"
+       // so we're just going to search for "/ctdl/r/" and work from there.
+       char *ctdlr = strstr(target_collection, "/ctdl/r/");
+       if (ctdlr == NULL) {    
+               do_412(h);              // badly formed target collection; fail out.
+               return;
+       }
+       safestrncpy(target_room, ctdlr+8, sizeof target_room);
+       char *slash = strchr(target_room, '/');
+       if (slash) {
+               *slash = 0;             // lop off the "filename" we don't need it
+       }
+       unescape_input(target_room);
+       syslog(LOG_DEBUG, "dest room: \"%s\"", target_room);
+
+       // Perform the move or copy operation
+       ctdl_printf(c, "MOVE %ld|%s|%d", msgnum, target_room, move_or_copy);    // Citadel Server: 0=move, 1=copy
+       ctdl_readline(c, buf, sizeof buf);
+       if (buf[0] == '2') {
+               do_204(h);              // succeed (no content)
+               return;
+       }
+       do_412(h);                      // fail (precondition failed)
 }
 
 
@@ -77,7 +111,7 @@ void dav_get_message(struct http_transaction *h, struct ctdlsession *c, long msg
                        if (v) {
                                *v = 0;
                                ++v;
-                               striplt(v);     // we now have a key (k) and a value (v)
+                               string_trim(v);                         // we now have a key (k) and a value (v)
                                if ((!strcasecmp(k, "content-type"))    // fields which can be passed from RFC822 to HTTP as-is
                                    || (!strcasecmp(k, "date"))
                                ) {
@@ -133,19 +167,27 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
        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
+               do_404(h);                              // Refuse to post a null message
                return;
        }
 
-       char *wefw = get_url_param(h, "wefw");  // references
+       // Extract metadata from the URL
+       char *wefw = get_url_param(h, "wefw");          // References:
        if (!wefw) wefw = "";
-       char *subj = get_url_param(h, "subj");  // subject
+       char *subj = get_url_param(h, "subj");          // Subject:
        if (!subj) subj = "";
+       char *mailto = get_url_param(h, "mailto");      // To:
+       if (!mailto) mailto = "";
+       char *mailcc = get_url_param(h, "mailcc");      // Cc:
+       if (!mailcc) mailcc = "";
+       char *mailbcc = get_url_param(h, "mailbcc");    // Bcc:
+       if (!mailbcc) mailbcc = "";
 
        // Mode 4 will give us metadata back after upload
-       ctdl_printf(c, "ENT0 1|||4|%s||1|||||%s|", subj, wefw);
+       ctdl_printf(c, "ENT0 1|%s||4|%s||1|%s|%s|||%s|", mailto, subj, mailcc, mailbcc, wefw);
        ctdl_readline(c, buf, sizeof buf);
        if (buf[0] != '8') {
                h->response_code = 502;
@@ -159,6 +201,17 @@ 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"));
@@ -166,10 +219,57 @@ void dav_put_message(struct http_transaction *h, struct ctdlsession *c, char *eu
        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.
+
+                       syslog(LOG_DEBUG, "💥 attachment: %s , len=%ld", one_att.filename, one_att.length);
+                       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, "");