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()?;
374eadef45 2026-03-28 58: core.tg.send(MyMessage::html_to_kb(format!("List of feeds owned by {:?}:", msg.sender.get_user_username()), 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" => {
374eadef45 2026-03-28 87: let res = conn.delete(number, sender).await.stack()?;
9adc69d514 2026-03-25 88: core.rm_feed(sender.into(), &number).await.stack()?;
374eadef45 2026-03-28 89: res
9adc69d514 2026-03-25 90: }
44575a91d3 2025-07-09 91: "/disable" => conn.disable(number, sender).await.stack()?.into(),
fae13a0e55 2025-06-28 92: _ => bail!("Command {command} {words:?} not handled."),
bb89b6fab8 2025-06-15 93: },
bb89b6fab8 2025-06-15 94: }
bb89b6fab8 2025-06-15 95: } else {
13265e7697 2026-01-10 96: "This command needs exactly one number.".into()
bb89b6fab8 2025-06-15 97: };
9adc69d514 2026-03-25 98: core.tg.send(MyMessage::html_to(reply, msg.chat.get_id())).await.stack()?;
bb89b6fab8 2025-06-15 99: Ok(())
bb89b6fab8 2025-06-15 100: }
bb89b6fab8 2025-06-15 101:
fabcca1eaf 2026-01-09 102: /// Validate command arguments, check permissions and update or add a channel feed configuration in the database.
fabcca1eaf 2026-01-09 103: ///
fabcca1eaf 2026-01-09 104: /// This function parses and validates parameters supplied by a user command (either "/update <id> ..." or "/add ..."),
fabcca1eaf 2026-01-09 105: /// verifies the channel username and feed URL, optionally validates an IV hash and a replacement regexp,
fabcca1eaf 2026-01-09 106: /// ensures both the bot and the command sender are administrators of the target channel, and performs the database update.
fabcca1eaf 2026-01-09 107: ///
fabcca1eaf 2026-01-09 108: /// # Parameters
fabcca1eaf 2026-01-09 109: ///
fabcca1eaf 2026-01-09 110: /// - `command` — the invoked command, expected to be either `"/update"` (followed by a numeric source id) or `"/add"`.
fabcca1eaf 2026-01-09 111: /// - `msg` — the incoming Telegram message; used to derive the command sender and target chat id for the reply.
9adc69d514 2026-03-25 112: /// - `words` — the command arguments: for `"/add"` expected `channel url [iv_hash|'-'] [url_re|'-']`; for `"/update"`
9adc69d514 2026-03-25 113: /// the first element must be a numeric `source_id` followed by the same parameters.
fae13a0e55 2025-06-28 114: pub async fn update (core: &Core, command: &str, msg: &Message, words: &[String]) -> Result<()> {
bb89b6fab8 2025-06-15 115: let sender = msg.sender.get_user_id()
44575a91d3 2025-07-09 116: .stack_err("Ignoring unreal users.")?;
bb89b6fab8 2025-06-15 117: let mut source_id: Option<i32> = None;
bb89b6fab8 2025-06-15 118: let at_least = "Requires at least 3 parameters.";
fae13a0e55 2025-06-28 119: let mut i_words = words.iter();
fae13a0e55 2025-06-28 120: match command {
bb89b6fab8 2025-06-15 121: "/update" => {
fae13a0e55 2025-06-28 122: let next_word = i_words.next().context(at_least)?;
e624ef9d66 2025-04-20 123: source_id = Some(next_word.parse::<i32>()
e624ef9d66 2025-04-20 124: .context(format!("I need a number, but got {next_word}."))?);
e624ef9d66 2025-04-20 125: },
e624ef9d66 2025-04-20 126: "/add" => {},
fae13a0e55 2025-06-28 127: _ => bail!("Passing {command} is not possible here."),
e624ef9d66 2025-04-20 128: };
e624ef9d66 2025-04-20 129: let (channel, url, iv_hash, url_re) = (
fae13a0e55 2025-06-28 130: i_words.next().context(at_least)?,
fae13a0e55 2025-06-28 131: i_words.next().context(at_least)?,
fae13a0e55 2025-06-28 132: i_words.next(),
fae13a0e55 2025-06-28 133: i_words.next());
e624ef9d66 2025-04-20 134: if ! RE_USERNAME.is_match(channel) {
e624ef9d66 2025-04-20 135: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}");
e624ef9d66 2025-04-20 136: };
7393d62235 2026-01-07 137: {
7393d62235 2026-01-07 138: let parsed_url = Url::parse(url)
7393d62235 2026-01-07 139: .stack_err("Expecting a valid link to ATOM/RSS feed.")?;
7393d62235 2026-01-07 140: match parsed_url.scheme() {
7393d62235 2026-01-07 141: "http" | "https" => {},
7393d62235 2026-01-07 142: scheme => {
7393d62235 2026-01-07 143: bail!("Unsupported URL scheme: {scheme}");
7393d62235 2026-01-07 144: },
7393d62235 2026-01-07 145: };
e624ef9d66 2025-04-20 146: }
e624ef9d66 2025-04-20 147: let iv_hash = match iv_hash {
e624ef9d66 2025-04-20 148: Some(hash) => {
bb89b6fab8 2025-06-15 149: match hash.as_ref() {
e624ef9d66 2025-04-20 150: "-" => None,
e624ef9d66 2025-04-20 151: thing => {
e624ef9d66 2025-04-20 152: if ! RE_IV_HASH.is_match(thing) {
e624ef9d66 2025-04-20 153: bail!("IV hash should be 14 hex digits.\nNot {thing:?}");
613a665847 2021-11-15 154: };
613a665847 2021-11-15 155: Some(thing)
613a665847 2021-11-15 156: },
c1e27b74ed 2021-11-13 157: }
10c25017bb 2021-11-13 158: },
10c25017bb 2021-11-13 159: None => None,
10c25017bb 2021-11-13 160: };
10c25017bb 2021-11-13 161: let url_re = match url_re {
10c25017bb 2021-11-13 162: Some(re) => {
bb89b6fab8 2025-06-15 163: match re.as_ref() {
c1e27b74ed 2021-11-13 164: "-" => None,
613a665847 2021-11-15 165: thing => {
613a665847 2021-11-15 166: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
613a665847 2021-11-15 167: Some(thing)
613a665847 2021-11-15 168: }
c1e27b74ed 2021-11-13 169: }
10c25017bb 2021-11-13 170: },
10c25017bb 2021-11-13 171: None => None,
10c25017bb 2021-11-13 172: };
fae13a0e55 2025-06-28 173: let chat_id = ChatUsername::from(channel.as_ref());
9adc69d514 2026-03-25 174: let channel_id = core.tg.client.execute(GetChat::new(chat_id.clone())).await.stack_err("getting GetChat")?.id;
9c4f09193a 2026-01-09 175: let chan_adm = core.tg.client.execute(GetChatAdministrators::new(chat_id)).await
bb89b6fab8 2025-06-15 176: .context("Sorry, I have no access to that chat.")?;
10c25017bb 2021-11-13 177: let (mut me, mut user) = (false, false);
10c25017bb 2021-11-13 178: for admin in chan_adm {
e624ef9d66 2025-04-20 179: let member_id = match admin {
e624ef9d66 2025-04-20 180: ChatMember::Creator(member) => member.user.id,
e624ef9d66 2025-04-20 181: ChatMember::Administrator(member) => member.user.id,
e624ef9d66 2025-04-20 182: ChatMember::Left(_)
e624ef9d66 2025-04-20 183: | ChatMember::Kicked(_)
bb89b6fab8 2025-06-15 184: | ChatMember::Member{..}
e624ef9d66 2025-04-20 185: | ChatMember::Restricted(_) => continue,
bb89b6fab8 2025-06-15 186: };
9c4f09193a 2026-01-09 187: if member_id == core.tg.me.id {
e624ef9d66 2025-04-20 188: me = true;
bb89b6fab8 2025-06-15 189: }
e624ef9d66 2025-04-20 190: if member_id == sender {
e624ef9d66 2025-04-20 191: user = true;
bb89b6fab8 2025-06-15 192: }
10c25017bb 2021-11-13 193: };
10c25017bb 2021-11-13 194: if ! me { bail!("I need to be admin on that channel."); };
10c25017bb 2021-11-13 195: if ! user { bail!("You should be admin on that channel."); };
44575a91d3 2025-07-09 196: let mut conn = core.db.begin().await.stack()?;
fabcca1eaf 2026-01-09 197: let update = conn.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await.stack()?;
9adc69d514 2026-03-25 198: core.tg.send(MyMessage::html_to(update, msg.chat.get_id())).await.stack()?;
9adc69d514 2026-03-25 199: if command == "/add" {
9adc69d514 2026-03-25 200: if let Some(new_record) = conn.get_one_name(sender, channel).await.stack()? {
9adc69d514 2026-03-25 201: core.add_feed(sender.into(), new_record.source_id, new_record.channel).await.stack()?;
9adc69d514 2026-03-25 202: } else {
9adc69d514 2026-03-25 203: bail!("Failed to read data on freshly inserted source.");
9adc69d514 2026-03-25 204: }
9adc69d514 2026-03-25 205: };
a7f91033c0 2021-11-13 206: Ok(())
9171c791eb 2021-11-13 207: }