update code for sendmail sender lib feature

This commit is contained in:
Clément DOUIN
2022-10-12 13:59:20 +02:00
parent 98929d687b
commit bb8f63e4b0
7 changed files with 43 additions and 41 deletions
+12 -12
View File
@@ -93,7 +93,7 @@ pub fn forward<'a, P: Printer, B: Backend<'a> + ?Sized, S: Sender + ?Sized>(
.into_forward(config)?
.add_attachments(attachments_paths)?
.encrypt(encrypt);
editor::edit_msg_with_editor(
editor::edit_email_with_editor(
msg,
TplOverride::default(),
config,
@@ -184,7 +184,7 @@ pub fn mailto<'a, P: Printer, B: Backend<'a> + ?Sized, S: Sender + ?Sized>(
};
trace!("message: {:?}", msg);
editor::edit_msg_with_editor(
editor::edit_email_with_editor(
msg,
TplOverride::default(),
config,
@@ -248,7 +248,7 @@ pub fn reply<'a, P: Printer, B: Backend<'a> + ?Sized, S: Sender + ?Sized>(
.into_reply(all, config)?
.add_attachments(attachments_paths)?
.encrypt(encrypt);
editor::edit_msg_with_editor(
editor::edit_email_with_editor(
msg,
TplOverride::default(),
config,
@@ -341,7 +341,7 @@ pub fn sort<'a, P: Printer, B: Backend<'a> + ?Sized>(
/// Send a raw message.
pub fn send<'a, P: Printer, B: Backend<'a> + ?Sized, S: Sender + ?Sized>(
raw_msg: &str,
raw_email: &str,
config: &AccountConfig,
printer: &mut P,
backend: &mut B,
@@ -357,8 +357,8 @@ pub fn send<'a, P: Printer, B: Backend<'a> + ?Sized, S: Sender + ?Sized>(
let sent_folder = config.folder_alias("sent")?;
debug!("sent folder: {:?}", sent_folder);
let raw_msg = if is_tty || is_json {
raw_msg.replace("\r", "").replace("\n", "\r\n")
let raw_email = if is_tty || is_json {
raw_email.replace("\r", "").replace("\n", "\r\n")
} else {
io::stdin()
.lock()
@@ -367,10 +367,10 @@ pub fn send<'a, P: Printer, B: Backend<'a> + ?Sized, S: Sender + ?Sized>(
.collect::<Vec<String>>()
.join("\r\n")
};
trace!("raw message: {:?}", raw_msg);
let msg = Email::from_tpl(&raw_msg)?;
sender.send(&config, &msg)?;
backend.email_add(&sent_folder, raw_msg.as_bytes(), "seen")?;
trace!("raw message: {:?}", raw_email);
let email = Email::from_tpl(&raw_email)?;
sender.send(&email)?;
backend.email_add(&sent_folder, raw_email.as_bytes(), "seen")?;
Ok(())
}
@@ -384,9 +384,9 @@ pub fn write<'a, P: Printer, B: Backend<'a> + ?Sized, S: Sender + ?Sized>(
backend: &mut B,
sender: &mut S,
) -> Result<()> {
let msg = Email::default()
let email = Email::default()
.add_attachments(attachments_paths)?
.encrypt(encrypt);
editor::edit_msg_with_editor(msg, tpl, config, printer, backend, sender)?;
editor::edit_email_with_editor(email, tpl, config, printer, backend, sender)?;
Ok(())
}
+5 -6
View File
@@ -71,16 +71,15 @@ pub fn save<'a, P: Printer, B: Backend<'a> + ?Sized>(
.collect::<Vec<String>>()
.join("\n")
};
let msg = Email::from_tpl(&tpl)?.add_attachments(attachments_paths)?;
let raw_msg = msg.into_sendable_msg(config)?.formatted();
backend.email_add(mbox, &raw_msg, "seen")?;
let email = Email::from_tpl(&tpl)?.add_attachments(attachments_paths)?;
let raw_email = email.into_sendable(config)?.formatted();
backend.email_add(mbox, &raw_email, "seen")?;
printer.print_struct("Template successfully saved")
}
/// Sends a message based on a template.
pub fn send<'a, P: Printer, B: Backend<'a> + ?Sized, S: Sender + ?Sized>(
mbox: &str,
account: &AccountConfig,
attachments_paths: Vec<&str>,
tpl: &str,
printer: &mut P,
@@ -97,8 +96,8 @@ pub fn send<'a, P: Printer, B: Backend<'a> + ?Sized, S: Sender + ?Sized>(
.collect::<Vec<String>>()
.join("\n")
};
let msg = Email::from_tpl(&tpl)?.add_attachments(attachments_paths)?;
let sent_msg = sender.send(account, &msg)?;
let email = Email::from_tpl(&tpl)?.add_attachments(attachments_paths)?;
let sent_msg = sender.send(&email)?;
backend.email_add(mbox, &sent_msg, "seen")?;
printer.print_struct("Template successfully sent")
}
-1
View File
@@ -302,7 +302,6 @@ fn main() -> Result<()> {
Some(tpl::args::Cmd::Send(atts, tpl)) => {
return tpl::handlers::send(
&folder,
&account_config,
atts,
tpl,
&mut printer,
+21 -17
View File
@@ -37,14 +37,18 @@ pub fn open_with_draft() -> Result<String> {
open_with_tpl(tpl)
}
fn _edit_msg_with_editor(msg: &Email, tpl: TplOverride, config: &AccountConfig) -> Result<Email> {
let tpl = msg.to_tpl(tpl, config)?;
fn _edit_email_with_editor(
email: &Email,
tpl: TplOverride,
config: &AccountConfig,
) -> Result<Email> {
let tpl = email.to_tpl(tpl, config)?;
let tpl = open_with_tpl(tpl)?;
Email::from_tpl(&tpl).context("cannot parse message from template")
Email::from_tpl(&tpl).context("cannot parse email from template")
}
pub fn edit_msg_with_editor<'a, P: Printer, B: Backend<'a> + ?Sized, S: Sender + ?Sized>(
mut msg: Email,
pub fn edit_email_with_editor<'a, P: Printer, B: Backend<'a> + ?Sized, S: Sender + ?Sized>(
mut email: Email,
tpl: TplOverride,
config: &AccountConfig,
printer: &mut P,
@@ -60,11 +64,11 @@ pub fn edit_msg_with_editor<'a, P: Printer, B: Backend<'a> + ?Sized, S: Sender +
Ok(choice) => match choice {
PreEditChoice::Edit => {
let tpl = open_with_draft()?;
msg.merge_with(Email::from_tpl(&tpl)?);
email.merge_with(Email::from_tpl(&tpl)?);
break;
}
PreEditChoice::Discard => {
msg.merge_with(_edit_msg_with_editor(&msg, tpl.clone(), config)?);
email.merge_with(_edit_email_with_editor(&email, tpl.clone(), config)?);
break;
}
PreEditChoice::Quit => return Ok(()),
@@ -76,35 +80,35 @@ pub fn edit_msg_with_editor<'a, P: Printer, B: Backend<'a> + ?Sized, S: Sender +
}
}
} else {
msg.merge_with(_edit_msg_with_editor(&msg, tpl.clone(), config)?);
email.merge_with(_edit_email_with_editor(&email, tpl.clone(), config)?);
}
loop {
match choice::post_edit() {
Ok(PostEditChoice::Send) => {
printer.print_str("Sending message")?;
let sent_msg: Vec<u8> = sender.send(config, &msg)?;
printer.print_str("Sending email")?;
let sent_email: Vec<u8> = sender.send(&email)?;
let sent_folder = config.folder_alias("sent")?;
printer.print_str(format!("Adding message to the {:?} folder…", sent_folder))?;
backend.email_add(&sent_folder, &sent_msg, "seen")?;
printer.print_str(format!("Adding email to the {:?} folder…", sent_folder))?;
backend.email_add(&sent_folder, &sent_email, "seen")?;
remove_local_draft()?;
printer.print_struct("Done!")?;
break;
}
Ok(PostEditChoice::Edit) => {
msg.merge_with(_edit_msg_with_editor(&msg, tpl.clone(), config)?);
email.merge_with(_edit_email_with_editor(&email, tpl.clone(), config)?);
continue;
}
Ok(PostEditChoice::LocalDraft) => {
printer.print_struct("Message successfully saved locally")?;
printer.print_struct("Email successfully saved locally")?;
break;
}
Ok(PostEditChoice::RemoteDraft) => {
let tpl = msg.to_tpl(TplOverride::default(), config)?;
let draft_folder = config.folder_alias("draft")?;
let tpl = email.to_tpl(TplOverride::default(), config)?;
let draft_folder = config.folder_alias("drafts")?;
backend.email_add(&draft_folder, tpl.as_bytes(), "seen draft")?;
remove_local_draft()?;
printer.print_struct(format!("Message successfully saved to {}", draft_folder))?;
printer.print_struct(format!("Email successfully saved to {}", draft_folder))?;
break;
}
Ok(PostEditChoice::Discard) => {