Skip to main content

smtp_test_tool/
config.rs

1//! TOML config file with named profiles.  Auto-load order:
2//!   1. `--config <FILE>` if explicit.
3//!   2. `smtp_test_tool.toml` in the executable's directory.
4//!   3. `smtp_test_tool.toml` in the current working directory.
5//!   4. OS-standard config dir (e.g. `%APPDATA%/smtp-test-tool/smtp_test_tool.toml`).
6
7use 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/// Full config = many named profiles.
18#[derive(Debug, Default, Serialize, Deserialize)]
19pub struct Config {
20    /// Profile selected when none is specified on the command line.
21    #[serde(default = "default_active")]
22    pub active: String,
23    /// All named profiles.  TOML representation: `[profiles.default]`, `[profiles.on-prem]` etc.
24    #[serde(default)]
25    pub profiles: BTreeMap<String, Profile>,
26}
27
28fn default_active() -> String {
29    "default".into()
30}
31
32/// All testable settings.  This is what gets serialised to TOML and what
33/// the GUI/CLI render and edit.
34#[derive(Debug, Clone, Serialize, Deserialize)]
35pub struct Profile {
36    // ---- credentials ----
37    #[serde(default, skip_serializing_if = "Option::is_none")]
38    pub user: Option<String>,
39
40    /// Held in memory for the current session only.  **NEVER** persisted
41    /// to disk - the `#[serde(skip)]` attribute makes this structurally
42    /// impossible.  Passwords belong in the user's keyboard or an OS
43    /// keychain (planned), not in a config file.  This rule is documented
44    /// in AGENTS.md and enforced by `tests/config_roundtrip.rs`.
45    #[serde(skip)]
46    pub password: Option<String>,
47
48    /// Same rule as `password`: an OAuth bearer token grants full
49    /// mailbox access until it expires and is therefore a credential.
50    /// Session-only, never written.
51    #[serde(skip)]
52    pub oauth_token: Option<String>,
53
54    // ---- SMTP ----
55    #[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    // ---- IMAP ----
64    #[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    // ---- POP3 ----
73    #[serde(default)]
74    pub pop_enabled: bool,
75    pub pop_host: String,
76    pub pop_port: u16,
77    pub pop_security: Security,
78
79    // ---- message (only when send_test) ----
80    // Defaults to TRUE so a fresh 'Run Test' click exercises the full
81    // end-to-end path including delivery / Send-As rights / spam
82    // filters, not just AUTH.  Users who want auth-only can untick the
83    // 'Actually send a test email' box on the Send Mail tab.  Existing
84    // v0.1.0 configs without a send_test entry get true on next load,
85    // matching what a fresh install would do.
86    #[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    // ---- advanced ----
106    #[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
150// ===========================================================================
151// File handling
152// ===========================================================================
153impl 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    /// Replace (or insert) one profile and persist.
177    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
190/// Locate the most relevant config file on this machine.  Trace-level
191/// events are emitted for each candidate so users can pinpoint a
192/// search miss by running with `RUST_LOG=smtp_test_tool=trace`.
193pub 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
228/// Where to save a brand-new config when the user clicks 'Save'.
229pub 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}