add feature to process multiple mailboxes #3

Merged
c4181 merged 3 commits from process-multiple-mailboxes into main 2024-09-21 21:15:19 +00:00
3 changed files with 34 additions and 11 deletions
Showing only changes of commit 6de17253d8 - Show all commits

2
Cargo.lock generated
View file

@ -187,7 +187,7 @@ dependencies = [
[[package]]
name = "email2matrix-message-service"
version = "1.0.0"
version = "1.1.0"
dependencies = [
"confy",
"imap",

View file

@ -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

View file

@ -11,7 +11,21 @@ fn main() {
let args: Vec<String> = 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<TcpStream> {
let stream = TcpStream::connect(&config.imap_ip).unwrap();
fn get_imap_session(imap_ip: &str, mailbox: &Mailbox) -> Result<imap::Session<TcpStream>, (imap::Error, imap::Client<TcpStream>)> {
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<Respon
#[derive(Debug, Serialize, Deserialize)]
struct AppConfig {
imap_ip: String,
imap_user: String,
imap_password: String,
imap: Imap,
mailboxes: HashMap<String, Mailbox>,
}
#[derive(Debug, Serialize, Deserialize)]
struct Imap {
ip: String
}
#[derive(Debug, Serialize, Deserialize)]
struct Mailbox {
user: String,
password: String,
hookshot_url: String
}