10c25017bb 2021-11-13 arcade: use crate::core::Core;
a2880e5100 2025-04-19 arcade:
e624ef9d66 2025-04-20 arcade: use lazy_static::lazy_static;
e624ef9d66 2025-04-20 arcade: use regex::Regex;
e624ef9d66 2025-04-20 arcade: use sedregex::ReplaceCommand;
44575a91d3 2025-07-09 arcade: use stacked_errors::{
44575a91d3 2025-07-09 arcade: Result,
44575a91d3 2025-07-09 arcade: StackableErr,
44575a91d3 2025-07-09 arcade: bail,
44575a91d3 2025-07-09 arcade: };
bb89b6fab8 2025-06-15 arcade: use tgbot::types::{
bb89b6fab8 2025-06-15 arcade: ChatMember,
bb89b6fab8 2025-06-15 arcade: ChatUsername,
bb89b6fab8 2025-06-15 arcade: GetChat,
bb89b6fab8 2025-06-15 arcade: GetChatAdministrators,
bb89b6fab8 2025-06-15 arcade: Message,
bb89b6fab8 2025-06-15 arcade: ParseMode::MarkdownV2,
bb89b6fab8 2025-06-15 arcade: };
7393d62235 2026-01-07 arcade: use url::Url;
e624ef9d66 2025-04-20 arcade:
e624ef9d66 2025-04-20 arcade: lazy_static! {
fae13a0e55 2025-06-28 arcade: static ref RE_USERNAME: Regex = Regex::new(r"^@([a-zA-Z][a-zA-Z0-9_]+)$").unwrap();
e624ef9d66 2025-04-20 arcade: static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
e624ef9d66 2025-04-20 arcade: }
e624ef9d66 2025-04-20 arcade:
fabcca1eaf 2026-01-09 arcade: /// Sends an informational message to the message's chat linking to the bot help channel.
fae13a0e55 2025-06-28 arcade: pub async fn start (core: &Core, msg: &Message) -> Result<()> {
9c4f09193a 2026-01-09 arcade: core.tg.send("We are open\\. Probably\\. Visit [channel](https://t.me/rsstg_bot_help/3) for details\\.",
44575a91d3 2025-07-09 arcade: Some(msg.chat.get_id()), Some(MarkdownV2)).await.stack()?;
fae13a0e55 2025-06-28 arcade: Ok(())
fae13a0e55 2025-06-28 arcade: }
fae13a0e55 2025-06-28 arcade:
fabcca1eaf 2026-01-09 arcade: /// Send the sender's subscription list to the chat.
fabcca1eaf 2026-01-09 arcade: ///
fabcca1eaf 2026-01-09 arcade: /// Retrieves the message sender's user ID, obtains their subscription list from `core`,
fabcca1eaf 2026-01-09 arcade: /// and sends the resulting reply into the message chat using MarkdownV2.
fae13a0e55 2025-06-28 arcade: pub async fn list (core: &Core, msg: &Message) -> Result<()> {
fae13a0e55 2025-06-28 arcade: let sender = msg.sender.get_user_id()
44575a91d3 2025-07-09 arcade: .stack_err("Ignoring unreal users.")?;
44575a91d3 2025-07-09 arcade: let reply = core.list(sender).await.stack()?;
9c4f09193a 2026-01-09 arcade: core.tg.send(reply, Some(msg.chat.get_id()), Some(MarkdownV2)).await.stack()?;
fae13a0e55 2025-06-28 arcade: Ok(())
fae13a0e55 2025-06-28 arcade: }
fae13a0e55 2025-06-28 arcade:
fabcca1eaf 2026-01-09 arcade: /// Handle channel-management commands that operate on a single numeric source ID.
fabcca1eaf 2026-01-09 arcade: ///
fabcca1eaf 2026-01-09 arcade: /// This validates that exactly one numeric argument is provided, performs the requested
fabcca1eaf 2026-01-09 arcade: /// operation (check, clean, enable, delete, disable) against the database or core,
fabcca1eaf 2026-01-09 arcade: /// and sends the resulting reply to the chat.
fabcca1eaf 2026-01-09 arcade: ///
fabcca1eaf 2026-01-09 arcade: /// # Parameters
fabcca1eaf 2026-01-09 arcade: ///
fabcca1eaf 2026-01-09 arcade: /// - `core`: application core containing database and Telegram clients.
fabcca1eaf 2026-01-09 arcade: /// - `command`: command string (e.g. "/check", "/clean", "/enable", "/delete", "/disable").
fabcca1eaf 2026-01-09 arcade: /// - `msg`: incoming Telegram message that triggered the command; used to determine sender and chat.
fabcca1eaf 2026-01-09 arcade: /// - `words`: command arguments; expected to contain exactly one element that parses as a 32-bit integer.
fae13a0e55 2025-06-28 arcade: pub async fn command (core: &Core, command: &str, msg: &Message, words: &[String]) -> Result<()> {
44575a91d3 2025-07-09 arcade: let mut conn = core.db.begin().await.stack()?;
fae13a0e55 2025-06-28 arcade: let sender = msg.sender.get_user_id()
44575a91d3 2025-07-09 arcade: .stack_err("Ignoring unreal users.")?;
fae13a0e55 2025-06-28 arcade: let reply = if words.len() == 1 {
fae13a0e55 2025-06-28 arcade: match words[0].parse::<i32>() {
fae13a0e55 2025-06-28 arcade: Err(err) => format!("I need a number.\n{}", &err).into(),
fae13a0e55 2025-06-28 arcade: Ok(number) => match command {
acb0a4ac54 2025-09-28 arcade: "/check" => core.check(number, false, None).await
fae13a0e55 2025-06-28 arcade: .context("Channel check failed.")?.into(),
44575a91d3 2025-07-09 arcade: "/clean" => conn.clean(number, sender).await.stack()?,
44575a91d3 2025-07-09 arcade: "/enable" => conn.enable(number, sender).await.stack()?.into(),
44575a91d3 2025-07-09 arcade: "/delete" => conn.delete(number, sender).await.stack()?,
44575a91d3 2025-07-09 arcade: "/disable" => conn.disable(number, sender).await.stack()?.into(),
fae13a0e55 2025-06-28 arcade: _ => bail!("Command {command} {words:?} not handled."),
bb89b6fab8 2025-06-15 arcade: },
bb89b6fab8 2025-06-15 arcade: }
bb89b6fab8 2025-06-15 arcade: } else {
fae13a0e55 2025-06-28 arcade: "This command needs exacly one number.".into()
bb89b6fab8 2025-06-15 arcade: };
9c4f09193a 2026-01-09 arcade: core.tg.send(reply, Some(msg.chat.get_id()), None).await.stack()?;
bb89b6fab8 2025-06-15 arcade: Ok(())
bb89b6fab8 2025-06-15 arcade: }
bb89b6fab8 2025-06-15 arcade:
fabcca1eaf 2026-01-09 arcade: /// Validate command arguments, check permissions and update or add a channel feed configuration in the database.
fabcca1eaf 2026-01-09 arcade: ///
fabcca1eaf 2026-01-09 arcade: /// This function parses and validates parameters supplied by a user command (either "/update <id> ..." or "/add ..."),
fabcca1eaf 2026-01-09 arcade: /// verifies the channel username and feed URL, optionally validates an IV hash and a replacement regexp,
fabcca1eaf 2026-01-09 arcade: /// ensures both the bot and the command sender are administrators of the target channel, and performs the database update.
fabcca1eaf 2026-01-09 arcade: ///
fabcca1eaf 2026-01-09 arcade: /// # Parameters
fabcca1eaf 2026-01-09 arcade: ///
fabcca1eaf 2026-01-09 arcade: /// - `command` — the invoked command, expected to be either `"/update"` (followed by a numeric source id) or `"/add"`.
fabcca1eaf 2026-01-09 arcade: /// - `msg` — the incoming Telegram message; used to derive the command sender and target chat id for the reply.
fabcca1eaf 2026-01-09 arcade: /// - `words` — the command arguments: for `"/add"` expected `channel url [iv_hash|'-'] [url_re|'-']`; for `"/update"` the first element must be a numeric `source_id` followed by the same parameters.
fae13a0e55 2025-06-28 arcade: pub async fn update (core: &Core, command: &str, msg: &Message, words: &[String]) -> Result<()> {
bb89b6fab8 2025-06-15 arcade: let sender = msg.sender.get_user_id()
44575a91d3 2025-07-09 arcade: .stack_err("Ignoring unreal users.")?;
bb89b6fab8 2025-06-15 arcade: let mut source_id: Option<i32> = None;
bb89b6fab8 2025-06-15 arcade: let at_least = "Requires at least 3 parameters.";
fae13a0e55 2025-06-28 arcade: let mut i_words = words.iter();
fae13a0e55 2025-06-28 arcade: match command {
bb89b6fab8 2025-06-15 arcade: "/update" => {
fae13a0e55 2025-06-28 arcade: let next_word = i_words.next().context(at_least)?;
e624ef9d66 2025-04-20 arcade: source_id = Some(next_word.parse::<i32>()
e624ef9d66 2025-04-20 arcade: .context(format!("I need a number, but got {next_word}."))?);
e624ef9d66 2025-04-20 arcade: },
e624ef9d66 2025-04-20 arcade: "/add" => {},
fae13a0e55 2025-06-28 arcade: _ => bail!("Passing {command} is not possible here."),
e624ef9d66 2025-04-20 arcade: };
e624ef9d66 2025-04-20 arcade: let (channel, url, iv_hash, url_re) = (
fae13a0e55 2025-06-28 arcade: i_words.next().context(at_least)?,
fae13a0e55 2025-06-28 arcade: i_words.next().context(at_least)?,
fae13a0e55 2025-06-28 arcade: i_words.next(),
fae13a0e55 2025-06-28 arcade: i_words.next());
fae13a0e55 2025-06-28 arcade: /*
fae13a0e55 2025-06-28 arcade: let channel = match RE_USERNAME.captures(channel) {
fae13a0e55 2025-06-28 arcade: Some(caps) => match caps.get(1) {
fae13a0e55 2025-06-28 arcade: Some(data) => data.as_str(),
fae13a0e55 2025-06-28 arcade: None => bail!("No string found in channel name"),
fae13a0e55 2025-06-28 arcade: },
fae13a0e55 2025-06-28 arcade: None => {
fae13a0e55 2025-06-28 arcade: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}");
fae13a0e55 2025-06-28 arcade: },
fae13a0e55 2025-06-28 arcade: };
fae13a0e55 2025-06-28 arcade: */
e624ef9d66 2025-04-20 arcade: if ! RE_USERNAME.is_match(channel) {
e624ef9d66 2025-04-20 arcade: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}");
e624ef9d66 2025-04-20 arcade: };
7393d62235 2026-01-07 arcade: {
7393d62235 2026-01-07 arcade: let parsed_url = Url::parse(url)
7393d62235 2026-01-07 arcade: .stack_err("Expecting a valid link to ATOM/RSS feed.")?;
7393d62235 2026-01-07 arcade: match parsed_url.scheme() {
7393d62235 2026-01-07 arcade: "http" | "https" => {},
7393d62235 2026-01-07 arcade: scheme => {
7393d62235 2026-01-07 arcade: bail!("Unsupported URL scheme: {scheme}");
7393d62235 2026-01-07 arcade: },
7393d62235 2026-01-07 arcade: };
e624ef9d66 2025-04-20 arcade: }
e624ef9d66 2025-04-20 arcade: let iv_hash = match iv_hash {
e624ef9d66 2025-04-20 arcade: Some(hash) => {
bb89b6fab8 2025-06-15 arcade: match hash.as_ref() {
e624ef9d66 2025-04-20 arcade: "-" => None,
e624ef9d66 2025-04-20 arcade: thing => {
e624ef9d66 2025-04-20 arcade: if ! RE_IV_HASH.is_match(thing) {
e624ef9d66 2025-04-20 arcade: bail!("IV hash should be 14 hex digits.\nNot {thing:?}");
613a665847 2021-11-15 arcade: };
613a665847 2021-11-15 arcade: Some(thing)
613a665847 2021-11-15 arcade: },
c1e27b74ed 2021-11-13 arcade: }
10c25017bb 2021-11-13 arcade: },
10c25017bb 2021-11-13 arcade: None => None,
10c25017bb 2021-11-13 arcade: };
10c25017bb 2021-11-13 arcade: let url_re = match url_re {
10c25017bb 2021-11-13 arcade: Some(re) => {
bb89b6fab8 2025-06-15 arcade: match re.as_ref() {
c1e27b74ed 2021-11-13 arcade: "-" => None,
613a665847 2021-11-15 arcade: thing => {
613a665847 2021-11-15 arcade: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
613a665847 2021-11-15 arcade: Some(thing)
613a665847 2021-11-15 arcade: }
c1e27b74ed 2021-11-13 arcade: }
10c25017bb 2021-11-13 arcade: },
10c25017bb 2021-11-13 arcade: None => None,
10c25017bb 2021-11-13 arcade: };
fae13a0e55 2025-06-28 arcade: let chat_id = ChatUsername::from(channel.as_ref());
9c4f09193a 2026-01-09 arcade: let channel_id = core.tg.client.execute(GetChat::new(chat_id.clone())).await.stack_err("gettting GetChat")?.id;
9c4f09193a 2026-01-09 arcade: let chan_adm = core.tg.client.execute(GetChatAdministrators::new(chat_id)).await
bb89b6fab8 2025-06-15 arcade: .context("Sorry, I have no access to that chat.")?;
10c25017bb 2021-11-13 arcade: let (mut me, mut user) = (false, false);
10c25017bb 2021-11-13 arcade: for admin in chan_adm {
e624ef9d66 2025-04-20 arcade: let member_id = match admin {
e624ef9d66 2025-04-20 arcade: ChatMember::Creator(member) => member.user.id,
e624ef9d66 2025-04-20 arcade: ChatMember::Administrator(member) => member.user.id,
e624ef9d66 2025-04-20 arcade: ChatMember::Left(_)
e624ef9d66 2025-04-20 arcade: | ChatMember::Kicked(_)
bb89b6fab8 2025-06-15 arcade: | ChatMember::Member{..}
e624ef9d66 2025-04-20 arcade: | ChatMember::Restricted(_) => continue,
bb89b6fab8 2025-06-15 arcade: };
9c4f09193a 2026-01-09 arcade: if member_id == core.tg.me.id {
e624ef9d66 2025-04-20 arcade: me = true;
bb89b6fab8 2025-06-15 arcade: }
e624ef9d66 2025-04-20 arcade: if member_id == sender {
e624ef9d66 2025-04-20 arcade: user = true;
bb89b6fab8 2025-06-15 arcade: }
10c25017bb 2021-11-13 arcade: };
10c25017bb 2021-11-13 arcade: if ! me { bail!("I need to be admin on that channel."); };
10c25017bb 2021-11-13 arcade: if ! user { bail!("You should be admin on that channel."); };
44575a91d3 2025-07-09 arcade: let mut conn = core.db.begin().await.stack()?;
fabcca1eaf 2026-01-09 arcade: let update = conn.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await.stack()?;
fabcca1eaf 2026-01-09 arcade: core.tg.send(update, Some(msg.chat.get_id()), None).await.stack()?;
a7f91033c0 2021-11-13 arcade: Ok(())
9171c791eb 2021-11-13 arcade: }