ec2eac7ea2 2024-05-14 1: use crate::core::Core;
a2880e5100 2025-04-19 2:
ec2eac7ea2 2024-05-14 3: use lazy_static::lazy_static;
ec2eac7ea2 2024-05-14 4: use regex::Regex;
ec2eac7ea2 2024-05-14 5: use sedregex::ReplaceCommand;
44575a91d3 2025-07-09 6: use stacked_errors::{
44575a91d3 2025-07-09 7: Result,
44575a91d3 2025-07-09 8: StackableErr,
44575a91d3 2025-07-09 9: bail,
44575a91d3 2025-07-09 10: };
bb89b6fab8 2025-06-15 11: use tgbot::types::{
bb89b6fab8 2025-06-15 12: ChatMember,
bb89b6fab8 2025-06-15 13: ChatUsername,
bb89b6fab8 2025-06-15 14: GetChat,
bb89b6fab8 2025-06-15 15: GetChatAdministrators,
bb89b6fab8 2025-06-15 16: Message,
bb89b6fab8 2025-06-15 17: ParseMode::MarkdownV2,
bb89b6fab8 2025-06-15 18: };
ec2eac7ea2 2024-05-14 19:
ec2eac7ea2 2024-05-14 20: lazy_static! {
fae13a0e55 2025-06-28 21: static ref RE_USERNAME: Regex = Regex::new(r"^@([a-zA-Z][a-zA-Z0-9_]+)$").unwrap();
ec2eac7ea2 2024-05-14 22: static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.:0-9/?=]+$").unwrap();
ec2eac7ea2 2024-05-14 23: static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
ec2eac7ea2 2024-05-14 24: }
ec2eac7ea2 2024-05-14 25:
fae13a0e55 2025-06-28 26: pub async fn start (core: &Core, msg: &Message) -> Result<()> {
fae13a0e55 2025-06-28 27: core.send("We are open\\. Probably\\. Visit [channel](https://t.me/rsstg_bot_help/3) for details\\.",
44575a91d3 2025-07-09 28: Some(msg.chat.get_id()), Some(MarkdownV2)).await.stack()?;
fae13a0e55 2025-06-28 29: Ok(())
fae13a0e55 2025-06-28 30: }
fae13a0e55 2025-06-28 31:
fae13a0e55 2025-06-28 32: pub async fn list (core: &Core, msg: &Message) -> Result<()> {
fae13a0e55 2025-06-28 33: let sender = msg.sender.get_user_id()
44575a91d3 2025-07-09 34: .stack_err("Ignoring unreal users.")?;
44575a91d3 2025-07-09 35: let reply = core.list(sender).await.stack()?;
44575a91d3 2025-07-09 36: core.send(reply, Some(msg.chat.get_id()), Some(MarkdownV2)).await.stack()?;
fae13a0e55 2025-06-28 37: Ok(())
fae13a0e55 2025-06-28 38: }
fae13a0e55 2025-06-28 39:
fae13a0e55 2025-06-28 40: pub async fn command (core: &Core, command: &str, msg: &Message, words: &[String]) -> Result<()> {
44575a91d3 2025-07-09 41: let mut conn = core.db.begin().await.stack()?;
fae13a0e55 2025-06-28 42: let sender = msg.sender.get_user_id()
44575a91d3 2025-07-09 43: .stack_err("Ignoring unreal users.")?;
fae13a0e55 2025-06-28 44: let reply = if words.len() == 1 {
fae13a0e55 2025-06-28 45: match words[0].parse::<i32>() {
fae13a0e55 2025-06-28 46: Err(err) => format!("I need a number.\n{}", &err).into(),
fae13a0e55 2025-06-28 47: Ok(number) => match command {
fae13a0e55 2025-06-28 48: "/check" => core.check(number, false).await
fae13a0e55 2025-06-28 49: .context("Channel check failed.")?.into(),
44575a91d3 2025-07-09 50: "/clean" => conn.clean(number, sender).await.stack()?,
44575a91d3 2025-07-09 51: "/enable" => conn.enable(number, sender).await.stack()?.into(),
44575a91d3 2025-07-09 52: "/delete" => conn.delete(number, sender).await.stack()?,
44575a91d3 2025-07-09 53: "/disable" => conn.disable(number, sender).await.stack()?.into(),
fae13a0e55 2025-06-28 54: _ => bail!("Command {command} {words:?} not handled."),
bb89b6fab8 2025-06-15 55: },
bb89b6fab8 2025-06-15 56: }
bb89b6fab8 2025-06-15 57: } else {
fae13a0e55 2025-06-28 58: "This command needs exacly one number.".into()
bb89b6fab8 2025-06-15 59: };
44575a91d3 2025-07-09 60: core.send(reply, Some(msg.chat.get_id()), None).await.stack()?;
bb89b6fab8 2025-06-15 61: Ok(())
bb89b6fab8 2025-06-15 62: }
bb89b6fab8 2025-06-15 63:
fae13a0e55 2025-06-28 64: pub async fn update (core: &Core, command: &str, msg: &Message, words: &[String]) -> Result<()> {
bb89b6fab8 2025-06-15 65: let sender = msg.sender.get_user_id()
44575a91d3 2025-07-09 66: .stack_err("Ignoring unreal users.")?;
bb89b6fab8 2025-06-15 67: let mut source_id: Option<i32> = None;
bb89b6fab8 2025-06-15 68: let at_least = "Requires at least 3 parameters.";
fae13a0e55 2025-06-28 69: let mut i_words = words.iter();
fae13a0e55 2025-06-28 70: match command {
bb89b6fab8 2025-06-15 71: "/update" => {
fae13a0e55 2025-06-28 72: let next_word = i_words.next().context(at_least)?;
e624ef9d66 2025-04-20 73: source_id = Some(next_word.parse::<i32>()
e624ef9d66 2025-04-20 74: .context(format!("I need a number, but got {next_word}."))?);
e624ef9d66 2025-04-20 75: },
e624ef9d66 2025-04-20 76: "/add" => {},
fae13a0e55 2025-06-28 77: _ => bail!("Passing {command} is not possible here."),
e624ef9d66 2025-04-20 78: };
e624ef9d66 2025-04-20 79: let (channel, url, iv_hash, url_re) = (
fae13a0e55 2025-06-28 80: i_words.next().context(at_least)?,
fae13a0e55 2025-06-28 81: i_words.next().context(at_least)?,
fae13a0e55 2025-06-28 82: i_words.next(),
fae13a0e55 2025-06-28 83: i_words.next());
fae13a0e55 2025-06-28 84: /*
fae13a0e55 2025-06-28 85: let channel = match RE_USERNAME.captures(channel) {
fae13a0e55 2025-06-28 86: Some(caps) => match caps.get(1) {
fae13a0e55 2025-06-28 87: Some(data) => data.as_str(),
fae13a0e55 2025-06-28 88: None => bail!("No string found in channel name"),
fae13a0e55 2025-06-28 89: },
fae13a0e55 2025-06-28 90: None => {
fae13a0e55 2025-06-28 91: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}");
fae13a0e55 2025-06-28 92: },
fae13a0e55 2025-06-28 93: };
fae13a0e55 2025-06-28 94: */
e624ef9d66 2025-04-20 95: if ! RE_USERNAME.is_match(channel) {
e624ef9d66 2025-04-20 96: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}");
e624ef9d66 2025-04-20 97: };
e624ef9d66 2025-04-20 98: if ! RE_LINK.is_match(url) {
e624ef9d66 2025-04-20 99: bail!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {url:?}");
e624ef9d66 2025-04-20 100: }
e624ef9d66 2025-04-20 101: let iv_hash = match iv_hash {
e624ef9d66 2025-04-20 102: Some(hash) => {
bb89b6fab8 2025-06-15 103: match hash.as_ref() {
e624ef9d66 2025-04-20 104: "-" => None,
e624ef9d66 2025-04-20 105: thing => {
e624ef9d66 2025-04-20 106: if ! RE_IV_HASH.is_match(thing) {
e624ef9d66 2025-04-20 107: bail!("IV hash should be 14 hex digits.\nNot {thing:?}");
613a665847 2021-11-15 108: };
613a665847 2021-11-15 109: Some(thing)
613a665847 2021-11-15 110: },
c1e27b74ed 2021-11-13 111: }
10c25017bb 2021-11-13 112: },
10c25017bb 2021-11-13 113: None => None,
10c25017bb 2021-11-13 114: };
10c25017bb 2021-11-13 115: let url_re = match url_re {
10c25017bb 2021-11-13 116: Some(re) => {
bb89b6fab8 2025-06-15 117: match re.as_ref() {
c1e27b74ed 2021-11-13 118: "-" => None,
613a665847 2021-11-15 119: thing => {
613a665847 2021-11-15 120: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
613a665847 2021-11-15 121: Some(thing)
613a665847 2021-11-15 122: }
c1e27b74ed 2021-11-13 123: }
10c25017bb 2021-11-13 124: },
10c25017bb 2021-11-13 125: None => None,
10c25017bb 2021-11-13 126: };
fae13a0e55 2025-06-28 127: let chat_id = ChatUsername::from(channel.as_ref());
44575a91d3 2025-07-09 128: let channel_id = core.tg.execute(GetChat::new(chat_id.clone())).await.stack_err("gettting GetChat")?.id;
bb89b6fab8 2025-06-15 129: let chan_adm = core.tg.execute(GetChatAdministrators::new(chat_id)).await
bb89b6fab8 2025-06-15 130: .context("Sorry, I have no access to that chat.")?;
10c25017bb 2021-11-13 131: let (mut me, mut user) = (false, false);
10c25017bb 2021-11-13 132: for admin in chan_adm {
e624ef9d66 2025-04-20 133: let member_id = match admin {
e624ef9d66 2025-04-20 134: ChatMember::Creator(member) => member.user.id,
e624ef9d66 2025-04-20 135: ChatMember::Administrator(member) => member.user.id,
e624ef9d66 2025-04-20 136: ChatMember::Left(_)
e624ef9d66 2025-04-20 137: | ChatMember::Kicked(_)
bb89b6fab8 2025-06-15 138: | ChatMember::Member{..}
e624ef9d66 2025-04-20 139: | ChatMember::Restricted(_) => continue,
bb89b6fab8 2025-06-15 140: };
bb89b6fab8 2025-06-15 141: if member_id == core.me.id {
e624ef9d66 2025-04-20 142: me = true;
bb89b6fab8 2025-06-15 143: }
e624ef9d66 2025-04-20 144: if member_id == sender {
e624ef9d66 2025-04-20 145: user = true;
bb89b6fab8 2025-06-15 146: }
10c25017bb 2021-11-13 147: };
10c25017bb 2021-11-13 148: if ! me { bail!("I need to be admin on that channel."); };
10c25017bb 2021-11-13 149: if ! user { bail!("You should be admin on that channel."); };
44575a91d3 2025-07-09 150: let mut conn = core.db.begin().await.stack()?;
44575a91d3 2025-07-09 151: core.send(conn.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await.stack()?, Some(msg.chat.get_id()), None).await.stack()?;
a7f91033c0 2021-11-13 152: Ok(())
9171c791eb 2021-11-13 153: }