From 7fa0eeddf1dda44969fe45c8d8973e6830282b78 Mon Sep 17 00:00:00 2001 From: Alie Date: Sun, 21 Jan 2024 17:23:03 +0100 Subject: [PATCH] changed permision scopes and rebritten some functions to add modulatiry --- config.toml | 5 ++-- src/main.rs | 74 +++++++++++++++++++++++++++++++++++------------------ 2 files changed, 52 insertions(+), 27 deletions(-) diff --git a/config.toml b/config.toml index 956bec4..d444ea1 100644 --- a/config.toml +++ b/config.toml @@ -6,8 +6,9 @@ bio = "Bot who posts images of sleeping girls every 6 hours." [files] urls = "./urls.csv" posted = "./posted.csv" -tempfile = ".tmp" +tempfile = "/tmp/botimage.png" [errors] -out_of_images = "@Sugui@awoo.fai.st @MeDueleLaTeta@awoo.fai.st me quedé sin chicas" +maintainers = "@Sugui@awoo.fai.st @MeDueleLaTeta@awoo.fai.st" +out_of_images = "me quedé sin chicas" retry = 10 \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 91d73ff..85dc04c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,6 +43,7 @@ struct Files { #[derive(Deserialize)] struct Errors { + maintainers: String, out_of_images: String, retry: u8, } @@ -55,25 +56,9 @@ async fn main() -> DynResult<()> { .verbosity(4) // Debug .timestamp(stderrlog::Timestamp::Second) .init()?; - let config: Config = match parse_config(CONFIG_FILENAME) { - Ok(config) => config, - Err(err) => { - log::error!("Config file parsing unsuccesful: {}", err); - exit(1); - } - }; - let mastodon = if let Ok(data) = masto_toml::from_file("mastodon-data.toml") { - Mastodon::from(data) - } else { - match register(&config).await { - Ok(account) => account, - Err(err) => { - log::error!("Api registation unsuccesful: {}", err); - exit(1); - } - } - }; + let config: Config = get_config(); + let mastodon = get_account(&config).await; match get_next_url(&config) { Ok(url) => match url { @@ -106,7 +91,16 @@ async fn main() -> DynResult<()> { } None => { let mut retry: u8 = 0; - while let Err(err) = post(&mastodon, &config.errors.out_of_images).await { + while let Err(err) = post( + &mastodon, + &format!( + "{}{}", + &config.errors.maintainers, &config.errors.out_of_images + ), + Visibility::Direct, + ) + .await + { log::warn!("Cannot post, retry: {}, {}", retry, err); async_std::task::sleep(Duration::new(1, 0)).await; retry += 1; @@ -119,7 +113,13 @@ async fn main() -> DynResult<()> { }, Err(err) => { log::error!("Cannot get next image: {}", err); - match post(&mastodon, &err.to_string()).await { + match post( + &mastodon, + &format!("{}{}", &config.errors.maintainers, &err.to_string()), + Visibility::Direct, + ) + .await + { Ok(_) => {} Err(err) => { log::error!("Cannot post error message: {}", err); @@ -131,6 +131,30 @@ async fn main() -> DynResult<()> { Ok(()) } +fn get_config() -> Config { + match parse_config(CONFIG_FILENAME) { + Ok(config) => config, + Err(err) => { + log::error!("Config file parsing unsuccesful: {}", err); + exit(1); + } + } +} + +async fn get_account(config: &Config) -> Mastodon { + if let Ok(data) = masto_toml::from_file("mastodon-data.toml") { + Mastodon::from(data) + } else { + match register(config).await { + Ok(account) => account, + Err(err) => { + log::error!("Api registation unsuccesful: {}", err); + exit(1); + } + } + } +} + /// Parses the given filename to a config struct fn parse_config(filename: &str) -> DynResult { let toml_file = std::fs::read_to_string(filename)?; //.expect("No config file, consider getting the original one and modifing it"); @@ -208,14 +232,14 @@ async fn update_bio(account: &Mastodon, config: &Config) -> DynResult<()> { Ok(()) } -async fn post(account: &Mastodon, msg: &str) -> DynResult<()> { +async fn post(account: &Mastodon, msg: &str, visibility: Visibility) -> DynResult { let status = StatusBuilder::new() - .visibility(Visibility::Direct) + .visibility(visibility) .status(msg) .build()?; //.expect("Error building error status"); - account.new_status(status).await?; //.expect("Error posting error status"); + let post = account.new_status(status).await?; //.expect("Error posting error status"); log::info!("Text status posted: {}", msg); - Ok(()) + Ok(post) } async fn fetch_url(url: &String, file_name: &String) -> DynResult<()> { @@ -229,7 +253,7 @@ async fn fetch_url(url: &String, file_name: &String) -> DynResult<()> { async fn register(config: &Config) -> DynResult { let registration = Registration::new(&config.bot.instance) .client_name(&config.bot.name) - .scopes(Scopes::write_all()) + .scopes(Scopes::all()) .build() .await?; let mastodon = cli::authenticate(registration).await?;