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