Lines of
src/command.rs
from check-in 374eadef45
that are changed by the sequence of edits moving toward
check-in 3fd8c40aa8:
1: use crate::{
2: core::Core,
3: tg_bot::{
4: Callback,
5: MyMessage,
6: get_kb,
7: },
8: };
9:
10: use lazy_static::lazy_static;
11: use regex::Regex;
12: use sedregex::ReplaceCommand;
13: use stacked_errors::{
14: Result,
15: StackableErr,
16: bail,
17: };
18: use tgbot::types::{
19: ChatMember,
20: ChatUsername,
21: GetChat,
22: GetChatAdministrators,
23: Message,
24: };
25: use url::Url;
26:
27: lazy_static! {
28: static ref RE_USERNAME: Regex = Regex::new(r"^@([a-zA-Z][a-zA-Z0-9_]+)$").unwrap();
29: static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
30: }
31:
32: /// Sends an informational message to the message's chat linking to the bot help channel.
33: pub async fn start (core: &Core, msg: &Message) -> Result<()> {
374eadef45 2026-03-28 34: core.tg.send(MyMessage::text_to(
374eadef45 2026-03-28 35: "We are open\\. Probably\\. Visit [channel](https://t.me/rsstg_bot_help/3) for details\\.",
36: msg.chat.get_id()
37: )).await.stack()?;
38: Ok(())
39: }
40:
41: /// Send the sender's subscription list to the chat.
42: ///
43: /// Retrieves the message sender's user ID, obtains their subscription list from `core`,
44: /// and sends the resulting reply into the message chat using MarkdownV2.
45: pub async fn list (core: &Core, msg: &Message) -> Result<()> {
46: let sender = msg.sender.get_user_id()
47: .stack_err("Ignoring unreal users.")?;
48: let reply = core.list(sender).await.stack()?;
374eadef45 2026-03-28 49: core.tg.send(MyMessage::text_to(reply, msg.chat.get_id())).await.stack()?;
50: Ok(())
51: }
52:
53: pub async fn test (core: &Core, msg: &Message) -> Result<()> {
54: let sender: i64 = msg.sender.get_user_id()
55: .stack_err("Ignoring unreal users.")?.into();
56: let feeds = core.get_feeds(sender).await.stack()?;
374eadef45 2026-03-28 57: let kb = get_kb(&Callback::list("", 0), feeds).await.stack()?;
374eadef45 2026-03-28 58: core.tg.send(MyMessage::html_to_kb(format!("List of feeds owned by {:?}:", msg.sender.get_user_username()), msg.chat.get_id(), kb)).await.stack()?;
59: Ok(())
60: }
61:
62: /// Handle channel-management commands that operate on a single numeric source ID.
63: ///
64: /// This validates that exactly one numeric argument is provided, performs the requested
65: /// operation (check, clean, enable, delete, disable) against the database or core,
66: /// and sends the resulting reply to the chat.
67: ///
68: /// # Parameters
69: ///
70: /// - `core`: application core containing database and Telegram clients.
71: /// - `command`: command string (e.g. "/check", "/clean", "/enable", "/delete", "/disable").
72: /// - `msg`: incoming Telegram message that triggered the command; used to determine sender and chat.
73: /// - `words`: command arguments; expected to contain exactly one element that parses as a 32-bit integer.
74: pub async fn command (core: &Core, command: &str, msg: &Message, words: &[String]) -> Result<()> {
75: let mut conn = core.db.begin().await.stack()?;
76: let sender = msg.sender.get_user_id()
77: .stack_err("Ignoring unreal users.")?;
78: let reply = if words.len() == 1 {
79: match words[0].parse::<i32>() {
80: Err(err) => format!("I need a number.\n{}", &err).into(),
81: Ok(number) => match command {
82: "/check" => core.check(number, false, None).await
83: .context("Channel check failed.")?.into(),
84: "/clean" => conn.clean(number, sender).await.stack()?,
85: "/enable" => conn.enable(number, sender).await.stack()?.into(),
86: "/delete" => {
87: let res = conn.delete(number, sender).await.stack()?;
88: core.rm_feed(sender.into(), &number).await.stack()?;
89: res
90: }
91: "/disable" => conn.disable(number, sender).await.stack()?.into(),
92: _ => bail!("Command {command} {words:?} not handled."),
93: },
94: }
95: } else {
96: "This command needs exactly one number.".into()
97: };
98: core.tg.send(MyMessage::html_to(reply, msg.chat.get_id())).await.stack()?;
99: Ok(())
100: }
101:
102: /// Validate command arguments, check permissions and update or add a channel feed configuration in the database.
103: ///
104: /// This function parses and validates parameters supplied by a user command (either "/update <id> ..." or "/add ..."),
105: /// verifies the channel username and feed URL, optionally validates an IV hash and a replacement regexp,
106: /// ensures both the bot and the command sender are administrators of the target channel, and performs the database update.
107: ///
108: /// # Parameters
109: ///
110: /// - `command` — the invoked command, expected to be either `"/update"` (followed by a numeric source id) or `"/add"`.
111: /// - `msg` — the incoming Telegram message; used to derive the command sender and target chat id for the reply.
112: /// - `words` — the command arguments: for `"/add"` expected `channel url [iv_hash|'-'] [url_re|'-']`; for `"/update"`
113: /// the first element must be a numeric `source_id` followed by the same parameters.
114: pub async fn update (core: &Core, command: &str, msg: &Message, words: &[String]) -> Result<()> {
115: let sender = msg.sender.get_user_id()
116: .stack_err("Ignoring unreal users.")?;
117: let mut source_id: Option<i32> = None;
118: let at_least = "Requires at least 3 parameters.";
119: let mut i_words = words.iter();
120: match command {
121: "/update" => {
122: let next_word = i_words.next().context(at_least)?;
123: source_id = Some(next_word.parse::<i32>()
124: .context(format!("I need a number, but got {next_word}."))?);
125: },
126: "/add" => {},
127: _ => bail!("Passing {command} is not possible here."),
128: };
129: let (channel, url, iv_hash, url_re) = (
130: i_words.next().context(at_least)?,
131: i_words.next().context(at_least)?,
132: i_words.next(),
133: i_words.next());
134: if ! RE_USERNAME.is_match(channel) {
135: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}");
136: };
137: {
138: let parsed_url = Url::parse(url)
139: .stack_err("Expecting a valid link to ATOM/RSS feed.")?;
140: match parsed_url.scheme() {
141: "http" | "https" => {},
142: scheme => {
143: bail!("Unsupported URL scheme: {scheme}");
144: },
145: };
146: }
147: let iv_hash = match iv_hash {
148: Some(hash) => {
149: match hash.as_ref() {
150: "-" => None,
151: thing => {
152: if ! RE_IV_HASH.is_match(thing) {
153: bail!("IV hash should be 14 hex digits.\nNot {thing:?}");
154: };
155: Some(thing)
156: },
157: }
158: },
159: None => None,
160: };
161: let url_re = match url_re {
162: Some(re) => {
163: match re.as_ref() {
164: "-" => None,
165: thing => {
166: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
167: Some(thing)
168: }
169: }
170: },
171: None => None,
172: };
173: let chat_id = ChatUsername::from(channel.as_ref());
174: let channel_id = core.tg.client.execute(GetChat::new(chat_id.clone())).await.stack_err("getting GetChat")?.id;
175: let chan_adm = core.tg.client.execute(GetChatAdministrators::new(chat_id)).await
176: .context("Sorry, I have no access to that chat.")?;
177: let (mut me, mut user) = (false, false);
178: for admin in chan_adm {
179: let member_id = match admin {
180: ChatMember::Creator(member) => member.user.id,
181: ChatMember::Administrator(member) => member.user.id,
182: ChatMember::Left(_)
183: | ChatMember::Kicked(_)
184: | ChatMember::Member{..}
185: | ChatMember::Restricted(_) => continue,
186: };
187: if member_id == core.tg.me.id {
188: me = true;
189: }
190: if member_id == sender {
191: user = true;
192: }
193: };
194: if ! me { bail!("I need to be admin on that channel."); };
195: if ! user { bail!("You should be admin on that channel."); };
196: let mut conn = core.db.begin().await.stack()?;
197: let update = conn.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await.stack()?;
198: core.tg.send(MyMessage::html_to(update, msg.chat.get_id())).await.stack()?;
199: if command == "/add" {
200: if let Some(new_record) = conn.get_one_name(sender, channel).await.stack()? {
201: core.add_feed(sender.into(), new_record.source_id, new_record.channel).await.stack()?;
202: } else {
203: bail!("Failed to read data on freshly inserted source.");
204: }
205: };
206: Ok(())
207: }