2023-05-20 13:13:46 +00:00
|
|
|
// 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)]
|
|
|
|
|
2023-05-20 14:27:34 +00:00
|
|
|
mod error;
|
2023-05-20 17:33:33 +00:00
|
|
|
mod web;
|
2023-05-20 18:30:56 +00:00
|
|
|
mod webhook;
|
|
|
|
mod xmpp;
|
2023-05-20 14:27:34 +00:00
|
|
|
|
2023-05-20 17:33:33 +00:00
|
|
|
use crate::web::webhooks;
|
2023-05-20 18:30:56 +00:00
|
|
|
use crate::webhook::WebHook;
|
|
|
|
use crate::xmpp::XmppClient;
|
2023-05-20 14:27:34 +00:00
|
|
|
|
2023-05-20 13:13:46 +00:00
|
|
|
use std::convert::Infallible;
|
|
|
|
use std::net::SocketAddr;
|
2023-05-20 18:30:56 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2023-05-20 13:13:46 +00:00
|
|
|
|
2023-05-21 21:40:54 +00:00
|
|
|
use clap::Parser;
|
2023-05-20 13:13:46 +00:00
|
|
|
use hyper::{
|
2023-05-20 17:34:08 +00:00
|
|
|
service::{make_service_fn, service_fn},
|
|
|
|
Server,
|
2023-05-20 13:13:46 +00:00
|
|
|
};
|
2023-05-20 18:30:56 +00:00
|
|
|
use tokio::sync::mpsc;
|
2023-05-21 21:40:54 +00:00
|
|
|
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,
|
2023-05-21 21:47:54 +00:00
|
|
|
|
|
|
|
/// 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,
|
2023-05-21 21:40:54 +00:00
|
|
|
}
|
2023-05-20 13:13:46 +00:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2023-05-20 17:34:08 +00:00
|
|
|
pretty_env_logger::init();
|
2023-05-20 13:13:46 +00:00
|
|
|
|
2023-05-21 21:40:54 +00:00
|
|
|
let args = Args::parse();
|
|
|
|
|
2023-05-20 17:34:08 +00:00
|
|
|
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
2023-05-20 18:30:56 +00:00
|
|
|
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)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
});
|
2023-05-20 17:34:08 +00:00
|
|
|
let server = Server::bind(&addr).serve(make_svc);
|
|
|
|
println!("Listening on http://{}", addr);
|
2023-05-20 13:13:46 +00:00
|
|
|
|
2023-05-20 18:30:56 +00:00
|
|
|
let _join = tokio::spawn(server);
|
2023-05-21 21:47:54 +00:00
|
|
|
let mut client = XmppClient::new(
|
|
|
|
&String::from(args.jid),
|
|
|
|
args.password.as_str(),
|
|
|
|
args.rooms,
|
|
|
|
args.nickname,
|
|
|
|
);
|
2023-05-20 18:30:56 +00:00
|
|
|
|
|
|
|
loop {
|
|
|
|
tokio::select! {
|
|
|
|
_ = client.next() => (),
|
|
|
|
wh = value_rx.recv() => {
|
|
|
|
if let Some(wh) = wh {
|
|
|
|
client.webhook(wh).await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-20 17:34:08 +00:00
|
|
|
}
|
2023-05-20 13:13:46 +00:00
|
|
|
}
|