Pārlūkot izejas kodu

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
Jaime Rodriguez 9 gadi atpakaļ
vecāks
revīzija
e321379501
3 mainītis faili ar 17 papildinājumiem un 7 dzēšanām
  1. 2 1
      src/main.rs
  2. 10 1
      src/session.rs
  3. 5 5
      src/spirc.rs

+ 2 - 1
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();
 }
 

+ 10 - 1
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"),
             _ => ()
         }

+ 5 - 5
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();