-
Notifications
You must be signed in to change notification settings - Fork 194
/
Copy pathmain.rs
206 lines (184 loc) · 5.69 KB
/
main.rs
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
45
46
47
48
49
50
51
52
53
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#![feature(error_reporter)]
#![recursion_limit = "256"]
use std::convert::TryInto;
use std::env;
use std::panic;
use std::path::PathBuf;
use std::process;
use std::sync::Arc;
use anyhow::{anyhow, Context};
use hyper_proxy::{Intercept, Proxy};
use std::sync::OnceLock;
use structopt::StructOpt;
use tbot::bot::Uri;
use tokio::{self, sync::Mutex};
// Include the tr! macro and localizations
include!(concat!(env!("OUT_DIR"), "/ctl10n_macros.rs"));
mod client;
mod commands;
mod data;
mod feed;
mod fetcher;
mod gardener;
mod messages;
mod opml;
use crate::data::Database;
static BOT_NAME: OnceLock<String> = OnceLock::new();
static BOT_ID: OnceLock<tbot::types::user::Id> = OnceLock::new();
#[derive(Debug, StructOpt)]
#[structopt(
about = "A simple Telegram RSS bot.",
after_help = "NOTE: You can get <user id> using bots like @userinfobot @getidsbot"
)]
pub struct Opt {
/// Telegram bot token
token: String,
/// Path to database
#[structopt(
short = "d",
long,
value_name = "path",
default_value = "./rssbot.json"
)]
database: PathBuf,
/// Minimum fetch interval
#[structopt(
long,
value_name = "seconds",
default_value = "300",
validator(check_interval)
)]
// default is 5 minutes
min_interval: u32,
/// Maximum fetch interval
#[structopt(
long,
value_name = "seconds",
default_value = "43200",
validator(check_interval)
)]
// default is 12 hours
max_interval: u32,
/// Maximum feed size, 0 is unlimited
#[structopt(long, value_name = "bytes", default_value = "2M")]
max_feed_size: String,
/// Private mode, only specified user can use this bot.
/// This argument can be passed multiple times to allow multiple admins
#[structopt(
long,
value_name = "user id",
number_of_values = 1,
alias = "single_user" // For compatibility
)]
admin: Vec<i64>,
/// Make bot commands only accessible for group admins.
#[structopt(long)]
restricted: bool,
/// Custom telegram api URI
#[structopt(
long,
value_name = "tgapi-uri",
default_value = "https://api.telegram.org/"
)]
api_uri: Uri,
/// DANGER: Insecure mode, accept invalid TLS certificates
#[structopt(long)]
insecure: bool,
}
fn check_interval(s: String) -> Result<(), String> {
s.parse::<u32>().map_err(|e| e.to_string()).and_then(|r| {
if r < 1 {
Err("must >= 1".into())
} else {
Ok(())
}
})
}
/// Parse human readable size into bytes.
fn parse_human_size(s: &str) -> anyhow::Result<u64> {
const BASE: u64 = 1024;
let s = s.trim().trim_end_matches(|x| x == 'B' || x == 'b');
match s.chars().last().map(|x| x.to_ascii_lowercase()) {
Some('b') => Ok(s[..s.len() - 1].parse()?),
Some('k') => Ok(s[..s.len() - 1].parse::<u64>()? * BASE),
Some('m') => Ok(s[..s.len() - 1].parse::<u64>()? * BASE.pow(2)),
Some('g') => Ok(s[..s.len() - 1].parse::<u64>()? * BASE.pow(3)),
Some('t') => Ok(s[..s.len() - 1].parse::<u64>()? * BASE.pow(4)),
Some(x) if x.is_ascii_digit() => Ok(s.parse()?),
Some(x) => Err(anyhow!("invalid size character: {}", x)),
None => Err(anyhow!("empty size")),
}
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
enable_fail_fast();
let opt = Opt::from_args();
let db = Arc::new(Mutex::new(Database::open(opt.database.clone())?));
let bot_builder =
tbot::bot::Builder::with_string_token(opt.token.clone()).server_uri(opt.api_uri.clone());
let bot = if let Some(proxy) = init_proxy() {
bot_builder.proxy(proxy).build()
} else {
bot_builder.build()
};
let me = bot
.get_me()
.call()
.await
.context("Initialization failed, check your network and Telegram token")?;
let bot_name = me.user.username.clone().unwrap();
crate::client::init_client(
&bot_name,
opt.insecure,
parse_human_size(&opt.max_feed_size).context("Invalid max_feed_size")?,
);
BOT_NAME.set(bot_name).unwrap();
BOT_ID.set(me.user.id).unwrap();
gardener::start_pruning(bot.clone(), db.clone());
fetcher::start(bot.clone(), db.clone(), opt.min_interval, opt.max_interval);
let opt = Arc::new(opt);
let mut event_loop = bot.event_loop();
event_loop.username(me.user.username.unwrap());
commands::register_commands(&mut event_loop, opt, db);
event_loop.polling().start().await.unwrap();
Ok(())
}
// Exit the process when any worker thread panicked
fn enable_fail_fast() {
let default_panic_hook = panic::take_hook();
panic::set_hook(Box::new(move |e| {
default_panic_hook(e);
process::exit(101);
}));
}
fn init_proxy() -> Option<Proxy> {
// Telegram Bot API only uses https, no need to check http_proxy
env::var("HTTPS_PROXY")
.or_else(|_| env::var("https_proxy"))
.map(|uri| {
let uri = uri
.try_into()
.unwrap_or_else(|e| panic!("Illegal HTTPS_PROXY: {}", e));
Proxy::new(Intercept::All, uri)
})
.ok()
}
fn print_error<E: std::error::Error>(err: E) {
eprintln!(
"Error: {}",
std::error::Report::new(err)
.pretty(true)
.show_backtrace(true)
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_parse_human_size() {
assert_eq!(parse_human_size("2M").unwrap(), 2_097_152);
assert_eq!(parse_human_size("2G").unwrap(), 2_147_483_648);
assert_eq!(parse_human_size("2mb").unwrap(), 2_097_152);
assert_eq!(parse_human_size("2097152").unwrap(), 2_097_152);
}
}