Lines of
src/command.rs
from check-in 44575a91d3
that are changed by the sequence of edits moving toward
check-in acb0a4ac54:
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:
20: lazy_static! {
21: static ref RE_USERNAME: Regex = Regex::new(r"^@([a-zA-Z][a-zA-Z0-9_]+)$").unwrap();
22: static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.:0-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.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.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 {
44575a91d3 2025-07-09 48: "/check" => core.check(number, false).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.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: if ! RE_LINK.is_match(url) {
99: bail!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {url:?}");
100: }
101: let iv_hash = match iv_hash {
102: Some(hash) => {
103: match hash.as_ref() {
104: "-" => None,
105: thing => {
106: if ! RE_IV_HASH.is_match(thing) {
107: bail!("IV hash should be 14 hex digits.\nNot {thing:?}");
108: };
109: Some(thing)
110: },
111: }
112: },
113: None => None,
114: };
115: let url_re = match url_re {
116: Some(re) => {
117: match re.as_ref() {
118: "-" => None,
119: thing => {
120: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
121: Some(thing)
122: }
123: }
124: },
125: None => None,
126: };
127: let chat_id = ChatUsername::from(channel.as_ref());
128: let channel_id = core.tg.execute(GetChat::new(chat_id.clone())).await.stack_err("gettting GetChat")?.id;
129: let chan_adm = core.tg.execute(GetChatAdministrators::new(chat_id)).await
130: .context("Sorry, I have no access to that chat.")?;
131: let (mut me, mut user) = (false, false);
132: for admin in chan_adm {
133: let member_id = match admin {
134: ChatMember::Creator(member) => member.user.id,
135: ChatMember::Administrator(member) => member.user.id,
136: ChatMember::Left(_)
137: | ChatMember::Kicked(_)
138: | ChatMember::Member{..}
139: | ChatMember::Restricted(_) => continue,
140: };
141: if member_id == core.me.id {
142: me = true;
143: }
144: if member_id == sender {
145: user = true;
146: }
147: };
148: if ! me { bail!("I need to be admin on that channel."); };
149: if ! user { bail!("You should be admin on that channel."); };
150: let mut conn = core.db.begin().await.stack()?;
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()?;
152: Ok(())
153: }