Annotation For src/command.rs
Logged in as anonymous

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

                         1: use anyhow::{bail, Context, Result};
                         2: use crate::core::Core;
                         3: use lazy_static::lazy_static;
                         4: use regex::Regex;
                         5: use sedregex::ReplaceCommand;
                         6: use std::borrow::Cow;
                         7: 
                         8: lazy_static! {
                         9: 	static ref RE_USERNAME: Regex = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$").unwrap();
                        10: 	static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.0-9/?=]+$").unwrap();
                        11: 	static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
                        12: }
                        13: 
                        14: pub async fn start(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
                        15: 	core.send("We are open. Probably. Visit [channel](https://t.me/rsstg_bot_help/3) for details.", Some(sender), None).await?;
                        16: 	Ok(())
                        17: }
                        18: 
                        19: pub async fn list(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
                        20: 	core.send(core.list(sender).await?, Some(sender), Some(telegram_bot::types::ParseMode::MarkdownV2)).await?;
                        21: 	Ok(())
                        22: }
                        23: 
                        24: pub async fn command(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
                        25: 	let msg: Cow<str> = match &command[1].parse::<i32>() {
                        26: 		Err(err) => format!("I need a number.\n{}", &err).into(),
                        27: 		Ok(number) => match command[0] {
                        28: 			"/check" => core.check(number, sender, false).await
                        29: 				.context("Channel check failed.")?,
                        30: 			"/clean" => core.clean(number, sender).await?,
                        31: 			"/enable" => core.enable(number, sender).await?.into(),
                        32: 			"/delete" => core.delete(number, sender).await?,
                        33: 			"/disable" => core.disable(number, sender).await?.into(),
                        34: 			_ => bail!("Command {} not handled.", &command[0]),
                        35: 		},
                        36: 	};
                        37: 	core.send(msg, Some(sender), None).await?;
                        38: 	Ok(())
                        39: }
                        40: 
                        41: pub async fn update(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
                        42: 	let mut source_id: Option<i32> = None;
                        43: 	let at_least = "Requires at least 3 parameters.";
28da2e2a00 2022-03-12   44: 	let first_word = command[0];
28da2e2a00 2022-03-12   45: 	let command = match first_word {
                        46: 		"/update" => {
28da2e2a00 2022-03-12   47: 			source_id = Some(command[1].parse::<i32>()
28da2e2a00 2022-03-12   48: 				.context(format!("I need a number, but got {}.", command[1]))?);
28da2e2a00 2022-03-12   49: 			&command[2..]
                        50: 		},
28da2e2a00 2022-03-12   51: 		"/add" => &command[1..],
28da2e2a00 2022-03-12   52: 		_ => bail!("Passing {} is not possible here.", command[1]),
                        53: 	};
28da2e2a00 2022-03-12   54: 	let mut i_command = command.iter();
                        55: 	let (channel, url, iv_hash, url_re) = (
                        56: 		i_command.next().context(at_least)?,
                        57: 		i_command.next().context(at_least)?,
                        58: 		i_command.next(),
                        59: 		i_command.next());
                        60: 	if ! RE_USERNAME.is_match(channel) {
                        61: 		bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {:?}", &channel);
                        62: 	};
                        63: 	if ! RE_LINK.is_match(url) {
                        64: 		bail!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {:?}", &url);
                        65: 	}
                        66: 	let iv_hash = match iv_hash {
                        67: 		Some(hash) => {
                        68: 			match *hash {
                        69: 				"-" => None,
                        70: 				thing => {
                        71: 					if ! RE_IV_HASH.is_match(thing) {
                        72: 						bail!("IV hash should be 14 hex digits.\nNot {:?}", thing);
                        73: 					};
                        74: 					Some(thing)
                        75: 				},
                        76: 			}
                        77: 		},
                        78: 		None => None,
                        79: 	};
                        80: 	let url_re = match url_re {
                        81: 		Some(re) => {
                        82: 			match *re {
                        83: 				"-" => None,
                        84: 				thing => {
                        85: 					let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
                        86: 					Some(thing)
                        87: 				}
                        88: 			}
                        89: 		},
                        90: 		None => None,
                        91: 	};
                        92: 	let channel_id = i64::from(core.tg.send(telegram_bot::GetChat::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await?.id());
                        93: 	let chan_adm = core.tg.send(telegram_bot::GetChatAdministrators::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await
                        94: 		.context("Sorry, I have no access to that chat.")?;
                        95: 	let (mut me, mut user) = (false, false);
                        96: 	for admin in chan_adm {
                        97: 		if admin.user.id == core.my.id {
                        98: 			me = true;
                        99: 		};
                       100: 		if admin.user.id == sender {
                       101: 			user = true;
                       102: 		};
                       103: 	};
                       104: 	if ! me   { bail!("I need to be admin on that channel."); };
                       105: 	if ! user { bail!("You should be admin on that channel."); };
                       106: 	core.send(core.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await?, Some(sender), None).await?;
                       107: 	Ok(())
                       108: }