2022-11-02 22:58:52 +00:00
|
|
|
use crate::db::Db;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use teloxide::{prelude::*, utils::command::BotCommands};
|
|
|
|
|
|
|
|
#[derive(BotCommands, Clone)]
|
|
|
|
#[command(
|
|
|
|
rename_rule = "lowercase",
|
|
|
|
description = "These commands are for administering AodeRelay"
|
|
|
|
)]
|
|
|
|
enum Command {
|
|
|
|
#[command(description = "Display this text.")]
|
|
|
|
Help,
|
|
|
|
|
|
|
|
#[command(description = "Block a domain from the relay.")]
|
2022-11-02 23:04:57 +00:00
|
|
|
Block { domain: String },
|
2022-11-02 22:58:52 +00:00
|
|
|
|
|
|
|
#[command(description = "Unblock a domain from the relay.")]
|
2022-11-02 23:04:57 +00:00
|
|
|
Unblock { domain: String },
|
2022-11-02 22:58:52 +00:00
|
|
|
|
|
|
|
#[command(description = "Allow a domain to connect to the relay (for RESTRICTED_MODE)")]
|
2022-11-02 23:04:57 +00:00
|
|
|
Allow { domain: String },
|
2022-11-02 22:58:52 +00:00
|
|
|
|
|
|
|
#[command(description = "Disallow a domain to connect to the relay (for RESTRICTED_MODE)")]
|
2022-11-02 23:04:57 +00:00
|
|
|
Disallow { domain: String },
|
2022-11-02 22:58:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn start(admin_handle: String, db: Db, token: &str) {
|
|
|
|
let bot = Bot::new(token);
|
|
|
|
let admin_handle = Arc::new(admin_handle);
|
|
|
|
|
|
|
|
actix_rt::spawn(async move {
|
|
|
|
teloxide::repl(bot, move |bot: Bot, msg: Message, cmd: Command| {
|
|
|
|
let admin_handle = admin_handle.clone();
|
|
|
|
let db = db.clone();
|
|
|
|
|
|
|
|
async move {
|
|
|
|
if !is_admin(&admin_handle, &msg) {
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
|
|
|
|
answer(bot, msg, cmd, db).await
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_admin(admin_handle: &str, message: &Message) -> bool {
|
|
|
|
message
|
|
|
|
.from()
|
|
|
|
.and_then(|user| user.username.as_deref())
|
|
|
|
.map(|username| username == admin_handle)
|
|
|
|
.unwrap_or(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn answer(bot: Bot, msg: Message, cmd: Command, db: Db) -> ResponseResult<()> {
|
|
|
|
match cmd {
|
|
|
|
Command::Help => {
|
|
|
|
bot.send_message(msg.chat.id, Command::descriptions().to_string())
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
Command::Block { domain } => {
|
2022-11-02 23:04:57 +00:00
|
|
|
if db.add_blocks(vec![domain.clone()]).await.is_ok() {
|
2022-11-02 22:58:52 +00:00
|
|
|
bot.send_message(msg.chat.id, format!("{} has been blocked", domain))
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Command::Unblock { domain } => {
|
2022-11-02 23:04:57 +00:00
|
|
|
if db.remove_blocks(vec![domain.clone()]).await.is_ok() {
|
2022-11-02 22:58:52 +00:00
|
|
|
bot.send_message(msg.chat.id, format!("{} has been unblocked", domain))
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Command::Allow { domain } => {
|
2022-11-02 23:04:57 +00:00
|
|
|
if db.add_allows(vec![domain.clone()]).await.is_ok() {
|
2022-11-02 22:58:52 +00:00
|
|
|
bot.send_message(msg.chat.id, format!("{} has been allowed", domain))
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Command::Disallow { domain } => {
|
2022-11-02 23:04:57 +00:00
|
|
|
if db.remove_allows(vec![domain.clone()]).await.is_ok() {
|
2022-11-02 22:58:52 +00:00
|
|
|
bot.send_message(msg.chat.id, format!("{} has been disallwoed", domain))
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|