Annotation For src/command.rs
Logged in as anonymous

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

                         1: use crate::core::Core;
                         2: 
                         3: use anyhow::{
                         4: 	bail,
                         5: 	Context,
                         6: 	Result
                         7: };
                         8: use lazy_static::lazy_static;
                         9: use regex::Regex;
                        10: use sedregex::ReplaceCommand;
a2880e5100 2025-04-19   11: use std::borrow::Cow;
a2880e5100 2025-04-19   12: use teloxide::{
a2880e5100 2025-04-19   13: 	Bot,
a2880e5100 2025-04-19   14: 	dispatching::dialogue::GetChatId,
a2880e5100 2025-04-19   15: 	payloads::GetChatAdministrators,
a2880e5100 2025-04-19   16: 	requests::{
a2880e5100 2025-04-19   17: 		Requester,
a2880e5100 2025-04-19   18: 		ResponseResult
a2880e5100 2025-04-19   19: 	},
a2880e5100 2025-04-19   20: 	types::{
a2880e5100 2025-04-19   21: 		Message,
a2880e5100 2025-04-19   22: 		UserId,
a2880e5100 2025-04-19   23: 	},
a2880e5100 2025-04-19   24: 	utils::command::BotCommands,
a2880e5100 2025-04-19   25: };
                        26: 
                        27: lazy_static! {
                        28: 	static ref RE_USERNAME: Regex = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$").unwrap();
                        29: 	static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.:0-9/?=]+$").unwrap();
                        30: 	static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
                        31: }
                        32: 
a2880e5100 2025-04-19   33: #[derive(BotCommands, Clone)]
a2880e5100 2025-04-19   34: #[command(rename_rule = "lowercase", description = "Supported commands:")]
a2880e5100 2025-04-19   35: enum Command {
a2880e5100 2025-04-19   36: 	#[command(description = "display this help.")]
a2880e5100 2025-04-19   37: 	Help,
a2880e5100 2025-04-19   38: 	#[command(description = "Does nothing.")]
a2880e5100 2025-04-19   39: 	Start,
a2880e5100 2025-04-19   40: 	#[commant(description = "List active subscriptions.")]
a2880e5100 2025-04-19   41: 	List,
a2880e5100 2025-04-19   42: }
a2880e5100 2025-04-19   43: 
a2880e5100 2025-04-19   44: pub async fn cmd_handler(bot: Bot, msg: Message, cmd: Command) -> ResponseResult<()> {
a2880e5100 2025-04-19   45: 	match cmd {
a2880e5100 2025-04-19   46: 		Command::Help => bot.send_message(msg.chat.id, Command::descriptions().to_string()).await?,
a2880e5100 2025-04-19   47: 		Command::Start => bot.send_message(msg.chat.id,
a2880e5100 2025-04-19   48: 			"We are open. Probably. Visit [channel](https://t.me/rsstg_bot_help/3) for details.").await?,
a2880e5100 2025-04-19   49: 		Command::List => bot.send_message(msg.chat.id, core.list(msg.from).await?).await?,
a2880e5100 2025-04-19   50: 	};
                        51: 	Ok(())
                        52: }
                        53: 
a2880e5100 2025-04-19   54: pub async fn list(core: &Core, sender: UserId) -> Result<()> {
a2880e5100 2025-04-19   55: 	core.send(core.list(sender).await?, Some(sender), Some(telegram_bot::types::ParseMode::MarkdownV2)).await?;
                        56: 	Ok(())
                        57: }
                        58: 
a2880e5100 2025-04-19   59: pub async fn command(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
                        60: 	if command.len() >= 2 {
                        61: 		let msg: Cow<str> = match &command[1].parse::<i32>() {
                        62: 			Err(err) => format!("I need a number.\n{}", &err).into(),
                        63: 			Ok(number) => match command[0] {
                        64: 				"/check" => core.check(number, sender, false).await
                        65: 					.context("Channel check failed.")?,
                        66: 				"/clean" => core.clean(number, sender).await?,
                        67: 				"/enable" => core.enable(number, sender).await?.into(),
                        68: 				"/delete" => core.delete(number, sender).await?,
                        69: 				"/disable" => core.disable(number, sender).await?.into(),
                        70: 				_ => bail!("Command {} not handled.", &command[0]),
                        71: 			},
                        72: 		};
                        73: 		core.send(msg, Some(sender), None).await?;
                        74: 	} else {
                        75: 		core.send("This command needs a number.", Some(sender), None).await?;
                        76: 	}
                        77: 	Ok(())
                        78: }
                        79: 
a2880e5100 2025-04-19   80: pub async fn update(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
                        81: 	let mut source_id: Option<i32> = None;
                        82: 	let at_least = "Requires at least 3 parameters.";
                        83: 	let mut i_command = command.iter();
                        84: 	let first_word = i_command.next().context(at_least)?;
                        85: 	match *first_word {
                        86: 		"/update" => {
                        87: 			let next_word = i_command.next().context(at_least)?;
                        88: 			source_id = Some(next_word.parse::<i32>()
a2880e5100 2025-04-19   89: 				.context(format!("I need a number, but got {}.", next_word))?);
                        90: 		},
                        91: 		"/add" => {},
a2880e5100 2025-04-19   92: 		_ => bail!("Passing {} is not possible here.", first_word),
                        93: 	};
                        94: 	let (channel, url, iv_hash, url_re) = (
                        95: 		i_command.next().context(at_least)?,
                        96: 		i_command.next().context(at_least)?,
                        97: 		i_command.next(),
                        98: 		i_command.next());
                        99: 	if ! RE_USERNAME.is_match(channel) {
a2880e5100 2025-04-19  100: 		bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {:?}", &channel);
                       101: 	};
                       102: 	if ! RE_LINK.is_match(url) {
a2880e5100 2025-04-19  103: 		bail!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {:?}", &url);
                       104: 	}
                       105: 	let iv_hash = match iv_hash {
                       106: 		Some(hash) => {
                       107: 			match *hash {
                       108: 				"-" => None,
                       109: 				thing => {
                       110: 					if ! RE_IV_HASH.is_match(thing) {
a2880e5100 2025-04-19  111: 						bail!("IV hash should be 14 hex digits.\nNot {:?}", thing);
                       112: 					};
                       113: 					Some(thing)
                       114: 				},
                       115: 			}
                       116: 		},
                       117: 		None => None,
                       118: 	};
                       119: 	let url_re = match url_re {
                       120: 		Some(re) => {
                       121: 			match *re {
                       122: 				"-" => None,
                       123: 				thing => {
                       124: 					let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
                       125: 					Some(thing)
                       126: 				}
                       127: 			}
                       128: 		},
                       129: 		None => None,
                       130: 	};
a2880e5100 2025-04-19  131: 	let channel_id = i64::from(core.request(telegram_bot::GetChat::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await?.id());
a2880e5100 2025-04-19  132: 	let chan_adm = core.request(telegram_bot::GetChatAdministrators::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await
a2880e5100 2025-04-19  133: 		.context("Sorry, I have no access to that chat.")?;
                       134: 	let (mut me, mut user) = (false, false);
                       135: 	for admin in chan_adm {
a2880e5100 2025-04-19  136: 		if admin.user.id == core.my.id {
                       137: 			me = true;
                       138: 		};
a2880e5100 2025-04-19  139: 		if admin.user.id == sender {
                       140: 			user = true;
                       141: 		};
                       142: 	};
                       143: 	if ! me   { bail!("I need to be admin on that channel."); };
                       144: 	if ! user { bail!("You should be admin on that channel."); };
                       145: 	core.send(core.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await?, Some(sender), None).await?;
                       146: 	Ok(())
                       147: }