Skip to content

Commit

Permalink
Use canonical username when subscribing to spirc.
Browse files Browse the repository at this point in the history
Uses canonical username that APWelcome message
contains once authenticated instead of username 
provided by user. SpircManager now accesses it via
its Session field. 

Fixes plietar#15
  • Loading branch information
JayRod12 committed Dec 30, 2015
1 parent 525b27d commit e321379
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}

11 changes: 10 additions & 1 deletion src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct Config {

pub struct SessionData {
pub country: String,
pub canonical_username: String,
}

pub struct SessionInternal {
Expand Down Expand Up @@ -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()),
Expand Down Expand Up @@ -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"),
_ => ()
}
Expand Down
10 changes: 5 additions & 5 deletions src/spirc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub struct SpircManager<'s, D: SpircDelegate> {
delegate: D,
session: &'s Session,

username: String,
state_update_id: i64,
seq_nr: u32,

Expand Down Expand Up @@ -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,

Expand All @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit e321379

Please sign in to comment.