9171c791eb 2021-11-13 arcade: use anyhow::{bail, Context, Result};
9171c791eb 2021-11-13 arcade: use crate::core::Core;
9171c791eb 2021-11-13 arcade: use regex::Regex;
9171c791eb 2021-11-13 arcade: use sedregex::ReplaceCommand;
26339860ce 2022-02-13 arcade: use std::borrow::Cow;
9171c791eb 2021-11-13 arcade:
9171c791eb 2021-11-13 arcade: lazy_static! {
9171c791eb 2021-11-13 arcade: static ref RE_USERNAME: Regex = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$").unwrap();
9171c791eb 2021-11-13 arcade: static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.0-9/?=]+$").unwrap();
9171c791eb 2021-11-13 arcade: static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
9171c791eb 2021-11-13 arcade: }
9171c791eb 2021-11-13 arcade:
9171c791eb 2021-11-13 arcade: pub async fn start(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
26339860ce 2022-02-13 arcade: 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 arcade: Ok(())
9171c791eb 2021-11-13 arcade: }
9171c791eb 2021-11-13 arcade:
9171c791eb 2021-11-13 arcade: pub async fn list(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
26339860ce 2022-02-13 arcade: core.send(core.list(sender).await?, Some(sender), Some(telegram_bot::types::ParseMode::MarkdownV2)).await?;
9171c791eb 2021-11-13 arcade: Ok(())
9171c791eb 2021-11-13 arcade: }
9171c791eb 2021-11-13 arcade:
9171c791eb 2021-11-13 arcade: pub async fn command(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
26339860ce 2022-02-13 arcade: let msg: Cow<str> = match &command[1].parse::<i32>() {
4b64438e28 2021-12-30 arcade: Err(err) => format!("I need a number.\n{}", &err).into(),
9171c791eb 2021-11-13 arcade: Ok(number) => match command[0] {
f988dfd28f 2022-02-13 arcade: "/check" => core.check(number, sender, false).await
9171c791eb 2021-11-13 arcade: .context("Channel check failed.")?,
f988dfd28f 2022-02-13 arcade: "/clean" => core.clean(number, sender).await?,
f988dfd28f 2022-02-13 arcade: "/enable" => core.enable(number, sender).await?.into(),
f988dfd28f 2022-02-13 arcade: "/delete" => core.delete(number, sender).await?,
f988dfd28f 2022-02-13 arcade: "/disable" => core.disable(number, sender).await?.into(),
9171c791eb 2021-11-13 arcade: _ => bail!("Command {} not handled.", &command[0]),
9171c791eb 2021-11-13 arcade: },
26339860ce 2022-02-13 arcade: };
26339860ce 2022-02-13 arcade: core.send(msg, Some(sender), None).await?;
9171c791eb 2021-11-13 arcade: Ok(())
9171c791eb 2021-11-13 arcade: }
9171c791eb 2021-11-13 arcade:
9171c791eb 2021-11-13 arcade: pub async fn update(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
9171c791eb 2021-11-13 arcade: let mut source_id: Option<i32> = None;
9171c791eb 2021-11-13 arcade: let at_least = "Requires at least 3 parameters.";
9171c791eb 2021-11-13 arcade: let first_word = command[0];
9171c791eb 2021-11-13 arcade: let command = match first_word {
9171c791eb 2021-11-13 arcade: "/update" => {
9171c791eb 2021-11-13 arcade: source_id = Some(command[1].parse::<i32>()
9171c791eb 2021-11-13 arcade: .context(format!("I need a number, but got {}.", command[1]))?);
9171c791eb 2021-11-13 arcade: &command[2..]
9171c791eb 2021-11-13 arcade: },
9171c791eb 2021-11-13 arcade: "/add" => &command[1..],
9171c791eb 2021-11-13 arcade: _ => bail!("Passing {} is not possible here.", command[1]),
9171c791eb 2021-11-13 arcade: };
f988dfd28f 2022-02-13 arcade: let mut i_command = command.iter();
9171c791eb 2021-11-13 arcade: let (channel, url, iv_hash, url_re) = (
9171c791eb 2021-11-13 arcade: i_command.next().context(at_least)?,
9171c791eb 2021-11-13 arcade: i_command.next().context(at_least)?,
9171c791eb 2021-11-13 arcade: i_command.next(),
9171c791eb 2021-11-13 arcade: i_command.next());
f988dfd28f 2022-02-13 arcade: if ! RE_USERNAME.is_match(channel) {
4632d20d39 2021-11-13 arcade: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {:?}", &channel);
4632d20d39 2021-11-13 arcade: };
f988dfd28f 2022-02-13 arcade: if ! RE_LINK.is_match(url) {
4632d20d39 2021-11-13 arcade: bail!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {:?}", &url);
9171c791eb 2021-11-13 arcade: }
9171c791eb 2021-11-13 arcade: let iv_hash = match iv_hash {
9171c791eb 2021-11-13 arcade: Some(hash) => {
c1e27b74ed 2021-11-13 arcade: match *hash {
c1e27b74ed 2021-11-13 arcade: "-" => None,
613a665847 2021-11-15 arcade: thing => {
613a665847 2021-11-15 arcade: if ! RE_IV_HASH.is_match(thing) {
613a665847 2021-11-15 arcade: bail!("IV hash should be 14 hex digits.\nNot {:?}", thing);
613a665847 2021-11-15 arcade: };
613a665847 2021-11-15 arcade: Some(thing)
613a665847 2021-11-15 arcade: },
c1e27b74ed 2021-11-13 arcade: }
10c25017bb 2021-11-13 arcade: },
10c25017bb 2021-11-13 arcade: None => None,
10c25017bb 2021-11-13 arcade: };
10c25017bb 2021-11-13 arcade: let url_re = match url_re {
10c25017bb 2021-11-13 arcade: Some(re) => {
c1e27b74ed 2021-11-13 arcade: match *re {
c1e27b74ed 2021-11-13 arcade: "-" => None,
613a665847 2021-11-15 arcade: thing => {
613a665847 2021-11-15 arcade: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
613a665847 2021-11-15 arcade: Some(thing)
613a665847 2021-11-15 arcade: }
c1e27b74ed 2021-11-13 arcade: }
10c25017bb 2021-11-13 arcade: },
9171c791eb 2021-11-13 arcade: None => None,
9171c791eb 2021-11-13 arcade: };
659724c658 2021-12-08 arcade: let s_channel = &channel.to_string();
659724c658 2021-12-08 arcade: 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 arcade: let chan_adm = core.tg.send(telegram_bot::GetChatAdministrators::new(telegram_bot::types::ChatRef::ChannelUsername(s_channel.into()))).await
4632d20d39 2021-11-13 arcade: .context("Sorry, I have no access to that chat.")?;
9171c791eb 2021-11-13 arcade: let (mut me, mut user) = (false, false);
9171c791eb 2021-11-13 arcade: for admin in chan_adm {
9171c791eb 2021-11-13 arcade: if admin.user.id == core.my.id {
9171c791eb 2021-11-13 arcade: me = true;
9171c791eb 2021-11-13 arcade: };
9171c791eb 2021-11-13 arcade: if admin.user.id == sender {
9171c791eb 2021-11-13 arcade: user = true;
9171c791eb 2021-11-13 arcade: };
9171c791eb 2021-11-13 arcade: };
4632d20d39 2021-11-13 arcade: if ! me { bail!("I need to be admin on that channel."); };
4632d20d39 2021-11-13 arcade: if ! user { bail!("You should be admin on that channel."); };
26339860ce 2022-02-13 arcade: core.send(core.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await?, Some(sender), None).await?;
a7f91033c0 2021-11-13 arcade: Ok(())
9171c791eb 2021-11-13 arcade: }