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