add feature to process multiple mailboxes
This commit is contained in:
parent
14435c84b8
commit
6de17253d8
3 changed files with 34 additions and 11 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -187,7 +187,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "email2matrix-message-service"
|
||||
version = "1.0.0"
|
||||
version = "1.1.0"
|
||||
dependencies = [
|
||||
"confy",
|
||||
"imap",
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
41
src/main.rs
41
src/main.rs
|
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue