matrix-wol/src/main.rs

52 lines
1.4 KiB
Rust

mod bot;
mod config;
mod wol;
use config::Config;
use bot::WakeOnLanBot;
use anyhow::{Context, Result};
use tracing::info;
use matrix_sdk::{self, Client, ClientConfig, JsonStore, SyncSettings};
use url::Url;
async fn login_and_sync(config: Config) -> Result<()> {
let homeserver_url = Url::parse(&config.hs_url).expect("Couldn't parse the homeserver URL");
let store = JsonStore::open(config.store_path)?;
let client_config = ClientConfig::new().state_store(Box::new(store));
let mut client = Client::new_with_config(homeserver_url, client_config)?;
client
.login(
&config.username,
&config.password,
None,
Some(&"command bot".to_string()),
)
.await?;
info!("logged in as {}", config.username);
client.sync_once(SyncSettings::default()).await?;
client
.add_event_emitter(Box::new(WakeOnLanBot::new(client.clone(), config.hosts)))
.await;
let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
client.sync(settings).await;
Ok(())
}
#[tokio::main]
async fn main() -> Result<()> {
let matches = config::setup_clap();
config::setup_logging(matches.occurrences_of("v"));
let config =
config::read_config(matches.value_of("config").unwrap()).context("Couldn't load config")?;
login_and_sync(config).await?;
Ok(())
}