// 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 . #![feature(let_chains)] #![feature(never_type)] mod bot; mod config; mod error; mod hook; mod web; use crate::bot::XmppClient; use crate::config::Config; use crate::error::Error; use crate::hook::Hook; use crate::web::hooks; use std::path::PathBuf; use std::sync::{Arc, Mutex}; 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 { pretty_env_logger::init(); let matches = command!() .arg( Arg::new("config") .short('c') .long("config") .required(false) .value_parser(value_parser!(PathBuf)), ) .get_matches(); let config = Config::from_arg(matches.get_one::("config"))?; let (value_tx, mut value_rx) = mpsc::unbounded_channel::(); let mut client = XmppClient::new( config.jid, config.password.as_str(), config.rooms, config.nickname, ); let tcp_bind = TcpListener::bind(config.addr).await?; let secret: &'static String = unsafe { core::mem::transmute::<&String, &'static String>(&config.secret) }; let value_tx = Arc::new(Mutex::new(value_tx)); loop { let value_tx = value_tx.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(); async move { hooks(request, secret, value_tx).await } })) .await { println!("Error serving connection: {:?}", err); } }); } } wh = value_rx.recv() => { if let Some(Hook::Gitlab(hook)) = wh { client.hook(hook).await } } } } }