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