Annotation For src/command.rs
Logged in as anonymous

Lines of src/command.rs from check-in be0b8602d1 that are changed by the sequence of edits moving toward check-in d8c1d259a2:

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