summaryrefslogtreecommitdiffstats
path: root/bin/domain/imag-mail/src/import.rs
blob: aded73be1a335e6a525fc6db476bfc48ead6f1d1 (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
//
// 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 anyhow::Result;

use libimagrt::runtime::Runtime;
use toml_query::read::TomlValueReadExt;
use libimagmail::store::MailStore;
use libimagmail::notmuch::connection::NotmuchConnection;
use libimagentrytag::tag::is_tag_str;

use config::MailConfig;

#[allow(clippy::let_and_return)]
pub fn import(rt: &Runtime) -> Result<()> {
    let config             = rt.config()
        .ok_or_else(|| anyhow!("Configuration missing"))?
        .read_partial::<MailConfig>()?
        .ok_or_else(|| anyhow!("Configuration for \"mail\" missing"))?;
    let scmd               = rt.cli().subcommand_matches("import").unwrap();
    let query              = scmd.value_of("query").unwrap();
    let quick              = scmd.is_present("quick");
    let store              = rt.store();
    let notmuch_path       = config.get_notmuch_database_path_or_cli(rt);
    let notmuch_connection = NotmuchConnection::open(notmuch_path)?;
    let import_nm_tags     = config.get_import_notmuch_tags();

    if let Some(ref tag) = config.get_import_tag() {
        let _ = is_tag_str(tag)?;
    }

    debug!("Importing mail with query = '{}'", query);

    let r = store
        .with_connection(&notmuch_connection)
        .import_with_query(query, config.get_import_tag().clone(), *import_nm_tags, quick)?
        .into_iter()
        .map(|fle| rt.report_touched(fle.get_location()))
        .collect::<Result<Vec<_>>>()
        .map(|_| ());

    r
}