137 lines
4.5 KiB
Rust
137 lines
4.5 KiB
Rust
use matrix_sdk::ruma::api::client::filter::{FilterDefinition, RoomFilter};
|
|
use matrix_sdk::ruma::api::client::sync::sync_events::v3::Filter;
|
|
use matrix_sdk::ruma::api::client::uiaa;
|
|
use matrix_sdk::{
|
|
config::SyncSettings,
|
|
ruma::{events::room::message::SyncRoomMessageEvent, OwnedRoomId, OwnedUserId},
|
|
Client, Session,
|
|
};
|
|
use serde::{Deserialize, Serialize};
|
|
use std::io::Write;
|
|
use std::path::PathBuf;
|
|
use tubest_common::PROG_NAME;
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
struct Config {
|
|
user: OwnedUserId,
|
|
room: OwnedRoomId,
|
|
password: Option<String>,
|
|
session: Option<Session>,
|
|
}
|
|
|
|
impl Config {
|
|
fn path() -> PathBuf {
|
|
#[allow(deprecated)]
|
|
let home = std::env::home_dir().expect("no coats and no home");
|
|
home.join(".config").join(PROG_NAME).join("matrix.json")
|
|
}
|
|
fn read() -> Self {
|
|
let path = Self::path();
|
|
let f =
|
|
std::fs::File::open(&path).unwrap_or_else(|e| panic!("couldn't read {path:?}: {e:?}"));
|
|
serde_json::from_reader(f).unwrap_or_else(|e| panic!("couldn't load config: {e:?}"))
|
|
}
|
|
fn write(&self) {
|
|
let json = serde_json::to_string_pretty(self).unwrap();
|
|
let path = Self::path();
|
|
std::fs::File::create(&path)
|
|
.unwrap_or_else(|e| panic!("couldn't create {path:?}: {e:?}"))
|
|
.write_all(json.as_bytes())
|
|
.unwrap_or_else(|e| panic!("couldn't write config: {e:?}"));
|
|
}
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
|
let mut cfg = Config::read();
|
|
|
|
let client = Client::builder()
|
|
.server_name(cfg.user.server_name())
|
|
.handle_refresh_tokens()
|
|
.build()
|
|
.await?;
|
|
|
|
let mut logged_in = false;
|
|
/*
|
|
if let Some(session) = cfg.session.clone() {
|
|
//logged_in = client.restore_login(session).await.is_ok();
|
|
client
|
|
.login_username(&cfg.user, cfg.password.as_ref().unwrap())
|
|
.device_id(session.device_id.as_str())
|
|
.initial_device_display_name(PROG_NAME)
|
|
.request_refresh_token()
|
|
.send()
|
|
.await?;
|
|
logged_in = true;
|
|
}
|
|
*/
|
|
if !logged_in {
|
|
if let Some(ref password) = cfg.password {
|
|
client
|
|
.login_username(&cfg.user, password)
|
|
.initial_device_display_name(PROG_NAME)
|
|
.request_refresh_token()
|
|
.send()
|
|
.await?;
|
|
logged_in = true;
|
|
let devices_to_delete: Vec<_> = client
|
|
.devices()
|
|
.await
|
|
.unwrap()
|
|
.devices
|
|
.into_iter()
|
|
.filter(|d| {
|
|
d.display_name.clone().unwrap_or_default().as_str() == PROG_NAME
|
|
&& d.device_id != client.device_id().unwrap()
|
|
})
|
|
.map(|d| d.device_id)
|
|
.collect();
|
|
if let Err(e) = client.delete_devices(&devices_to_delete, None).await {
|
|
if let Some(info) = e.uiaa_response() {
|
|
let mut password = uiaa::Password::new(
|
|
uiaa::UserIdentifier::UserIdOrLocalpart(cfg.user.localpart()),
|
|
password,
|
|
);
|
|
password.session = info.session.as_deref();
|
|
client
|
|
.delete_devices(
|
|
&devices_to_delete,
|
|
Some(uiaa::AuthData::Password(password)),
|
|
)
|
|
.await?;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
assert!(logged_in);
|
|
if let Some(session) = client.session() {
|
|
cfg.session = Some(session);
|
|
cfg.write();
|
|
}
|
|
|
|
client.add_room_event_handler(cfg.room.as_ref(), |ev: SyncRoomMessageEvent| async {
|
|
match ev {
|
|
SyncRoomMessageEvent::Original(orig) => {
|
|
let body = orig.content.body();
|
|
// TODO: my own IPC wrapper
|
|
println!("{}", body);
|
|
eprintln!("{}", body);
|
|
}
|
|
SyncRoomMessageEvent::Redacted(_) => {}
|
|
}
|
|
});
|
|
|
|
// Syncing is important to synchronize the client state with the server.
|
|
// This method will never return.
|
|
let mut filter_def = FilterDefinition::empty();
|
|
let mut room_filter = RoomFilter::empty();
|
|
let room_list = [cfg.room];
|
|
room_filter.rooms = Some(&room_list);
|
|
filter_def.room = room_filter;
|
|
client
|
|
.sync(SyncSettings::default().filter(Filter::FilterDefinition(filter_def)))
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|