reuse previous session information

This commit is contained in:
Antoine Martin 2021-02-02 05:37:43 +01:00
parent 567fc1674e
commit e6e41b9ad2
3 changed files with 48 additions and 7 deletions

1
Cargo.lock generated
View file

@ -124,6 +124,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"clap", "clap",
"matrix-sdk", "matrix-sdk",
"serde_json",
"tokio", "tokio",
"tracing-subscriber", "tracing-subscriber",
"url", "url",

View file

@ -11,6 +11,7 @@ clap = "3.0.0-beta.2"
tokio = { version = "1", features = [ "full" ] } tokio = { version = "1", features = [ "full" ] }
tracing-subscriber = "0.2" tracing-subscriber = "0.2"
url = "2.2" url = "2.2"
serde_json = "1"
[dependencies.matrix-sdk] [dependencies.matrix-sdk]
git = "https://github.com/matrix-org/matrix-rust-sdk" git = "https://github.com/matrix-org/matrix-rust-sdk"

View file

@ -1,3 +1,7 @@
use std::{
fs::File,
io::{BufReader, BufWriter},
};
use std::{path::PathBuf, time::Duration}; use std::{path::PathBuf, time::Duration};
use clap::Clap; use clap::Clap;
@ -14,7 +18,7 @@ use matrix_sdk::{
}, },
StrippedStateEvent, SyncMessageEvent, StrippedStateEvent, SyncMessageEvent,
}, },
Client, ClientConfig, EventEmitter, RoomState, SyncSettings, Client, ClientConfig, EventEmitter, RoomState, Session, SyncSettings,
}; };
#[derive(Clap)] #[derive(Clap)]
@ -35,6 +39,10 @@ struct Opts {
/// Folder to store the client state into /// Folder to store the client state into
#[clap(long, parse(from_os_str))] #[clap(long, parse(from_os_str))]
store_path: PathBuf, store_path: PathBuf,
/// File where session information will be saved
#[clap(long, parse(from_os_str))]
session: PathBuf,
} }
struct AutoJoinBot { struct AutoJoinBot {
@ -109,23 +117,53 @@ impl EventEmitter for AutoJoinBot {
} }
} }
// TODO: use nice error handling
async fn load_or_init_session(
client: &Client,
session_file: PathBuf,
username: &str,
password: &str,
) {
if session_file.is_file() {
let reader = BufReader::new(File::open(session_file).unwrap());
let session: Session = serde_json::from_reader(reader).unwrap();
client.restore_login(session.clone()).await.unwrap();
println!("Reused session: {}, {}", session.user_id, session.device_id);
} else {
let response = client
.login(username, password, None, Some("autojoin bot"))
.await
.unwrap();
println!("logged in as {}", username);
let session = Session {
access_token: response.access_token,
user_id: response.user_id,
device_id: response.device_id,
};
let writer = BufWriter::new(File::create(session_file).unwrap());
serde_json::to_writer(writer, &session).unwrap();
}
}
async fn login_and_sync( async fn login_and_sync(
homeserver_url: String, homeserver_url: String,
username: &str, username: &str,
password: &str, password: &str,
store_path: PathBuf, store_path: PathBuf,
session_file: PathBuf,
) -> Result<(), matrix_sdk::Error> { ) -> Result<(), matrix_sdk::Error> {
let client_config = ClientConfig::new().store_path(store_path); let client_config = ClientConfig::new().store_path(store_path);
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL"); let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
let client = Client::new_with_config(homeserver_url, client_config).unwrap(); let client = Client::new_with_config(homeserver_url, client_config).unwrap();
// TODO: restore session on subsequent logins load_or_init_session(&client, session_file, username, password).await;
client
.login(username, password, None, Some("autojoin bot"))
.await?;
println!("logged in as {}", username);
client client
.add_event_emitter(Box::new(AutoJoinBot::new(client.clone()))) .add_event_emitter(Box::new(AutoJoinBot::new(client.clone())))
@ -146,6 +184,7 @@ async fn main() -> Result<(), matrix_sdk::Error> {
&opts.username, &opts.username,
&opts.password, &opts.password,
opts.store_path, opts.store_path,
opts.session,
) )
.await .await
} }