Annotation For src/command.rs
Logged in as anonymous

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

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