matrix-wol/src/main.rs

50 lines
1.2 KiB
Rust
Raw 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-15 20:05:21 +00:00
use matrix_sdk::{self, Client, SyncSettings};
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");
let mut client = Client::new(homeserver_url).unwrap();
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);
client.sync(SyncSettings::default()).await.unwrap();
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-15 20:05:21 +00:00
client.sync_forever(settings, |_| async {}).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"));
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(())
}