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