diff --git a/src/email.rs b/src/email.rs index b1d56ab..91b9338 100644 --- a/src/email.rs +++ b/src/email.rs @@ -1,7 +1,20 @@ extern crate imap; extern crate native_tls; +use telegram_bot::{Api, Error}; +use crate::tele::TStruct; +use std::result::Result; +use imap::types::{Fetch, Seq}; +use imap::Session; -pub fn fetch_inbox_top(imap_username: &str, imap_password: &str) -> imap::error::Result> { + +//fn delete(seq: imap::types::Seq, s: &mut Session) -> imap::error::Result<()> { +// s.store(format!("{}", seq), "+FLAGS (\\Deleted)")?; +// s.expunge()?; +// Ok(()) +//} + + +pub fn fetch_inbox_top(imap_username: &str, imap_password: &str, telegram: &TStruct) -> imap::error::Result> { let domain = "imap.gmail.com"; let tls = native_tls::TlsConnector::builder().build().unwrap(); @@ -16,11 +29,12 @@ pub fn fetch_inbox_top(imap_username: &str, imap_password: &str) -> imap::error: .map_err(|e| e.0)?; // we want to fetch the first email in the INBOX mailbox - imap_session.select("INBOX")?; + imap_session.select("wgbot")?; // fetch message number 1 in this mailbox, along with its RFC822 field. // RFC 822 dictates the format of the body of e-mails let messages = imap_session.fetch("1", "RFC822")?; + let message = if let Some(m) = messages.iter().next() { m } else { @@ -34,9 +48,30 @@ pub fn fetch_inbox_top(imap_username: &str, imap_password: &str) -> imap::error: .to_string(); println!("{}", body); + let mut split = body.lines(); + let split_vec: Vec<&str> = split.collect(); + let mut to_return : Option = None; + for line in split_vec { + if line.contains("suchauftrag_detail") { + to_return = Some(String::from(line.trim_start())) + } + } + + // now delete the email + let seq : Seq = message.message; + imap_session.store(format!("{}", seq), "+FLAGS (\\Deleted)")?; + imap_session.expunge()?; + + +// let result : Result<(), Error> = telegram.log("test message 3").await; +// match result { +// Ok(v) => println!("++ logged"), +// Err(e) => println!("++ error logging: {?}", e) +// } // be nice to the server and log out imap_session.logout()?; - Ok(Some(body)) +// Ok(Some(body)) + Ok(to_return) } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 7acda0d..c9eba3e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,11 @@ use futures::StreamExt; use telegram_bot::*; use telegram_bot::{Api, Message, SendMessage, MessageKind, UpdateKind}; mod email; +mod tele; +use tele::TStruct; +use std::result::Result; +use std::{thread, time}; + #[tokio::main] @@ -19,11 +24,30 @@ async fn main() -> Result<(), Error> { let imap_password = env::var("IMAP_PASSWORD").expect("imap password is not set"); let telegram_log_id: i64 = env::var("TELEGRAM_LOG_ID").expect("TELEGRAM_LOG_ID is not set").parse().unwrap(); - let chatId = ChatId::new(telegram_log_id); - let s:SendMessage = SendMessage::new(chatId, "very cool that this is working"); - api.send(s).await?; + let t: TStruct = TStruct::new(api, telegram_log_id); + t.test(); + t.log("++ wgbot is online").await?; + + loop { + println!("++ fetching new emails"); + let email = email::fetch_inbox_top(&imap_username, &imap_password, &t); + match email { + Ok(v) => { + println!("positive result"); + match v { + Some(x) => { + let log_msg = format!("++ new listing: {}", x); + t.log(&log_msg).await?; + } + None => println!("None returned") + } + }, + Err(e) => println!("error: {:?}", e), + } + let wait_time = time::Duration::from_millis(5000); + thread::sleep(wait_time); + } -// email::fetch_inbox_top(&imap_username, &imap_password); // // // Fetch new updates via long poll method // let mut stream = api.stream(); diff --git a/src/tele.rs b/src/tele.rs new file mode 100644 index 0000000..a105818 --- /dev/null +++ b/src/tele.rs @@ -0,0 +1,29 @@ +use telegram_bot::*; +use telegram_bot::{Api, Message, SendMessage, MessageKind, UpdateKind}; +use std::result::Result; + + +pub struct TStruct { + api: Api, + log_id: i64, + chat_id: ChatId +} + +impl TStruct { + pub fn new(api: Api, log_id: i64) -> Self { + Self { + api, + log_id, + chat_id: ChatId::new(log_id) + } + } + pub fn test(&self) { + println!("++ calling test function: {}", self.log_id); + } + pub async fn log(&self, msg: &str) -> Result<(), telegram_bot::Error> { + println!("++ attempting to log {}", msg); + let s:SendMessage = SendMessage::new(&self.chat_id, msg); + self.api.send(s).await?; + Ok(()) + } +} \ No newline at end of file