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