feat: implement message saving

This commit is contained in:
Patrick 2024-09-06 00:49:52 +02:00
parent a45e1ca7ee
commit cea456db6c
Signed by: patrick
GPG key ID: 451F95EFB8BECD0F

View file

@ -1,4 +1,5 @@
use std::fs::{create_dir, write};
use std::fs::{create_dir_all, write, OpenOptions};
use std::io::Write;
use std::path::Path;
use std::process::Stdio;
@ -25,11 +26,18 @@ struct Config {
///The allowed sender
#[arg(short, long)]
allowed_sender: String,
///The data folder to use for temporary storage
#[arg(short, long)]
data_folder: String,
}
#[tokio::main]
async fn main() -> Result<()> {
let config = Config::parse();
if !Path::new(&config.data_folder).exists() {
create_dir_all(&config.data_folder)?
}
let mut cmd = Command::new(COMMAND_PATH)
.arg("jsonRpc")
@ -49,8 +57,9 @@ async fn main() -> Result<()> {
let v = stream.next().await.unwrap()?;
let sender = get_msg_sender(&v);
let text = get_msg_text(&v);
let at = get_msg_attachments(&mut client, &v).await;
println!("{:?}", sender);
if text.is_none() {
if text.is_none() && at.is_none() {
continue;
} else if sender != Some(&config.allowed_sender) {
if let Some(x) = sender {
@ -84,24 +93,29 @@ async fn main() -> Result<()> {
continue;
};
println!("{v}");
println!("{:?}", get_msg_text(&v));
let at = get_msg_attachments(&mut client, &v).await;
if let Some(i) = at {
for (id, content) in i {
save_picture(id, &content)?;
}
}
println!("{:?}", text);
let mut file = OpenOptions::new()
.append(true)
.create(true)
.open(format!("{}/texts.json", config.data_folder))?;
writeln!(file, "{}", v.to_string())?;
//let at = get_msg_attachments(&mut client, &v).await;
//if let Some(i) = at {
// for (id, content) in i {
// let path = format!("{}/{}", &config.data_folder, id);
// save_picture(&path, &content)?;
// }
//}
}
//stream.unsubscribe().await?;
//Ok(())
}
/// make sure the directory exists before trying to save into it
fn save_picture(name: &str, content: &str) -> Result<()> {
if !Path::new("./data/").exists() {
create_dir("./data/")?
}
write(format!("./data/{}", name), BASE64_STANDARD.decode(content)?)?;
write(format!("{}", name), BASE64_STANDARD.decode(content)?)?;
Ok(())
}