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
|
use std::{
fs::File,
io::Cursor,
time::{Duration, UNIX_EPOCH},
};
use anyhow::Result;
use rpgpie::{
certificate::Certificate,
message::{SignatureMode, encrypt},
policy::Seipd,
};
use tar::Builder;
use teloxide::{Bot, prelude::Requester, types::InputFile};
use tokio::time::sleep;
const MAX_FILE_SIZE: usize = 50000000;
#[tokio::main]
async fn main() -> Result<()> {
let chat_id = std::env::var("CHAT_ID").unwrap();
let tar_data = archive("test")?;
let encrypted_tar = encrypt_data("pub.txt", tar_data)?;
let bot = Bot::from_env();
let documents: Vec<InputFile> = encrypted_tar
.chunks(MAX_FILE_SIZE)
.enumerate()
.map(|(part, data)| InputFile::memory(data.to_vec()).file_name(format!("part_{:06}", part)))
.collect();
bot.send_message(
chat_id.clone(),
format!("{}", UNIX_EPOCH.elapsed()?.as_secs().to_string()),
)
.await?;
for x in documents {
loop {
match bot.send_document(chat_id.clone(), x.clone()).await {
Ok(_) => break,
Err(teloxide::RequestError::RetryAfter(n)) => {
println!("Awaiting {} seconds", n.seconds());
sleep(Duration::from_secs(n.seconds() as u64)).await
}
Err(err) => {
println!("{}\nAwaiting 8 seconds", err);
sleep(Duration::from_secs(8)).await
}
}
}
}
Ok(())
}
fn archive(location: &str) -> Result<Vec<u8>> {
let mut b = Builder::new(Vec::new());
b.append_dir_all(location, location)?;
Ok(b.into_inner()?)
}
fn encrypt_data(key_file: &str, message: Vec<u8>) -> Result<Vec<u8>> {
let rec = Certificate::load(&mut File::open(key_file)?)?;
let mut plaintext = Cursor::new(message);
let mut output = Cursor::new(Vec::new());
encrypt(
Some(Seipd::SEIPD2),
rec,
Vec::new(),
Vec::new(),
&Vec::new(),
&mut plaintext,
SignatureMode::Binary,
&mut output,
false,
)?;
Ok(output.into_inner())
}
|