Annotation For src/command.rs
Logged in as anonymous

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

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