9adc69d514 2026-03-25 1: use crate::{
9adc69d514 2026-03-25 2: core::Core,
9adc69d514 2026-03-25 3: tg_bot::{
9adc69d514 2026-03-25 4: Callback,
9adc69d514 2026-03-25 5: MyMessage,
9adc69d514 2026-03-25 6: get_kb,
9adc69d514 2026-03-25 7: },
9adc69d514 2026-03-25 8: };
44575a91d3 2025-07-09 9:
28da2e2a00 2022-03-12 10: use lazy_static::lazy_static;
9171c791eb 2021-11-13 11: use regex::Regex;
9171c791eb 2021-11-13 12: use sedregex::ReplaceCommand;
44575a91d3 2025-07-09 13: use stacked_errors::{
44575a91d3 2025-07-09 14: Result,
44575a91d3 2025-07-09 15: StackableErr,
44575a91d3 2025-07-09 16: bail,
44575a91d3 2025-07-09 17: };
bb89b6fab8 2025-06-15 18: use tgbot::types::{
3fd8c40aa8 2026-03-30 19: CallbackQuery,
bb89b6fab8 2025-06-15 20: ChatMember,
bb89b6fab8 2025-06-15 21: ChatUsername,
bb89b6fab8 2025-06-15 22: GetChat,
bb89b6fab8 2025-06-15 23: GetChatAdministrators,
bb89b6fab8 2025-06-15 24: Message,
bb89b6fab8 2025-06-15 25: };
7393d62235 2026-01-07 26: use url::Url;
9171c791eb 2021-11-13 27:
9171c791eb 2021-11-13 28: lazy_static! {
fae13a0e55 2025-06-28 29: static ref RE_USERNAME: Regex = Regex::new(r"^@([a-zA-Z][a-zA-Z0-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:
fabcca1eaf 2026-01-09 33: /// Sends an informational message to the message's chat linking to the bot help channel.
fae13a0e55 2025-06-28 34: pub async fn start (core: &Core, msg: &Message) -> Result<()> {
3fd8c40aa8 2026-03-30 35: core.tg.send(MyMessage::html_to(
3fd8c40aa8 2026-03-30 36: "We are open. Probably. Visit <a href=\"https://t.me/rsstg_bot_help/3\">channel</a>) for details.",
9adc69d514 2026-03-25 37: msg.chat.get_id()
9adc69d514 2026-03-25 38: )).await.stack()?;
44575a91d3 2025-07-09 39: Ok(())
44575a91d3 2025-07-09 40: }
44575a91d3 2025-07-09 41:
fabcca1eaf 2026-01-09 42: /// Send the sender's subscription list to the chat.
fabcca1eaf 2026-01-09 43: ///
fabcca1eaf 2026-01-09 44: /// Retrieves the message sender's user ID, obtains their subscription list from `core`,
fabcca1eaf 2026-01-09 45: /// and sends the resulting reply into the message chat using MarkdownV2.
44575a91d3 2025-07-09 46: pub async fn list (core: &Core, msg: &Message) -> Result<()> {
44575a91d3 2025-07-09 47: let sender = msg.sender.get_user_id()
44575a91d3 2025-07-09 48: .stack_err("Ignoring unreal users.")?;
44575a91d3 2025-07-09 49: let reply = core.list(sender).await.stack()?;
3fd8c40aa8 2026-03-30 50: core.tg.send(MyMessage::html_to(reply, msg.chat.get_id())).await.stack()?;
9adc69d514 2026-03-25 51: Ok(())
9adc69d514 2026-03-25 52: }
9adc69d514 2026-03-25 53:
9adc69d514 2026-03-25 54: pub async fn test (core: &Core, msg: &Message) -> Result<()> {
9adc69d514 2026-03-25 55: let sender: i64 = msg.sender.get_user_id()
9adc69d514 2026-03-25 56: .stack_err("Ignoring unreal users.")?.into();
9adc69d514 2026-03-25 57: let feeds = core.get_feeds(sender).await.stack()?;
3fd8c40aa8 2026-03-30 58: let kb = get_kb(&Callback::menu(), feeds).await.stack()?;
3fd8c40aa8 2026-03-30 59: core.tg.send(MyMessage::html_to_kb("Main menu:", msg.chat.get_id(), kb)).await.stack()?;
fae13a0e55 2025-06-28 60: Ok(())
fae13a0e55 2025-06-28 61: }
fae13a0e55 2025-06-28 62:
fabcca1eaf 2026-01-09 63: /// Handle channel-management commands that operate on a single numeric source ID.
fabcca1eaf 2026-01-09 64: ///
fabcca1eaf 2026-01-09 65: /// This validates that exactly one numeric argument is provided, performs the requested
fabcca1eaf 2026-01-09 66: /// operation (check, clean, enable, delete, disable) against the database or core,
fabcca1eaf 2026-01-09 67: /// and sends the resulting reply to the chat.
fabcca1eaf 2026-01-09 68: ///
fabcca1eaf 2026-01-09 69: /// # Parameters
fabcca1eaf 2026-01-09 70: ///
fabcca1eaf 2026-01-09 71: /// - `core`: application core containing database and Telegram clients.
fabcca1eaf 2026-01-09 72: /// - `command`: command string (e.g. "/check", "/clean", "/enable", "/delete", "/disable").
fabcca1eaf 2026-01-09 73: /// - `msg`: incoming Telegram message that triggered the command; used to determine sender and chat.
fabcca1eaf 2026-01-09 74: /// - `words`: command arguments; expected to contain exactly one element that parses as a 32-bit integer.
fae13a0e55 2025-06-28 75: pub async fn command (core: &Core, command: &str, msg: &Message, words: &[String]) -> Result<()> {
44575a91d3 2025-07-09 76: let mut conn = core.db.begin().await.stack()?;
fae13a0e55 2025-06-28 77: let sender = msg.sender.get_user_id()
44575a91d3 2025-07-09 78: .stack_err("Ignoring unreal users.")?;
fae13a0e55 2025-06-28 79: let reply = if words.len() == 1 {
fae13a0e55 2025-06-28 80: match words[0].parse::<i32>() {
fae13a0e55 2025-06-28 81: Err(err) => format!("I need a number.\n{}", &err).into(),
fae13a0e55 2025-06-28 82: Ok(number) => match command {
acb0a4ac54 2025-09-28 83: "/check" => core.check(number, false, None).await
fae13a0e55 2025-06-28 84: .context("Channel check failed.")?.into(),
44575a91d3 2025-07-09 85: "/clean" => conn.clean(number, sender).await.stack()?,
44575a91d3 2025-07-09 86: "/enable" => conn.enable(number, sender).await.stack()?.into(),
9adc69d514 2026-03-25 87: "/delete" => {
374eadef45 2026-03-28 88: let res = conn.delete(number, sender).await.stack()?;
9adc69d514 2026-03-25 89: core.rm_feed(sender.into(), &number).await.stack()?;
374eadef45 2026-03-28 90: res
9adc69d514 2026-03-25 91: }
44575a91d3 2025-07-09 92: "/disable" => conn.disable(number, sender).await.stack()?.into(),
fae13a0e55 2025-06-28 93: _ => bail!("Command {command} {words:?} not handled."),
bb89b6fab8 2025-06-15 94: },
bb89b6fab8 2025-06-15 95: }
bb89b6fab8 2025-06-15 96: } else {
13265e7697 2026-01-10 97: "This command needs exactly one number.".into()
bb89b6fab8 2025-06-15 98: };
9adc69d514 2026-03-25 99: core.tg.send(MyMessage::html_to(reply, msg.chat.get_id())).await.stack()?;
bb89b6fab8 2025-06-15 100: Ok(())
bb89b6fab8 2025-06-15 101: }
bb89b6fab8 2025-06-15 102:
fabcca1eaf 2026-01-09 103: /// Validate command arguments, check permissions and update or add a channel feed configuration in the database.
fabcca1eaf 2026-01-09 104: ///
fabcca1eaf 2026-01-09 105: /// This function parses and validates parameters supplied by a user command (either "/update <id> ..." or "/add ..."),
fabcca1eaf 2026-01-09 106: /// verifies the channel username and feed URL, optionally validates an IV hash and a replacement regexp,
fabcca1eaf 2026-01-09 107: /// ensures both the bot and the command sender are administrators of the target channel, and performs the database update.
fabcca1eaf 2026-01-09 108: ///
fabcca1eaf 2026-01-09 109: /// # Parameters
fabcca1eaf 2026-01-09 110: ///
fabcca1eaf 2026-01-09 111: /// - `command` — the invoked command, expected to be either `"/update"` (followed by a numeric source id) or `"/add"`.
fabcca1eaf 2026-01-09 112: /// - `msg` — the incoming Telegram message; used to derive the command sender and target chat id for the reply.
9adc69d514 2026-03-25 113: /// - `words` — the command arguments: for `"/add"` expected `channel url [iv_hash|'-'] [url_re|'-']`; for `"/update"`
9adc69d514 2026-03-25 114: /// the first element must be a numeric `source_id` followed by the same parameters.
fae13a0e55 2025-06-28 115: pub async fn update (core: &Core, command: &str, msg: &Message, words: &[String]) -> Result<()> {
bb89b6fab8 2025-06-15 116: let sender = msg.sender.get_user_id()
44575a91d3 2025-07-09 117: .stack_err("Ignoring unreal users.")?;
bb89b6fab8 2025-06-15 118: let mut source_id: Option<i32> = None;
bb89b6fab8 2025-06-15 119: let at_least = "Requires at least 3 parameters.";
fae13a0e55 2025-06-28 120: let mut i_words = words.iter();
fae13a0e55 2025-06-28 121: match command {
bb89b6fab8 2025-06-15 122: "/update" => {
fae13a0e55 2025-06-28 123: let next_word = i_words.next().context(at_least)?;
e624ef9d66 2025-04-20 124: source_id = Some(next_word.parse::<i32>()
e624ef9d66 2025-04-20 125: .context(format!("I need a number, but got {next_word}."))?);
e624ef9d66 2025-04-20 126: },
e624ef9d66 2025-04-20 127: "/add" => {},
fae13a0e55 2025-06-28 128: _ => bail!("Passing {command} is not possible here."),
e624ef9d66 2025-04-20 129: };
e624ef9d66 2025-04-20 130: let (channel, url, iv_hash, url_re) = (
fae13a0e55 2025-06-28 131: i_words.next().context(at_least)?,
fae13a0e55 2025-06-28 132: i_words.next().context(at_least)?,
fae13a0e55 2025-06-28 133: i_words.next(),
fae13a0e55 2025-06-28 134: i_words.next());
e624ef9d66 2025-04-20 135: if ! RE_USERNAME.is_match(channel) {
e624ef9d66 2025-04-20 136: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}");
e624ef9d66 2025-04-20 137: };
7393d62235 2026-01-07 138: {
7393d62235 2026-01-07 139: let parsed_url = Url::parse(url)
7393d62235 2026-01-07 140: .stack_err("Expecting a valid link to ATOM/RSS feed.")?;
7393d62235 2026-01-07 141: match parsed_url.scheme() {
7393d62235 2026-01-07 142: "http" | "https" => {},
7393d62235 2026-01-07 143: scheme => {
7393d62235 2026-01-07 144: bail!("Unsupported URL scheme: {scheme}");
7393d62235 2026-01-07 145: },
7393d62235 2026-01-07 146: };
e624ef9d66 2025-04-20 147: }
e624ef9d66 2025-04-20 148: let iv_hash = match iv_hash {
e624ef9d66 2025-04-20 149: Some(hash) => {
bb89b6fab8 2025-06-15 150: match hash.as_ref() {
e624ef9d66 2025-04-20 151: "-" => None,
e624ef9d66 2025-04-20 152: thing => {
e624ef9d66 2025-04-20 153: if ! RE_IV_HASH.is_match(thing) {
e624ef9d66 2025-04-20 154: bail!("IV hash should be 14 hex digits.\nNot {thing:?}");
613a665847 2021-11-15 155: };
613a665847 2021-11-15 156: Some(thing)
613a665847 2021-11-15 157: },
c1e27b74ed 2021-11-13 158: }
10c25017bb 2021-11-13 159: },
10c25017bb 2021-11-13 160: None => None,
10c25017bb 2021-11-13 161: };
10c25017bb 2021-11-13 162: let url_re = match url_re {
10c25017bb 2021-11-13 163: Some(re) => {
bb89b6fab8 2025-06-15 164: match re.as_ref() {
c1e27b74ed 2021-11-13 165: "-" => None,
613a665847 2021-11-15 166: thing => {
613a665847 2021-11-15 167: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
613a665847 2021-11-15 168: Some(thing)
613a665847 2021-11-15 169: }
c1e27b74ed 2021-11-13 170: }
10c25017bb 2021-11-13 171: },
10c25017bb 2021-11-13 172: None => None,
10c25017bb 2021-11-13 173: };
fae13a0e55 2025-06-28 174: let chat_id = ChatUsername::from(channel.as_ref());
9adc69d514 2026-03-25 175: let channel_id = core.tg.client.execute(GetChat::new(chat_id.clone())).await.stack_err("getting GetChat")?.id;
9c4f09193a 2026-01-09 176: let chan_adm = core.tg.client.execute(GetChatAdministrators::new(chat_id)).await
bb89b6fab8 2025-06-15 177: .context("Sorry, I have no access to that chat.")?;
10c25017bb 2021-11-13 178: let (mut me, mut user) = (false, false);
10c25017bb 2021-11-13 179: for admin in chan_adm {
e624ef9d66 2025-04-20 180: let member_id = match admin {
e624ef9d66 2025-04-20 181: ChatMember::Creator(member) => member.user.id,
e624ef9d66 2025-04-20 182: ChatMember::Administrator(member) => member.user.id,
e624ef9d66 2025-04-20 183: ChatMember::Left(_)
e624ef9d66 2025-04-20 184: | ChatMember::Kicked(_)
bb89b6fab8 2025-06-15 185: | ChatMember::Member{..}
e624ef9d66 2025-04-20 186: | ChatMember::Restricted(_) => continue,
bb89b6fab8 2025-06-15 187: };
9c4f09193a 2026-01-09 188: if member_id == core.tg.me.id {
e624ef9d66 2025-04-20 189: me = true;
bb89b6fab8 2025-06-15 190: }
e624ef9d66 2025-04-20 191: if member_id == sender {
e624ef9d66 2025-04-20 192: user = true;
bb89b6fab8 2025-06-15 193: }
10c25017bb 2021-11-13 194: };
10c25017bb 2021-11-13 195: if ! me { bail!("I need to be admin on that channel."); };
10c25017bb 2021-11-13 196: if ! user { bail!("You should be admin on that channel."); };
44575a91d3 2025-07-09 197: let mut conn = core.db.begin().await.stack()?;
fabcca1eaf 2026-01-09 198: let update = conn.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await.stack()?;
9adc69d514 2026-03-25 199: core.tg.send(MyMessage::html_to(update, msg.chat.get_id())).await.stack()?;
9adc69d514 2026-03-25 200: if command == "/add" {
9adc69d514 2026-03-25 201: if let Some(new_record) = conn.get_one_name(sender, channel).await.stack()? {
9adc69d514 2026-03-25 202: core.add_feed(sender.into(), new_record.source_id, new_record.channel).await.stack()?;
9adc69d514 2026-03-25 203: } else {
9adc69d514 2026-03-25 204: bail!("Failed to read data on freshly inserted source.");
9adc69d514 2026-03-25 205: }
9adc69d514 2026-03-25 206: };
a7f91033c0 2021-11-13 207: Ok(())
9171c791eb 2021-11-13 208: }