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