Close attachments window on Cancel
[citadel.git] / webcit-ng / static / js / view_mail.js
1 // This module handles the view for "mailbox" rooms.
2 //
3 // Copyright (c) 2016-2023 by the citadel.org team
4 //
5 // This program is open source software.  Use, duplication, or
6 // disclosure are subject to the GNU General Public License v3.
7
8
9 var displayed_message = 0;                                                      // ID of message currently being displayed
10 var RefreshMailboxInterval;                                                     // We store our refresh timer here
11 var highest_mailnum;                                                            // This is used to detect newly arrived mail
12 var newmail_notify = {
13         NO  : 0,                                                                // do not perform new mail notifications
14         YES : 1                                                                 // yes, perform new mail notifications
15 };
16 var num_attachments = 0;                                                        // number of attachments in current composed msg
17
18
19 // This is the async back end for mail_delete_selected()
20 mail_delete_func = async(table, row) => {
21         let m = parseInt(row["id"].substring(12));                              // derive msgnum from row id
22
23         if (is_trash_folder) {
24                 response = await fetch(
25                         "/ctdl/r/" + escapeHTMLURI(current_room) + "/" + m,
26                         {
27                                 method: "DELETE"                                // If this is the Trash folder, delete permanently
28                         },
29                 );
30         }
31         else {
32                 response = await fetch(
33                         "/ctdl/r/" + escapeHTMLURI(current_room) + "/" + m,
34                         {
35                                 method: "MOVE",                                 // Otherwise, move to the Trash folder
36                                 headers: { "Destination" : "/ctdl/r/_TRASH_" }
37                         },
38                 );
39         }
40
41         if (response.ok) {                              // If the server accepted the delete, blank out the message div
42                 table.deleteRow(row.rowIndex);
43                 if (m == displayed_message) {
44                         document.getElementById("ctdl-mailbox-reading-pane").innerHTML = "";
45                         displayed_message = 0;
46                 }
47         }
48 }
49
50
51 // Delete the selected messages (can be activated by mouse click or keypress)
52 function mail_delete_selected() {
53         let table = document.getElementById("ctdl-onscreen-mailbox");
54         let i, row;
55         for (i=0; row=table.rows[i]; ++i) {
56                 if (row.classList.contains("ctdl-mail-selected")) {
57                         mail_delete_func(table, row);
58                 }
59         }
60 }
61
62
63 // Handler function for keypresses detected while the mail view is displayed.  Mainly for deleting messages.
64 function mail_keypress(event) {
65
66         // If the "ctdl-mailbox-pane" no longer exists, the user has navigated to a different part of the site,
67         // so cancel the event listener.
68         try {
69                 document.getElementById("ctdl-mailbox-pane").innerHTML;
70         }
71         catch {
72                 document.removeEventListener("keydown", mail_keypress);
73                 return;
74         }
75
76         const key = event.key.toLowerCase();
77         if (key == "delete") {
78                 mail_delete_selected();
79         }
80
81 }
82
83
84 // Handler function for dragging email messages to other folders
85 function mail_dragstart(event) {
86         let i;
87         let count = 0;
88         let table = document.getElementById("ctdl-onscreen-mailbox");
89         let messages_being_dragged = [] ;
90
91         if (event.target.classList.contains("ctdl-mail-selected")) {
92                 // The row being dragged IS selected.  See if any OTHER rows are selected, and they will come along for the ride.
93                 for (i=1; row=table.rows[i]; ++i) {
94                         if (row.classList.contains("ctdl-mail-selected")) {
95                                 count = count + 1;
96                                 messages_being_dragged.push(row.id);    // Tell the clipboard what's being moved.
97                         }
98                 }
99         }
100         else {
101                 // The row being dragged is NOT selected.  It will be dragged on its own, ignoring the selected rows.
102                 count = 1;
103                 messages_being_dragged.push(event.target.id);           // Tell the clipboard what's being moved.
104         }
105
106         // Set the custom drag image to an envelope + number of messages being dragged
107         d = document.getElementById("ctdl_draggo");
108         d.innerHTML = "<font size='+2'><i class='fa fa-envelope' style='color: red'></i> " + count + "</font>"
109         event.dataTransfer.setDragImage(d, 0, 0);
110         event.dataTransfer.setData("text", messages_being_dragged);
111 }
112
113
114 // Render reply address for a message (FIXME figure out how to deal with "reply-to:")
115 function reply_addr(msg) {
116         //if (msg.locl) {
117                 //return([msg.from]);
118         //}
119         //else {
120                 return([msg.from + " &lt;" + msg.rfca + "&gt;"]);
121         //}
122 }
123
124
125 // Render the To: recipients for a reply-all operation
126 function replyall_to(msg) {
127         return([...reply_addr(msg), ...msg.rcpt]);
128 }
129
130
131 // Render a message into the mailbox view
132 // (We want the message number and the message itself because we need to keep the msgnum for reply purposes)
133 function mail_render_one(msgnum, msg, target_div, include_controls) {
134         let div = "";
135         try {
136                 outmsg =
137                   "<div class=\"ctdl-mmsg-wrapper\">"                           // begin message wrapper
138                 ;
139
140                 if (include_controls) {                                         // omit controls if this is a pull quote
141                         outmsg +=
142                           render_userpic(msg.from)                              // user avatar
143                         + "<div class=\"ctdl-mmsg-content\">"                   // begin content
144                         + "<div class=\"ctdl-msg-header\">"                     // begin header
145                         + "<span class=\"ctdl-msg-header-info\">"               // begin header info on left side
146                         + render_msg_author(msg, views.VIEW_MAILBOX)
147                         + "<span class=\"ctdl-msgdate\">"
148                         + string_timestamp(msg.time,0)
149                         + "</span>"                                             // end msgdate
150                         + "</span>"                                             // end header info on left side
151                         + "<span class=\"ctdl-msg-header-buttons\">"            // begin buttons on right side
152                 
153                         + "<span class=\"ctdl-msg-button\">"                    // Reply (mail is always Quoted)
154                         + "<a href=\"javascript:mail_compose(true,'"+msg.wefw+"','"+msgnum+"', reply_addr(msg), [], 'Re: '+msg.subj);\">"
155                         + "<i class=\"fa fa-reply\"></i> " 
156                         + _("Reply")
157                         + "</a></span>"
158                 
159                         + "<span class=\"ctdl-msg-button\">"                    // Reply-All (mail is always Quoted)
160                         + "<a href=\"javascript:mail_compose(true,'"+msg.wefw+"','"+msgnum+"', replyall_to(msg), msg.cccc, 'Re: '+msg.subj);\">"
161                         + "<i class=\"fa fa-reply-all\"></i> " 
162                         + _("ReplyAll")
163                         + "</a></span>";
164                 
165                         if (can_delete_messages) {
166                                 outmsg +=
167                                 "<span class=\"ctdl-msg-button\">"
168                                 + "<a href=\"javascript:forum_delete_message('"+div+"','"+msg.msgnum+"');\">"
169                                 + "<i class=\"fa fa-trash\"></i> " 
170                                 + _("Delete")
171                                 + "</a></span>";
172                         }
173                 
174                         outmsg +=
175                           "</span>";                                            // end buttons on right side
176
177                         // Display the To: recipients, if any are present
178                         if (msg.rcpt) {
179                                 outmsg += "<br><span>" + _("To:") + " ";
180                                 for (let r=0; r<msg.rcpt.length; ++r) {
181                                         if (r != 0) {
182                                                 outmsg += ", ";
183                                         }
184                                         outmsg += escapeHTML(msg.rcpt[r]);
185                                 }
186                                 outmsg += "</span>";
187                         }
188
189                         // Display the Cc: recipients, if any are present
190                         if (msg.cccc) {
191                                 outmsg += "<br><span>" + _("Cc:") + " ";
192                                 for (let r=0; r<msg.cccc.length; ++r) {
193                                         if (r != 0) {
194                                                 outmsg += ", ";
195                                         }
196                                         outmsg += escapeHTML(msg.cccc[r]);
197                                 }
198                                 outmsg += "</span>";
199                         }
200
201                         // Display a subject line, but only if the message has a subject (internal Citadel messages often don't)
202                         if (msg.subj) {
203                                 outmsg +=
204                                 "<br><span class=\"ctdl-msgsubject\">" + msg.subj + "</span>";
205                         }
206
207                         outmsg +=
208                           "</div>";                                             // end header
209                 }
210
211                 // Display attachments, if any are present
212                 if (msg.part) {
213                         let display_attachments = 0;
214                         for (let r=0; r<msg.part.length; ++r) {
215                                 if (msg.part[r].disp == "attachment") {
216                                         if (display_attachments == 0) {
217                                                 outmsg += "<ul>";
218                                         }
219                                         display_attachments += 1;
220                                         outmsg += "<li>"
221                                                 + "<a href=\"/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgnum + "/" + msg.part[r].partnum + "/" + escapeHTMLURI(msg.part[r].filename) + "\" target=\"_blank\">"
222                                                 + "<i class=\"fa fa-paperclip\"></i>&nbsp;" + msg.part[r].partnum + ": " + msg.part[r].filename
223                                                 + " (" + msg.part[r].len + " " + _("bytes") + ")"
224                                                 + "</a>"
225                                                 + "</li>";
226                                 }
227                         }
228                         if (display_attachments > 0) {
229                                 outmsg += "</ul><br>";
230                         }
231                 }
232
233
234                 outmsg +=
235                   "<div class=\"ctdl-msg-body\" id=\"" + div + "_body\">"       // begin body
236                 + msg.text
237                 + "</div>"                                                      // end body
238                 + "</div>"                                                      // end content
239                 + "</div>"                                                      // end wrapper
240                 ;
241         }
242         catch(err) {
243                 outmsg = "<div class=\"ctdl-mmsg-wrapper\">" + err.message + "</div>";
244         }
245
246         target_div.innerHTML = outmsg;
247 }
248
249
250 // display an individual message (note: this wants an actual div object, not a string containing the name of a div)
251 function mail_display_message(msgnum, target_div, include_controls) {
252         url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/" + msgnum + "/json";
253         mail_fetch_msg = async() => {
254                 response = await fetch(url);
255                 msg = await(response.json());
256                 if (response.ok) {
257                         mail_render_one(msgnum, msg, target_div, include_controls);
258                 }
259         }
260         mail_fetch_msg();
261 }
262
263
264 // A message has been selected...
265 function click_message(event, msgnum) {
266         var table = document.getElementById("ctdl-onscreen-mailbox");
267         var i, m, row;
268
269         // ctrl + click = toggle an individual message without changing existing selection
270         if (event.ctrlKey) {
271                 document.getElementById("ctdl-msgsum-" + msgnum).classList.toggle("ctdl-mail-selected");
272         }
273
274         // shift + click = select a range of messages (start with row 1 because row 0 is the header)
275         else if (event.shiftKey) {
276                 for (i=1; row=table.rows[i]; ++i) {
277                         m = parseInt(row["id"].substring(12));                          // derive msgnum from row id
278                         if (
279                                 ((msgnum >= displayed_message) && (m >= displayed_message) && (m <= msgnum))
280                                 || ((msgnum <= displayed_message) && (m <= displayed_message) && (m >= msgnum))
281                         ) {
282                                 row.classList.add("ctdl-mail-selected");
283                         }
284                         else {
285                                 row.classList.remove("ctdl-mail-selected");
286                         }
287                 }
288         }
289
290         // click + no modifiers = select one message and unselect all others (start with row 1 because row 0 is the header)
291         else {
292                 for (i=1; row=table.rows[i]; ++i) {
293                         if (row["id"] == "ctdl-msgsum-" + msgnum) {
294                                 row.classList.add("ctdl-mail-selected");
295                         }
296                         else {
297                                 row.classList.remove("ctdl-mail-selected");
298                         }
299                 }
300         }
301
302         // display the message if it isn't already displayed
303         if (displayed_message != msgnum) {
304                 displayed_message = msgnum;
305                 mail_display_message(msgnum, document.getElementById("ctdl-mailbox-reading-pane"), 1);
306         }
307 }
308
309
310 // render one row in the mailbox table (this could be called from one of several places)
311 function mail_render_row(msg, is_selected) {
312         let row = "<tr "
313                 + "id=\"ctdl-msgsum-" + msg["msgnum"] + "\" "
314                 + (is_selected ? "class=\"ctdl-mail-selected\" " : "")
315                 + "onClick=\"click_message(event," + msg["msgnum"] + ");\""
316                 + "onselectstart=\"return false;\" "
317                 + "draggable=\"true\" "
318                 + "ondragstart=\"mail_dragstart(event)\" "
319                 + ">"
320                 + "<td class=\"ctdl-mail-subject\">" + msg["subject"] + "</td>"
321                 + "<td class=\"ctdl-mail-sender\">" + msg["author"] + "</td>"
322                 + "<td class=\"ctdl-mail-date\">" + string_timestamp(msg["time"],1) + "</td>"
323                 + "<td class=\"ctdl-mail-msgnum\">" + msg["msgnum"] + "</td>"
324                 + "</tr>";
325         return(row);
326 }
327
328
329 // RENDERER FOR THIS VIEW
330 function view_render_mail() {
331         // Put the "enter new message" button into the topbar
332         document.getElementById("ctdl-newmsg-button").innerHTML = "<i class=\"fa fa-edit\"></i>" + _("Write mail");
333         document.getElementById("ctdl-newmsg-button").style.display = "block";
334
335         // Put the "delete message(s)" button into the topbar
336         let d = document.getElementById("ctdl-delete-button");
337         d.innerHTML = "<i class=\"fa fa-trash\"></i>" + _("Delete");
338         d.style.display = "block";
339         //d.addEventListener("click", mail_delete_selected);
340
341         document.getElementById("ctdl-main").innerHTML
342                 = "<div id=\"ctdl-mailbox-grid-container\" class=\"ctdl-mailbox-grid-container\">"
343                 + "<div id=\"ctdl-mailbox-pane\" class=\"ctdl-mailbox-pane\"></div>"
344                 + "<div id=\"ctdl-mailbox-reading-pane\" class=\"ctdl-mailbox-reading-pane\"></div>"
345                 + "</div>"
346         ;
347
348         highest_mailnum = 0;                                    // Keep track of highest message number to track newly arrived messages
349         render_mailbox_display(newmail_notify.NO);
350         try {                                                   // if this was already set up, clear it so there aren't multiple
351                 clearInterval(RefreshMailboxInterval);
352         }
353         catch {
354         }
355         RefreshMailboxInterval = setInterval(refresh_mail_display, 10000);
356 }
357
358
359 // Refresh the mailbox, either for the first time or whenever needed
360 function refresh_mail_display() {
361         // If the "ctdl-mailbox-pane" no longer exists, the user has navigated to a different part of the site,
362         // so cancel the refresh.
363         try {
364                 document.getElementById("ctdl-mailbox-pane").innerHTML;
365         }
366         catch {
367                 clearInterval(RefreshMailboxInterval);
368                 return;
369         }
370
371         // Ask the server if the room has been written to since our last look at it.
372         url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/stat";
373         fetch_stat = async() => {
374                 response = await fetch(url);
375                 stat = await(response.json());
376                 if (stat.room_mtime > room_mtime) {                     // FIXME commented out to force refreshes
377                         room_mtime = stat.room_mtime;
378                         render_mailbox_display(newmail_notify.YES);
379                 }
380         }
381         fetch_stat();
382 }
383
384
385 // This is where the rendering of the message list in the mailbox view is performed.
386 // Set notify to newmail_notify.NO or newmail_notify.YES depending on whether we are interested in the arrival of new messages.
387 function render_mailbox_display(notify) {
388
389         url = "/ctdl/r/" + escapeHTMLURI(current_room) + "/mailbox";
390         fetch_mailbox = async() => {
391                 response = await fetch(url);
392                 msgs = await(response.json());
393                 if (response.ok) {
394                         var previously_selected = [];
395                         var oldtable = document.getElementById("ctdl-onscreen-mailbox");
396                         var i, row;
397
398                         // If one or more messages was already selected, remember them so we can re-select them
399                         if ( (displayed_message > 0) && (oldtable) ) {
400                                 for (i=0; row=oldtable.rows[i]; ++i) {
401                                         if (row.classList.contains("ctdl-mail-selected")) {
402                                                 previously_selected.push(parseInt(row["id"].substring(12)));
403                                         }
404                                 }
405                         }
406
407                         // begin rendering the mailbox table
408                         box =   "<table id=\"ctdl-onscreen-mailbox\" class=\"ctdl-mailbox-table\" width=100%><tr>"
409                                 + "<th>" + _("Subject") + "</th>"
410                                 + "<th>" + _("Sender") + "</th>"
411                                 + "<th>" + _("Date") + "</th>"
412                                 + "<th>#</th>"
413                                 + "</tr>";
414
415                         for (let i=0; i<msgs.length; ++i) {
416                                 let m = parseInt(msgs[i].msgnum);
417                                 let s = (previously_selected.includes(m));
418                                 box += mail_render_row(msgs[i], s);
419                                 if (m > highest_mailnum) {
420                                         highest_mailnum = m;
421                                 }
422                         }
423
424                         box +=  "</table>";
425                         document.getElementById("ctdl-mailbox-pane").innerHTML = box;
426                         document.addEventListener("keydown", mail_keypress);
427                 }
428         }
429         fetch_mailbox();
430 }
431
432
433 // Compose a new mail message (called by the Reply button here, or by the dispatcher in views.js)
434 function mail_compose(is_quoted, references, quoted_msgnum, m_to, m_cc, m_subject) {
435         // m_to will be an array of zero or more recipients for the To: field.  Convert it to a string.
436         if (m_to) {
437                 m_to = Array.from(new Set(m_to));       // remove dupes
438                 m_to_str = "";
439                 for (i=0; i<m_to.length; ++i) {
440                         if (i > 0) {
441                                 m_to_str += ", ";
442                         }
443                         m_to_str += m_to[i].replaceAll("<", "&lt;").replaceAll(">", "&gt;");
444                 }
445         }
446         else {
447                 m_to_str = "";
448         }
449
450         // m_to will be an array of zero or more recipients for the Cc: field.  Convert it to a string.
451         if (m_cc) {
452                 m_cc = Array.from(new Set(m_cc));       // remove dupes
453                 m_cc_str = "";
454                 for (i=0; i<m_cc.length; ++i) {
455                         if (i > 0) {
456                                 m_cc_str += ", ";
457                         }
458                         m_cc_str += m_cc[i].replaceAll("<", "&lt;").replaceAll(">", "&gt;");
459                 }
460         }
461         else {
462                 m_cc_str = "";
463         }
464
465         quoted_div_name = randomString();
466
467         // Make the "Write mail" button disappear.  We're already there!
468         document.getElementById("ctdl-newmsg-button").style.display = "none";
469
470         // is_quoted    true or false depending on whether the user selected "reply quoted" (is this appropriate for mail?)
471         // references   list of references, be sure to use this in a reply
472         // msgid        if a reply, the msgid of the most recent message in the chain, the one to which we are replying
473
474         // Now display the screen.
475         compose_screen =
476                 // Hidden values that we are storing right here in the document tree for later
477                   "<input id=\"ctdl_mc_is_quoted\" style=\"display:none\" value=\"" + is_quoted + "\"></input>"
478                 + "<input id=\"ctdl_mc_references\" style=\"display:none\" value=\"" + references + "\"></input>"
479
480                 // Header fields, the composition window, and the button bar are arranged using a Grid layout.
481                 + "<div id=\"ctdl-compose-mail\" class=\"ctdl-compose-mail\">"
482
483                 // Visible To: field, plus a box to make the CC/BCC lines appear
484                 + "<div class=\"ctdl-compose-to-label\">" + _("To:") + "</div>"
485                 + "<div class=\"ctdl-compose-to-line\">"
486                 + "<div class=\"ctdl-compose-to-field\" id=\"ctdl-compose-to-field\" contenteditable=\"true\">" + m_to_str + "</div>"
487                 + "<div class=\"ctdl-cc-bcc-buttons ctdl-msg-button\" id=\"ctdl-cc-bcc-buttons\" "
488                 + "onClick=\"make_cc_bcc_visible()\">"
489                 + _("CC:") + "/" + _("BCC:") + "</div>"
490                 + "</div>"
491
492                 // CC/BCC
493                 + "<div class=\"ctdl-compose-cc-label\" id=\"ctdl-compose-cc-label\">" + _("CC:") + "</div>"
494                 + "<div class=\"ctdl-compose-cc-field\" id=\"ctdl-compose-cc-field\" contenteditable=\"true\">" + m_cc_str + "</div>"
495                 + "<div class=\"ctdl-compose-bcc-label\" id=\"ctdl-compose-bcc-label\">" + _("BCC:") + "</div>"
496                 + "<div class=\"ctdl-compose-bcc-field\" id=\"ctdl-compose-bcc-field\" contenteditable=\"true\"></div>"
497
498                 // Visible subject field
499                 + "<div class=\"ctdl-compose-subject-label\">" + _("Subject:") + "</div>"
500                 + "<div class=\"ctdl-compose-subject-field\" id=\"ctdl-compose-subject-field\" contenteditable=\"true\">" + m_subject + "</div>"
501
502                 // Message composition box
503                 + "<div class=\"ctdl-compose-message-box\" id=\"ctdl-editor-body\" contenteditable=\"true\">"
504         ;
505
506         if (is_quoted) {
507                 compose_screen += "<br><br><blockquote><div id=\"" + quoted_div_name + "\"></div></blockquote>";
508         }
509
510         compose_screen +=
511                   "</div>"
512
513                 // The button bar is a Grid element, and is also a Flexbox container.
514                 + "<div class=\"ctdl-compose-toolbar\">"
515                 + "<span class=\"ctdl-msg-button\" onclick=\"mail_send_message()\"><i class=\"fa fa-paper-plane\" style=\"color:green\"></i> " + _("Send message") + "</span>"
516                 + "<span class=\"ctdl-msg-button\">" + _("Save to Drafts") + "</span>"
517                 + "<span class=\"ctdl-msg-button\" onClick=\"show_or_hide_attachments()\"><i class=\"fa fa-paperclip\" style=\"color:grey\"></i> " + _("Attachments:") + " <span id=\"ctdl_num_attachments\">" + num_attachments + "</span></span>"
518                 + "<span class=\"ctdl-msg-button\">" + _("Contacts") + "</span>"
519                 + "<span class=\"ctdl-msg-button\" onClick=\"document.getElementById('ctdl_big_modal').style.display='none';gotoroom(current_room)\"><i class=\"fa fa-trash\" style=\"color:red\"></i> " + _("Cancel") + "</span>"
520                 + "</div>"
521         ;
522
523         document.getElementById("ctdl-main").innerHTML = compose_screen;
524         mail_display_message(quoted_msgnum, document.getElementById(quoted_div_name), 0);
525         if (m_cc) {
526                 document.getElementById("ctdl-compose-cc-label").style.display = "block";
527                 document.getElementById("ctdl-compose-cc-field").style.display = "block";
528         }
529
530         // Now the compose screen is set up properly; set up the attachments modal in case the user wants it
531
532         document.getElementById("ctdl_big_modal").innerHTML = ""
533                 + "<div id=\"ctdl_attachments_outer\">"
534                 + "<div id=\"ctdl_attachments_title\" class=\"ctdl-compose-attachments-title\">"
535                 + "<div><h1><i class=\"fa fa-paperclip\" style=\"color:grey\"></i> " + _("Attachments:") + "</h1></div>"
536                 + "<div><h1><i class=\"fas fa-window-close\" style=\"color:red\" onClick=\"show_or_hide_attachments()\"></i></h1></div>"
537                 + "</div>"
538                 + "<br>"
539                 + "this is the attachments modal.  there are many like it, but this one is mine."
540                 + "</div>"
541         ;
542
543 }
544
545
546 // Show or hide the attachments window in the composer
547 function show_or_hide_attachments() {
548
549         if (document.getElementById("ctdl_big_modal").style.display == "block") {
550                 document.getElementById("ctdl_big_modal").style.display = "none";
551         }
552         else {
553                 document.getElementById("ctdl_big_modal").style.display = "block";
554                 document.getElementById("ctdl_attachments_outer").style.width =
555                         Math.trunc((document.getElementById("ctdl-editor-body").getBoundingClientRect().width) * 0.90).toString() + "px";
556                 document.getElementById("ctdl_attachments_outer").style.height =
557                         Math.trunc((document.getElementById("ctdl-editor-body").getBoundingClientRect().height) * 0.90).toString() + "px";
558         }
559
560 }
561
562
563 // Called when the user clicks the button to make the hidden "CC" and "BCC" lines appear.
564 // It is also called automatically during a Reply when CC is pre-populated.
565 function make_cc_bcc_visible() {
566         document.getElementById("ctdl-cc-bcc-buttons").style.display = "none";
567         document.getElementById("ctdl-compose-bcc-label").style.display = "block";
568         document.getElementById("ctdl-compose-bcc-field").style.display = "block";
569 }
570
571
572 // Helper function for mail_send_messages() to extract and decode metadata values.
573 function msm_field(element_name, separator) {
574         let s1 = document.getElementById(element_name).innerHTML;
575         let s2 = s1.replaceAll("|",separator);          // Replace "|" with "!" because "|" is a field separator in Citadel
576         let s3 = decodeURI(s2);
577         let s4 = document.createElement("textarea");    // This One Weird Trick Unescapes All HTML Entities
578         s4.innerHTML = s3;
579         let s5 = s4.value;
580         return(s5);
581 }
582
583
584 // Save the posted message to the server
585 function mail_send_message() {
586
587         document.body.style.cursor = "wait";
588         let url = "/ctdl/r/" + escapeHTMLURI(current_room)
589                 + "/dummy_name_for_new_mail"
590                 + "?wefw="      + msm_field("ctdl_mc_references", "!")                          // references (if present)
591                 + "&subj="      + msm_field("ctdl-compose-subject-field", " ")                  // subject (if present)
592                 + "&mailto="    + msm_field("ctdl-compose-to-field", ",")                       // To: (required)
593                 + "&mailcc="    + msm_field("ctdl-compose-cc-field", ",")                       // Cc: (if present)
594                 + "&mailbcc="   + msm_field("ctdl-compose-bcc-field", ",")                      // Bcc: (if present)
595         ;
596         boundary = randomString();
597         body_text =
598                 "--" + boundary + "\r\n"
599                 + "Content-type: text/html\r\n"
600                 + "Content-transfer-encoding: quoted-printable\r\n"
601                 + "\r\n"
602                 + quoted_printable_encode(
603                         "<html><body>" + document.getElementById("ctdl-editor-body").innerHTML + "</body></html>"
604                 ) + "\r\n"
605                 + "--" + boundary + "--\r\n"
606         ;
607
608         var request = new XMLHttpRequest();
609         request.open("PUT", url, true);
610         request.setRequestHeader("Content-type", "multipart/mixed; boundary=\"" + boundary + "\"");
611         request.onreadystatechange = function() {
612                 if (request.readyState == 4) {
613                         document.body.style.cursor = "default";
614                         if (Math.trunc(request.status / 100) == 2) {
615                                 headers = request.getAllResponseHeaders().split("\n");
616                                 for (var i in headers) {
617                                         if (headers[i].startsWith("etag: ")) {
618                                                 new_msg_num = headers[i].split(" ")[1];
619                                         }
620                                 }
621
622                                 // After saving the message, go back to the mailbox view.
623                                 gotoroom(current_room);
624
625                         }
626                         else {
627                                 error_message = request.responseText;
628                                 if (error_message.length == 0) {
629                                         error_message = _("An error has occurred.");
630                                 }
631                                 alert(error_message);                                           // editor remains open
632                         }
633                 }
634         };
635         request.send(body_text);
636 }