summaryrefslogtreecommitdiffstats
path: root/tools/src/imapshell.rs
blob: e6df86b2a1a18aa901320ebe47b36ac35703da6a (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
extern crate melib;

use melib::backends::ImapType;
use melib::{futures, smol, Result};
use melib::{AccountSettings, BackendEventConsumer};

/// Opens an interactive shell on an IMAP server. Suggested use is with rlwrap(1)
///
/// # Example invocation:
/// ```sh
/// ./imapshell server_hostname server_username server_password server_port");
/// ```
///
/// `danger_accept_invalid_certs` is turned on by default, so no certificate validation is performed.

fn main() -> Result<()> {
    let mut args = std::env::args().skip(1).collect::<Vec<String>>();
    if args.len() != 4 {
        eprintln!("Usage: imapshell server_hostname server_username server_password server_port");
        std::process::exit(1);
    }

    let (a, b, c, d) = (
        std::mem::replace(&mut args[0], String::new()),
        std::mem::replace(&mut args[1], String::new()),
        std::mem::replace(&mut args[2], String::new()),
        std::mem::replace(&mut args[3], String::new()),
    );
    let set = AccountSettings {
        extra: [
            ("server_hostname".to_string(), a),
            ("server_username".to_string(), b),
            ("server_password".to_string(), c),
            ("server_port".to_string(), d),
            (
                "danger_accept_invalid_certs".to_string(),
                "true".to_string(),
            ),
        ]
        .iter()
        .cloned()
        .collect(),
        ..Default::default()
    };
    let mut imap = ImapType::new(
        &set,
        Box::new(|_| true),
        BackendEventConsumer::new(std::sync::Arc::new(|_, _| ())),
    )?;

    std::thread::spawn(move || {
        let ex = smol::Executor::new();
        futures::executor::block_on(ex.run(futures::future::pending::<()>()));
    });
    (imap.as_any_mut())
        .downcast_mut::<ImapType>()
        .unwrap()
        .shell();
    Ok(())
}