aerogramme/src/main.rs

158 lines
4 KiB
Rust
Raw Normal View History

#![feature(async_fn_in_trait)]
mod timestamp;
2022-05-18 10:24:37 +00:00
mod bayou;
2022-05-19 10:10:48 +00:00
mod config;
2022-05-18 10:24:37 +00:00
mod cryptoblob;
2022-06-17 16:39:36 +00:00
mod imap;
2022-07-13 10:30:35 +00:00
mod k2v_util;
2022-05-31 15:07:34 +00:00
mod lmtp;
2022-05-19 10:10:48 +00:00
mod login;
2022-06-27 14:56:20 +00:00
mod mail;
2022-05-19 13:14:36 +00:00
mod server;
2023-10-30 17:07:40 +00:00
mod storage;
2022-05-18 10:24:37 +00:00
2022-05-19 13:14:36 +00:00
use std::path::PathBuf;
2023-11-23 14:04:47 +00:00
use anyhow::Result;
2022-05-19 13:14:36 +00:00
use clap::{Parser, Subcommand};
2022-05-19 10:10:48 +00:00
use config::*;
2022-05-19 12:33:49 +00:00
use server::Server;
2022-05-18 10:24:37 +00:00
2022-05-19 13:14:36 +00:00
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
#[clap(subcommand)]
command: Command,
2023-12-06 19:57:25 +00:00
#[clap(short, long, env = "CONFIG_FILE", default_value = "aerogramme.toml")]
config_file: PathBuf,
2022-05-19 13:14:36 +00:00
}
#[derive(Subcommand, Debug)]
enum Command {
2023-12-06 19:57:25 +00:00
#[clap(subcommand)]
Companion(CompanionCommand),
#[clap(subcommand)]
Provider(ProviderCommand),
//Test,
}
#[derive(Subcommand, Debug)]
enum CompanionCommand {
/// Runs the IMAP proxy
Daemon,
Reload {
#[clap(short, long, env = "AEROGRAMME_PID")]
pid: Option<u64>,
2023-11-24 10:44:42 +00:00
},
2023-12-06 19:57:25 +00:00
Wizard,
#[clap(subcommand)]
Account(AccountManagement),
2022-05-19 13:14:36 +00:00
}
2023-12-06 19:57:25 +00:00
#[derive(Subcommand, Debug)]
enum ProviderCommand {
/// Runs the IMAP+LMTP server daemon
Daemon,
Reload,
#[clap(subcommand)]
Account(AccountManagement),
}
#[derive(Subcommand, Debug)]
enum AccountManagement {
Add {
#[clap(short, long)]
login: String,
#[clap(short, long)]
setup: PathBuf,
},
Delete {
#[clap(short, long)]
login: String,
},
ChangePassword {
#[clap(short, long)]
login: String
},
}
2022-05-18 10:24:37 +00:00
#[tokio::main]
2022-05-19 13:14:36 +00:00
async fn main() -> Result<()> {
2022-05-20 11:36:45 +00:00
if std::env::var("RUST_LOG").is_err() {
std::env::set_var("RUST_LOG", "main=info,aerogramme=info,k2v_client=info")
2022-05-20 11:36:45 +00:00
}
2022-06-02 15:59:29 +00:00
2022-06-29 18:00:38 +00:00
// Abort on panic (same behavior as in Go)
std::panic::set_hook(Box::new(|panic_info| {
2023-05-15 16:23:23 +00:00
eprintln!("{}", panic_info);
2022-07-15 14:15:48 +00:00
eprintln!("{:?}", backtrace::Backtrace::new());
2022-06-29 18:00:38 +00:00
std::process::abort();
}));
2022-06-02 15:59:29 +00:00
tracing_subscriber::fmt::init();
2022-05-20 11:36:45 +00:00
2022-05-19 13:14:36 +00:00
let args = Args::parse();
2023-12-06 19:57:25 +00:00
let any_config = read_config(args.config_file)?;
2022-05-19 13:14:36 +00:00
2023-12-06 19:57:25 +00:00
match (args.command, any_config) {
(Command::Companion(subcommand), AnyConfig::Companion(config)) => match subcommand {
CompanionCommand::Daemon => {
let server = Server::from_companion_config(config).await?;
server.run().await?;
},
CompanionCommand::Reload { pid } => {
unimplemented!();
},
CompanionCommand::Wizard => {
unimplemented!();
},
CompanionCommand::Account(cmd) => {
let user_file = config.users.user_list;
account_management(cmd, user_file);
}
},
(Command::Provider(subcommand), AnyConfig::Provider(config)) => match subcommand {
ProviderCommand::Daemon => {
let server = Server::from_provider_config(config).await?;
server.run().await?;
},
ProviderCommand::Reload => {
unimplemented!();
},
ProviderCommand::Account(cmd) => {
let user_file = match config.users {
UserManagement::Static(conf) => conf.user_list,
UserManagement::Ldap(_) => panic!("LDAP account management is not supported from Aerogramme.")
};
account_management(cmd, user_file);
}
},
(Command::Provider(_), AnyConfig::Companion(_)) => {
panic!("Your want to run a 'Provider' command but your configuration file has role 'Companion'.");
},
(Command::Companion(_), AnyConfig::Provider(_)) => {
panic!("Your want to run a 'Companion' command but your configuration file has role 'Provider'.");
},
2022-05-19 10:10:48 +00:00
}
2022-05-19 13:14:36 +00:00
Ok(())
2022-05-18 10:24:37 +00:00
}
2023-12-06 19:57:25 +00:00
fn account_management(cmd: AccountManagement, users: PathBuf) {
match cmd {
Add => {
unimplemented!();
},
Delete => {
unimplemented!();
},
ChangePassword => {
unimplemented!();
},
}
}