Lines of src/command.rs from check-in 13265e7697 that are changed by the sequence of edits moving toward check-in 9adc69d514:
13265e7697 2026-01-10 1: use crate::core::Core; 2: 3: use lazy_static::lazy_static; 4: use regex::Regex; 5: use sedregex::ReplaceCommand; 6: use stacked_errors::{ 7: Result, 8: StackableErr, 9: bail, 10: }; 11: use tgbot::types::{ 12: ChatMember, 13: ChatUsername, 14: GetChat, 15: GetChatAdministrators, 16: Message, 13265e7697 2026-01-10 17: ParseMode::MarkdownV2, 18: }; 19: use url::Url; 20: 21: lazy_static! { 22: static ref RE_USERNAME: Regex = Regex::new(r"^@([a-zA-Z][a-zA-Z0-9_]+)$").unwrap(); 23: static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap(); 24: } 25: 26: /// Sends an informational message to the message's chat linking to the bot help channel. 27: pub async fn start (core: &Core, msg: &Message) -> Result<()> { 13265e7697 2026-01-10 28: core.tg.send("We are open\\. Probably\\. Visit [channel](https://t.me/rsstg_bot_help/3) for details\\.", 13265e7697 2026-01-10 29: Some(msg.chat.get_id()), Some(MarkdownV2)).await.stack()?; 30: Ok(()) 31: } 32: 33: /// Send the sender's subscription list to the chat. 34: /// 35: /// Retrieves the message sender's user ID, obtains their subscription list from `core`, 36: /// and sends the resulting reply into the message chat using MarkdownV2. 37: pub async fn list (core: &Core, msg: &Message) -> Result<()> { 38: let sender = msg.sender.get_user_id() 39: .stack_err("Ignoring unreal users.")?; 40: let reply = core.list(sender).await.stack()?; 13265e7697 2026-01-10 41: core.tg.send(reply, Some(msg.chat.get_id()), Some(MarkdownV2)).await.stack()?; 42: Ok(()) 43: } 44: 45: /// Handle channel-management commands that operate on a single numeric source ID. 46: /// 47: /// This validates that exactly one numeric argument is provided, performs the requested 48: /// operation (check, clean, enable, delete, disable) against the database or core, 49: /// and sends the resulting reply to the chat. 50: /// 51: /// # Parameters 52: /// 53: /// - `core`: application core containing database and Telegram clients. 54: /// - `command`: command string (e.g. "/check", "/clean", "/enable", "/delete", "/disable"). 55: /// - `msg`: incoming Telegram message that triggered the command; used to determine sender and chat. 56: /// - `words`: command arguments; expected to contain exactly one element that parses as a 32-bit integer. 57: pub async fn command (core: &Core, command: &str, msg: &Message, words: &[String]) -> Result<()> { 58: let mut conn = core.db.begin().await.stack()?; 59: let sender = msg.sender.get_user_id() 60: .stack_err("Ignoring unreal users.")?; 61: let reply = if words.len() == 1 { 62: match words[0].parse::<i32>() { 63: Err(err) => format!("I need a number.\n{}", &err).into(), 64: Ok(number) => match command { 65: "/check" => core.check(number, false, None).await 66: .context("Channel check failed.")?.into(), 67: "/clean" => conn.clean(number, sender).await.stack()?, 68: "/enable" => conn.enable(number, sender).await.stack()?.into(), 13265e7697 2026-01-10 69: "/delete" => conn.delete(number, sender).await.stack()?, 70: "/disable" => conn.disable(number, sender).await.stack()?.into(), 71: _ => bail!("Command {command} {words:?} not handled."), 72: }, 73: } 74: } else { 75: "This command needs exactly one number.".into() 76: }; 13265e7697 2026-01-10 77: core.tg.send(reply, Some(msg.chat.get_id()), None).await.stack()?; 78: Ok(()) 79: } 80: 81: /// Validate command arguments, check permissions and update or add a channel feed configuration in the database. 82: /// 83: /// This function parses and validates parameters supplied by a user command (either "/update <id> ..." or "/add ..."), 84: /// verifies the channel username and feed URL, optionally validates an IV hash and a replacement regexp, 85: /// ensures both the bot and the command sender are administrators of the target channel, and performs the database update. 86: /// 87: /// # Parameters 88: /// 89: /// - `command` — the invoked command, expected to be either `"/update"` (followed by a numeric source id) or `"/add"`. 90: /// - `msg` — the incoming Telegram message; used to derive the command sender and target chat id for the reply. 13265e7697 2026-01-10 91: /// - `words` — the command arguments: for `"/add"` expected `channel url [iv_hash|'-'] [url_re|'-']`; for `"/update"` the first element must be a numeric `source_id` followed by the same parameters. 92: pub async fn update (core: &Core, command: &str, msg: &Message, words: &[String]) -> Result<()> { 93: let sender = msg.sender.get_user_id() 94: .stack_err("Ignoring unreal users.")?; 95: let mut source_id: Option<i32> = None; 96: let at_least = "Requires at least 3 parameters."; 97: let mut i_words = words.iter(); 98: match command { 99: "/update" => { 100: let next_word = i_words.next().context(at_least)?; 101: source_id = Some(next_word.parse::<i32>() 102: .context(format!("I need a number, but got {next_word}."))?); 103: }, 104: "/add" => {}, 105: _ => bail!("Passing {command} is not possible here."), 106: }; 107: let (channel, url, iv_hash, url_re) = ( 108: i_words.next().context(at_least)?, 109: i_words.next().context(at_least)?, 110: i_words.next(), 111: i_words.next()); 13265e7697 2026-01-10 112: /* 13265e7697 2026-01-10 113: let channel = match RE_USERNAME.captures(channel) { 13265e7697 2026-01-10 114: Some(caps) => match caps.get(1) { 13265e7697 2026-01-10 115: Some(data) => data.as_str(), 13265e7697 2026-01-10 116: None => bail!("No string found in channel name"), 13265e7697 2026-01-10 117: }, 13265e7697 2026-01-10 118: None => { 13265e7697 2026-01-10 119: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}"); 13265e7697 2026-01-10 120: }, 13265e7697 2026-01-10 121: }; 13265e7697 2026-01-10 122: */ 123: if ! RE_USERNAME.is_match(channel) { 124: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}"); 125: }; 126: { 127: let parsed_url = Url::parse(url) 128: .stack_err("Expecting a valid link to ATOM/RSS feed.")?; 129: match parsed_url.scheme() { 130: "http" | "https" => {}, 131: scheme => { 132: bail!("Unsupported URL scheme: {scheme}"); 133: }, 134: }; 135: } 136: let iv_hash = match iv_hash { 137: Some(hash) => { 138: match hash.as_ref() { 139: "-" => None, 140: thing => { 141: if ! RE_IV_HASH.is_match(thing) { 142: bail!("IV hash should be 14 hex digits.\nNot {thing:?}"); 143: }; 144: Some(thing) 145: }, 146: } 147: }, 148: None => None, 149: }; 150: let url_re = match url_re { 151: Some(re) => { 152: match re.as_ref() { 153: "-" => None, 154: thing => { 155: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?; 156: Some(thing) 157: } 158: } 159: }, 160: None => None, 161: }; 162: let chat_id = ChatUsername::from(channel.as_ref()); 13265e7697 2026-01-10 163: let channel_id = core.tg.client.execute(GetChat::new(chat_id.clone())).await.stack_err("gettting GetChat")?.id; 164: let chan_adm = core.tg.client.execute(GetChatAdministrators::new(chat_id)).await 165: .context("Sorry, I have no access to that chat.")?; 166: let (mut me, mut user) = (false, false); 167: for admin in chan_adm { 168: let member_id = match admin { 169: ChatMember::Creator(member) => member.user.id, 170: ChatMember::Administrator(member) => member.user.id, 171: ChatMember::Left(_) 172: | ChatMember::Kicked(_) 173: | ChatMember::Member{..} 174: | ChatMember::Restricted(_) => continue, 175: }; 176: if member_id == core.tg.me.id { 177: me = true; 178: } 179: if member_id == sender { 180: user = true; 181: } 182: }; 183: if ! me { bail!("I need to be admin on that channel."); }; 184: if ! user { bail!("You should be admin on that channel."); }; 185: let mut conn = core.db.begin().await.stack()?; 186: let update = conn.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await.stack()?; 13265e7697 2026-01-10 187: core.tg.send(update, Some(msg.chat.get_id()), None).await.stack()?; 188: Ok(()) 189: }