Use a file to handle multiple patterns

Signed-off-by: baalajimaestro <me@baalajimaestro.me>
This commit is contained in:
baalajimaestro 2022-08-22 11:10:04 +05:30
parent bd5b25a9b8
commit 2ff13e7f97
Signed by: baalajimaestro
GPG key ID: F93C394FE9BBAFD5

View file

@ -8,6 +8,8 @@
use imap; use imap;
use regex::Regex; use regex::Regex;
use std::fs::File;
use std::io::{self, prelude::*, BufReader};
fn main() { fn main() {
fetch_inbox_top().unwrap(); fetch_inbox_top().unwrap();
@ -17,12 +19,16 @@ fn fetch_inbox_top() -> imap::error::Result<Option<String>> {
let domain = std::env::var("IMAP_DOMAIN").unwrap_or("none".to_string()); 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 username = std::env::var("IMAP_USERNAME").unwrap_or("none".to_string());
let password = std::env::var("IMAP_PASSWORD").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 client = imap::ClientBuilder::new(domain, 993).native_tls()?;
let mut imap_session = client.login(username, password).map_err(|e| e.0)?; let mut imap_session = client.login(username, password).map_err(|e| e.0)?;
let inbox = imap_session.select("Inbox")?; let inbox = imap_session.select("Inbox")?;
let re = Regex::new(format!("({})+", &pattern).as_str()).unwrap();
let subject = Regex::new(r"Subject: (.*)").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 { for i in 1 as u32..inbox.exists {
let messages = imap_session.fetch((inbox.exists - i).to_string(), "RFC822")?; let messages = imap_session.fetch((inbox.exists - i).to_string(), "RFC822")?;
let message = if let Some(m) = messages.iter().next() { let message = if let Some(m) = messages.iter().next() {
@ -40,6 +46,7 @@ fn fetch_inbox_top() -> imap::error::Result<Option<String>> {
println!("Deleted Mail with Subject: {}", &subject_re[1]); println!("Deleted Mail with Subject: {}", &subject_re[1]);
} }
} }
}
imap_session.expunge().unwrap(); imap_session.expunge().unwrap();
imap_session.logout()?; imap_session.logout()?;
Ok(Some("logout".to_string())) Ok(Some("logout".to_string()))