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
No known key found for this signature in database
GPG Key ID: 353E4A18EE0FAB72
7 changed files with 43 additions and 41 deletions

3
.gitignore vendored
View File

@ -1,3 +1,6 @@
# Cargo config directory
.cargo/
# Cargo build directory
target/
debug/

1
Cargo.lock generated
View File

@ -519,7 +519,6 @@ dependencies = [
[[package]]
name = "himalaya-lib"
version = "0.3.1"
source = "git+https://git.sr.ht/~soywod/himalaya-lib?branch=develop#883b4d9217e52d67596eb73b5bf77f89abdea0f6"
dependencies = [
"ammonia",
"chrono",

View File

@ -33,8 +33,7 @@ clap = { version = "2.33.3", default-features = false, features = ["suggestions"
convert_case = "0.5.0"
env_logger = "0.8.3"
erased-serde = "0.3.18"
himalaya-lib = { version = "=0.3.1", git = "https://git.sr.ht/~soywod/himalaya-lib", branch = "develop" }
# himalaya-lib = "=0.3.1"
himalaya-lib = "=0.3.1"
html-escape = "0.2.9"
lettre = { version = "=0.10.0-rc.7", features = ["serde"] }
log = "0.4.14"
@ -52,8 +51,7 @@ unicode-width = "0.1.7"
url = "2.2.2"
uuid = { version = "0.8", features = ["v4"] }
# Optional dependencies:
# [dependencies.optional]
imap = { version = "=3.0.0-alpha.4", optional = true }
imap-proto = { version = "0.14.3", optional = true }
maildir = { version = "0.6.1", optional = true }

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(())
}

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")
}

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,

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) => {