Annotation For src/command.rs
Logged in as anonymous

Origin for each line in src/command.rs from check-in 26339860ce:

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