diff --git a/Cargo.lock b/Cargo.lock index 428e0b5..771f8af 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -187,7 +187,7 @@ dependencies = [ [[package]] name = "email2matrix-message-service" -version = "1.0.0" +version = "1.1.0" dependencies = [ "confy", "imap", diff --git a/Cargo.toml b/Cargo.toml index 783321a..023a9c4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "email2matrix-message-service" -version = "1.0.0" +version = "1.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/main.rs b/src/main.rs index d2116d7..cf22180 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,21 @@ fn main() { let args: Vec = env::args().collect(); let cfg: AppConfig = confy::load_path(Path::new(&args[1])).expect("Couldn't read config"); - let mut imap_session = get_imap_session(&cfg); + for mailbox in cfg.mailboxes.values() { + process_mailbox(&cfg.imap.ip, &mailbox); + } +} + +fn process_mailbox(imap_ip: &str, mailbox: &Mailbox) { + let mut imap_session = match get_imap_session(imap_ip, mailbox) { + Ok(x) => x, + Err(_) => { + println!("Unable to start session for {}", mailbox.user); + return; + } + }; + + imap_session.select("INBOX").unwrap(); let message = get_emails(&mut imap_session); @@ -19,7 +33,7 @@ fn main() { Some(x) => { let mut messages_sent_successfully = Vec::new(); for email in x { - let response = post_to_hookshot(&cfg.hookshot_url, &email.1); + let response = post_to_hookshot(&mailbox.hookshot_url, &email.1); match response { Ok(x) => { if x.status().is_success() { @@ -38,11 +52,10 @@ fn main() { imap_session.logout().unwrap(); } -fn get_imap_session(config: &AppConfig) -> imap::Session { - let stream = TcpStream::connect(&config.imap_ip).unwrap(); +fn get_imap_session(imap_ip: &str, mailbox: &Mailbox) -> Result, (imap::Error, imap::Client)> { + let stream = TcpStream::connect(imap_ip).unwrap(); let client = imap::Client::new(stream); - let mut imap_session = client.login(&config.imap_user, &config.imap_password).expect("Couldn't login"); - imap_session.select("INBOX").unwrap(); + let mut imap_session = client.login(&mailbox.user, &mailbox.password); return imap_session; } @@ -119,9 +132,19 @@ fn post_to_hookshot(hookshot_url: &str, email: &Email) -> reqwest::Result, +} + +#[derive(Debug, Serialize, Deserialize)] +struct Imap { + ip: String +} + +#[derive(Debug, Serialize, Deserialize)] +struct Mailbox { + user: String, + password: String, hookshot_url: String }