Set of administration tools for 42l's infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.5 KiB

use crate::db_config::CONFIG;
use crate::db_structs::*;
use crate::globals::FastConfig;
use crate::helper_structs::*;
use crate::term::*;
use crate::texts::*;
use std::fs::File;
use std::io::prelude::*;
use std::{thread, time};
pub fn news_main(fast_config: FastConfig) -> usize {
display_template();
display_menu(&NEWS_MAIN_TXT, fast_config)
}
pub fn news_letter(fast_config: FastConfig) -> usize {
display_template();
print_title(&NEWS_MAIN_TXT, 0);
println!("Backend email addresses are encrypted.");
println!("The key isn't stored in the server.");
println!("You can use wassup to send emails to encrypted addresses.");
let path = format!("{}export-news.txt", &CONFIG.export_lists_path);
let question = format!("Save the email list to {} ? [y/N]", &path);
if input_line(&question).trim().to_lowercase() == "y" {
let mut file = match File::create(path) {
Ok(f) => f,
Err(e) => {
println!("Failed to create the file: {:?}", e);
thread::sleep(time::Duration::from_millis(2000));
return news_main(fast_config);
}
};
let mut export: Vec<String> = Vec::new();
for user in User::get_sub_newsletter() {
export.push(format!(
"{}|{}\n",
user.id,
base64::encode(&user.notify_email)
));
}
match file.write_all(export.into_iter().collect::<String>().as_bytes()) {
Ok(_) => println!("List exported successfully."),
Err(e) => println!("Couldn't write to file: {:?}", e),
}
} else {
println!("Operation cancelled.");
}
thread::sleep(time::Duration::from_millis(2000));
news_main(fast_config)
}
pub fn news_ag(fast_config: FastConfig) -> usize {
display_template();
print_title(&NEWS_MAIN_TXT, 1);
println!("Backend email addresses are encrypted.");
println!("The key isn't stored in the server.");
println!("You can use wassup to send emails to encrypted addresses.");
let path = format!("{}export-ag.txt", &CONFIG.export_lists_path);
let question = format!("Save the email list to {} ? [y/N]", &path);
if input_line(&question).trim().to_lowercase() == "y" {
let mut file = match File::create(path) {
Ok(f) => f,
Err(e) => {
println!("Failed to create the file: {:?}", e);
thread::sleep(time::Duration::from_millis(2000));
return news_main(fast_config);
}
};
let mut export: Vec<String> = Vec::new();
for user in User::get_sub_ag() {
export.push(format!(
"{}|{}\n",
user.id,
base64::encode(&user.notify_email)
));
}
match file.write_all(export.into_iter().collect::<String>().as_bytes()) {
Ok(_) => println!("List exported successfully."),
Err(e) => println!("Couldn't write to file: {:?}", e),
}
} else {
println!("Operation cancelled.");
}
thread::sleep(time::Duration::from_millis(2000));
news_main(fast_config)
}
pub fn news_subnotice(fast_config: FastConfig) -> usize {
display_template();
print_title(&NEWS_MAIN_TXT, 2);
println!("You can send an email to all users whose adhesion has expired.");
println!("The users will be listed if their adhesion has expired since X days.");
let days_end = input_line("How many days ? [30]");
let days_end = if let Ok(v) = days_end.trim().parse::<i64>() {
v
} else if days_end.trim().is_empty() {
30
} else {
println!("You must enter a number.");
thread::sleep(time::Duration::from_millis(2000));
return news_main(fast_config);
};
let all_users = User::get_all();
let mut sel_users: Vec<DisplayUser> = Vec::new();
for user in all_users.iter() {
let display = DisplayUser::from_user(user);
if display.adh_days.is_some()
&& display.adh_days.unwrap() < days_end
&& display.adh_days.unwrap() > 0
{
sel_users.push(display);
}
}
println!("\nThe following users will receive a notice:");
for user in sel_users.iter() {
println!(
"{:<5}{:<20}{:<20}",
user.id, user.username_plain, user.adh_status
);
}
let path = format!("{}export-expiry.txt", &CONFIG.export_lists_path);
let question = format!(
"Would you like to export the encrypted emails to {} ? [y/N]",
&path
);
if input_line(&question).trim().to_lowercase() == "y" {
let mut file = match File::create(path) {
Ok(f) => f,
Err(e) => {
println!("Failed to create the file: {:?}", e);
thread::sleep(time::Duration::from_millis(2000));
return news_main(fast_config);
}
};
let mut export: Vec<String> = Vec::new();
for user in sel_users {
export.push(format!(
"{}|{}\n",
user.id,
base64::encode(&user.notify_email)
));
}
match file.write_all(export.into_iter().collect::<String>().as_bytes()) {
Ok(_) => println!("List exported successfully."),
Err(e) => println!("Couldn't write to file: {:?}", e),
}
} else {
println!("Operation cancelled.");
}
thread::sleep(time::Duration::from_millis(2000));
news_main(fast_config)
}