1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
use anyhow::{bail, Context, Result};
use crate::core::Core;
use regex::Regex;
use sedregex::ReplaceCommand;
use telegram_bot;
lazy_static! {
static ref RE_USERNAME: Regex = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$").unwrap();
static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.0-9/?=]+$").unwrap();
static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
}
pub async fn start(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
core.send("We are open\\. Probably\\. Visit [channel](https://t.me/rsstg_bot_help/3) for details\\.", Some(sender))?;
Ok(())
}
pub async fn list(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
core.send(core.list(sender).await?.join("\n"), Some(sender))?;
Ok(())
}
pub async fn command(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
core.send( match &command[1].parse::<i32>() {
Err(err) => format!("I need a number\\.\n{}", &err),
Ok(number) => match command[0] {
"/check" => core.check(&number, sender, false).await
.context("Channel check failed.")?,
"/clean" => core.clean(&number, sender).await?,
"/enable" => core.enable(&number, sender).await?
.to_string(),
"/delete" => core.delete(&number, sender).await?,
"/disable" => core.disable(&number, sender).await?
.to_string(),
_ => bail!("Command {} not handled.", &command[0]),
},
}, Some(sender))?;
Ok(())
}
pub async fn update(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
let mut source_id: Option<i32> = None;
let at_least = "Requires at least 3 parameters.";
let first_word = command[0];
|
|
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
use anyhow::{bail, Context, Result};
use crate::core::Core;
use regex::Regex;
use sedregex::ReplaceCommand;
use telegram_bot;
lazy_static! {
static ref RE_USERNAME: Regex = Regex::new(r"^@[a-zA-Z][a-zA-Z0-9_]+$").unwrap();
static ref RE_LINK: Regex = Regex::new(r"^https?://[a-zA-Z.0-9-]+/[-_a-zA-Z.0-9/?=]+$").unwrap();
static ref RE_IV_HASH: Regex = Regex::new(r"^[a-f0-9]{14}$").unwrap();
}
pub async fn start(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
core.send("We are open\\. Probably\\. Visit [channel](https://t.me/rsstg_bot_help/3) for details\\.", Some(sender), None)?;
Ok(())
}
pub async fn list(core: &Core, sender: telegram_bot::UserId) -> Result<()> {
core.send(core.list(sender).await?, Some(sender), Some(telegram_bot::types::ParseMode::MarkdownV2))?;
Ok(())
}
pub async fn command(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
core.send( match &command[1].parse::<i32>() {
Err(err) => format!("I need a number\\.\n{}", &err),
Ok(number) => match command[0] {
"/check" => core.check(&number, sender, false).await
.context("Channel check failed.")?,
"/clean" => core.clean(&number, sender).await?,
"/enable" => core.enable(&number, sender).await?
.to_string(),
"/delete" => core.delete(&number, sender).await?,
"/disable" => core.disable(&number, sender).await?
.to_string(),
_ => bail!("Command {} not handled.", &command[0]),
},
}, Some(sender), None)?;
Ok(())
}
pub async fn update(core: &Core, sender: telegram_bot::UserId, command: Vec<&str>) -> Result<()> {
let mut source_id: Option<i32> = None;
let at_least = "Requires at least 3 parameters.";
let first_word = command[0];
|
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
let mut i_command = command.into_iter();
let (channel, url, iv_hash, url_re) = (
i_command.next().context(at_least)?,
i_command.next().context(at_least)?,
i_command.next(),
i_command.next());
if ! RE_USERNAME.is_match(&channel) {
core.send(format!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {:?}", &channel), Some(sender))?;
return Ok(())
}
if ! RE_LINK.is_match(&url) {
core.send(format!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {:?}", &url), Some(sender))?;
return Ok(())
}
let iv_hash = match iv_hash {
Some(hash) => {
if ! RE_IV_HASH.is_match(hash) {
core.send(format!("IV hash should be 14 hex digits.\nNot {:?}", hash), Some(sender))?;
return Ok(())
};
Some(*hash)
}
None => None,
};
if let Some(rex) = url_re {
|
|
|
|
|
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
let mut i_command = command.into_iter();
let (channel, url, iv_hash, url_re) = (
i_command.next().context(at_least)?,
i_command.next().context(at_least)?,
i_command.next(),
i_command.next());
if ! RE_USERNAME.is_match(&channel) {
core.send(format!("Usernames should be something like \"@\\[a\\-zA\\-Z]\\[a\\-zA\\-Z0\\-9\\_]+\", aren't they?\nNot {:?}", &channel), Some(sender), None)?;
return Ok(())
}
if ! RE_LINK.is_match(&url) {
core.send(format!("Link should be a link to atom/rss feed, something like \"https://domain/path\".\nNot {:?}", &url), Some(sender), None)?;
return Ok(())
}
let iv_hash = match iv_hash {
Some(hash) => {
if ! RE_IV_HASH.is_match(hash) {
core.send(format!("IV hash should be 14 hex digits.\nNot {:?}", hash), Some(sender), None)?;
return Ok(())
};
Some(*hash)
}
None => None,
};
if let Some(rex) = url_re {
|
88
89
90
91
92
93
94
95
96
|
};
if admin.user.id == sender {
user = true;
};
};
if ! me { bail!("I need to be admin on that channel\\."); };
if ! user { bail!("You should be admin on that channel\\."); };
core.send(core.update(source_id, channel, channel_id, url, iv_hash, None, sender).await?, Some(sender))
}
|
|
>
|
88
89
90
91
92
93
94
95
96
97
|
};
if admin.user.id == sender {
user = true;
};
};
if ! me { bail!("I need to be admin on that channel\\."); };
if ! user { bail!("You should be admin on that channel\\."); };
core.send(core.update(source_id, channel, channel_id, url, iv_hash, None, sender).await?, Some(sender), None)?;
Ok(())
}
|