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