Lines of
src/command.rs
from check-in c1e27b74ed
that are changed by the sequence of edits moving toward
check-in 613a665847:
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<()> {
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>() {
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: },
37: }, Some(sender), None)?;
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) {
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) => {
c1e27b74ed 2021-11-13 68: if ! RE_IV_HASH.is_match(hash) {
c1e27b74ed 2021-11-13 69: bail!("IV hash should be 14 hex digits.\nNot {:?}", hash);
c1e27b74ed 2021-11-13 70: };
71: match *hash {
72: "-" => None,
c1e27b74ed 2021-11-13 73: thing => Some(thing),
74: }
75: },
76: None => None,
77: };
78: let url_re = match url_re {
79: Some(re) => {
c1e27b74ed 2021-11-13 80: let _url_rex = ReplaceCommand::new(re).context("Regexp parsing error:")?;
81: match *re {
82: "-" => None,
c1e27b74ed 2021-11-13 83: thing => Some(thing),
84: }
85: },
86: None => None,
87: };
88: let channel_id = i64::from(core.tg.send(telegram_bot::GetChat::new(telegram_bot::types::ChatRef::ChannelUsername(channel.to_string()))).await?.id());
89: let chan_adm = core.tg.send(telegram_bot::GetChatAdministrators::new(telegram_bot::types::ChatRef::ChannelUsername(channel.to_string()))).await
90: .context("Sorry, I have no access to that chat.")?;
91: let (mut me, mut user) = (false, false);
92: for admin in chan_adm {
93: if admin.user.id == core.my.id {
94: me = true;
95: };
96: if admin.user.id == sender {
97: user = true;
98: };
99: };
100: if ! me { bail!("I need to be admin on that channel."); };
101: if ! user { bail!("You should be admin on that channel."); };
102: core.send(core.update(source_id, channel, channel_id, url, iv_hash, url_re, sender).await?, Some(sender), None)?;
103: Ok(())
104: }