cusku/src/main.rs

99 lines
2.4 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)]
mod error;
mod web;
mod webhook;
mod xmpp;
use crate::web::webhooks;
use crate::webhook::WebHook;
use crate::xmpp::XmppClient;
use std::convert::Infallible;
use std::net::SocketAddr;
use std::sync::{Arc, Mutex};
use clap::Parser;
use hyper::{
service::{make_service_fn, service_fn},
Server,
};
use tokio::sync::mpsc;
use xmpp_parsers::BareJid;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Account address
#[arg(short, long)]
jid: BareJid,
/// Account password
#[arg(short, long)]
password: String,
/// Rooms to join, e.g., room@chat.example.org
#[arg(short, long = "room", value_name = "ROOM")]
rooms: Vec<BareJid>,
/// Nickname to use in rooms
#[arg(short, long, default_value = "bot")]
nickname: String,
}
#[tokio::main]
async fn main() {
pretty_env_logger::init();
let args = Args::parse();
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
let (value_tx, mut value_rx) = mpsc::unbounded_channel::<WebHook>();
let value_tx = Arc::new(Mutex::new(value_tx));
let make_svc = make_service_fn(move |_conn| {
let value_tx = value_tx.clone();
async move {
Ok::<_, Infallible>(service_fn(move |req| {
let value_tx = value_tx.clone();
webhooks(req, value_tx)
}))
}
});
let server = Server::bind(&addr).serve(make_svc);
println!("Listening on http://{}", addr);
let _join = tokio::spawn(server);
let mut client = XmppClient::new(
&String::from(args.jid),
args.password.as_str(),
args.rooms,
args.nickname,
);
loop {
tokio::select! {
_ = client.next() => (),
wh = value_rx.recv() => {
if let Some(wh) = wh {
client.webhook(wh).await
}
}
}
}
}