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-27 15:20:58 +00:00
|
|
|
#![feature(never_type)]
|
2023-05-20 13:13:46 +00:00
|
|
|
|
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-27 15:20:58 +00:00
|
|
|
use crate::error::Error;
|
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;
|
2023-05-27 15:20:58 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{Error as IoError, ErrorKind as IoErrorKind, Read};
|
|
|
|
use std::net::{IpAddr, Ipv6Addr, SocketAddr};
|
|
|
|
use std::path::{Path, PathBuf};
|
2023-05-20 18:30:56 +00:00
|
|
|
use std::sync::{Arc, Mutex};
|
2023-05-20 13:13:46 +00:00
|
|
|
|
2023-05-27 15:37:28 +00:00
|
|
|
use clap::{command, value_parser, Arg};
|
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-27 15:37:28 +00:00
|
|
|
use log::debug;
|
2023-05-27 15:20:58 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-05-20 18:30:56 +00:00
|
|
|
use tokio::sync::mpsc;
|
2023-05-21 21:40:54 +00:00
|
|
|
use xmpp_parsers::BareJid;
|
|
|
|
|
2023-05-27 15:20:58 +00:00
|
|
|
#[derive(Debug, Serialize, Deserialize)]
|
|
|
|
struct Config {
|
|
|
|
/// Account address
|
2023-05-21 21:40:54 +00:00
|
|
|
jid: BareJid,
|
|
|
|
|
|
|
|
/// Account password
|
|
|
|
password: String,
|
2023-05-21 21:47:54 +00:00
|
|
|
|
|
|
|
/// Rooms to join, e.g., room@chat.example.org
|
2023-05-27 15:20:58 +00:00
|
|
|
#[serde(default = "Vec::new")]
|
2023-05-21 21:47:54 +00:00
|
|
|
rooms: Vec<BareJid>,
|
|
|
|
|
|
|
|
/// Nickname to use in rooms
|
2023-05-27 15:20:58 +00:00
|
|
|
#[serde(default = "default_nickname")]
|
2023-05-21 21:47:54 +00:00
|
|
|
nickname: String,
|
2023-05-21 22:37:40 +00:00
|
|
|
|
|
|
|
/// Token to match the one provided by the Webhook service
|
2023-05-27 15:20:58 +00:00
|
|
|
#[serde(rename = "webhook-token")]
|
2023-05-21 22:37:40 +00:00
|
|
|
webhook_token: Option<String>,
|
|
|
|
|
|
|
|
/// HTTP Webhook listening address and port, e.g., 127.0.0.1:1234 or [::1]:1234
|
2023-05-27 15:20:58 +00:00
|
|
|
#[serde(default = "default_addr")]
|
2023-05-21 22:37:40 +00:00
|
|
|
addr: SocketAddr,
|
2023-05-21 21:40:54 +00:00
|
|
|
}
|
2023-05-20 13:13:46 +00:00
|
|
|
|
2023-05-27 15:20:58 +00:00
|
|
|
fn default_nickname() -> String {
|
|
|
|
String::from("cusku")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_addr() -> SocketAddr {
|
|
|
|
SocketAddr::new(IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)), 3000)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn config_from_file(file: PathBuf) -> Result<Config, Error> {
|
|
|
|
if file.try_exists().is_err() {
|
|
|
|
let err = IoError::new(IoErrorKind::NotFound, format!("{:?} not found", file));
|
|
|
|
return Err(Error::Io(err));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut buf = String::new();
|
|
|
|
let mut f = File::open(file)?;
|
|
|
|
f.read_to_string(&mut buf)?;
|
|
|
|
|
|
|
|
Ok(toml::from_str(&buf)?)
|
|
|
|
}
|
|
|
|
|
2023-05-20 13:13:46 +00:00
|
|
|
#[tokio::main]
|
2023-05-27 15:20:58 +00:00
|
|
|
async fn main() -> Result<!, Error> {
|
2023-05-20 17:34:08 +00:00
|
|
|
pretty_env_logger::init();
|
2023-05-20 13:13:46 +00:00
|
|
|
|
2023-05-27 15:37:28 +00:00
|
|
|
let matches = command!()
|
|
|
|
.arg(
|
|
|
|
Arg::new("config")
|
|
|
|
.short('c')
|
|
|
|
.long("config")
|
|
|
|
.required(false)
|
|
|
|
.value_parser(value_parser!(PathBuf)),
|
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
2023-05-27 15:20:58 +00:00
|
|
|
let config = {
|
2023-05-27 15:37:28 +00:00
|
|
|
let path = match matches.get_one::<PathBuf>("config") {
|
2023-05-27 15:20:58 +00:00
|
|
|
Some(path) => {
|
|
|
|
if !path.starts_with("/") {
|
|
|
|
std::env::current_dir()?.join(path)
|
|
|
|
} else {
|
2023-05-27 15:37:28 +00:00
|
|
|
path.to_path_buf()
|
2023-05-27 15:20:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
let confdir: PathBuf = match std::env::var("XDG_CONFIG_HOME") {
|
|
|
|
Ok(ref dir) => Path::new(dir).to_path_buf(),
|
|
|
|
Err(_) => {
|
|
|
|
let home = std::env::var("HOME")?;
|
|
|
|
Path::new(home.as_str()).join(".config")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
confdir.join("cusku/config.toml")
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-27 15:37:28 +00:00
|
|
|
debug!("Using configuration file: {:?}", path);
|
|
|
|
|
2023-05-27 15:20:58 +00:00
|
|
|
match config_from_file(path) {
|
|
|
|
Ok(config) => config,
|
|
|
|
Err(err) => return Err(err),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-20 18:30:56 +00:00
|
|
|
let (value_tx, mut value_rx) = mpsc::unbounded_channel::<WebHook>();
|
2023-05-20 13:13:46 +00:00
|
|
|
|
2023-05-27 15:20:58 +00:00
|
|
|
if let Some(token) = config.webhook_token {
|
2023-05-21 22:37:40 +00:00
|
|
|
let value_tx = Arc::new(Mutex::new(value_tx));
|
|
|
|
let make_svc = make_service_fn(move |_conn| {
|
|
|
|
let value_tx = value_tx.clone();
|
|
|
|
let token = token.clone();
|
|
|
|
async move {
|
|
|
|
Ok::<_, Infallible>(service_fn(move |req| {
|
|
|
|
let value_tx = value_tx.clone();
|
|
|
|
let token = token.clone();
|
|
|
|
webhooks(req, token, value_tx)
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
});
|
2023-05-27 15:20:58 +00:00
|
|
|
let server = Server::bind(&config.addr).serve(make_svc);
|
|
|
|
println!("Listening on http://{}", &config.addr);
|
2023-05-21 22:37:40 +00:00
|
|
|
|
|
|
|
let _join = tokio::spawn(server);
|
|
|
|
}
|
|
|
|
|
2023-05-21 21:47:54 +00:00
|
|
|
let mut client = XmppClient::new(
|
2023-05-27 15:20:58 +00:00
|
|
|
&String::from(config.jid),
|
|
|
|
config.password.as_str(),
|
|
|
|
config.rooms,
|
|
|
|
config.nickname,
|
2023-05-21 21:47:54 +00:00
|
|
|
);
|
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
|
|
|
}
|