Lines of
src/command.rs
from check-in 9adc69d514
that are changed by the sequence of edits moving toward
check-in 374eadef45:
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<()> {
34: core.tg.send(MyMessage::text_to(
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()?;
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()?;
57: let kb = get_kb(&Callback::list("", 0), feeds).await.stack()?;
9adc69d514 2026-03-25 58: core.tg.send(MyMessage::html_to_kb(format!("List of feeds owned by {:?}:", msg.sender), 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: core.rm_feed(sender.into(), &number).await.stack()?;
9adc69d514 2026-03-25 88: conn.delete(number, sender).await.stack()?
89: }
90: "/disable" => conn.disable(number, sender).await.stack()?.into(),
91: _ => bail!("Command {command} {words:?} not handled."),
92: },
93: }
94: } else {
95: "This command needs exactly one number.".into()
96: };
97: core.tg.send(MyMessage::html_to(reply, msg.chat.get_id())).await.stack()?;
98: Ok(())
99: }
100:
101: /// Validate command arguments, check permissions and update or add a channel feed configuration in the database.
102: ///
103: /// This function parses and validates parameters supplied by a user command (either "/update <id> ..." or "/add ..."),
104: /// verifies the channel username and feed URL, optionally validates an IV hash and a replacement regexp,
105: /// ensures both the bot and the command sender are administrators of the target channel, and performs the database update.
106: ///
107: /// # Parameters
108: ///
109: /// - `command` — the invoked command, expected to be either `"/update"` (followed by a numeric source id) or `"/add"`.
110: /// - `msg` — the incoming Telegram message; used to derive the command sender and target chat id for the reply.
111: /// - `words` — the command arguments: for `"/add"` expected `channel url [iv_hash|'-'] [url_re|'-']`; for `"/update"`
112: /// the first element must be a numeric `source_id` followed by the same parameters.
113: pub async fn update (core: &Core, command: &str, msg: &Message, words: &[String]) -> Result<()> {
114: let sender = msg.sender.get_user_id()
115: .stack_err("Ignoring unreal users.")?;
116: let mut source_id: Option<i32> = None;
117: let at_least = "Requires at least 3 parameters.";
118: let mut i_words = words.iter();
119: match command {
120: "/update" => {
121: let next_word = i_words.next().context(at_least)?;
122: source_id = Some(next_word.parse::<i32>()
123: .context(format!("I need a number, but got {next_word}."))?);
124: },
125: "/add" => {},
126: _ => bail!("Passing {command} is not possible here."),
127: };
128: let (channel, url, iv_hash, url_re) = (
129: i_words.next().context(at_least)?,
130: i_words.next().context(at_least)?,
131: i_words.next(),
132: i_words.next());
133: if ! RE_USERNAME.is_match(channel) {
134: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {channel:?}");
135: };
136: {
137: let parsed_url = Url::parse(url)
138: .stack_err("Expecting a valid link to ATOM/RSS feed.")?;
139: match parsed_url.scheme() {
140: "http" | "https" => {},
141: scheme => {
142: bail!("Unsupported URL scheme: {scheme}");
143: },
144: };
145: }
146: let iv_hash = match iv_hash {
147: Some(hash) => {
148: match hash.as_ref() {
149: "-" => None,
150: thing => {
151: if ! RE_IV_HASH.is_match(thing) {
152: bail!("IV hash should be 14 hex digits.\nNot {thing:?}");
153: };
154: Some(thing)
155: },
156: }
157: },
158: None => None,
159: };
160: let url_re = match url_re {
161: Some(re) => {
162: match re.as_ref() {
163: "-" => None,
164: thing => {
165: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
166: Some(thing)
167: }
168: }
169: },
170: None => None,
171: };
172: let chat_id = ChatUsername::from(channel.as_ref());
173: let channel_id = core.tg.client.execute(GetChat::new(chat_id.clone())).await.stack_err("getting GetChat")?.id;
174: let chan_adm = core.tg.client.execute(GetChatAdministrators::new(chat_id)).await
175: .context("Sorry, I have no access to that chat.")?;
176: let (mut me, mut user) = (false, false);
177: for admin in chan_adm {
178: let member_id = match admin {
179: ChatMember::Creator(member) => member.user.id,
180: ChatMember::Administrator(member) => member.user.id,
181: ChatMember::Left(_)
182: | ChatMember::Kicked(_)
183: | ChatMember::Member{..}
184: | ChatMember::Restricted(_) => continue,
185: };
186: if member_id == core.tg.me.id {
187: me = true;
188: }
189: if member_id == sender {
190: user = true;
191: }
192: };
193: if ! me { bail!("I need to be admin on that channel."); };
194: if ! user { bail!("You should be admin on that channel."); };
195: let mut conn = core.db.begin().await.stack()?;
196: let update = conn.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await.stack()?;
197: core.tg.send(MyMessage::html_to(update, msg.chat.get_id())).await.stack()?;
198: if command == "/add" {
199: if let Some(new_record) = conn.get_one_name(sender, channel).await.stack()? {
200: core.add_feed(sender.into(), new_record.source_id, new_record.channel).await.stack()?;
201: } else {
202: bail!("Failed to read data on freshly inserted source.");
203: }
204: };
205: Ok(())
206: }