9171c791eb 2021-11-13 1: use crate::core::Core;
a2880e5100 2025-04-19 2:
a2880e5100 2025-04-19 3: use anyhow::{
a2880e5100 2025-04-19 4: bail,
a2880e5100 2025-04-19 5: Context,
a2880e5100 2025-04-19 6: Result
a2880e5100 2025-04-19 7: };
28da2e2a00 2022-03-12 8: use lazy_static::lazy_static;
9171c791eb 2021-11-13 9: use regex::Regex;
9171c791eb 2021-11-13 10: use sedregex::ReplaceCommand;
26339860ce 2022-02-13 11: use std::borrow::Cow;
a2880e5100 2025-04-19 12: use teloxide::{
a2880e5100 2025-04-19 13: Bot,
a2880e5100 2025-04-19 14: dispatching::dialogue::GetChatId,
a2880e5100 2025-04-19 15: payloads::GetChatAdministrators,
a2880e5100 2025-04-19 16: requests::{
a2880e5100 2025-04-19 17: Requester,
a2880e5100 2025-04-19 18: ResponseResult
a2880e5100 2025-04-19 19: },
a2880e5100 2025-04-19 20: types::{
a2880e5100 2025-04-19 21: Message,
a2880e5100 2025-04-19 22: UserId,
a2880e5100 2025-04-19 23: },
a2880e5100 2025-04-19 24: utils::command::BotCommands,
a2880e5100 2025-04-19 25: };
9171c791eb 2021-11-13 26:
9171c791eb 2021-11-13 27: lazy_static! {
9171c791eb 2021-11-13 28: static ref RE_USERNAME: Regex = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$").unwrap();
ec2eac7ea2 2024-05-14 29: static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.:0-9/?=]+$").unwrap();
9171c791eb 2021-11-13 30: static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
9171c791eb 2021-11-13 31: }
9171c791eb 2021-11-13 32:
a2880e5100 2025-04-19 33: #[derive(BotCommands, Clone)]
a2880e5100 2025-04-19 34: #[command(rename_rule = "lowercase", description = "Supported commands:")]
a2880e5100 2025-04-19 35: enum Command {
a2880e5100 2025-04-19 36: #[command(description = "display this help.")]
a2880e5100 2025-04-19 37: Help,
a2880e5100 2025-04-19 38: #[command(description = "Does nothing.")]
a2880e5100 2025-04-19 39: Start,
a2880e5100 2025-04-19 40: #[commant(description = "List active subscriptions.")]
a2880e5100 2025-04-19 41: List,
a2880e5100 2025-04-19 42: }
a2880e5100 2025-04-19 43:
a2880e5100 2025-04-19 44: pub async fn cmd_handler(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {
a2880e5100 2025-04-19 45: match cmd {
a2880e5100 2025-04-19 46: Command::Help => bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?,
a2880e5100 2025-04-19 47: Command::Start => bot.send_message(msg.chat.id,
a2880e5100 2025-04-19 48: "We are open. Probably. Visit [channel](https://t.me/rsstg_bot_help/3) for details.").await?,
a2880e5100 2025-04-19 49: Command::List => bot.send_message(msg.chat.id, core.list(msg.from).await?).await?,
a2880e5100 2025-04-19 50: };
9171c791eb 2021-11-13 51: Ok(())
9171c791eb 2021-11-13 52: }
9171c791eb 2021-11-13 53:
a2880e5100 2025-04-19 54: pub async fn list(core: &Core, sender: UserId) -> Result<()> {
26339860ce 2022-02-13 55: core.send(core.list(sender).await?, Some(sender), Some(telegram_bot::types::ParseMode::MarkdownV2)).await?;
9171c791eb 2021-11-13 56: Ok(())
9171c791eb 2021-11-13 57: }
9171c791eb 2021-11-13 58:
9171c791eb 2021-11-13 59: pub async fn command(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
409eb5bafc 2022-06-12 60: if command.len() >= 2 {
409eb5bafc 2022-06-12 61: let msg: Cow<str> = match &command[1].parse::<i32>() {
409eb5bafc 2022-06-12 62: Err(err) => format!("I need a number.\n{}", &err).into(),
409eb5bafc 2022-06-12 63: Ok(number) => match command[0] {
409eb5bafc 2022-06-12 64: "/check" => core.check(number, sender, false).await
409eb5bafc 2022-06-12 65: .context("Channel check failed.")?,
409eb5bafc 2022-06-12 66: "/clean" => core.clean(number, sender).await?,
409eb5bafc 2022-06-12 67: "/enable" => core.enable(number, sender).await?.into(),
409eb5bafc 2022-06-12 68: "/delete" => core.delete(number, sender).await?,
409eb5bafc 2022-06-12 69: "/disable" => core.disable(number, sender).await?.into(),
409eb5bafc 2022-06-12 70: _ => bail!("Command {} not handled.", &command[0]),
409eb5bafc 2022-06-12 71: },
409eb5bafc 2022-06-12 72: };
409eb5bafc 2022-06-12 73: core.send(msg, Some(sender), None).await?;
409eb5bafc 2022-06-12 74: } else {
409eb5bafc 2022-06-12 75: core.send("This command needs a number.", Some(sender), None).await?;
409eb5bafc 2022-06-12 76: }
9171c791eb 2021-11-13 77: Ok(())
9171c791eb 2021-11-13 78: }
9171c791eb 2021-11-13 79:
9171c791eb 2021-11-13 80: pub async fn update(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
9171c791eb 2021-11-13 81: let mut source_id: Option<i32> = None;
9171c791eb 2021-11-13 82: let at_least = "Requires at least 3 parameters.";
cab82b7ff4 2022-03-23 83: let mut i_command = command.iter();
cab82b7ff4 2022-03-23 84: let first_word = i_command.next().context(at_least)?;
cab82b7ff4 2022-03-23 85: match *first_word {
9171c791eb 2021-11-13 86: "/update" => {
cab82b7ff4 2022-03-23 87: let next_word = i_command.next().context(at_least)?;
cab82b7ff4 2022-03-23 88: source_id = Some(next_word.parse::<i32>()
cab82b7ff4 2022-03-23 89: .context(format!("I need a number, but got {}.", next_word))?);
9171c791eb 2021-11-13 90: },
cab82b7ff4 2022-03-23 91: "/add" => {},
cab82b7ff4 2022-03-23 92: _ => bail!("Passing {} is not possible here.", first_word),
9171c791eb 2021-11-13 93: };
9171c791eb 2021-11-13 94: let (channel, url, iv_hash, url_re) = (
9171c791eb 2021-11-13 95: i_command.next().context(at_least)?,
9171c791eb 2021-11-13 96: i_command.next().context(at_least)?,
9171c791eb 2021-11-13 97: i_command.next(),
9171c791eb 2021-11-13 98: i_command.next());
f988dfd28f 2022-02-13 99: if ! RE_USERNAME.is_match(channel) {
4632d20d39 2021-11-13 100: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {:?}", &channel);
4632d20d39 2021-11-13 101: };
f988dfd28f 2022-02-13 102: if ! RE_LINK.is_match(url) {
4632d20d39 2021-11-13 103: bail!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {:?}", &url);
9171c791eb 2021-11-13 104: }
9171c791eb 2021-11-13 105: let iv_hash = match iv_hash {
9171c791eb 2021-11-13 106: Some(hash) => {
c1e27b74ed 2021-11-13 107: match *hash {
c1e27b74ed 2021-11-13 108: "-" => None,
613a665847 2021-11-15 109: thing => {
613a665847 2021-11-15 110: if ! RE_IV_HASH.is_match(thing) {
613a665847 2021-11-15 111: bail!("IV hash should be 14 hex digits.\nNot {:?}", thing);
613a665847 2021-11-15 112: };
613a665847 2021-11-15 113: Some(thing)
613a665847 2021-11-15 114: },
c1e27b74ed 2021-11-13 115: }
10c25017bb 2021-11-13 116: },
10c25017bb 2021-11-13 117: None => None,
10c25017bb 2021-11-13 118: };
10c25017bb 2021-11-13 119: let url_re = match url_re {
10c25017bb 2021-11-13 120: Some(re) => {
c1e27b74ed 2021-11-13 121: match *re {
c1e27b74ed 2021-11-13 122: "-" => None,
613a665847 2021-11-15 123: thing => {
613a665847 2021-11-15 124: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
613a665847 2021-11-15 125: Some(thing)
613a665847 2021-11-15 126: }
c1e27b74ed 2021-11-13 127: }
10c25017bb 2021-11-13 128: },
9171c791eb 2021-11-13 129: None => None,
9171c791eb 2021-11-13 130: };
79c91a5357 2024-08-02 131: let channel_id = i64::from(core.request(telegram_bot::GetChat::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await?.id());
79c91a5357 2024-08-02 132: let chan_adm = core.request(telegram_bot::GetChatAdministrators::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await
4632d20d39 2021-11-13 133: .context("Sorry, I have no access to that chat.")?;
9171c791eb 2021-11-13 134: let (mut me, mut user) = (false, false);
9171c791eb 2021-11-13 135: for admin in chan_adm {
9171c791eb 2021-11-13 136: if admin.user.id == core.my.id {
9171c791eb 2021-11-13 137: me = true;
9171c791eb 2021-11-13 138: };
9171c791eb 2021-11-13 139: if admin.user.id == sender {
9171c791eb 2021-11-13 140: user = true;
9171c791eb 2021-11-13 141: };
9171c791eb 2021-11-13 142: };
4632d20d39 2021-11-13 143: if ! me { bail!("I need to be admin on that channel."); };
4632d20d39 2021-11-13 144: if ! user { bail!("You should be admin on that channel."); };
26339860ce 2022-02-13 145: core.send(core.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await?, Some(sender), None).await?;
a7f91033c0 2021-11-13 146: Ok(())
9171c791eb 2021-11-13 147: }