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