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.

244 lines
7.7 KiB

use crate::db_structs::*;
use crate::globals::FastConfig;
use crate::helper_structs::*;
use crate::term::*;
use crate::texts::*;
use crate::crossterm::style::Colorize;
use crate::crossterm::style::Styler;
use std::{thread, time};
pub fn emaildom_main(fast_config: FastConfig) -> usize {
display_template();
// display domain name list
let all_domains = EmailDomains::get_all();
println!(
"{:<5}{:<25}{:<14}{:<10}",
"ID", "Domain name", "Free access", "Owner"
);
for domain in all_domains.iter() {
let display = DisplayDomain::from_domain(domain);
println!(
"{:<5}{:<25}{:<14}{:<10}",
display.id, display.fqdn, display.free_access, display.owner
);
}
println!("\n\n");
display_menu(&EMAILDOM_MAIN_TXT, fast_config)
}
pub fn emaildom_add(fast_config: FastConfig) -> usize {
display_template();
print_title(&EMAILDOM_MAIN_TXT, 0);
let fqdn = input_line("Enter the domain's FQDN (ex.: 42l.fr)")
.trim()
.to_owned();
if fqdn.len() < 4 || !fqdn.contains('.') {
println!("Invalid input. Please retry.");
thread::sleep(time::Duration::from_millis(2000));
return emaildom_main(fast_config);
}
let access = match input_line("Is the domain in free access? (y/n)").trim() {
"y" => true,
"n" => false,
_ => {
println!("Invalid input. Please retry.");
thread::sleep(time::Duration::from_millis(2000));
return emaildom_main(fast_config);
}
};
let owner = if let Some(user) = User::get(input_line("Enter the owner's username:").trim()) {
user.id
} else {
println!("User not found. Please retry.");
thread::sleep(time::Duration::from_millis(2000));
return emaildom_main(fast_config);
};
EmailDomains::insert(fqdn, access, owner);
println!("{}", "Entry updated.".green().bold());
thread::sleep(time::Duration::from_millis(1000));
emaildom_main(fast_config)
}
pub fn emaildom_manage(fast_config: FastConfig) -> usize {
let question = "Enter a domain ID from the list above:";
let domain = if let Ok(id) = input_line(question).trim().parse::<i32>() {
if let Some(dom) = EmailDomains::get_by_id(id) {
dom
} else {
println!("Domain ID not found in database. Please retry.");
thread::sleep(time::Duration::from_millis(2000));
return emaildom_main(fast_config);
}
} else {
println!("You must enter a number. Please retry.");
thread::sleep(time::Duration::from_millis(2000));
return emaildom_main(fast_config);
};
emaildom_display_panel(FastConfig {
user: fast_config.user,
domain: Some(&domain),
mail_cache: fast_config.mail_cache,
})
}
pub fn emaildom_display_panel(fast_config: FastConfig) -> usize {
display_template();
print_title(&EMAILDOM_MAIN_TXT, 1);
let domain = fast_config.domain.unwrap();
let display = DisplayDomain::from_domain(&domain);
println!(
"\tID: {}\n\
\tName: {}\n\
\tFree access: {}\n\
\tOwner: {}\n\
\tAddresses created with this domain: {}\n",
display.id,
display.fqdn,
display.free_access,
display.owner,
domain.get_created_addresses()
);
let userlist = EmailGroups::get_allowed_users(display.id);
println!("\tAllowed users for this domain:");
for u in userlist {
println!("\t\t- {}", u.username_plain);
}
println!("\n\n");
display_menu(&DOMMAN_TXT, fast_config)
}
// adds an user to the allowed users list for a domain
pub fn domman_adduser(fast_config: FastConfig) -> usize {
let new_user = if let Some(user) = User::get(input_line("Enter an username:").trim()) {
user.id
} else {
println!("User not found. Please retry.");
thread::sleep(time::Duration::from_millis(2000));
return emaildom_main(fast_config);
};
let dom = fast_config.domain.unwrap();
if EmailGroups::check_exists(dom.id, new_user) {
println!("User is already in the list. Please retry.");
thread::sleep(time::Duration::from_millis(2000));
return emaildom_main(fast_config);
}
EmailGroups::add_user(dom.id, new_user);
println!("{}", "Entry updated.".green().bold());
thread::sleep(time::Duration::from_millis(1000));
emaildom_main(fast_config)
}
// removes an user from the allowed users list for a domain
pub fn domman_remuser(fast_config: FastConfig) -> usize {
let new_user = if let Some(user) = User::get(input_line("Enter an username:").trim()) {
user
} else {
println!("User not found. Please retry.");
thread::sleep(time::Duration::from_millis(2000));
return emaildom_main(fast_config);
};
println!("Warning : All addresses created by removed users will be kept.");
let question = format!(
"Are you sure you want to remove {}? [y/N]",
new_user.username_plain
);
if input_line(&question).trim().to_lowercase() == "y" {
let dom = fast_config.domain.unwrap();
if !EmailGroups::check_exists(dom.id, new_user.id) {
println!("User isn't in the allowed user list. Please retry.");
thread::sleep(time::Duration::from_millis(2000));
return emaildom_main(fast_config);
}
EmailGroups::del_by_id(dom.id, new_user.id);
println!("{}", "Entry updated.".green().bold());
thread::sleep(time::Duration::from_millis(1000));
}
emaildom_main(fast_config)
}
// empties the allowed users list for a domain
pub fn domman_remall(fast_config: FastConfig) -> usize {
let question = "Are you sure you want to remove EVERYONE? [y/N]";
println!("Warning : All addresses created by removed users will be kept.");
if input_line(question).trim().to_lowercase() == "y" {
let dom = fast_config.domain.unwrap();
if EmailGroups::get_allowed_users(dom.id).is_empty() {
println!("The allowed user list is already empty.");
thread::sleep(time::Duration::from_millis(2000));
return emaildom_main(fast_config);
}
EmailGroups::del_all(dom.id);
println!("{}", "All entries deleted.".green().bold());
thread::sleep(time::Duration::from_millis(1000));
}
emaildom_main(fast_config)
}
// toggles free access for a domain
pub fn domman_togglefree(fast_config: FastConfig) -> usize {
let dom = fast_config.domain.unwrap();
dom.toggle_free_access();
println!("{}", "Entry updated.".green().bold());
thread::sleep(time::Duration::from_millis(1000));
emaildom_main(fast_config)
}
// changes domain owner
pub fn domman_chown(fast_config: FastConfig) -> usize {
let dom = fast_config.domain.unwrap();
let new_user = if let Some(user) = User::get(input_line("Enter an username:").trim()) {
user
} else {
println!("User not found. Please retry.");
thread::sleep(time::Duration::from_millis(2000));
return emaildom_main(fast_config);
};
dom.change_owner(new_user.id);
println!("{}", "Entry updated.".green().bold());
thread::sleep(time::Duration::from_millis(1000));
emaildom_main(fast_config)
}
// deletes domain
pub fn domman_del(fast_config: FastConfig) -> usize {
println!("The mailboxes and aliases created with this domain must be removed manually");
if input_line("Are you SURE? [y/N]").trim().to_lowercase() == "y"
&& input_line("REALLY SURE? I'm not joking. [y/N]")
.trim()
.to_lowercase()
== "y"
{
let dom = fast_config.domain.unwrap();
println!("Now deleting {}.", dom.fqdn);
dom.delete();
}
input_line("Press Enter to continue...");
emaildom_main(fast_config)
}