bad-news/src/main.rs

61 lines
1.3 KiB
Rust
Raw Normal View History

2021-02-02 05:37:43 +01:00
use std::{
fs::File,
2021-02-02 07:28:14 +01:00
io::{self, BufReader},
2021-02-02 06:43:23 +01:00
path::PathBuf,
2021-02-02 05:37:43 +01:00
};
2021-02-02 04:20:27 +01:00
use clap::Clap;
2021-02-02 06:04:06 +01:00
use serde::Deserialize;
use thiserror::Error;
2021-02-02 06:43:23 +01:00
use url::Url;
2021-02-02 07:28:14 +01:00
mod autojoin;
mod bot;
2021-02-02 07:11:16 +01:00
2021-02-02 07:28:14 +01:00
use bot::BadNewsBot;
2021-02-02 04:20:27 +01:00
#[derive(Error, Debug)]
enum BadNewsError {
#[error("problem accessing configuration file")]
ConfigFile(#[from] io::Error),
#[error("Matrix communication error")]
Matrix(#[from] matrix_sdk::Error),
}
2021-02-02 06:04:06 +01:00
#[derive(Clap)]
#[clap(version = "0.1", author = "Antoine Martin")]
struct Opts {
/// File where session information will be saved
#[clap(short, long, parse(from_os_str))]
config: PathBuf,
}
2021-02-02 06:39:05 +01:00
/// Holds the configuration for the bot.
2021-02-02 06:04:06 +01:00
#[derive(Deserialize)]
2021-02-02 07:28:14 +01:00
pub struct Config {
2021-02-02 06:39:05 +01:00
/// The URL for the homeserver we should connect to
2021-02-02 06:04:06 +01:00
homeserver: Url,
2021-02-02 06:39:05 +01:00
/// The bot's account username
2021-02-02 06:04:06 +01:00
username: String,
2021-02-02 06:39:05 +01:00
/// The bot's account password
2021-02-02 06:04:06 +01:00
password: String,
2021-02-02 06:39:05 +01:00
/// Path to a directory where the bot will store Matrix state and current session information.
2021-02-02 06:04:06 +01:00
state_dir: PathBuf,
}
2021-02-02 04:20:27 +01:00
#[tokio::main]
async fn main() -> anyhow::Result<()> {
2021-02-02 04:20:27 +01:00
tracing_subscriber::fmt::init();
2021-02-02 06:04:06 +01:00
2021-02-02 04:20:27 +01:00
let opts = Opts::parse();
2021-02-02 06:04:06 +01:00
let config_file = opts.config;
let config: Config = serde_yaml::from_reader(BufReader::new(File::open(config_file)?))?;
2021-02-02 04:20:27 +01:00
2021-02-02 07:11:16 +01:00
let bot = BadNewsBot::new(config)?;
bot.init().await?;
bot.run().await;
Ok(())
2021-02-02 04:20:27 +01:00
}