Lines of src/command.rs from check-in 79c91a5357 that are changed by the sequence of edits moving toward check-in a2880e5100:
79c91a5357 2024-08-02 1: use anyhow::{bail, Context, Result}; 2: use crate::core::Core; 3: use lazy_static::lazy_static; 4: use regex::Regex; 5: use sedregex::ReplaceCommand; 6: use std::borrow::Cow; 7: 8: lazy_static! { 9: static ref RE_USERNAME: Regex = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$").unwrap(); 10: static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.:0-9/?=]+$").unwrap(); 11: static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap(); 12: } 13: 79c91a5357 2024-08-02 14: pub async fn start(core: &Core, sender: telegram_bot::UserId) -> Result<()> { 79c91a5357 2024-08-02 15: core.send("We are open. Probably. Visit [channel](https://t.me/rsstg_bot_help/3) for details.", Some(sender), None).await?; 16: Ok(()) 17: } 18: 79c91a5357 2024-08-02 19: pub async fn list(core: &Core, sender: telegram_bot::UserId) -> Result<()> { 20: core.send(core.list(sender).await?, Some(sender), Some(telegram_bot::types::ParseMode::MarkdownV2)).await?; 21: Ok(()) 22: } 23: 24: pub async fn command(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> { 25: if command.len() >= 2 { 26: let msg: Cow<str> = match &command[1].parse::<i32>() { 27: Err(err) => format!("I need a number.\n{}", &err).into(), 28: Ok(number) => match command[0] { 29: "/check" => core.check(number, sender, false).await 30: .context("Channel check failed.")?, 31: "/clean" => core.clean(number, sender).await?, 32: "/enable" => core.enable(number, sender).await?.into(), 33: "/delete" => core.delete(number, sender).await?, 34: "/disable" => core.disable(number, sender).await?.into(), 35: _ => bail!("Command {} not handled.", &command[0]), 36: }, 37: }; 38: core.send(msg, Some(sender), None).await?; 39: } else { 40: core.send("This command needs a number.", Some(sender), None).await?; 41: } 42: Ok(()) 43: } 44: 45: pub async fn update(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> { 46: let mut source_id: Option<i32> = None; 47: let at_least = "Requires at least 3 parameters."; 48: let mut i_command = command.iter(); 49: let first_word = i_command.next().context(at_least)?; 50: match *first_word { 51: "/update" => { 52: let next_word = i_command.next().context(at_least)?; 53: source_id = Some(next_word.parse::<i32>() 54: .context(format!("I need a number, but got {}.", next_word))?); 55: }, 56: "/add" => {}, 57: _ => bail!("Passing {} is not possible here.", first_word), 58: }; 59: let (channel, url, iv_hash, url_re) = ( 60: i_command.next().context(at_least)?, 61: i_command.next().context(at_least)?, 62: i_command.next(), 63: i_command.next()); 64: if ! RE_USERNAME.is_match(channel) { 65: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {:?}", &channel); 66: }; 67: if ! RE_LINK.is_match(url) { 68: bail!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {:?}", &url); 69: } 70: let iv_hash = match iv_hash { 71: Some(hash) => { 72: match *hash { 73: "-" => None, 74: thing => { 75: if ! RE_IV_HASH.is_match(thing) { 76: bail!("IV hash should be 14 hex digits.\nNot {:?}", thing); 77: }; 78: Some(thing) 79: }, 80: } 81: }, 82: None => None, 83: }; 84: let url_re = match url_re { 85: Some(re) => { 86: match *re { 87: "-" => None, 88: thing => { 89: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?; 90: Some(thing) 91: } 92: } 93: }, 94: None => None, 95: }; 96: let channel_id = i64::from(core.request(telegram_bot::GetChat::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await?.id()); 97: let chan_adm = core.request(telegram_bot::GetChatAdministrators::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await 98: .context("Sorry, I have no access to that chat.")?; 99: let (mut me, mut user) = (false, false); 100: for admin in chan_adm { 101: if admin.user.id == core.my.id { 102: me = true; 103: }; 104: if admin.user.id == sender { 105: user = true; 106: }; 107: }; 108: if ! me { bail!("I need to be admin on that channel."); }; 109: if ! user { bail!("You should be admin on that channel."); }; 110: core.send(core.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await?, Some(sender), None).await?; 111: Ok(()) 112: }