diff options
| author | Teddy Wing | 2021-05-22 18:26:24 +0200 |
|---|---|---|
| committer | Teddy Wing | 2021-05-22 18:29:16 +0200 |
| commit | 8da3502b3f065a10e5faf4cd24bc988d843d798e (patch) | |
| tree | 8a2a603ac6267c1bf22896804ff3fc65f601fcd4 /src/main.rs | |
| parent | e40ea0fc324b1a0b47a7b3433ee3abe2b5171b40 (diff) | |
| download | google-calendar-rsvp-8da3502b3f065a10e5faf4cd24bc988d843d798e.tar.bz2 | |
Get event eid from email on standard input
If the `--email` flag is supplied, parse a Google Calendar email from
standard input and extract the event's eid from there.
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index a5e7a05..1d3595a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,12 +4,15 @@ use google_calendar3::CalendarHub; use home; use hyper; use hyper_rustls; +use mailparse; +use regex::Regex; use tokio; use yup_oauth2 as oauth2; use std::env; use std::fmt; use std::fs; +use std::io::{self, Read}; use std::process; use std::str; @@ -38,6 +41,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { let mut action_opt: Option<EventResponseStatus> = None; let mut email = false; + let mut email_eid = String::new(); let mut event_ids = Vec::new(); for arg in &args[1..] { @@ -66,6 +70,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { }, }; + if email { + let mut stdin = io::stdin(); + let mut email_input: Vec<u8> = Vec::new(); + stdin.read_to_end(&mut email_input)?; + + email_eid = eid_from_email(&email_input); + + event_ids.push(&email_eid); + } + if event_ids.is_empty() { eprintln!("error: missing event ID argument"); @@ -163,6 +177,28 @@ fn event_id_from_base64(event_id: &str) -> String { id } +fn eid_from_email(email: &[u8]) -> String { + let email = mailparse::parse_mail(&email).unwrap(); + let re = Regex::new("eid=([^&]+)&").unwrap(); + + // Assume email is multipart/alternative. + for part in &email.subparts { + if part.ctype.mimetype == "multipart/alternative" { + for part in &part.subparts { + if part.ctype.mimetype == "text/plain" { + let body = part.get_body().unwrap(); + let captures = re.captures(&body).unwrap(); + let eid = captures.get(1).unwrap(); + + return eid.as_str().to_owned(); + } + } + } + } + + todo!(); +} + #[cfg(test)] mod tests { |
