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