Annotation For src/command.rs
Logged in as anonymous

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

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