ui: add format=flowed if text/plain att is the only one

memfd
Manos Pitsidianakis 2019-12-06 16:34:31 +02:00
parent c431fb6dff
commit d048d8566d
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
2 changed files with 86 additions and 62 deletions

View File

@ -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<AttachmentBuilder>) {
use ContentType::*;
let boundary = ContentType::make_boundary(&parts);
ret.extend(
format!(
@ -322,18 +324,27 @@ fn build_multipart(ret: &mut String, kind: MultipartType, parts: Vec<AttachmentB
ret.push_str("--");
ret.extend(boundary.chars());
ret.push('\n');
match sub.content_type {
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(sub.raw()));
ret.push_str(&String::from_utf8_lossy(a.raw()));
ret.push('\n');
}
Text { .. } => {
ret.extend(sub.build().into_raw().chars());
ret.extend(a.build().into_raw().chars());
ret.push('\n');
}
Multipart {
@ -354,39 +365,34 @@ fn build_multipart(ret: &mut String, kind: MultipartType, parts: Vec<AttachmentB
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_str(&String::from_utf8_lossy(a.raw()));
ret.push('\n');
}
_ => {
let content_transfer_encoding: ContentTransferEncoding =
ContentTransferEncoding::Base64;
if let Some(name) = sub.content_type().name() {
if let Some(name) = a.content_type().name() {
ret.extend(
format!(
"Content-Type: {}; name=\"{}\"; charset=\"utf-8\"\n",
sub.content_type(),
a.content_type(),
name
)
.chars(),
);
} else {
ret.extend(
format!("Content-Type: {}; charset=\"utf-8\"\n", sub.content_type())
.chars(),
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(sub.raw()).trim());
ret.push_str(&BASE64_MIME.encode(a.raw()).trim());
ret.push('\n');
}
}
}
ret.push_str("--");
ret.extend(boundary.chars());
ret.push_str("--\n");
}
#[cfg(test)]

View File

@ -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