659724c658 2021-12-08 1: use crate::core::Core;
a2880e5100 2025-04-19 2:
e624ef9d66 2025-04-20 3: use std::borrow::Cow;
e624ef9d66 2025-04-20 4:
a2880e5100 2025-04-19 5: use anyhow::{
a2880e5100 2025-04-19 6: bail,
a2880e5100 2025-04-19 7: Context,
a2880e5100 2025-04-19 8: Result
a2880e5100 2025-04-19 9: };
e624ef9d66 2025-04-20 10: use frankenstein::{
e624ef9d66 2025-04-20 11: methods::{
e624ef9d66 2025-04-20 12: GetChatAdministratorsParams,
e624ef9d66 2025-04-20 13: GetChatParams,
e624ef9d66 2025-04-20 14: },
e624ef9d66 2025-04-20 15: types::{
e624ef9d66 2025-04-20 16: ChatId,
e624ef9d66 2025-04-20 17: ChatMember,
e624ef9d66 2025-04-20 18: },
e624ef9d66 2025-04-20 19: AsyncTelegramApi,
e624ef9d66 2025-04-20 20: ParseMode,
e624ef9d66 2025-04-20 21: };
28da2e2a00 2022-03-12 22: use lazy_static::lazy_static;
659724c658 2021-12-08 23: use regex::Regex;
659724c658 2021-12-08 24: use sedregex::ReplaceCommand;
659724c658 2021-12-08 25:
659724c658 2021-12-08 26: lazy_static! {
659724c658 2021-12-08 27: static ref RE_USERNAME: Regex = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$").unwrap();
ec2eac7ea2 2024-05-14 28: static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.:0-9/?=]+$").unwrap();
26339860ce 2022-02-13 29: static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
26339860ce 2022-02-13 30: }
26339860ce 2022-02-13 31:
e624ef9d66 2025-04-20 32: pub async fn start(core: &Core, chat_id: i64) -> Result<()> {
e624ef9d66 2025-04-20 33: core.send("We are open\\. Probably\\. Visit [channel](https://t.me/rsstg_bot_help/3) for details\\.",
e624ef9d66 2025-04-20 34: Some(chat_id), Some(ParseMode::MarkdownV2)).await?;
e624ef9d66 2025-04-20 35: Ok(())
e624ef9d66 2025-04-20 36: }
e624ef9d66 2025-04-20 37:
e624ef9d66 2025-04-20 38: pub async fn list(core: &Core, sender: i64) -> Result<()> {
e624ef9d66 2025-04-20 39: core.send(core.list(sender).await?, Some(sender), Some(ParseMode::MarkdownV2)).await?;
e624ef9d66 2025-04-20 40: Ok(())
e624ef9d66 2025-04-20 41: }
e624ef9d66 2025-04-20 42:
e624ef9d66 2025-04-20 43: pub async fn command(core: &Core, sender: i64, command: Vec<&str>) -> Result<()> {
409eb5bafc 2022-06-12 44: if command.len() >= 2 {
409eb5bafc 2022-06-12 45: let msg: Cow<str> = match &command[1].parse::<i32>() {
409eb5bafc 2022-06-12 46: Err(err) => format!("I need a number.\n{}", &err).into(),
409eb5bafc 2022-06-12 47: Ok(number) => match command[0] {
409eb5bafc 2022-06-12 48: "/check" => core.check(number, sender, false).await
409eb5bafc 2022-06-12 49: .context("Channel check failed.")?,
409eb5bafc 2022-06-12 50: "/clean" => core.clean(number, sender).await?,
409eb5bafc 2022-06-12 51: "/enable" => core.enable(number, sender).await?.into(),
409eb5bafc 2022-06-12 52: "/delete" => core.delete(number, sender).await?,
409eb5bafc 2022-06-12 53: "/disable" => core.disable(number, sender).await?.into(),
409eb5bafc 2022-06-12 54: _ => bail!("Command {} not handled.", &command[0]),
409eb5bafc 2022-06-12 55: },
409eb5bafc 2022-06-12 56: };
409eb5bafc 2022-06-12 57: core.send(msg, Some(sender), None).await?;
409eb5bafc 2022-06-12 58: } else {
409eb5bafc 2022-06-12 59: core.send("This command needs a number.", Some(sender), None).await?;
409eb5bafc 2022-06-12 60: }
409eb5bafc 2022-06-12 61: Ok(())
409eb5bafc 2022-06-12 62: }
409eb5bafc 2022-06-12 63:
e624ef9d66 2025-04-20 64: pub async fn update(core: &Core, sender: i64, command: Vec<&str>) -> Result<()> {
cab82b7ff4 2022-03-23 65: let mut source_id: Option<i32> = None;
cab82b7ff4 2022-03-23 66: let at_least = "Requires at least 3 parameters.";
cab82b7ff4 2022-03-23 67: let mut i_command = command.iter();
cab82b7ff4 2022-03-23 68: let first_word = i_command.next().context(at_least)?;
cab82b7ff4 2022-03-23 69: match *first_word {
cab82b7ff4 2022-03-23 70: "/update" => {
cab82b7ff4 2022-03-23 71: let next_word = i_command.next().context(at_least)?;
cab82b7ff4 2022-03-23 72: source_id = Some(next_word.parse::<i32>()
e624ef9d66 2025-04-20 73: .context(format!("I need a number, but got {next_word}."))?);
cab82b7ff4 2022-03-23 74: },
cab82b7ff4 2022-03-23 75: "/add" => {},
e624ef9d66 2025-04-20 76: _ => bail!("Passing {first_word} is not possible here."),
cab82b7ff4 2022-03-23 77: };
9171c791eb 2021-11-13 78: let (channel, url, iv_hash, url_re) = (
9171c791eb 2021-11-13 79: i_command.next().context(at_least)?,
9171c791eb 2021-11-13 80: i_command.next().context(at_least)?,
9171c791eb 2021-11-13 81: i_command.next(),
9171c791eb 2021-11-13 82: i_command.next());
f988dfd28f 2022-02-13 83: if ! RE_USERNAME.is_match(channel) {
e624ef9d66 2025-04-20 84: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}");
4632d20d39 2021-11-13 85: };
f988dfd28f 2022-02-13 86: if ! RE_LINK.is_match(url) {
e624ef9d66 2025-04-20 87: bail!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {url:?}");
9171c791eb 2021-11-13 88: }
9171c791eb 2021-11-13 89: let iv_hash = match iv_hash {
9171c791eb 2021-11-13 90: Some(hash) => {
c1e27b74ed 2021-11-13 91: match *hash {
c1e27b74ed 2021-11-13 92: "-" => None,
613a665847 2021-11-15 93: thing => {
613a665847 2021-11-15 94: if ! RE_IV_HASH.is_match(thing) {
e624ef9d66 2025-04-20 95: bail!("IV hash should be 14 hex digits.\nNot {thing:?}");
613a665847 2021-11-15 96: };
613a665847 2021-11-15 97: Some(thing)
613a665847 2021-11-15 98: },
c1e27b74ed 2021-11-13 99: }
10c25017bb 2021-11-13 100: },
10c25017bb 2021-11-13 101: None => None,
10c25017bb 2021-11-13 102: };
10c25017bb 2021-11-13 103: let url_re = match url_re {
10c25017bb 2021-11-13 104: Some(re) => {
c1e27b74ed 2021-11-13 105: match *re {
c1e27b74ed 2021-11-13 106: "-" => None,
613a665847 2021-11-15 107: thing => {
613a665847 2021-11-15 108: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
613a665847 2021-11-15 109: Some(thing)
613a665847 2021-11-15 110: }
c1e27b74ed 2021-11-13 111: }
10c25017bb 2021-11-13 112: },
9171c791eb 2021-11-13 113: None => None,
9171c791eb 2021-11-13 114: };
e624ef9d66 2025-04-20 115: let chat_id = ChatId::String((*channel).into());
e624ef9d66 2025-04-20 116: let channel_id = core.tg.get_chat(&GetChatParams { chat_id: chat_id.clone() }).await?.result.id;
e624ef9d66 2025-04-20 117: let chan_adm = core.tg.get_chat_administrators(&GetChatAdministratorsParams { chat_id }).await
e624ef9d66 2025-04-20 118: .context("Sorry, I have no access to that chat.")?.result;
9171c791eb 2021-11-13 119: let (mut me, mut user) = (false, false);
9171c791eb 2021-11-13 120: for admin in chan_adm {
e624ef9d66 2025-04-20 121: let member_id = match admin {
e624ef9d66 2025-04-20 122: ChatMember::Creator(member) => member.user.id,
e624ef9d66 2025-04-20 123: ChatMember::Administrator(member) => member.user.id,
e624ef9d66 2025-04-20 124: ChatMember::Left(_)
e624ef9d66 2025-04-20 125: | ChatMember::Kicked(_)
e624ef9d66 2025-04-20 126: | ChatMember::Member(_)
e624ef9d66 2025-04-20 127: | ChatMember::Restricted(_) => continue,
e624ef9d66 2025-04-20 128: } as i64;
e624ef9d66 2025-04-20 129: if member_id == core.me.id as i64 {
9171c791eb 2021-11-13 130: me = true;
9171c791eb 2021-11-13 131: };
e624ef9d66 2025-04-20 132: if member_id == sender {
9171c791eb 2021-11-13 133: user = true;
9171c791eb 2021-11-13 134: };
9171c791eb 2021-11-13 135: };
4632d20d39 2021-11-13 136: if ! me { bail!("I need to be admin on that channel."); };
4632d20d39 2021-11-13 137: if ! user { bail!("You should be admin on that channel."); };
26339860ce 2022-02-13 138: core.send(core.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await?, Some(sender), None).await?;
a7f91033c0 2021-11-13 139: Ok(())
9171c791eb 2021-11-13 140: }