9171c791eb 2021-11-13 1: use anyhow::{bail, Context, Result};
9171c791eb 2021-11-13 2: use crate::core::Core;
28da2e2a00 2022-03-12 3: use lazy_static::lazy_static;
9171c791eb 2021-11-13 4: use regex::Regex;
9171c791eb 2021-11-13 5: use sedregex::ReplaceCommand;
26339860ce 2022-02-13 6: use std::borrow::Cow;
9171c791eb 2021-11-13 7:
9171c791eb 2021-11-13 8: lazy_static! {
9171c791eb 2021-11-13 9: static ref RE_USERNAME: Regex = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$").unwrap();
9171c791eb 2021-11-13 10: static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.0-9/?=]+$").unwrap();
9171c791eb 2021-11-13 11: static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
9171c791eb 2021-11-13 12: }
9171c791eb 2021-11-13 13:
9171c791eb 2021-11-13 14: pub async fn start(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
26339860ce 2022-02-13 15: core.send("We are open. Probably. Visit [channel](https://t.me/rsstg_bot_help/3) for details.", Some(sender), None).await?;
9171c791eb 2021-11-13 16: Ok(())
9171c791eb 2021-11-13 17: }
9171c791eb 2021-11-13 18:
9171c791eb 2021-11-13 19: pub async fn list(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
26339860ce 2022-02-13 20: core.send(core.list(sender).await?, Some(sender), Some(telegram_bot::types::ParseMode::MarkdownV2)).await?;
9171c791eb 2021-11-13 21: Ok(())
9171c791eb 2021-11-13 22: }
9171c791eb 2021-11-13 23:
9171c791eb 2021-11-13 24: pub async fn command(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
26339860ce 2022-02-13 25: let msg: Cow<str> = match &command[1].parse::<i32>() {
4b64438e28 2021-12-30 26: Err(err) => format!("I need a number.\n{}", &err).into(),
9171c791eb 2021-11-13 27: Ok(number) => match command[0] {
f988dfd28f 2022-02-13 28: "/check" => core.check(number, sender, false).await
9171c791eb 2021-11-13 29: .context("Channel check failed.")?,
f988dfd28f 2022-02-13 30: "/clean" => core.clean(number, sender).await?,
f988dfd28f 2022-02-13 31: "/enable" => core.enable(number, sender).await?.into(),
f988dfd28f 2022-02-13 32: "/delete" => core.delete(number, sender).await?,
f988dfd28f 2022-02-13 33: "/disable" => core.disable(number, sender).await?.into(),
9171c791eb 2021-11-13 34: _ => bail!("Command {} not handled.", &command[0]),
9171c791eb 2021-11-13 35: },
26339860ce 2022-02-13 36: };
26339860ce 2022-02-13 37: core.send(msg, Some(sender), None).await?;
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: };
f988dfd28f 2022-02-13 54: let mut i_command = command.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());
f988dfd28f 2022-02-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: };
f988dfd28f 2022-02-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) => {
c1e27b74ed 2021-11-13 68: match *hash {
c1e27b74ed 2021-11-13 69: "-" => None,
613a665847 2021-11-15 70: thing => {
613a665847 2021-11-15 71: if ! RE_IV_HASH.is_match(thing) {
613a665847 2021-11-15 72: bail!("IV hash should be 14 hex digits.\nNot {:?}", thing);
613a665847 2021-11-15 73: };
613a665847 2021-11-15 74: Some(thing)
613a665847 2021-11-15 75: },
c1e27b74ed 2021-11-13 76: }
10c25017bb 2021-11-13 77: },
10c25017bb 2021-11-13 78: None => None,
10c25017bb 2021-11-13 79: };
10c25017bb 2021-11-13 80: let url_re = match url_re {
10c25017bb 2021-11-13 81: Some(re) => {
c1e27b74ed 2021-11-13 82: match *re {
c1e27b74ed 2021-11-13 83: "-" => None,
613a665847 2021-11-15 84: thing => {
613a665847 2021-11-15 85: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
613a665847 2021-11-15 86: Some(thing)
613a665847 2021-11-15 87: }
c1e27b74ed 2021-11-13 88: }
10c25017bb 2021-11-13 89: },
9171c791eb 2021-11-13 90: None => None,
9171c791eb 2021-11-13 91: };
093ae6c75b 2022-02-15 92: let channel_id = i64::from(core.tg.send(telegram_bot::GetChat::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await?.id());
093ae6c75b 2022-02-15 93: let chan_adm = core.tg.send(telegram_bot::GetChatAdministrators::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await
4632d20d39 2021-11-13 94: .context("Sorry, I have no access to that chat.")?;
9171c791eb 2021-11-13 95: let (mut me, mut user) = (false, false);
9171c791eb 2021-11-13 96: for admin in chan_adm {
9171c791eb 2021-11-13 97: if admin.user.id == core.my.id {
9171c791eb 2021-11-13 98: me = true;
9171c791eb 2021-11-13 99: };
9171c791eb 2021-11-13 100: if admin.user.id == sender {
9171c791eb 2021-11-13 101: user = true;
9171c791eb 2021-11-13 102: };
9171c791eb 2021-11-13 103: };
4632d20d39 2021-11-13 104: if ! me { bail!("I need to be admin on that channel."); };
4632d20d39 2021-11-13 105: if ! user { bail!("You should be admin on that channel."); };
26339860ce 2022-02-13 106: core.send(core.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await?, Some(sender), None).await?;
a7f91033c0 2021-11-13 107: Ok(())
9171c791eb 2021-11-13 108: }