From d048d8566d63c09736b94edf6044fcf9db96bf20 Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Fri, 6 Dec 2019 16:34:31 +0200 Subject: [PATCH] ui: add format=flowed if text/plain att is the only one --- melib/src/email/compose.rs | 130 ++++++++++++++++-------------- ui/src/components/mail/compose.rs | 18 +++++ 2 files changed, 86 insertions(+), 62 deletions(-) diff --git a/melib/src/email/compose.rs b/melib/src/email/compose.rs index ef09e3bb..bb67a6df 100644 --- a/melib/src/email/compose.rs +++ b/melib/src/email/compose.rs @@ -272,6 +272,9 @@ impl Draft { ); ret.push('\n'); ret.push_str(&self.body); + } else if self.body.is_empty() && self.attachments.len() == 1 { + let attachment = std::mem::replace(&mut self.attachments, Vec::new()).remove(0); + print_attachment(&mut ret, &Default::default(), attachment); } else { let mut parts = Vec::with_capacity(self.attachments.len() + 1); let attachments = std::mem::replace(&mut self.attachments, Vec::new()); @@ -306,7 +309,6 @@ fn ignore_header(header: &[u8]) -> bool { } fn build_multipart(ret: &mut String, kind: MultipartType, parts: Vec) { - use ContentType::*; let boundary = ContentType::make_boundary(&parts); ret.extend( format!( @@ -322,73 +324,77 @@ fn build_multipart(ret: &mut String, kind: MultipartType, parts: Vec { - ret.push('\n'); - ret.push_str(&String::from_utf8_lossy(sub.raw())); - ret.push('\n'); - } - Text { .. } => { - ret.extend(sub.build().into_raw().chars()); - ret.push('\n'); - } - Multipart { - boundary: _boundary, - kind, - parts: subparts, - } => { - build_multipart( - ret, - kind, - subparts - .into_iter() - .map(|s| s.into()) - .collect::>(), - ); - ret.push('\n'); - } - MessageRfc822 | PGPSignature => { - ret.extend(format!("Content-Type: {}; charset=\"utf-8\"\n", kind).chars()); - ret.push('\n'); - ret.push_str(&String::from_utf8_lossy(sub.raw())); - ret.push('\n'); - } - _ => { - let content_transfer_encoding: ContentTransferEncoding = - ContentTransferEncoding::Base64; - if let Some(name) = sub.content_type().name() { - ret.extend( - format!( - "Content-Type: {}; name=\"{}\"; charset=\"utf-8\"\n", - sub.content_type(), - name - ) - .chars(), - ); - } else { - ret.extend( - format!("Content-Type: {}; charset=\"utf-8\"\n", sub.content_type()) - .chars(), - ); - } - ret.extend( - format!("Content-Transfer-Encoding: {}\n", content_transfer_encoding).chars(), - ); - ret.push('\n'); - ret.push_str(&BASE64_MIME.encode(sub.raw()).trim()); - ret.push('\n'); - } - } + print_attachment(ret, &kind, sub); } ret.push_str("--"); ret.extend(boundary.chars()); ret.push_str("--\n"); } +fn print_attachment(ret: &mut String, kind: &MultipartType, a: AttachmentBuilder) { + use ContentType::*; + match a.content_type { + ContentType::Text { + kind: crate::email::attachment_types::Text::Plain, + charset: Charset::UTF8, + parameters: ref v, + } if v.is_empty() => { + ret.push('\n'); + ret.push_str(&String::from_utf8_lossy(a.raw())); + ret.push('\n'); + } + Text { .. } => { + ret.extend(a.build().into_raw().chars()); + ret.push('\n'); + } + Multipart { + boundary: _boundary, + kind, + parts: subparts, + } => { + build_multipart( + ret, + kind, + subparts + .into_iter() + .map(|s| s.into()) + .collect::>(), + ); + ret.push('\n'); + } + MessageRfc822 | PGPSignature => { + ret.extend(format!("Content-Type: {}; charset=\"utf-8\"\n", kind).chars()); + ret.push('\n'); + ret.push_str(&String::from_utf8_lossy(a.raw())); + ret.push('\n'); + } + _ => { + let content_transfer_encoding: ContentTransferEncoding = + ContentTransferEncoding::Base64; + if let Some(name) = a.content_type().name() { + ret.extend( + format!( + "Content-Type: {}; name=\"{}\"; charset=\"utf-8\"\n", + a.content_type(), + name + ) + .chars(), + ); + } else { + ret.extend( + format!("Content-Type: {}; charset=\"utf-8\"\n", a.content_type()).chars(), + ); + } + ret.extend( + format!("Content-Transfer-Encoding: {}\n", content_transfer_encoding).chars(), + ); + ret.push('\n'); + ret.push_str(&BASE64_MIME.encode(a.raw()).trim()); + ret.push('\n'); + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/ui/src/components/mail/compose.rs b/ui/src/components/mail/compose.rs index 25c136fc..66f278b0 100644 --- a/ui/src/components/mail/compose.rs +++ b/ui/src/components/mail/compose.rs @@ -1153,6 +1153,24 @@ pub fn send_draft( return false; } draft.attachments.push(output.unwrap()); + } else { + let mut content_type = ContentType::default(); + if format_flowed { + if let ContentType::Text { + ref mut parameters, .. + } = content_type + { + parameters.push((b"format".to_vec(), b"flowed".to_vec())); + } + + let body: AttachmentBuilder = Attachment::new( + content_type, + Default::default(), + std::mem::replace(&mut draft.body, String::new()).into_bytes(), + ) + .into(); + draft.attachments.insert(0, body); + } } bytes = draft.finalise().unwrap(); stdin