summaryrefslogtreecommitdiffstats
path: root/bin/domain/imag-mail/src/config.rs
blob: aaa47013f35db7389f76b9537adb5e22e082ceb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> 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 failure::Fallible as Result;
use clap::ArgMatches;

use libimagrt::runtime::Runtime;

#[derive(Debug, Serialize, Deserialize, Partial)]
#[location = "mail"]
pub struct MailConfig {
    #[serde(rename = "list_format")]
    list_format: String,

    #[serde(rename = "notmuch_database")]
    notmuch_database_path: PathBuf,

    #[serde(rename = "import_tag")]
    import_tag: Option<String>,

    #[serde(rename = "import_notmuch_tags")]
    import_notmuch_tags: bool,

    #[serde(rename = "edit_headers")]
    edit_headers: bool,

    #[serde(rename = "default_template")]
    default_template: String,

    #[serde(rename = "header_template")]
    header_template: String,

    #[serde(rename = "from_address")]
    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")]
    outgoing_maildir: PathBuf,

    /// Path script to run before writing outgoing mail to outgoing_maildir (optional)
    #[serde(rename = "pre_outgoing")]
    pre_outgoing: Option<PathBuf>,

    /// Path script to run after writing outgoing mail to outgoing_maildir (optional)
    #[serde(rename = "post_outgoing")]
    post_outgoing: Option<PathBuf>,

    /// The location where to put drafted mails
    #[serde(rename = "draft_maildir")]
    draft_maildir: PathBuf,

    /// Path script to run before writing draft mail to draft_maildir (optional)
    #[serde(rename = "pre_draft")]
    pre_draft: Option<PathBuf>,

    /// Path script to run after writing draft mail to draft_maildir (optional)
    #[serde(rename = "post_draft")]
    post_draft: Option<PathBuf>,

    /// The location where to put sent mails
    #[serde(rename = "sent_maildir")]
    sent_maildir: PathBuf,

    /// Path script to run before writing mail to sent_maildir (optional)
    #[serde(rename = "pre_sent")]
    pre_sent: Option<PathBuf>,

    /// Path script to run after writing mail to sent_maildir (optional)
    #[serde(rename = "post_sent")]
    post_sent: Option<PathBuf>,


    //
    // 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")]
    recv_script: PathBuf,

    /// Whether to show output for each mail
    recv_output: bool,

    /// Output format for each mail
    recv_output_format: Option<String>,

    /// Whether to show summary output after receiving
    recv_output_summary: bool,

    /// Summary output format after receiving
    recv_output_summary_format: Option<String>,

    /// Script to be invoked before recving. (optional)
    ///
    /// STDERR of the script is logged by imag.
    #[serde(rename = "pre_recv_script")]
    pre_recv_script: Option<PathBuf>,

    /// Script to be invoked after recving. (optional)
    ///
    /// STDERR of the script is logged by imag.
    #[serde(rename = "post_recv_script")]
    post_recv_script: Option<PathBuf>,

    /// Whether to automatically import new emails
    #[serde(rename = "auto_import")]
    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")]
    send_script: PathBuf,

    /// Whether to show a progress bar when sending
    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")]
    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")]
    pre_send_script: Option<PathBuf>,

    /// Script to be invoked after sending. (optional)
    ///
    /// Exit code ignored.
    ///
    /// STDERR of the script is logged by imag.
    #[serde(rename = "post_send_script")]
    post_send_script: Option<PathBuf>,
}

impl MailConfig {

    pub fn get_list_format_or_cli(&self, scmd: &ArgMatches) -> Result<Handlebars> {
        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()
        }
    }

    pub fn get_import_tag(&self) -> Option<&String> {
        self.import_tag.as_ref()
    }

    pub fn get_import_notmuch_tags(&self) -> bool {
        self.import_notmuch_tags
    }

    pub fn get_edit_headers(&self) -> bool {
        self.edit_headers
    }

    pub fn get_default_template(&self) -> &String {
        &self.default_template
    }

    pub fn get_header_template(&self) -> &String {
        &self.header_template
    }

    pub fn get_from_address(&self) -> &String {
        &self.from_address
    }
}