Annotation For src/command.rs
Logged in as anonymous

Origin for each line in src/command.rs from check-in 79c91a5357:

9171c791eb 2021-11-13        arcade: use anyhow::{bail, Context, Result};
9171c791eb 2021-11-13        arcade: use crate::core::Core;
28da2e2a00 2022-03-12        arcade: use lazy_static::lazy_static;
9171c791eb 2021-11-13        arcade: use regex::Regex;
9171c791eb 2021-11-13        arcade: use sedregex::ReplaceCommand;
26339860ce 2022-02-13        arcade: use std::borrow::Cow;
9171c791eb 2021-11-13        arcade: 
9171c791eb 2021-11-13        arcade: lazy_static! {
9171c791eb 2021-11-13        arcade: 	static ref RE_USERNAME: Regex = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$").unwrap();
ec2eac7ea2 2024-05-14        arcade: 	static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.:0-9/?=]+$").unwrap();
9171c791eb 2021-11-13        arcade: 	static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
9171c791eb 2021-11-13        arcade: }
9171c791eb 2021-11-13        arcade: 
9171c791eb 2021-11-13        arcade: pub async fn start(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
26339860ce 2022-02-13        arcade: 	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        arcade: 	Ok(())
9171c791eb 2021-11-13        arcade: }
9171c791eb 2021-11-13        arcade: 
9171c791eb 2021-11-13        arcade: pub async fn list(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
26339860ce 2022-02-13        arcade: 	core.send(core.list(sender).await?, Some(sender), Some(telegram_bot::types::ParseMode::MarkdownV2)).await?;
9171c791eb 2021-11-13        arcade: 	Ok(())
9171c791eb 2021-11-13        arcade: }
9171c791eb 2021-11-13        arcade: 
9171c791eb 2021-11-13        arcade: pub async fn command(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
409eb5bafc 2022-06-12        arcade: 	if command.len() >= 2 {
409eb5bafc 2022-06-12        arcade: 		let msg: Cow<str> = match &command[1].parse::<i32>() {
409eb5bafc 2022-06-12        arcade: 			Err(err) => format!("I need a number.\n{}", &err).into(),
409eb5bafc 2022-06-12        arcade: 			Ok(number) => match command[0] {
409eb5bafc 2022-06-12        arcade: 				"/check" => core.check(number, sender, false).await
409eb5bafc 2022-06-12        arcade: 					.context("Channel check failed.")?,
409eb5bafc 2022-06-12        arcade: 				"/clean" => core.clean(number, sender).await?,
409eb5bafc 2022-06-12        arcade: 				"/enable" => core.enable(number, sender).await?.into(),
409eb5bafc 2022-06-12        arcade: 				"/delete" => core.delete(number, sender).await?,
409eb5bafc 2022-06-12        arcade: 				"/disable" => core.disable(number, sender).await?.into(),
409eb5bafc 2022-06-12        arcade: 				_ => bail!("Command {} not handled.", &command[0]),
409eb5bafc 2022-06-12        arcade: 			},
409eb5bafc 2022-06-12        arcade: 		};
409eb5bafc 2022-06-12        arcade: 		core.send(msg, Some(sender), None).await?;
409eb5bafc 2022-06-12        arcade: 	} else {
409eb5bafc 2022-06-12        arcade: 		core.send("This command needs a number.", Some(sender), None).await?;
409eb5bafc 2022-06-12        arcade: 	}
9171c791eb 2021-11-13        arcade: 	Ok(())
9171c791eb 2021-11-13        arcade: }
9171c791eb 2021-11-13        arcade: 
9171c791eb 2021-11-13        arcade: pub async fn update(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
9171c791eb 2021-11-13        arcade: 	let mut source_id: Option<i32> = None;
9171c791eb 2021-11-13        arcade: 	let at_least = "Requires at least 3 parameters.";
cab82b7ff4 2022-03-23        arcade: 	let mut i_command = command.iter();
cab82b7ff4 2022-03-23        arcade: 	let first_word = i_command.next().context(at_least)?;
cab82b7ff4 2022-03-23        arcade: 	match *first_word {
9171c791eb 2021-11-13        arcade: 		"/update" => {
cab82b7ff4 2022-03-23        arcade: 			let next_word = i_command.next().context(at_least)?;
cab82b7ff4 2022-03-23        arcade: 			source_id = Some(next_word.parse::<i32>()
cab82b7ff4 2022-03-23        arcade: 				.context(format!("I need a number, but got {}.", next_word))?);
9171c791eb 2021-11-13        arcade: 		},
cab82b7ff4 2022-03-23        arcade: 		"/add" => {},
cab82b7ff4 2022-03-23        arcade: 		_ => bail!("Passing {} is not possible here.", first_word),
9171c791eb 2021-11-13        arcade: 	};
9171c791eb 2021-11-13        arcade: 	let (channel, url, iv_hash, url_re) = (
9171c791eb 2021-11-13        arcade: 		i_command.next().context(at_least)?,
9171c791eb 2021-11-13        arcade: 		i_command.next().context(at_least)?,
9171c791eb 2021-11-13        arcade: 		i_command.next(),
9171c791eb 2021-11-13        arcade: 		i_command.next());
f988dfd28f 2022-02-13        arcade: 	if ! RE_USERNAME.is_match(channel) {
4632d20d39 2021-11-13        arcade: 		bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {:?}", &channel);
4632d20d39 2021-11-13        arcade: 	};
f988dfd28f 2022-02-13        arcade: 	if ! RE_LINK.is_match(url) {
4632d20d39 2021-11-13        arcade: 		bail!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {:?}", &url);
9171c791eb 2021-11-13        arcade: 	}
9171c791eb 2021-11-13        arcade: 	let iv_hash = match iv_hash {
9171c791eb 2021-11-13        arcade: 		Some(hash) => {
c1e27b74ed 2021-11-13        arcade: 			match *hash {
c1e27b74ed 2021-11-13        arcade: 				"-" => None,
613a665847 2021-11-15        arcade: 				thing => {
613a665847 2021-11-15        arcade: 					if ! RE_IV_HASH.is_match(thing) {
613a665847 2021-11-15        arcade: 						bail!("IV hash should be 14 hex digits.\nNot {:?}", thing);
613a665847 2021-11-15        arcade: 					};
613a665847 2021-11-15        arcade: 					Some(thing)
613a665847 2021-11-15        arcade: 				},
c1e27b74ed 2021-11-13        arcade: 			}
10c25017bb 2021-11-13        arcade: 		},
10c25017bb 2021-11-13        arcade: 		None => None,
10c25017bb 2021-11-13        arcade: 	};
10c25017bb 2021-11-13        arcade: 	let url_re = match url_re {
10c25017bb 2021-11-13        arcade: 		Some(re) => {
c1e27b74ed 2021-11-13        arcade: 			match *re {
c1e27b74ed 2021-11-13        arcade: 				"-" => None,
613a665847 2021-11-15        arcade: 				thing => {
613a665847 2021-11-15        arcade: 					let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
613a665847 2021-11-15        arcade: 					Some(thing)
613a665847 2021-11-15        arcade: 				}
c1e27b74ed 2021-11-13        arcade: 			}
10c25017bb 2021-11-13        arcade: 		},
9171c791eb 2021-11-13        arcade: 		None => None,
9171c791eb 2021-11-13        arcade: 	};
79c91a5357 2024-08-02        arcade: 	let channel_id = i64::from(core.request(telegram_bot::GetChat::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await?.id());
79c91a5357 2024-08-02        arcade: 	let chan_adm = core.request(telegram_bot::GetChatAdministrators::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await
4632d20d39 2021-11-13        arcade: 		.context("Sorry, I have no access to that chat.")?;
9171c791eb 2021-11-13        arcade: 	let (mut me, mut user) = (false, false);
9171c791eb 2021-11-13        arcade: 	for admin in chan_adm {
9171c791eb 2021-11-13        arcade: 		if admin.user.id == core.my.id {
9171c791eb 2021-11-13        arcade: 			me = true;
9171c791eb 2021-11-13        arcade: 		};
9171c791eb 2021-11-13        arcade: 		if admin.user.id == sender {
9171c791eb 2021-11-13        arcade: 			user = true;
9171c791eb 2021-11-13        arcade: 		};
9171c791eb 2021-11-13        arcade: 	};
4632d20d39 2021-11-13        arcade: 	if ! me   { bail!("I need to be admin on that channel."); };
4632d20d39 2021-11-13        arcade: 	if ! user { bail!("You should be admin on that channel."); };
26339860ce 2022-02-13        arcade: 	core.send(core.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await?, Some(sender), None).await?;
a7f91033c0 2021-11-13        arcade: 	Ok(())
9171c791eb 2021-11-13        arcade: }