From 2ff13e7f973f65d932d3014da5e3144523b366e0 Mon Sep 17 00:00:00 2001 From: baalajimaestro Date: Mon, 22 Aug 2022 11:10:04 +0530 Subject: [PATCH] Use a file to handle multiple patterns Signed-off-by: baalajimaestro --- src/main.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 694aca9..cac0e14 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,8 @@ use imap; use regex::Regex; +use std::fs::File; +use std::io::{self, prelude::*, BufReader}; fn main() { fetch_inbox_top().unwrap(); @@ -17,12 +19,16 @@ fn fetch_inbox_top() -> imap::error::Result> { let domain = std::env::var("IMAP_DOMAIN").unwrap_or("none".to_string()); let username = std::env::var("IMAP_USERNAME").unwrap_or("none".to_string()); let password = std::env::var("IMAP_PASSWORD").unwrap_or("none".to_string()); - let pattern = std::env::var("DELETE_PATTERN").unwrap_or("none".to_string()); let client = imap::ClientBuilder::new(domain, 993).native_tls()?; let mut imap_session = client.login(username, password).map_err(|e| e.0)?; let inbox = imap_session.select("Inbox")?; - let re = Regex::new(format!("({})+", &pattern).as_str()).unwrap(); let subject = Regex::new(r"Subject: (.*)").unwrap(); + let file = File::open("patterns.txt")?; + let reader = BufReader::new(file); + + for line in reader.lines() { + let re = Regex::new(format!("({})+", line?).as_str()).unwrap(); + println!("Working on Pattern: {}", re.as_str()); for i in 1 as u32..inbox.exists { let messages = imap_session.fetch((inbox.exists - i).to_string(), "RFC822")?; let message = if let Some(m) = messages.iter().next() { @@ -40,6 +46,7 @@ fn fetch_inbox_top() -> imap::error::Result> { println!("Deleted Mail with Subject: {}", &subject_re[1]); } } + } imap_session.expunge().unwrap(); imap_session.logout()?; Ok(Some("logout".to_string()))