Lines of
src/command.rs
from check-in 659724c658
that are changed by the sequence of edits moving toward
check-in 4b64438e28:
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<()> {
659724c658 2021-12-08 14: core.send("We are open\\. Probably\\. Visit [channel](https://t.me/rsstg_bot_help/3) for details\\.", Some(sender), None)?;
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))?;
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>() {
659724c658 2021-12-08 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: }, Some(sender), None)?;
36: Ok(())
37: }
38:
39: pub async fn update(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
40: let mut source_id: Option<i32> = None;
41: let at_least = "Requires at least 3 parameters.";
42: let first_word = command[0];
43: let command = match first_word {
44: "/update" => {
45: source_id = Some(command[1].parse::<i32>()
46: .context(format!("I need a number, but got {}.", command[1]))?);
47: &command[2..]
48: },
49: "/add" => &command[1..],
50: _ => bail!("Passing {} is not possible here.", command[1]),
51: };
52: let mut i_command = command.into_iter();
53: let (channel, url, iv_hash, url_re) = (
54: i_command.next().context(at_least)?,
55: i_command.next().context(at_least)?,
56: i_command.next(),
57: i_command.next());
58: if ! RE_USERNAME.is_match(&channel) {
59: bail!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {:?}", &channel);
60: };
61: if ! RE_LINK.is_match(&url) {
62: bail!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {:?}", &url);
63: }
64: let iv_hash = match iv_hash {
65: Some(hash) => {
66: match *hash {
67: "-" => None,
68: thing => {
69: if ! RE_IV_HASH.is_match(thing) {
70: bail!("IV hash should be 14 hex digits.\nNot {:?}", thing);
71: };
72: Some(thing)
73: },
74: }
75: },
76: None => None,
77: };
78: let url_re = match url_re {
79: Some(re) => {
80: match *re {
81: "-" => None,
82: thing => {
83: let _url_rex = ReplaceCommand::new(thing).context("Regexp parsing error:")?;
84: Some(thing)
85: }
86: }
87: },
88: None => None,
89: };
90: let s_channel = &channel.to_string();
91: let channel_id = i64::from(core.tg.send(telegram_bot::GetChat::new(telegram_bot::types::ChatRef::ChannelUsername(s_channel.into()))).await?.id());
92: let chan_adm = core.tg.send(telegram_bot::GetChatAdministrators::new(telegram_bot::types::ChatRef::ChannelUsername(s_channel.into()))).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)?;
106: Ok(())
107: }