Annotation For src/command.rs
Logged in as anonymous

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

ec2eac7ea2 2024-05-14    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: };
ec2eac7ea2 2024-05-14    9: use lazy_static::lazy_static;
ec2eac7ea2 2024-05-14   10: use regex::Regex;
ec2eac7ea2 2024-05-14   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: };
ec2eac7ea2 2024-05-14   20: 
ec2eac7ea2 2024-05-14   21: lazy_static! {
fae13a0e55 2025-06-28   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();
ec2eac7ea2 2024-05-14   24: 	static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
ec2eac7ea2 2024-05-14   25: }
ec2eac7ea2 2024-05-14   26: 
fae13a0e55 2025-06-28   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: 
fae13a0e55 2025-06-28   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: 
fae13a0e55 2025-06-28   41: pub async fn command (core: &Core, command: &str, msg: &Message, words: &[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."))?;
fae13a0e55 2025-06-28   45: 	let reply = if words.len() == 1 {
fae13a0e55 2025-06-28   46: 		match words[0].parse::<i32>() {
bb89b6fab8 2025-06-15   47: 			Err(err) => format!("I need a number.\n{}", &err).into(),
fae13a0e55 2025-06-28   48: 			Ok(number) => match command {
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(),
fae13a0e55 2025-06-28   55: 				_ => bail!("Command {command} {words:?} not handled."),
bb89b6fab8 2025-06-15   56: 			},
bb89b6fab8 2025-06-15   57: 		}
bb89b6fab8 2025-06-15   58: 	} else {
fae13a0e55 2025-06-28   59: 		"This command needs exacly one 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: 
fae13a0e55 2025-06-28   65: pub async fn update (core: &Core, command: &str, msg: &Message, words: &[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."))?;
bb89b6fab8 2025-06-15   68: 	let mut source_id: Option<i32> = None;
bb89b6fab8 2025-06-15   69: 	let at_least = "Requires at least 3 parameters.";
fae13a0e55 2025-06-28   70: 	let mut i_words = words.iter();
fae13a0e55 2025-06-28   71: 	match command {
bb89b6fab8 2025-06-15   72: 		"/update" => {
fae13a0e55 2025-06-28   73: 			let next_word = i_words.next().context(at_least)?;
e624ef9d66 2025-04-20   74: 			source_id = Some(next_word.parse::<i32>()
e624ef9d66 2025-04-20   75: 				.context(format!("I need a number, but got {next_word}."))?);
e624ef9d66 2025-04-20   76: 		},
e624ef9d66 2025-04-20   77: 		"/add" => {},
fae13a0e55 2025-06-28   78: 		_ => bail!("Passing {command} is not possible here."),
e624ef9d66 2025-04-20   79: 	};
e624ef9d66 2025-04-20   80: 	let (channel, url, iv_hash, url_re) = (
fae13a0e55 2025-06-28   81: 		i_words.next().context(at_least)?,
fae13a0e55 2025-06-28   82: 		i_words.next().context(at_least)?,
fae13a0e55 2025-06-28   83: 		i_words.next(),
fae13a0e55 2025-06-28   84: 		i_words.next());
fae13a0e55 2025-06-28   85: 	/*
fae13a0e55 2025-06-28   86: 	let channel = match RE_USERNAME.captures(channel) {
fae13a0e55 2025-06-28   87: 		Some(caps) => match caps.get(1) {
fae13a0e55 2025-06-28   88: 			Some(data) => data.as_str(),
fae13a0e55 2025-06-28   89: 			None => bail!("No string found in channel name"),
fae13a0e55 2025-06-28   90: 		},
fae13a0e55 2025-06-28   91: 		None => {
fae13a0e55 2025-06-28   92: 			bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}");
fae13a0e55 2025-06-28   93: 		},
fae13a0e55 2025-06-28   94: 	};
fae13a0e55 2025-06-28   95: 	*/
e624ef9d66 2025-04-20   96: 	if ! RE_USERNAME.is_match(channel) {
e624ef9d66 2025-04-20   97: 		bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}");
e624ef9d66 2025-04-20   98: 	};
e624ef9d66 2025-04-20   99: 	if ! RE_LINK.is_match(url) {
e624ef9d66 2025-04-20  100: 		bail!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {url:?}");
e624ef9d66 2025-04-20  101: 	}
e624ef9d66 2025-04-20  102: 	let iv_hash = match iv_hash {
e624ef9d66 2025-04-20  103: 		Some(hash) => {
bb89b6fab8 2025-06-15  104: 			match hash.as_ref() {
e624ef9d66 2025-04-20  105: 				"-" => None,
e624ef9d66 2025-04-20  106: 				thing => {
e624ef9d66 2025-04-20  107: 					if ! RE_IV_HASH.is_match(thing) {
e624ef9d66 2025-04-20  108: 						bail!("IV hash should be 14 hex digits.\nNot {thing:?}");
613a665847 2021-11-15  109: 					};
613a665847 2021-11-15  110: 					Some(thing)
613a665847 2021-11-15  111: 				},
c1e27b74ed 2021-11-13  112: 			}
10c25017bb 2021-11-13  113: 		},
10c25017bb 2021-11-13  114: 		None => None,
10c25017bb 2021-11-13  115: 	};
10c25017bb 2021-11-13  116: 	let url_re = match url_re {
10c25017bb 2021-11-13  117: 		Some(re) => {
bb89b6fab8 2025-06-15  118: 			match re.as_ref() {
c1e27b74ed 2021-11-13  119: 				"-" => None,
613a665847 2021-11-15  120: 				thing => {
613a665847 2021-11-15  121: 					let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
613a665847 2021-11-15  122: 					Some(thing)
613a665847 2021-11-15  123: 				}
c1e27b74ed 2021-11-13  124: 			}
10c25017bb 2021-11-13  125: 		},
10c25017bb 2021-11-13  126: 		None => None,
10c25017bb 2021-11-13  127: 	};
fae13a0e55 2025-06-28  128: 	let chat_id = ChatUsername::from(channel.as_ref());
bb89b6fab8 2025-06-15  129: 	let channel_id = core.tg.execute(GetChat::new(chat_id.clone())).await?.id;
bb89b6fab8 2025-06-15  130: 	let chan_adm = core.tg.execute(GetChatAdministrators::new(chat_id)).await
bb89b6fab8 2025-06-15  131: 		.context("Sorry, I have no access to that chat.")?;
10c25017bb 2021-11-13  132: 	let (mut me, mut user) = (false, false);
10c25017bb 2021-11-13  133: 	for admin in chan_adm {
e624ef9d66 2025-04-20  134: 		let member_id = match admin {
e624ef9d66 2025-04-20  135: 			ChatMember::Creator(member) => member.user.id,
e624ef9d66 2025-04-20  136: 			ChatMember::Administrator(member) => member.user.id,
e624ef9d66 2025-04-20  137: 			ChatMember::Left(_)
e624ef9d66 2025-04-20  138: 			| ChatMember::Kicked(_)
bb89b6fab8 2025-06-15  139: 			| ChatMember::Member{..}
e624ef9d66 2025-04-20  140: 			| ChatMember::Restricted(_) => continue,
bb89b6fab8 2025-06-15  141: 		};
bb89b6fab8 2025-06-15  142: 		if member_id == core.me.id {
e624ef9d66 2025-04-20  143: 			me = true;
bb89b6fab8 2025-06-15  144: 		}
e624ef9d66 2025-04-20  145: 		if member_id == sender {
e624ef9d66 2025-04-20  146: 			user = true;
bb89b6fab8 2025-06-15  147: 		}
10c25017bb 2021-11-13  148: 	};
10c25017bb 2021-11-13  149: 	if ! me   { bail!("I need to be admin on that channel."); };
10c25017bb 2021-11-13  150: 	if ! user { bail!("You should be admin on that channel."); };
0340541002 2025-04-24  151: 	let mut conn = core.db.begin().await?;
bb89b6fab8 2025-06-15  152: 	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  153: 	Ok(())
9171c791eb 2021-11-13  154: }