cusku/src/main.rs
pep 849ecbd5dc bot: pass in Config as a parameter
In an attempt to pass in more stuff to the bot

Signed-off-by: pep <pep@bouah.net>
2026-01-08 23:44:06 +01:00

110 lines
2.8 KiB
Rust

// 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/>.
mod bot;
mod config;
mod error;
mod hooks;
mod web;
use crate::bot::XmppClient;
use crate::config::Config;
use crate::error::Error;
use crate::hooks::Hook;
use crate::web::hooks;
use camino::Utf8PathBuf;
use clap::{Arg, command, value_parser};
use hyper::{server::conn::http1, service::service_fn};
use hyper_util::rt::tokio::{TokioIo, TokioTimer};
use log::error;
use tokio::{net::TcpListener, signal::ctrl_c, sync::mpsc};
use xmpp::tokio_xmpp::rustls;
#[tokio::main]
async fn main() -> Result<(), Error> {
pretty_env_logger::init();
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.expect("Failed to install rustls crypto provider");
let matches = command!()
.arg(
Arg::new("config")
.short('c')
.long("config")
.required(false)
.value_parser(value_parser!(Utf8PathBuf)),
)
.get_matches();
let config = Config::from_arg(matches.get_one::<Utf8PathBuf>("config")).await?;
let (value_tx, value_rx) = mpsc::unbounded_channel::<Hook>();
let mut bot = XmppClient::new(
config.jid.clone(),
config.password.as_str(),
config.nickname.clone(),
config.clone(),
);
let xmpp_handle = tokio::task::spawn(async move {
bot.receive(value_rx).await;
match bot.disconnect().await {
Err(err) => error!("XMPP disconnect error: {err}"),
_ => (),
}
});
let tcp_bind = TcpListener::bind(config.addr).await?;
loop {
let value_tx = value_tx.clone();
let secret = config.secret.clone();
tokio::select! {
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();
let secret = secret.clone();
async move { hooks(request, &secret, value_tx).await }
}),
)
.await
{
println!("Error serving connection: {:?}", err);
}
});
}
},
_ = ctrl_c() => {
break;
},
}
}
let _ = xmpp_handle.await;
Ok(())
}