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