From e32137950140ebfd2d566c144ac85e0c41d69ba4 Mon Sep 17 00:00:00 2001 From: Jaime Rodriguez Date: Wed, 30 Dec 2015 19:50:23 +0100 Subject: [PATCH] Use canonical username when subscribing to spirc. Uses canonical username that APWelcome message contains once authenticated instead of username provided by user. SpircManager now accesses it via its Session field. Fixes #15 --- src/main.rs | 3 ++- src/session.rs | 11 ++++++++++- src/spirc.rs | 10 +++++----- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4b8d9f9b..e3429792 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,6 +63,7 @@ fn main() { device_id: name.clone(), cache_location: PathBuf::from(cache_location) }; + let session = Session::new(config); session.login(username.clone(), password); session.poll(); @@ -76,7 +77,7 @@ fn main() { let player = Player::new(&session); - let mut spirc_manager = SpircManager::new(&session, player, username, name); + let mut spirc_manager = SpircManager::new(&session, player, name); spirc_manager.run(); } diff --git a/src/session.rs b/src/session.rs index 94cdb7dd..38979601 100644 --- a/src/session.rs +++ b/src/session.rs @@ -29,6 +29,7 @@ pub struct Config { pub struct SessionData { pub country: String, + pub canonical_username: String, } pub struct SessionInternal { @@ -125,6 +126,7 @@ impl Session { config: config, data: RwLock::new(SessionData { country: String::new(), + canonical_username: String::new(), }), rx_connection: Mutex::new(cipher_connection.clone()), @@ -179,7 +181,14 @@ impl Session { }, 0xb2...0xb6 => self.0.mercury.lock().unwrap().handle(cmd, data), - 0xac => eprintln!("Authentication succeedded"), + 0xac => { + let welcome_data : protocol::authentication::APWelcome = + protobuf::parse_from_bytes(&data).unwrap(); + self.0.data.write().unwrap().canonical_username = + welcome_data.get_canonical_username().to_string(); + eprintln!("Authentication succeeded") + }, + 0xad => eprintln!("Authentication failed"), _ => () } diff --git a/src/spirc.rs b/src/spirc.rs index de038849..6fa86b43 100644 --- a/src/spirc.rs +++ b/src/spirc.rs @@ -15,7 +15,6 @@ pub struct SpircManager<'s, D: SpircDelegate> { delegate: D, session: &'s Session, - username: String, state_update_id: i64, seq_nr: u32, @@ -61,12 +60,11 @@ pub trait SpircState { impl <'s, D: SpircDelegate> SpircManager<'s, D> { pub fn new(session: &'s Session, delegate: D, - username: String, name: String) -> SpircManager<'s, D> { + name: String) -> SpircManager<'s, D> { SpircManager { delegate: delegate, session: &session, - username: username.clone(), state_update_id: 0, seq_nr: 0, @@ -91,7 +89,8 @@ impl <'s, D: SpircDelegate> SpircManager<'s, D> { } pub fn run(&mut self) { - let rx = self.session.mercury_sub(format!("hm://remote/3/user/{}/", self.username)); + let rx = self.session.mercury_sub(format!("hm://remote/3/user/{}/", + self.session.0.data.read().unwrap().canonical_username.clone())); let updates = self.delegate.updates(); loop { @@ -192,7 +191,8 @@ impl <'s, D: SpircDelegate> SpircManager<'s, D> { self.session.mercury(MercuryRequest{ method: MercuryMethod::SEND, - uri: format!("hm://remote/user/{}", self.username), + uri: format!("hm://remote/user/{}", + self.session.0.data.read().unwrap().canonical_username.clone()), content_type: None, payload: vec![ pkt.write_to_bytes().unwrap() ] }).await().unwrap();