]> code.citadel.org Git - citadel.git/commitdiff
Sending mail to the server (needs some more work)
authorArt Cancro <ajc@citadel.org>
Wed, 19 Oct 2022 23:34:53 +0000 (19:34 -0400)
committerArt Cancro <ajc@citadel.org>
Wed, 19 Oct 2022 23:34:53 +0000 (19:34 -0400)
webcit-ng/static/js/view_mail.js

index 2038ec51e5b95815db7dd446eff36151dd9dc52c..b33fbc691ccc6186e53b3107f9fa07659862468d 100644 (file)
@@ -247,14 +247,13 @@ function mail_compose(is_quoted, references, msgid) {
 
                // The button bar is a Grid element, and is also a Flexbox container.
                + "<div class=\"ctdl-compose-toolbar\">"
-               + "<span class=\"ctdl-msg-button\"><i class=\"fa fa-check\" style=\"color:green\"></i> " + _("Send message") + "</span>"
+               + "<span class=\"ctdl-msg-button\" onclick=\"mail_save_message()\"><i class=\"fa fa-check\" style=\"color:green\"></i> " + _("Send message") + "</span>"
                + "<span class=\"ctdl-msg-button\">" + _("Save to Drafts") + "</span>"
                + "<span class=\"ctdl-msg-button\">" + _("Attachments:") + " 0" + "</span>"
                + "<span class=\"ctdl-msg-button\">" + _("Contacts") + "</span>"
                + "<span class=\"ctdl-msg-button\" onClick=\"gotoroom(current_room)\"><i class=\"fa fa-trash\" style=\"color:red\"></i> " + _("Cancel") + "</span>"
                + "</div>"
        ;
-
 }
 
 function make_cc_bcc_visible() {
@@ -265,3 +264,65 @@ function make_cc_bcc_visible() {
        document.getElementById("ctdl-compose-bcc-field").style.display = "block";
 }
 
+
+// Helper function for mail_save_messages() to extract form values.
+// (We have to replace "|" with "!" because "|" is a field separator in the Citadel protocol)
+function msm_field(element_name) {
+       return (document.getElementById(element_name).innerHTML).replaceAll("|","!");
+}
+
+
+// Save the posted message to the server
+function mail_save_message() {
+
+       document.body.style.cursor = "wait";
+       url = "/ctdl/r/" + escapeHTMLURI(current_room)
+               + "/dummy_name_for_new_mail"
+               + "?wefw=" + msm_field("ctdl_mc_references")                            // references (if present)
+               + "&subj=" + msm_field("ctdl-compose-subject-field")                    // subject (if present)
+               + "&mailto=" + msm_field("ctdl-compose-to-field")                       // To: (required)
+               + "&mailcc=" + msm_field("ctdl-compose-cc-field")                       // Cc: (if present)
+               + "&mailbcc=" + msm_field("ctdl-compose-bcc-field")                     // Bcc: (if present)
+               ;
+       boundary = randomString();
+       body_text =
+               "--" + boundary + "\r\n"
+               + "Content-type: text/html\r\n"
+               + "Content-transfer-encoding: quoted-printable\r\n"
+               + "\r\n"
+               + quoted_printable_encode(
+                       "<html><body>" + document.getElementById("ctdl-editor-body").innerHTML + "</body></html>"
+               ) + "\r\n"
+               + "--" + boundary + "--\r\n"
+       ;
+
+       var request = new XMLHttpRequest();
+       request.open("PUT", url, true);
+       request.setRequestHeader("Content-type", "multipart/mixed; boundary=\"" + boundary + "\"");
+       request.onreadystatechange = function() {
+               if (request.readyState == 4) {
+                       document.body.style.cursor = "default";
+                       if (Math.trunc(request.status / 100) == 2) {
+                               headers = request.getAllResponseHeaders().split("\n");
+                               for (var i in headers) {
+                                       if (headers[i].startsWith("etag: ")) {
+                                               new_msg_num = headers[i].split(" ")[1];
+                                       }
+                               }
+
+                               // After saving the message, go back to the mailbox view.
+                               gotoroom(current_room);
+
+                       }
+                       else {
+                               error_message = request.responseText;
+                               if (error_message.length == 0) {
+                                       error_message = _("An error has occurred.");
+                               }
+                               alert(error_message);                                           // editor remains open
+                       }
+               }
+       };
+       request.send(body_text);
+}
+