From 2fa4be52d9cbf76aaf28cfeab47bfef6115add9e Mon Sep 17 00:00:00 2001 From: Carson McManus Date: Sat, 14 Aug 2021 12:16:40 -0400 Subject: [PATCH] parse descriptions for confirmations --- src/demos.rs | 4 ++++ src/tui.rs | 10 ++++++++-- steamguard/src/confirmation.rs | 5 +++-- steamguard/src/lib.rs | 17 +++++++++++++++++ 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/src/demos.rs b/src/demos.rs index 445ae64..94221d3 100644 --- a/src/demos.rs +++ b/src/demos.rs @@ -10,24 +10,28 @@ pub fn demo_confirmation_menu() { key: 12345, conf_type: ConfirmationType::Trade, creator: 09870987, + description: "example confirmation".into(), }, Confirmation { id: 1234, key: 12345, conf_type: ConfirmationType::MarketSell, creator: 09870987, + description: "example confirmation".into(), }, Confirmation { id: 1234, key: 12345, conf_type: ConfirmationType::AccountRecovery, creator: 09870987, + description: "example confirmation".into(), }, Confirmation { id: 1234, key: 12345, conf_type: ConfirmationType::Trade, creator: 09870987, + description: "example confirmation".into(), }, ]); println!("accept: {}, deny: {}", accept.len(), deny.len()); diff --git a/src/tui.rs b/src/tui.rs index bdabc11..4ed82c9 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -213,8 +213,14 @@ pub fn prompt_confirmation_menu( } return ( - to_accept_idx.iter().map(|i| confirmations[*i]).collect(), - to_deny_idx.iter().map(|i| confirmations[*i]).collect(), + to_accept_idx + .iter() + .map(|i| confirmations[*i].clone()) + .collect(), + to_deny_idx + .iter() + .map(|i| confirmations[*i].clone()) + .collect(), ); } diff --git a/steamguard/src/confirmation.rs b/steamguard/src/confirmation.rs index f8bd575..b4d9672 100644 --- a/steamguard/src/confirmation.rs +++ b/steamguard/src/confirmation.rs @@ -1,17 +1,18 @@ /// A mobile confirmation. There are multiple things that can be confirmed, like trade offers. -#[derive(Debug, Clone, Copy, PartialEq)] +#[derive(Debug, Clone, PartialEq)] pub struct Confirmation { pub id: u64, pub key: u64, /// Trade offer ID or market transaction ID pub creator: u64, pub conf_type: ConfirmationType, + pub description: String, } impl Confirmation { /// Human readable representation of this confirmation. pub fn description(&self) -> String { - format!("{:?} id={} key={}", self.conf_type, self.id, self.key) + format!("{:?} - {}", self.conf_type, self.description) } } diff --git a/steamguard/src/lib.rs b/steamguard/src/lib.rs index a9b5403..dcd38e1 100644 --- a/steamguard/src/lib.rs +++ b/steamguard/src/lib.rs @@ -3,6 +3,7 @@ use anyhow::Result; pub use confirmation::{Confirmation, ConfirmationType}; use hmacsha1::hmac_sha1; use log::*; +use regex::Regex; use reqwest::{ cookie::CookieStore, header::{COOKIE, USER_AGENT}, @@ -272,8 +273,18 @@ fn parse_confirmations(text: String) -> anyhow::Result> { let fragment = Html::parse_fragment(&text); let selector = Selector::parse(".mobileconf_list_entry").unwrap(); + let desc_selector = Selector::parse(".mobileconf_list_entry_description").unwrap(); let mut confirmations = vec![]; for elem in fragment.select(&selector) { + let desc: String = elem + .select(&desc_selector) + .next() + .unwrap() + .text() + .map(|t| t.trim()) + .filter(|t| t.len() > 0) + .collect::>() + .join(" "); let conf = Confirmation { id: elem.value().attr("data-confid").unwrap().parse()?, key: elem.value().attr("data-key").unwrap().parse()?, @@ -284,6 +295,7 @@ fn parse_confirmations(text: String) -> anyhow::Result> { .try_into() .unwrap_or(ConfirmationType::Unknown), creator: elem.value().attr("data-creator").unwrap().parse()?, + description: desc, }; confirmations.push(conf); } @@ -338,6 +350,7 @@ mod tests { key: 15509106087034649470, conf_type: ConfirmationType::MarketSell, creator: 3392884950693131245, + description: "Sell - Summer 2021 - Horror $0.05 ($0.03) 2 minutes ago".into(), } ); assert_eq!( @@ -347,6 +360,7 @@ mod tests { key: 2661901169510258722, conf_type: ConfirmationType::MarketSell, creator: 3392884950693130525, + description: "Sell - Summer 2021 - Horror $0.05 ($0.03) 2 minutes ago".into(), } ); assert_eq!( @@ -356,6 +370,7 @@ mod tests { key: 15784514761287735229, conf_type: ConfirmationType::MarketSell, creator: 3392884950693129565, + description: "Sell - Summer 2021 - Horror $0.05 ($0.03) 2 minutes ago".into(), } ); assert_eq!( @@ -365,6 +380,7 @@ mod tests { key: 5049250785011653560, conf_type: ConfirmationType::MarketSell, creator: 3392884950693128685, + description: "Sell - Summer 2021 - Rogue $0.05 ($0.03) 2 minutes ago".into(), } ); assert_eq!( @@ -374,6 +390,7 @@ mod tests { key: 6133112455066694993, conf_type: ConfirmationType::MarketSell, creator: 3392884950693127345, + description: "Sell - Summer 2021 - Horror $0.05 ($0.03) 2 minutes ago".into(), } ); }