add feature to process multiple mailboxes #3
3 changed files with 34 additions and 11 deletions
2
Cargo.lock
generated
2
Cargo.lock
generated
|
|
@ -187,7 +187,7 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "email2matrix-message-service"
|
name = "email2matrix-message-service"
|
||||||
version = "1.0.0"
|
version = "1.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"confy",
|
"confy",
|
||||||
"imap",
|
"imap",
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "email2matrix-message-service"
|
name = "email2matrix-message-service"
|
||||||
version = "1.0.0"
|
version = "1.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# 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 args: Vec<String> = env::args().collect();
|
||||||
let cfg: AppConfig = confy::load_path(Path::new(&args[1])).expect("Couldn't read config");
|
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);
|
let message = get_emails(&mut imap_session);
|
||||||
|
|
||||||
|
|
@ -19,7 +33,7 @@ fn main() {
|
||||||
Some(x) => {
|
Some(x) => {
|
||||||
let mut messages_sent_successfully = Vec::new();
|
let mut messages_sent_successfully = Vec::new();
|
||||||
for email in x {
|
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 {
|
match response {
|
||||||
Ok(x) => {
|
Ok(x) => {
|
||||||
if x.status().is_success() {
|
if x.status().is_success() {
|
||||||
|
|
@ -38,11 +52,10 @@ fn main() {
|
||||||
imap_session.logout().unwrap();
|
imap_session.logout().unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_imap_session(config: &AppConfig) -> imap::Session<TcpStream> {
|
fn get_imap_session(imap_ip: &str, mailbox: &Mailbox) -> Result<imap::Session<TcpStream>, (imap::Error, imap::Client<TcpStream>)> {
|
||||||
let stream = TcpStream::connect(&config.imap_ip).unwrap();
|
let stream = TcpStream::connect(imap_ip).unwrap();
|
||||||
let client = imap::Client::new(stream);
|
let client = imap::Client::new(stream);
|
||||||
let mut imap_session = client.login(&config.imap_user, &config.imap_password).expect("Couldn't login");
|
let mut imap_session = client.login(&mailbox.user, &mailbox.password);
|
||||||
imap_session.select("INBOX").unwrap();
|
|
||||||
|
|
||||||
return imap_session;
|
return imap_session;
|
||||||
}
|
}
|
||||||
|
|
@ -119,9 +132,19 @@ fn post_to_hookshot(hookshot_url: &str, email: &Email) -> reqwest::Result<Respon
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
struct AppConfig {
|
struct AppConfig {
|
||||||
imap_ip: String,
|
imap: Imap,
|
||||||
imap_user: String,
|
mailboxes: HashMap<String, Mailbox>,
|
||||||
imap_password: String,
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
struct Imap {
|
||||||
|
ip: String
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
struct Mailbox {
|
||||||
|
user: String,
|
||||||
|
password: String,
|
||||||
hookshot_url: String
|
hookshot_url: String
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue