Use toml config file instead of arguments
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
2637458bbd
commit
135a2c1611
3 changed files with 104 additions and 15 deletions
|
@ -10,9 +10,12 @@ license = "AGPL-3.0+"
|
||||||
clap = { version = "4.3", features = [ "cargo", "derive" ] }
|
clap = { version = "4.3", features = [ "cargo", "derive" ] }
|
||||||
gitlab = "0.1511.0"
|
gitlab = "0.1511.0"
|
||||||
hyper = { version = "0.14", features = [ "full" ] }
|
hyper = { version = "0.14", features = [ "full" ] }
|
||||||
|
jid = { version = "*", features = [ "serde" ] }
|
||||||
log = "0.4"
|
log = "0.4"
|
||||||
tokio = { version = "1", features = [ "full" ] }
|
tokio = { version = "1", features = [ "full" ] }
|
||||||
pretty_env_logger = "0.5"
|
pretty_env_logger = "0.5"
|
||||||
|
serde = { version = "1.0", features = [ "derive" ] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
toml = "0.7"
|
||||||
xmpp = "0.4"
|
xmpp = "0.4"
|
||||||
xmpp-parsers = "0.19"
|
xmpp-parsers = "0.19"
|
||||||
|
|
26
src/error.rs
26
src/error.rs
|
@ -13,7 +13,9 @@
|
||||||
// You should have received a copy of the GNU Affero General Public License
|
// You should have received a copy of the GNU Affero General Public License
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
use std::env::VarError;
|
||||||
use std::error::Error as StdError;
|
use std::error::Error as StdError;
|
||||||
|
use std::io::Error as IoError;
|
||||||
use std::str::Utf8Error;
|
use std::str::Utf8Error;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -22,8 +24,11 @@ pub(crate) enum Error {
|
||||||
InvalidToken,
|
InvalidToken,
|
||||||
InvalidContentType,
|
InvalidContentType,
|
||||||
Hyper(hyper::Error),
|
Hyper(hyper::Error),
|
||||||
|
Io(IoError),
|
||||||
SerdeJson(serde_json::Error),
|
SerdeJson(serde_json::Error),
|
||||||
|
Toml(toml::de::Error),
|
||||||
Utf8(Utf8Error),
|
Utf8(Utf8Error),
|
||||||
|
Var(VarError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl StdError for Error {}
|
impl StdError for Error {}
|
||||||
|
@ -35,8 +40,11 @@ impl std::fmt::Display for Error {
|
||||||
Error::InvalidToken => write!(fmt, "the token is invalid"),
|
Error::InvalidToken => write!(fmt, "the token is invalid"),
|
||||||
Error::InvalidContentType => write!(fmt, "the content-type is invalid"),
|
Error::InvalidContentType => write!(fmt, "the content-type is invalid"),
|
||||||
Error::Hyper(e) => write!(fmt, "hyper error: {}", e),
|
Error::Hyper(e) => write!(fmt, "hyper error: {}", e),
|
||||||
|
Error::Io(e) => write!(fmt, "Io error: {}", e),
|
||||||
Error::SerdeJson(e) => write!(fmt, "serde_json error: {}", e),
|
Error::SerdeJson(e) => write!(fmt, "serde_json error: {}", e),
|
||||||
|
Error::Toml(e) => write!(fmt, "toml deserialization error: {}", e),
|
||||||
Error::Utf8(e) => write!(fmt, "Utf8 error: {}", e),
|
Error::Utf8(e) => write!(fmt, "Utf8 error: {}", e),
|
||||||
|
Error::Var(e) => write!(fmt, "Var error: {}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -47,14 +55,32 @@ impl From<hyper::Error> for Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<IoError> for Error {
|
||||||
|
fn from(err: IoError) -> Error {
|
||||||
|
Error::Io(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<serde_json::Error> for Error {
|
impl From<serde_json::Error> for Error {
|
||||||
fn from(err: serde_json::Error) -> Error {
|
fn from(err: serde_json::Error) -> Error {
|
||||||
Error::SerdeJson(err)
|
Error::SerdeJson(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<toml::de::Error> for Error {
|
||||||
|
fn from(err: toml::de::Error) -> Error {
|
||||||
|
Error::Toml(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<Utf8Error> for Error {
|
impl From<Utf8Error> for Error {
|
||||||
fn from(err: Utf8Error) -> Error {
|
fn from(err: Utf8Error) -> Error {
|
||||||
Error::Utf8(err)
|
Error::Utf8(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<VarError> for Error {
|
||||||
|
fn from(err: VarError) -> Error {
|
||||||
|
Error::Var(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
90
src/main.rs
90
src/main.rs
|
@ -14,18 +14,23 @@
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
#![feature(let_chains)]
|
#![feature(let_chains)]
|
||||||
|
#![feature(never_type)]
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
mod web;
|
mod web;
|
||||||
mod webhook;
|
mod webhook;
|
||||||
mod xmpp;
|
mod xmpp;
|
||||||
|
|
||||||
|
use crate::error::Error;
|
||||||
use crate::web::webhooks;
|
use crate::web::webhooks;
|
||||||
use crate::webhook::WebHook;
|
use crate::webhook::WebHook;
|
||||||
use crate::xmpp::XmppClient;
|
use crate::xmpp::XmppClient;
|
||||||
|
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
use std::net::SocketAddr;
|
use std::fs::File;
|
||||||
|
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read};
|
||||||
|
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
@ -33,45 +38,100 @@ use hyper::{
|
||||||
service::{make_service_fn, service_fn},
|
service::{make_service_fn, service_fn},
|
||||||
Server,
|
Server,
|
||||||
};
|
};
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use xmpp_parsers::BareJid;
|
use xmpp_parsers::BareJid;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(author, version, about, long_about = None)]
|
#[command(author, version, about, long_about = None)]
|
||||||
struct Args {
|
struct Args {
|
||||||
/// Account address
|
/// Config file path
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
|
config: Option<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
struct Config {
|
||||||
|
/// Account address
|
||||||
jid: BareJid,
|
jid: BareJid,
|
||||||
|
|
||||||
/// Account password
|
/// Account password
|
||||||
#[arg(short, long)]
|
|
||||||
password: String,
|
password: String,
|
||||||
|
|
||||||
/// Rooms to join, e.g., room@chat.example.org
|
/// Rooms to join, e.g., room@chat.example.org
|
||||||
#[arg(short, long = "room", value_name = "ROOM")]
|
#[serde(default = "Vec::new")]
|
||||||
rooms: Vec<BareJid>,
|
rooms: Vec<BareJid>,
|
||||||
|
|
||||||
/// Nickname to use in rooms
|
/// Nickname to use in rooms
|
||||||
#[arg(short, long, default_value = "bot")]
|
#[serde(default = "default_nickname")]
|
||||||
nickname: String,
|
nickname: String,
|
||||||
|
|
||||||
/// Token to match the one provided by the Webhook service
|
/// Token to match the one provided by the Webhook service
|
||||||
#[arg(short, long)]
|
#[serde(rename = "webhook-token")]
|
||||||
webhook_token: Option<String>,
|
webhook_token: Option<String>,
|
||||||
|
|
||||||
/// HTTP Webhook listening address and port, e.g., 127.0.0.1:1234 or [::1]:1234
|
/// HTTP Webhook listening address and port, e.g., 127.0.0.1:1234 or [::1]:1234
|
||||||
#[arg(long, default_value = "[::1]:3000")]
|
#[serde(default = "default_addr")]
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn default_nickname() -> String {
|
||||||
|
String::from("cusku")
|
||||||
|
}
|
||||||
|
|
||||||
|
fn default_addr() -> SocketAddr {
|
||||||
|
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 3000)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn config_from_file(file: PathBuf) -> Result<Config, Error> {
|
||||||
|
if file.try_exists().is_err() {
|
||||||
|
let err = IoError::new(IoErrorKind::NotFound, format!("{:?} not found", file));
|
||||||
|
return Err(Error::Io(err));
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut buf = String::new();
|
||||||
|
let mut f = File::open(file)?;
|
||||||
|
f.read_to_string(&mut buf)?;
|
||||||
|
|
||||||
|
Ok(toml::from_str(&buf)?)
|
||||||
|
}
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() -> Result<!, Error> {
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
|
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
let config = {
|
||||||
|
let path = match args.config {
|
||||||
|
Some(path) => {
|
||||||
|
if !path.starts_with("/") {
|
||||||
|
std::env::current_dir()?.join(path)
|
||||||
|
} else {
|
||||||
|
path
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
let confdir: PathBuf = match std::env::var("XDG_CONFIG_HOME") {
|
||||||
|
Ok(ref dir) => Path::new(dir).to_path_buf(),
|
||||||
|
Err(_) => {
|
||||||
|
let home = std::env::var("HOME")?;
|
||||||
|
Path::new(home.as_str()).join(".config")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
confdir.join("cusku/config.toml")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match config_from_file(path) {
|
||||||
|
Ok(config) => config,
|
||||||
|
Err(err) => return Err(err),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let (value_tx, mut value_rx) = mpsc::unbounded_channel::<WebHook>();
|
let (value_tx, mut value_rx) = mpsc::unbounded_channel::<WebHook>();
|
||||||
|
|
||||||
if let Some(token) = args.webhook_token {
|
if let Some(token) = config.webhook_token {
|
||||||
let value_tx = Arc::new(Mutex::new(value_tx));
|
let value_tx = Arc::new(Mutex::new(value_tx));
|
||||||
let make_svc = make_service_fn(move |_conn| {
|
let make_svc = make_service_fn(move |_conn| {
|
||||||
let value_tx = value_tx.clone();
|
let value_tx = value_tx.clone();
|
||||||
|
@ -84,17 +144,17 @@ async fn main() {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
let server = Server::bind(&args.addr).serve(make_svc);
|
let server = Server::bind(&config.addr).serve(make_svc);
|
||||||
println!("Listening on http://{}", &args.addr);
|
println!("Listening on http://{}", &config.addr);
|
||||||
|
|
||||||
let _join = tokio::spawn(server);
|
let _join = tokio::spawn(server);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut client = XmppClient::new(
|
let mut client = XmppClient::new(
|
||||||
&String::from(args.jid),
|
&String::from(config.jid),
|
||||||
args.password.as_str(),
|
config.password.as_str(),
|
||||||
args.rooms,
|
config.rooms,
|
||||||
args.nickname,
|
config.nickname,
|
||||||
);
|
);
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
|
|
Loading…
Reference in a new issue