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