cusku/src/main.rs

97 lines
2.5 KiB
Rust
Raw Normal View History

// Copyright (C) 2023-2099 The crate authors.
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published by the
// Free Software Foundation, either version 3 of the License, or (at your
// option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
// for more details.
//
// 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/>.
#![feature(let_chains)]
#![feature(never_type)]
mod bot;
2024-08-07 11:28:24 +02:00
mod config;
mod error;
mod hooks;
mod web;
use crate::bot::XmppClient;
2024-08-07 11:28:24 +02:00
use crate::config::Config;
use crate::error::Error;
use crate::hooks::Hook;
use crate::web::hooks;
use camino::Utf8PathBuf;
use clap::{command, value_parser, Arg};
use hyper::{server::conn::http1, service::service_fn};
use hyper_util::rt::tokio::{TokioIo, TokioTimer};
use tokio::{net::TcpListener, sync::mpsc};
#[tokio::main]
async fn main() -> Result<!, Error> {
pretty_env_logger::init();
let matches = command!()
.arg(
Arg::new("config")
.short('c')
.long("config")
.required(false)
.value_parser(value_parser!(Utf8PathBuf)),
)
.get_matches();
2024-08-07 11:48:55 +02:00
let config = Config::from_arg(matches.get_one::<Utf8PathBuf>("config")).await?;
let (value_tx, mut value_rx) = mpsc::unbounded_channel::<Hook>();
let mut client = XmppClient::new(
config.jid,
config.password.as_str(),
config.rooms,
config.nickname,
);
let tcp_bind = TcpListener::bind(config.addr).await?;
loop {
let value_tx = value_tx.clone();
2024-08-07 11:53:54 +02:00
let secret = config.secret.clone();
tokio::select! {
_ = client.next() => (),
accept = tcp_bind.accept() => {
if let Ok((tcp, _)) = accept {
let io = TokioIo::new(tcp);
tokio::task::spawn(async move {
if let Err(err) = http1::Builder::new()
.timer(TokioTimer::new())
.serve_connection(io, service_fn(|request| {
let value_tx = value_tx.clone();
2024-08-07 11:53:54 +02:00
let secret = secret.clone();
async move {
2024-08-07 11:53:54 +02:00
hooks(request, &secret, value_tx).await
}
}))
.await
{
println!("Error serving connection: {:?}", err);
}
});
}
}
wh = value_rx.recv() => {
if let Some(hook) = wh {
client.hook(hook).await
}
}
}
}
}