Annotation For src/command.rs
Logged in as anonymous

Origin for each line in src/command.rs from check-in be0b8602d1:

9adc69d514 2026-03-25        arcade: use crate::{
9adc69d514 2026-03-25        arcade: 	core::Core,
9adc69d514 2026-03-25        arcade: 	tg_bot::{
9adc69d514 2026-03-25        arcade: 		Callback,
9adc69d514 2026-03-25        arcade: 		MyMessage,
9adc69d514 2026-03-25        arcade: 		get_kb,
9adc69d514 2026-03-25        arcade: 	},
9adc69d514 2026-03-25        arcade: };
44575a91d3 2025-07-09        arcade: 
28da2e2a00 2022-03-12        arcade: use lazy_static::lazy_static;
9171c791eb 2021-11-13        arcade: use regex::Regex;
9171c791eb 2021-11-13        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::{
3fd8c40aa8 2026-03-30        arcade: 	CallbackQuery,
be0b8602d1 2026-04-18        arcade: 	Chat,
bb89b6fab8 2025-06-15        arcade: 	ChatMember,
bb89b6fab8 2025-06-15        arcade: 	ChatUsername,
bb89b6fab8 2025-06-15        arcade: 	GetChat,
bb89b6fab8 2025-06-15        arcade: 	GetChatAdministrators,
be0b8602d1 2026-04-18        arcade: 	MaybeInaccessibleMessage,
bb89b6fab8 2025-06-15        arcade: 	Message,
bb89b6fab8 2025-06-15        arcade: };
7393d62235 2026-01-07        arcade: use url::Url;
9171c791eb 2021-11-13        arcade: 
9171c791eb 2021-11-13        arcade: lazy_static! {
fae13a0e55 2025-06-28        arcade: 	static ref RE_USERNAME: Regex = Regex::new(r"^@([a-zA-Z][a-zA-Z0-9_]+)$").unwrap();
9171c791eb 2021-11-13        arcade: 	static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
9171c791eb 2021-11-13        arcade: }
9171c791eb 2021-11-13        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<()> {
3fd8c40aa8 2026-03-30        arcade: 	core.tg.send(MyMessage::html_to(
3fd8c40aa8 2026-03-30        arcade: 		"We are open. Probably. Visit <a href=\"https://t.me/rsstg_bot_help/3\">channel</a>) for details.",
9adc69d514 2026-03-25        arcade: 		msg.chat.get_id()
9adc69d514 2026-03-25        arcade: 	)).await.stack()?;
44575a91d3 2025-07-09        arcade: 	Ok(())
44575a91d3 2025-07-09        arcade: }
44575a91d3 2025-07-09        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.
44575a91d3 2025-07-09        arcade: pub async fn list (core: &Core, msg: &Message) -> Result<()> {
44575a91d3 2025-07-09        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()?;
3fd8c40aa8 2026-03-30        arcade: 	core.tg.send(MyMessage::html_to(reply, msg.chat.get_id())).await.stack()?;
9adc69d514 2026-03-25        arcade: 	Ok(())
9adc69d514 2026-03-25        arcade: }
9adc69d514 2026-03-25        arcade: 
9adc69d514 2026-03-25        arcade: pub async fn test (core: &Core, msg: &Message) -> Result<()> {
9adc69d514 2026-03-25        arcade: 	let sender: i64 = msg.sender.get_user_id()
9adc69d514 2026-03-25        arcade: 		.stack_err("Ignoring unreal users.")?.into();
9adc69d514 2026-03-25        arcade: 	let feeds = core.get_feeds(sender).await.stack()?;
be0b8602d1 2026-04-18        arcade: 	let kb = get_kb(&Callback::menu(), &feeds).await.stack()?;
3fd8c40aa8 2026-03-30        arcade: 	core.tg.send(MyMessage::html_to_kb("Main menu:", msg.chat.get_id(), kb)).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(),
9adc69d514 2026-03-25        arcade: 				"/delete" => {
374eadef45 2026-03-28        arcade: 					let res = conn.delete(number, sender).await.stack()?;
9adc69d514 2026-03-25        arcade: 					core.rm_feed(sender.into(), &number).await.stack()?;
374eadef45 2026-03-28        arcade: 					res
9adc69d514 2026-03-25        arcade: 				}
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 {
13265e7697 2026-01-10        arcade: 		"This command needs exactly one number.".into()
bb89b6fab8 2025-06-15        arcade: 	};
9adc69d514 2026-03-25        arcade: 	core.tg.send(MyMessage::html_to(reply, msg.chat.get_id())).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.
9adc69d514 2026-03-25        arcade: /// - `words` — the command arguments: for `"/add"` expected `channel url [iv_hash|'-'] [url_re|'-']`; for `"/update"`
9adc69d514 2026-03-25        arcade: ///   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());
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: 		},
9171c791eb 2021-11-13        arcade: 		None => None,
9171c791eb 2021-11-13        arcade: 	};
fae13a0e55 2025-06-28        arcade: 	let chat_id = ChatUsername::from(channel.as_ref());
9adc69d514 2026-03-25        arcade: 	let channel_id = core.tg.client.execute(GetChat::new(chat_id.clone())).await.stack_err("getting 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.")?;
9171c791eb 2021-11-13        arcade: 	let (mut me, mut user) = (false, false);
9171c791eb 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: 		}
4632d20d39 2021-11-13        arcade: 	};
4632d20d39 2021-11-13        arcade: 	if ! me   { bail!("I need to be admin on that channel."); };
4632d20d39 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()?;
9adc69d514 2026-03-25        arcade: 	core.tg.send(MyMessage::html_to(update, msg.chat.get_id())).await.stack()?;
9adc69d514 2026-03-25        arcade: 	if command == "/add" {
9adc69d514 2026-03-25        arcade: 		if let Some(new_record) = conn.get_one_name(sender, channel).await.stack()? {
9adc69d514 2026-03-25        arcade: 			core.add_feed(sender.into(), new_record.source_id, new_record.channel).await.stack()?;
9adc69d514 2026-03-25        arcade: 		} else {
9adc69d514 2026-03-25        arcade: 			bail!("Failed to read data on freshly inserted source.");
9adc69d514 2026-03-25        arcade: 		}
be0b8602d1 2026-04-18        arcade: 	};
be0b8602d1 2026-04-18        arcade: 	Ok(())
be0b8602d1 2026-04-18        arcade: }
be0b8602d1 2026-04-18        arcade: 
be0b8602d1 2026-04-18        arcade: pub async fn answer_cb (core: &Core, query: &CallbackQuery, cb: &str) -> Result<()> {
be0b8602d1 2026-04-18        arcade: 	let cb: Callback = toml::from_str(cb).stack()?;
be0b8602d1 2026-04-18        arcade: 	let sender = &query.from;
be0b8602d1 2026-04-18        arcade: 	//let mut conn = core.db.begin().await.stack()?;
be0b8602d1 2026-04-18        arcade: 	let text = "Sample".to_owned();
be0b8602d1 2026-04-18        arcade: 	if let Some(msg) = &query.message {
be0b8602d1 2026-04-18        arcade: 		match msg {
be0b8602d1 2026-04-18        arcade: 			MaybeInaccessibleMessage::Message(message) => {
be0b8602d1 2026-04-18        arcade: 				if let Some(owner) = message.sender.get_user()
be0b8602d1 2026-04-18        arcade: 					&& sender == owner
be0b8602d1 2026-04-18        arcade: 				{
be0b8602d1 2026-04-18        arcade: 					let feeds = core.get_feeds(owner.id.into()).await.stack()?;
be0b8602d1 2026-04-18        arcade: 					core.tg.update_message(message.chat.get_id().into(), message.id, text, &feeds, cb).await?;
be0b8602d1 2026-04-18        arcade: 				} else {
be0b8602d1 2026-04-18        arcade: 					core.tg.send(MyMessage::html(format!("Can't identify request sender:<br><pre>{:?}</pre>", message))).await.stack()?;
be0b8602d1 2026-04-18        arcade: 				}
be0b8602d1 2026-04-18        arcade: 			},
be0b8602d1 2026-04-18        arcade: 			MaybeInaccessibleMessage::InaccessibleMessage(message) => {
be0b8602d1 2026-04-18        arcade: 				let sender: i64 = sender.id.into();
be0b8602d1 2026-04-18        arcade: 				if let Chat::Private(priv_chat) = &message.chat
be0b8602d1 2026-04-18        arcade: 					&& priv_chat.id == sender
be0b8602d1 2026-04-18        arcade: 				{
be0b8602d1 2026-04-18        arcade: 					let feeds = core.get_feeds(priv_chat.id.into()).await.stack()?;
be0b8602d1 2026-04-18        arcade: 					core.tg.update_message(message.chat.get_id().into(), message.message_id, text, &feeds, cb).await?;
be0b8602d1 2026-04-18        arcade: 				} else {
be0b8602d1 2026-04-18        arcade: 					core.tg.send(MyMessage::html(format!("Can't identify request sender:<br><pre>{:?}</pre>", message))).await.stack()?;
be0b8602d1 2026-04-18        arcade: 				}
be0b8602d1 2026-04-18        arcade: 			},
be0b8602d1 2026-04-18        arcade: 		};
9adc69d514 2026-03-25        arcade: 	};
a7f91033c0 2021-11-13        arcade: 	Ok(())
9171c791eb 2021-11-13        arcade: }