Annotation For src/command.rs
Logged in as anonymous

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

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