matrix-wol/src/main.rs

52 lines
1.4 KiB
Rust
Raw Permalink Normal View History

2020-10-15 20:05:21 +00:00
mod bot;
mod config;
mod wol;
2020-10-14 20:32:44 +00:00
2020-10-15 20:05:21 +00:00
use config::Config;
2020-10-14 20:32:44 +00:00
2020-10-15 20:05:21 +00:00
use bot::WakeOnLanBot;
2020-10-14 20:32:44 +00:00
2020-10-15 20:05:21 +00:00
use anyhow::{Context, Result};
2020-10-14 20:32:44 +00:00
2020-10-15 20:05:21 +00:00
use tracing::info;
2020-10-14 20:32:44 +00:00
2020-10-26 22:12:25 +00:00
use matrix_sdk::{self, Client, ClientConfig, JsonStore, SyncSettings};
2020-10-15 20:05:21 +00:00
use url::Url;
2020-10-14 20:32:44 +00:00
async fn login_and_sync(config: Config) -> Result<()> {
let homeserver_url = Url::parse(&config.hs_url).expect("Couldn't parse the homeserver URL");
2020-10-26 22:12:25 +00:00
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)?;
2020-10-14 20:32:44 +00:00
client
2020-10-15 20:05:21 +00:00
.login(
&config.username,
&config.password,
None,
Some(&"command bot".to_string()),
)
2020-10-14 20:32:44 +00:00
.await?;
info!("logged in as {}", config.username);
2020-10-26 22:12:25 +00:00
client.sync_once(SyncSettings::default()).await?;
2020-10-14 20:32:44 +00:00
client
2020-10-15 20:05:21 +00:00
.add_event_emitter(Box::new(WakeOnLanBot::new(client.clone(), config.hosts)))
2020-10-14 20:32:44 +00:00
.await;
let settings = SyncSettings::default().token(client.sync_token().await.unwrap());
2020-10-26 22:12:25 +00:00
client.sync(settings).await;
2020-10-14 20:32:44 +00:00
Ok(())
}
#[tokio::main]
async fn main() -> Result<()> {
2020-10-15 20:05:21 +00:00
let matches = config::setup_clap();
config::setup_logging(matches.occurrences_of("v"));
2020-10-26 22:12:25 +00:00
let config =
config::read_config(matches.value_of("config").unwrap()).context("Couldn't load config")?;
2020-10-14 20:32:44 +00:00
login_and_sync(config).await?;
Ok(())
}