diff --git a/Cargo.lock b/Cargo.lock index b32b6c7..bcafa54 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -92,6 +92,56 @@ dependencies = [ "libc", ] +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + [[package]] name = "anyhow" version = "1.0.102" @@ -485,9 +535,11 @@ dependencies = [ name = "bot" version = "0.1.0" dependencies = [ + "clap", "deltachat", "eui48", "serde", + "serde_yaml", ] [[package]] @@ -672,6 +724,46 @@ dependencies = [ "zeroize", ] +[[package]] +name = "clap" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b193af5b67834b676abd72466a96c1024e6a6ad978a1f484bd90b85c94041351" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "714a53001bf66416adb0e2ef5ac857140e7dc3a0c48fb28b2f10762fc4b5069f" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1110bd8a634a1ab8cb04345d8d878267d57c3cf1b38d91b71af6686408bbca6a" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c8d4a3bb8b1e0c1050499d1815f5ab16d04f0959b233085fb31653fbfc9d98f9" + [[package]] name = "cmac" version = "0.7.2" @@ -698,6 +790,12 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + [[package]] name = "colorutils-rs" version = "0.7.6" @@ -2750,6 +2848,12 @@ dependencies = [ "z32", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "itoa" version = "1.0.17" @@ -3401,6 +3505,12 @@ dependencies = [ "portable-atomic", ] +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "opaque-debug" version = "0.3.1" @@ -4720,6 +4830,19 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "serdect" version = "0.2.0" @@ -5691,6 +5814,12 @@ dependencies = [ "subtle", ] +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + [[package]] name = "untrusted" version = "0.9.0" @@ -5716,6 +5845,12 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "uuid" version = "1.22.0" diff --git a/Cargo.toml b/Cargo.toml index 684884e..26664e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,8 @@ version = "0.1.0" edition = "2024" [dependencies] +clap = { version = "4", features = [ "derive" ] } deltachat = { path = "./chatmail-core" } eui48 = { version = "1.1.0", features = [ "serde" ] } serde = { version = "1", features = [ "derive" ] } +serde_yaml = { version = "0.9" } diff --git a/src/config.rs b/src/config.rs index 281a20a..af0b768 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,31 +1,75 @@ use eui48::MacAddress; -use serde::{Deserialize, self}; -use std::{collections::HashMap, net::IpAddr}; +use serde::Deserialize; +use std::{collections::HashMap, fs, io, net::IpAddr, path::Path}; -#[derive(Deserialize)] -struct BotConfig { +#[derive(Deserialize, Debug)] +pub struct BotConfig { pub auth: BotUserAuthConfig, pub machines: HashMap, #[serde(rename = "deltaChat")] pub delta_chat: BotDeltaChatConfig, } -#[derive(Deserialize)] -struct BotUserAuthConfig { +#[derive(Deserialize, Debug)] +pub struct BotUserAuthConfig { pub password: String, } -#[derive(Deserialize)] -struct BotTargetMachineConfig { +#[derive(Deserialize, Debug)] +pub struct BotTargetMachineConfig { #[serde(rename = "default")] - is_default: bool, - mac: MacAddress, + pub is_default: bool, + pub mac: MacAddress, #[serde(rename = "staticIp")] - static_ip: IpAddr, + pub static_ip: IpAddr, + pub hostname: String, + #[serde(rename = "remoteUnlock")] + pub remote_unlock: Option, + #[serde(rename = "remoteAccess")] + pub remote_access: BotSshAccessConfig, } -#[derive(Deserialize)] -struct BotDeltaChatConfig { - email: String, - password: String, +#[derive(Deserialize, Debug)] +pub struct BotSshAccessConfig { + #[serde(rename = "sshKeyFile")] + pub ssh_key_file: String, +} + +#[derive(Deserialize, Debug)] +pub struct BotDeltaChatConfig { + pub email: String, + pub password: String, +} + +#[derive(Debug)] +pub enum ConfigError { + Io(io::Error), + Parse(serde_yaml::Error), +} + +impl std::fmt::Display for ConfigError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + ConfigError::Io(e) => write!(f, "Failed to read config file: {e}"), + ConfigError::Parse(e) => write!(f, "Failed to parse config file: {e}"), + } + } +} + +impl From for ConfigError { + fn from(e: io::Error) -> Self { + ConfigError::Io(e) + } +} + +impl From for ConfigError { + fn from(e: serde_yaml::Error) -> Self { + ConfigError::Parse(e) + } +} + +pub fn read_config(path: &Path) -> Result { + let contents = fs::read_to_string(path)?; + let config = serde_yaml::from_str(&contents)?; + Ok(config) } diff --git a/src/main.rs b/src/main.rs index 4cb4dc9..8041f30 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,25 @@ mod config; -fn main() { +use clap::Parser; +use std::path::PathBuf; +/// Delta Chat bot for remote control of local network machines. +#[derive(Parser)] +#[command(version, about)] +struct Args { + /// Path to the YAML configuration file. + #[arg(short = 'c', long = "config")] + config: PathBuf, +} + +fn main() { + let args = Args::parse(); + + let _ = match config::read_config(&args.config) { + Ok(c) => c, + Err(e) => { + eprintln!("Error: {e}"); + std::process::exit(1); + } + }; }