From bb8f63e4b09f3225bfa7089e385fcd8a83afaa63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20DOUIN?= Date: Wed, 12 Oct 2022 13:59:20 +0200 Subject: [PATCH] update code for sendmail sender lib feature --- .gitignore | 3 +++ Cargo.lock | 1 - Cargo.toml | 6 ++---- src/domain/email/handlers.rs | 24 +++++++++++------------ src/domain/tpl/handlers.rs | 11 +++++------ src/main.rs | 1 - src/ui/editor.rs | 38 ++++++++++++++++++++---------------- 7 files changed, 43 insertions(+), 41 deletions(-) diff --git a/.gitignore b/.gitignore index e4a6d9f..f2d0780 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# Cargo config directory +.cargo/ + # Cargo build directory target/ debug/ diff --git a/Cargo.lock b/Cargo.lock index 7398d51..d1307e2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index f07c903..fc7cfe2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 } diff --git a/src/domain/email/handlers.rs b/src/domain/email/handlers.rs index b632907..2e88280 100644 --- a/src/domain/email/handlers.rs +++ b/src/domain/email/handlers.rs @@ -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::>() .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(()) } diff --git a/src/domain/tpl/handlers.rs b/src/domain/tpl/handlers.rs index 0fcb078..0c2f0a7 100644 --- a/src/domain/tpl/handlers.rs +++ b/src/domain/tpl/handlers.rs @@ -71,16 +71,15 @@ pub fn save<'a, P: Printer, B: Backend<'a> + ?Sized>( .collect::>() .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::>() .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") } diff --git a/src/main.rs b/src/main.rs index de4360a..2997150 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, diff --git a/src/ui/editor.rs b/src/ui/editor.rs index 0557028..e78ac4c 100644 --- a/src/ui/editor.rs +++ b/src/ui/editor.rs @@ -37,14 +37,18 @@ pub fn open_with_draft() -> Result { open_with_tpl(tpl) } -fn _edit_msg_with_editor(msg: &Email, tpl: TplOverride, config: &AccountConfig) -> Result { - let tpl = msg.to_tpl(tpl, config)?; +fn _edit_email_with_editor( + email: &Email, + tpl: TplOverride, + config: &AccountConfig, +) -> Result { + 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 = sender.send(config, &msg)?; + printer.print_str("Sending email…")?; + let sent_email: Vec = 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) => {