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