// // imag - the personal information management suite for the commandline // Copyright (C) 2015-2020 Matthias Beyer and contributors // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; version // 2.1 of the License. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // use std::path::PathBuf; use handlebars::Handlebars; use anyhow::Result; use clap::ArgMatches; use libimagrt::runtime::Runtime; #[derive(Debug, Serialize, Deserialize, Partial, AddGetter)] #[location = "mail"] pub struct MailConfig { #[serde(rename = "list_format")] #[get] list_format: String, #[serde(rename = "notmuch_database")] #[get] notmuch_database_path: PathBuf, #[serde(rename = "import_tag")] #[get] import_tag: Option, #[serde(rename = "import_notmuch_tags")] #[get] import_notmuch_tags: bool, #[serde(rename = "edit_headers")] #[get] edit_headers: bool, #[serde(rename = "default_template")] #[get] default_template: String, #[serde(rename = "header_template")] #[get] header_template: String, #[serde(rename = "from_address")] #[get] from_address: String, /// The location where the mail is stored after it is written, but before it is sent. /// /// This is where imag puts the mail, intended to be send at some point. /// The mail-send-script should pick up each mail in here and process it. /// Not to be confused with "drafts" which are not intended for sending yet. #[serde(rename = "outgoing_maildir")] #[get] outgoing_maildir: PathBuf, /// Path script to run before writing outgoing mail to outgoing_maildir (optional) #[serde(rename = "pre_outgoing")] #[get] pre_outgoing: Option, /// Path script to run after writing outgoing mail to outgoing_maildir (optional) #[serde(rename = "post_outgoing")] #[get] post_outgoing: Option, /// The location where to put drafted mails #[serde(rename = "draft_maildir")] #[get] draft_maildir: PathBuf, /// Path script to run before writing draft mail to draft_maildir (optional) #[serde(rename = "pre_draft")] #[get] pre_draft: Option, /// Path script to run after writing draft mail to draft_maildir (optional) #[serde(rename = "post_draft")] #[get] post_draft: Option, /// The location where to put sent mails #[serde(rename = "sent_maildir")] #[get] sent_maildir: PathBuf, /// Path script to run before writing mail to sent_maildir (optional) #[serde(rename = "pre_sent")] #[get] pre_sent: Option, /// Path script to run after writing mail to sent_maildir (optional) #[serde(rename = "post_sent")] #[get] post_sent: Option, // // Receive-config // /// Path to script to receive email /// /// Script should output absolute pathes to emails linewise /// /// STDERR of the script is logged by imag. #[serde(rename = "recv_script")] #[get] recv_script: PathBuf, /// Whether to show output for each mail #[get] recv_output: bool, /// Output format for each mail #[get] recv_output_format: Option, /// Whether to show summary output after receiving #[get] recv_output_summary: bool, /// Summary output format after receiving #[get] recv_output_summary_format: Option, /// Script to be invoked before recving. (optional) /// /// STDERR of the script is logged by imag. #[serde(rename = "pre_recv_script")] #[get] pre_recv_script: Option, /// Script to be invoked after recving. (optional) /// /// STDERR of the script is logged by imag. #[serde(rename = "post_recv_script")] #[get] post_recv_script: Option, /// Whether to automatically import new emails #[serde(rename = "auto_import")] #[get] auto_import: bool, // // Send config // /// Path to script to send email /// /// The script gets the pathes to the email file on stdin. /// This script is called once for each mail. /// /// If the script exits with a nonzero exit code, the sending is expected to be failed. The mail /// is not considered sent. /// /// The script is expected to not alter the mails and not move them. /// /// STDERR of the script is logged by imag. #[serde(rename = "send_script")] #[get] send_script: PathBuf, /// Whether to show a progress bar when sending #[serde(rename = "send_progress")] #[get] send_progress: bool, /// If false, the invokation of the send scripts are chained. If one fails, the chain is /// aborted. /// /// If true, the script is invoked in parallel for all mails. If one fails, the others are still /// running. #[serde(rename = "send_parallel")] #[get] send_parallel: bool, /// Script to be invoked before sending. (optional) /// /// Invoked exactly the same as the send_script (once for each mail, either in parallel or in /// sequence). If this exits with a nonzero exit code, the send_script is not invoked. The /// operation is aborted in case of sequencial sending. /// /// STDERR of the script is logged by imag. #[serde(rename = "pre_send_script")] #[get] pre_send_script: Option, /// Script to be invoked after sending. (optional) /// /// Exit code ignored. /// /// STDERR of the script is logged by imag. #[serde(rename = "post_send_script")] #[get] post_send_script: Option, } impl MailConfig { pub fn get_list_format_or_cli(&self, scmd: &ArgMatches) -> Result { let fmt = scmd .value_of("format") .map(String::from) .unwrap_or_else(|| self.list_format.clone()); let mut hb = Handlebars::new(); hb.register_template_string("format", fmt)?; hb.register_escape_fn(::handlebars::no_escape); ::libimaginteraction::format::register_all_color_helpers(&mut hb); ::libimaginteraction::format::register_all_format_helpers(&mut hb); Ok(hb) } /// Get the notmuch database path either from CLI or from config pub fn get_notmuch_database_path_or_cli(&self, rt: &Runtime) -> PathBuf { if let Some(pb) = rt.cli() .value_of("database_path") .map(String::from) .map(PathBuf::from) { pb } else { self.notmuch_database_path.clone() } } }