Lines of
src/command.rs
from check-in cab82b7ff4
that are changed by the sequence of edits moving toward
check-in 409eb5bafc:
1: use anyhow::{bail, Context, Result};
2: use crate::core::Core;
3: use lazy_static::lazy_static;
4: use regex::Regex;
5: use sedregex::ReplaceCommand;
6: use std::borrow::Cow;
7:
8: lazy_static! {
9: static ref RE_USERNAME: Regex = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$").unwrap();
10: static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.0-9/?=]+$").unwrap();
11: static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
12: }
13:
14: pub async fn start(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
15: core.send("We are open. Probably. Visit [channel](https://t.me/rsstg_bot_help/3) for details.", Some(sender), None).await?;
16: Ok(())
17: }
18:
19: pub async fn list(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
20: core.send(core.list(sender).await?, Some(sender), Some(telegram_bot::types::ParseMode::MarkdownV2)).await?;
21: Ok(())
22: }
23:
24: pub async fn command(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
cab82b7ff4 2022-03-23 25: let msg: Cow<str> = match &command[1].parse::<i32>() {
cab82b7ff4 2022-03-23 26: Err(err) => format!("I need a number.\n{}", &err).into(),
cab82b7ff4 2022-03-23 27: Ok(number) => match command[0] {
cab82b7ff4 2022-03-23 28: "/check" => core.check(number, sender, false).await
cab82b7ff4 2022-03-23 29: .context("Channel check failed.")?,
cab82b7ff4 2022-03-23 30: "/clean" => core.clean(number, sender).await?,
cab82b7ff4 2022-03-23 31: "/enable" => core.enable(number, sender).await?.into(),
cab82b7ff4 2022-03-23 32: "/delete" => core.delete(number, sender).await?,
cab82b7ff4 2022-03-23 33: "/disable" => core.disable(number, sender).await?.into(),
cab82b7ff4 2022-03-23 34: _ => bail!("Command {} not handled.", &command[0]),
cab82b7ff4 2022-03-23 35: },
cab82b7ff4 2022-03-23 36: };
cab82b7ff4 2022-03-23 37: core.send(msg, Some(sender), None).await?;
38: Ok(())
39: }
40:
41: pub async fn update(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
42: let mut source_id: Option<i32> = None;
43: let at_least = "Requires at least 3 parameters.";
44: let mut i_command = command.iter();
45: let first_word = i_command.next().context(at_least)?;
46: match *first_word {
47: "/update" => {
48: let next_word = i_command.next().context(at_least)?;
49: source_id = Some(next_word.parse::<i32>()
50: .context(format!("I need a number, but got {}.", next_word))?);
51: },
52: "/add" => {},
53: _ => bail!("Passing {} is not possible here.", first_word),
54: };
55: let (channel, url, iv_hash, url_re) = (
56: i_command.next().context(at_least)?,
57: i_command.next().context(at_least)?,
58: i_command.next(),
59: i_command.next());
60: if ! RE_USERNAME.is_match(channel) {
61: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {:?}", &channel);
62: };
63: if ! RE_LINK.is_match(url) {
64: bail!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {:?}", &url);
65: }
66: let iv_hash = match iv_hash {
67: Some(hash) => {
68: match *hash {
69: "-" => None,
70: thing => {
71: if ! RE_IV_HASH.is_match(thing) {
72: bail!("IV hash should be 14 hex digits.\nNot {:?}", thing);
73: };
74: Some(thing)
75: },
76: }
77: },
78: None => None,
79: };
80: let url_re = match url_re {
81: Some(re) => {
82: match *re {
83: "-" => None,
84: thing => {
85: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
86: Some(thing)
87: }
88: }
89: },
90: None => None,
91: };
92: let channel_id = i64::from(core.tg.send(telegram_bot::GetChat::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await?.id());
93: let chan_adm = core.tg.send(telegram_bot::GetChatAdministrators::new(telegram_bot::ChatRef::ChannelUsername(channel.to_string()))).await
94: .context("Sorry, I have no access to that chat.")?;
95: let (mut me, mut user) = (false, false);
96: for admin in chan_adm {
97: if admin.user.id == core.my.id {
98: me = true;
99: };
100: if admin.user.id == sender {
101: user = true;
102: };
103: };
104: if ! me { bail!("I need to be admin on that channel."); };
105: if ! user { bail!("You should be admin on that channel."); };
106: core.send(core.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await?, Some(sender), None).await?;
107: Ok(())
108: }