-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move email utilities into their own module
- Loading branch information
Showing
2 changed files
with
49 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use ayb::error::AybError; | ||
use quoted_printable; | ||
use serde::{Deserialize, Serialize}; | ||
use std::fs; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct EmailEntry { | ||
from: String, | ||
to: String, | ||
reply_to: String, | ||
subject: String, | ||
content_type: String, | ||
content_transfer_encoding: String, | ||
date: String, | ||
content: Vec<String>, | ||
} | ||
|
||
pub fn extract_token(email: &EmailEntry) -> Result<String, AybError> { | ||
let prefix = "\tayb client confirm "; | ||
assert_eq!(email.subject, "Your login credentials"); | ||
for line in &email.content { | ||
if line.starts_with(prefix) && line.len() > prefix.len() { | ||
return Ok(String::from_utf8(quoted_printable::decode( | ||
line[prefix.len()..].to_owned(), | ||
quoted_printable::ParseMode::Robust, | ||
)?)?); | ||
} | ||
} | ||
return Err(AybError { | ||
message: "No token found in email".to_owned(), | ||
}); | ||
} | ||
|
||
pub fn parse_smtp_log(file_path: &str) -> Result<Vec<EmailEntry>, serde_json::Error> { | ||
let mut entries = Vec::new(); | ||
for line in fs::read_to_string(file_path).unwrap().lines() { | ||
entries.push(serde_json::from_str(line)?); | ||
} | ||
return Ok(entries); | ||
} |