1use crate::smtp::AuthMech;
8use crate::tls::Security;
9use anyhow::{Context, Result};
10use serde::{Deserialize, Serialize};
11use std::collections::BTreeMap;
12use std::path::{Path, PathBuf};
13use std::{env, fs};
14
15pub const DEFAULT_FILE_NAME: &str = "smtp_test_tool.toml";
16
17#[derive(Debug, Default, Serialize, Deserialize)]
19pub struct Config {
20 #[serde(default = "default_active")]
22 pub active: String,
23 #[serde(default)]
25 pub profiles: BTreeMap<String, Profile>,
26}
27
28fn default_active() -> String {
29 "default".into()
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct Profile {
36 #[serde(default, skip_serializing_if = "Option::is_none")]
38 pub user: Option<String>,
39
40 #[serde(skip)]
46 pub password: Option<String>,
47
48 #[serde(skip)]
52 pub oauth_token: Option<String>,
53
54 #[serde(default = "yes")]
56 pub smtp_enabled: bool,
57 pub smtp_host: String,
58 pub smtp_port: u16,
59 pub smtp_security: Security,
60 #[serde(default)]
61 pub auth_mech: AuthMech,
62
63 #[serde(default = "yes")]
65 pub imap_enabled: bool,
66 pub imap_host: String,
67 pub imap_port: u16,
68 pub imap_security: Security,
69 #[serde(default = "inbox")]
70 pub imap_folder: String,
71
72 #[serde(default)]
74 pub pop_enabled: bool,
75 pub pop_host: String,
76 pub pop_port: u16,
77 pub pop_security: Security,
78
79 #[serde(default = "yes")]
87 pub send_test: bool,
88 #[serde(default, skip_serializing_if = "Option::is_none")]
89 pub mail_from: Option<String>,
90 #[serde(default, skip_serializing_if = "Option::is_none")]
91 pub from_addr: Option<String>,
92 #[serde(default, skip_serializing_if = "Vec::is_empty")]
93 pub to: Vec<String>,
94 #[serde(default, skip_serializing_if = "Vec::is_empty")]
95 pub cc: Vec<String>,
96 #[serde(default, skip_serializing_if = "Vec::is_empty")]
97 pub bcc: Vec<String>,
98 #[serde(default, skip_serializing_if = "Option::is_none")]
99 pub reply_to: Option<String>,
100 #[serde(default = "default_subject")]
101 pub subject: String,
102 #[serde(default = "default_body")]
103 pub body: String,
104
105 #[serde(default, skip_serializing_if = "Option::is_none")]
107 pub ehlo_name: Option<String>,
108 #[serde(default = "default_timeout")]
109 pub timeout_secs: u64,
110 #[serde(default)]
111 pub insecure_tls: bool,
112 #[serde(default, skip_serializing_if = "Option::is_none")]
113 pub ca_file: Option<PathBuf>,
114 #[serde(default = "default_log_level")]
115 pub log_level: String,
116 #[serde(default)]
117 pub wire_trace: bool,
118 #[serde(default = "default_theme")]
119 pub theme: String,
120}
121
122fn yes() -> bool {
123 true
124}
125fn inbox() -> String {
126 "INBOX".into()
127}
128fn default_subject() -> String {
129 "Email server connectivity test".into()
130}
131fn default_body() -> String {
132 "This is a connectivity test sent by email-tester.\n".into()
133}
134fn default_timeout() -> u64 {
135 20
136}
137fn default_log_level() -> String {
138 "info".into()
139}
140fn default_theme() -> String {
141 "auto".into()
142}
143
144impl Default for Profile {
145 fn default() -> Self {
146 crate::outlook_defaults()
147 }
148}
149
150impl Config {
154 pub fn load(path: &Path) -> Result<Self> {
155 let text = fs::read_to_string(path)
156 .with_context(|| format!("reading config file {}", path.display()))?;
157 let cfg: Config =
158 toml::from_str(&text).with_context(|| format!("parsing TOML {}", path.display()))?;
159 Ok(cfg)
160 }
161
162 pub fn save(&self, path: &Path) -> Result<()> {
163 if let Some(parent) = path.parent() {
164 fs::create_dir_all(parent).ok();
165 }
166 let mut text = String::from(
167 "# smtp-test-tool configuration\n\
168 # Multiple [profiles.<name>] sections can coexist; pick one with --profile.\n\
169 # The file 'smtp_test_tool.toml' next to the executable is auto-loaded.\n\n",
170 );
171 text.push_str(&toml::to_string_pretty(self).context("serialising config to TOML")?);
172 fs::write(path, text).with_context(|| format!("writing config file {}", path.display()))?;
173 Ok(())
174 }
175
176 pub fn upsert_profile(&mut self, name: &str, p: Profile) {
178 self.profiles.insert(name.to_string(), p);
179 }
180
181 pub fn profile_names(&self) -> Vec<String> {
182 self.profiles.keys().cloned().collect()
183 }
184
185 pub fn profile(&self, name: &str) -> Option<&Profile> {
186 self.profiles.get(name)
187 }
188}
189
190pub fn discover_config_path() -> Option<PathBuf> {
194 if let Ok(exe) = env::current_exe() {
195 if let Some(dir) = exe.parent() {
196 let p = dir.join(DEFAULT_FILE_NAME);
197 tracing::trace!(
198 "config probe (next-to-exe): {} exists={}",
199 p.display(),
200 p.exists()
201 );
202 if p.exists() {
203 return Some(p);
204 }
205 }
206 }
207 if let Ok(cwd) = env::current_dir() {
208 let p = cwd.join(DEFAULT_FILE_NAME);
209 tracing::trace!("config probe (cwd): {} exists={}", p.display(), p.exists());
210 if p.exists() {
211 return Some(p);
212 }
213 }
214 if let Some(dir) = dirs::config_dir() {
215 let p = dir.join("smtp-test-tool").join(DEFAULT_FILE_NAME);
216 tracing::trace!(
217 "config probe (xdg/appdata): {} exists={}",
218 p.display(),
219 p.exists()
220 );
221 if p.exists() {
222 return Some(p);
223 }
224 }
225 None
226}
227
228pub fn default_save_path() -> PathBuf {
230 if let Ok(exe) = env::current_exe() {
231 if let Some(dir) = exe.parent() {
232 return dir.join(DEFAULT_FILE_NAME);
233 }
234 }
235 if let Some(dir) = dirs::config_dir() {
236 return dir.join("smtp-test-tool").join(DEFAULT_FILE_NAME);
237 }
238 PathBuf::from(DEFAULT_FILE_NAME)
239}