Annotation For src/command.rs
Logged in as anonymous

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

                         1: use crate::core::Core;
                         2: 
                         3: use anyhow::{
                         4: 	anyhow,
                         5: 	bail,
                         6: 	Context,
                         7: 	Result,
                         8: };
                         9: use lazy_static::lazy_static;
                        10: use regex::Regex;
                        11: use sedregex::ReplaceCommand;
                        12: use tgbot::types::{
                        13: 	ChatMember,
                        14: 	ChatUsername,
                        15: 	GetChat,
                        16: 	GetChatAdministrators,
                        17: 	Message,
                        18: 	ParseMode::MarkdownV2,
                        19: };
                        20: 
                        21: lazy_static! {
bb89b6fab8 2025-06-15   22: 	static ref RE_USERNAME: Regex = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$").unwrap();
                        23: 	static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.:0-9/?=]+$").unwrap();
                        24: 	static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
                        25: }
                        26: 
bb89b6fab8 2025-06-15   27: pub async fn start(core: &Core, msg: &Message) -> Result<()> {
                        28: 	core.send("We are open\\. Probably\\. Visit [channel](https://t.me/rsstg_bot_help/3) for details\\.",
                        29: 		Some(msg.chat.get_id()), Some(MarkdownV2)).await?;
                        30: 	Ok(())
                        31: }
                        32: 
bb89b6fab8 2025-06-15   33: pub async fn list(core: &Core, msg: &Message) -> Result<()> {
                        34: 	let sender = msg.sender.get_user_id()
                        35: 		.ok_or(anyhow!("Ignoring unreal users."))?;
                        36: 	let reply = core.list(sender).await?;
                        37: 	core.send(reply, Some(msg.chat.get_id()), Some(MarkdownV2)).await?;
                        38: 	Ok(())
                        39: }
                        40: 
bb89b6fab8 2025-06-15   41: pub async fn command (core: &Core, msg: &Message, command: &[String]) -> Result<()> {
                        42: 	let mut conn = core.db.begin().await?;
                        43: 	let sender = msg.sender.get_user_id()
                        44: 		.ok_or(anyhow!("Ignoring unreal users."))?;
bb89b6fab8 2025-06-15   45: 	let reply = if command.len() >= 2 {
bb89b6fab8 2025-06-15   46: 		match command[1].parse::<i32>() {
                        47: 			Err(err) => format!("I need a number.\n{}", &err).into(),
bb89b6fab8 2025-06-15   48: 			Ok(number) => match &command[0][..] {
                        49: 				"/check" => core.check(number, false).await
                        50: 					.context("Channel check failed.")?.into(),
                        51: 				"/clean" => conn.clean(number, sender).await?,
                        52: 				"/enable" => conn.enable(number, sender).await?.into(),
                        53: 				"/delete" => conn.delete(number, sender).await?,
                        54: 				"/disable" => conn.disable(number, sender).await?.into(),
bb89b6fab8 2025-06-15   55: 				_ => bail!("Command {} not handled.", &command[0]),
                        56: 			},
                        57: 		}
                        58: 	} else {
bb89b6fab8 2025-06-15   59: 		"This command needs a number.".into()
                        60: 	};
                        61: 	core.send(reply, Some(msg.chat.get_id()), None).await?;
                        62: 	Ok(())
                        63: }
                        64: 
bb89b6fab8 2025-06-15   65: pub async fn update (core: &Core, msg: &Message, command: &[String]) -> Result<()> {
                        66: 	let sender = msg.sender.get_user_id()
                        67: 		.ok_or(anyhow!("Ignoring unreal users."))?;
                        68: 	let mut source_id: Option<i32> = None;
                        69: 	let at_least = "Requires at least 3 parameters.";
bb89b6fab8 2025-06-15   70: 	let mut i_command = command.iter();
bb89b6fab8 2025-06-15   71: 	let first_word = i_command.next().context(at_least)?;
bb89b6fab8 2025-06-15   72: 	match first_word.as_ref() {
                        73: 		"/update" => {
bb89b6fab8 2025-06-15   74: 			let next_word = i_command.next().context(at_least)?;
                        75: 			source_id = Some(next_word.parse::<i32>()
                        76: 				.context(format!("I need a number, but got {next_word}."))?);
                        77: 		},
                        78: 		"/add" => {},
bb89b6fab8 2025-06-15   79: 		_ => bail!("Passing {first_word} is not possible here."),
                        80: 	};
                        81: 	let (channel, url, iv_hash, url_re) = (
bb89b6fab8 2025-06-15   82: 		i_command.next().context(at_least)?,
bb89b6fab8 2025-06-15   83: 		i_command.next().context(at_least)?,
bb89b6fab8 2025-06-15   84: 		i_command.next(),
bb89b6fab8 2025-06-15   85: 		i_command.next());
                        86: 	if ! RE_USERNAME.is_match(channel) {
                        87: 		bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}");
                        88: 	};
                        89: 	if ! RE_LINK.is_match(url) {
                        90: 		bail!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {url:?}");
                        91: 	}
                        92: 	let iv_hash = match iv_hash {
                        93: 		Some(hash) => {
                        94: 			match hash.as_ref() {
                        95: 				"-" => None,
                        96: 				thing => {
                        97: 					if ! RE_IV_HASH.is_match(thing) {
                        98: 						bail!("IV hash should be 14 hex digits.\nNot {thing:?}");
                        99: 					};
                       100: 					Some(thing)
                       101: 				},
                       102: 			}
                       103: 		},
                       104: 		None => None,
                       105: 	};
                       106: 	let url_re = match url_re {
                       107: 		Some(re) => {
                       108: 			match re.as_ref() {
                       109: 				"-" => None,
                       110: 				thing => {
                       111: 					let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
                       112: 					Some(thing)
                       113: 				}
                       114: 			}
                       115: 		},
                       116: 		None => None,
                       117: 	};
bb89b6fab8 2025-06-15  118: 	let chat_id = ChatUsername::from(channel.clone());
                       119: 	let channel_id = core.tg.execute(GetChat::new(chat_id.clone())).await?.id;
                       120: 	let chan_adm = core.tg.execute(GetChatAdministrators::new(chat_id)).await
                       121: 		.context("Sorry, I have no access to that chat.")?;
                       122: 	let (mut me, mut user) = (false, false);
                       123: 	for admin in chan_adm {
                       124: 		let member_id = match admin {
                       125: 			ChatMember::Creator(member) => member.user.id,
                       126: 			ChatMember::Administrator(member) => member.user.id,
                       127: 			ChatMember::Left(_)
                       128: 			| ChatMember::Kicked(_)
                       129: 			| ChatMember::Member{..}
                       130: 			| ChatMember::Restricted(_) => continue,
                       131: 		};
                       132: 		if member_id == core.me.id {
                       133: 			me = true;
                       134: 		}
                       135: 		if member_id == sender {
                       136: 			user = true;
                       137: 		}
                       138: 	};
                       139: 	if ! me   { bail!("I need to be admin on that channel."); };
                       140: 	if ! user { bail!("You should be admin on that channel."); };
                       141: 	let mut conn = core.db.begin().await?;
                       142: 	core.send(conn.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await?, Some(msg.chat.get_id()), None).await?;
                       143: 	Ok(())
                       144: }