neverlight-mail-core 0.0.2

Headless email engine for Neverlight Mail
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::PathBuf;

use crate::keyring;

// ---------------------------------------------------------------------------
// AccountId — stable UUIDv4 per account
// ---------------------------------------------------------------------------

pub type AccountId = String;

pub fn new_account_id() -> AccountId {
    uuid::Uuid::new_v4().to_string()
}

/// Synthetic ID for env-var-based accounts (stable across restarts).
pub const ENV_ACCOUNT_ID: &str = "env-account";

// ---------------------------------------------------------------------------
// SMTP overrides — per-field optional, merged onto IMAP defaults
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct SmtpOverrides {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub server: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub port: Option<u16>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub username: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub password: Option<PasswordBackend>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub use_starttls: Option<bool>,
}

// ---------------------------------------------------------------------------
// On-disk per-account config
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileAccountConfig {
    pub id: AccountId,
    pub label: String,
    pub server: String,
    pub port: u16,
    pub username: String,
    pub starttls: bool,
    pub password: PasswordBackend,
    #[serde(default)]
    pub email_addresses: Vec<String>,
    #[serde(default)]
    pub smtp: SmtpOverrides,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "backend")]
pub enum PasswordBackend {
    #[serde(rename = "keyring")]
    Keyring,
    #[serde(rename = "plaintext")]
    Plaintext { value: String },
}

// ---------------------------------------------------------------------------
// Multi-account file config (new on-disk format)
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiAccountFileConfig {
    pub accounts: Vec<FileAccountConfig>,
}

// ---------------------------------------------------------------------------
// Legacy single-account file config (for migration)
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileConfig {
    pub server: String,
    pub port: u16,
    pub username: String,
    pub starttls: bool,
    pub password: PasswordBackend,
    #[serde(default)]
    pub email_addresses: Vec<String>,
}

// ---------------------------------------------------------------------------
// Runtime SMTP config (fully resolved)
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct SmtpConfig {
    pub server: String,
    pub port: u16,
    pub username: String,
    pub password: String,
    pub use_starttls: bool,
}

impl SmtpConfig {
    /// Resolve SMTP config: start from IMAP defaults, overlay SmtpOverrides.
    pub fn resolve(
        imap_server: &str,
        imap_username: &str,
        imap_password: &str,
        overrides: &SmtpOverrides,
        account_id: &str,
    ) -> Self {
        let server = overrides
            .server
            .clone()
            .unwrap_or_else(|| imap_server.to_string());
        let port = overrides.port.unwrap_or(587);
        let username = overrides
            .username
            .clone()
            .unwrap_or_else(|| imap_username.to_string());
        let password = match &overrides.password {
            Some(PasswordBackend::Plaintext { value }) => value.clone(),
            Some(PasswordBackend::Keyring) => {
                keyring::get_smtp_password(account_id).unwrap_or_else(|_| imap_password.to_string())
            }
            None => imap_password.to_string(),
        };
        let use_starttls = overrides.use_starttls.unwrap_or(true);

        SmtpConfig {
            server,
            port,
            username,
            password,
            use_starttls,
        }
    }

    /// Legacy: build from IMAP config with env var overrides (for env-var accounts).
    pub fn from_imap_config(config: &Config) -> Self {
        let server = std::env::var("NEVERLIGHT_MAIL_SMTP_SERVER")
            .unwrap_or_else(|_| config.imap_server.clone());
        let port = std::env::var("NEVERLIGHT_MAIL_SMTP_PORT")
            .ok()
            .and_then(|p| p.parse().ok())
            .unwrap_or(587);
        SmtpConfig {
            server,
            port,
            username: config.username.clone(),
            password: config.password.clone(),
            use_starttls: true,
        }
    }
}

// ---------------------------------------------------------------------------
// Runtime account config (resolved passwords, ready to use)
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
pub struct Config {
    pub imap_server: String,
    pub imap_port: u16,
    pub username: String,
    pub password: String,
    pub use_starttls: bool,
    pub email_addresses: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct AccountConfig {
    pub id: AccountId,
    pub label: String,
    pub imap_server: String,
    pub imap_port: u16,
    pub username: String,
    pub password: String,
    pub use_starttls: bool,
    pub email_addresses: Vec<String>,
    pub smtp: SmtpConfig,
    pub smtp_overrides: SmtpOverrides,
}

impl AccountConfig {
    /// Build an AccountConfig from a FileAccountConfig + resolved password.
    pub fn from_file_account(fac: &FileAccountConfig, password: String) -> Self {
        let smtp = SmtpConfig::resolve(
            &fac.server,
            &fac.username,
            &password,
            &fac.smtp,
            &fac.id,
        );
        AccountConfig {
            id: fac.id.clone(),
            label: fac.label.clone(),
            imap_server: fac.server.clone(),
            imap_port: fac.port,
            username: fac.username.clone(),
            password,
            use_starttls: fac.starttls,
            email_addresses: fac.email_addresses.clone(),
            smtp,
            smtp_overrides: fac.smtp.clone(),
        }
    }

    /// Convert to a legacy Config for ImapSession::connect (temporary bridge).
    pub fn to_imap_config(&self) -> Config {
        Config {
            imap_server: self.imap_server.clone(),
            imap_port: self.imap_port,
            username: self.username.clone(),
            password: self.password.clone(),
            use_starttls: self.use_starttls,
            email_addresses: self.email_addresses.clone(),
        }
    }
}

// ---------------------------------------------------------------------------
// What the dialog needs to show when credentials can't be resolved
// ---------------------------------------------------------------------------

/// What the dialog needs to show when credentials can't be resolved automatically.
#[derive(Debug, Clone)]
pub enum ConfigNeedsInput {
    /// No config file exists — show full setup form.
    FullSetup,
    /// Config exists but password is missing from keyring.
    PasswordOnly {
        account_id: AccountId,
        server: String,
        port: u16,
        username: String,
        starttls: bool,
        error: Option<String>,
    },
}

// ---------------------------------------------------------------------------
// File paths
// ---------------------------------------------------------------------------

fn config_dir() -> PathBuf {
    dirs::config_dir()
        .unwrap_or_else(|| PathBuf::from("."))
        .join("neverlight-mail")
}

fn config_path() -> PathBuf {
    config_dir().join("config.json")
}

// ---------------------------------------------------------------------------
// Layout config (unchanged)
// ---------------------------------------------------------------------------

/// Persisted pane layout ratios.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LayoutConfig {
    /// Ratio of the outer split (sidebar vs rest). Default ~0.15.
    pub sidebar_ratio: f32,
    /// Ratio of the inner split (message list vs message view). Default ~0.40.
    pub list_ratio: f32,
}

impl Default for LayoutConfig {
    fn default() -> Self {
        Self {
            sidebar_ratio: 0.15,
            list_ratio: 0.40,
        }
    }
}

impl LayoutConfig {
    pub fn load() -> Self {
        let path = config_dir().join("layout.json");
        if let Ok(data) = fs::read_to_string(&path) {
            if let Ok(cfg) = serde_json::from_str::<LayoutConfig>(&data) {
                // Clamp to sane range
                return LayoutConfig {
                    sidebar_ratio: cfg.sidebar_ratio.clamp(0.05, 0.50),
                    list_ratio: cfg.list_ratio.clamp(0.15, 0.85),
                };
            }
        }
        Self::default()
    }

    pub fn save(&self) {
        let path = config_dir().join("layout.json");
        if let Some(parent) = path.parent() {
            let _ = fs::create_dir_all(parent);
        }
        if let Ok(data) = serde_json::to_string_pretty(self) {
            let _ = fs::write(&path, data);
        }
    }
}

// ---------------------------------------------------------------------------
// Multi-account config: load / save / migrate
// ---------------------------------------------------------------------------

impl MultiAccountFileConfig {
    /// Load config, auto-migrating from legacy single-account format if needed.
    pub fn load() -> Result<Option<Self>, String> {
        let path = config_path();
        if !path.exists() {
            return Ok(None);
        }
        let data = fs::read_to_string(&path).map_err(|e| format!("read config: {e}"))?;

        // Try new multi-account format first
        if let Ok(multi) = serde_json::from_str::<MultiAccountFileConfig>(&data) {
            return Ok(Some(multi));
        }

        // Try legacy single-account format (JSON object with "server" key)
        if let Ok(legacy) = serde_json::from_str::<FileConfig>(&data) {
            log::info!("Migrating legacy single-account config to multi-account format");
            let id = new_account_id();
            let label = legacy.username.clone();
            let migrated = MultiAccountFileConfig {
                accounts: vec![FileAccountConfig {
                    id: id.clone(),
                    label,
                    server: legacy.server,
                    port: legacy.port,
                    username: legacy.username,
                    starttls: legacy.starttls,
                    password: legacy.password,
                    email_addresses: legacy.email_addresses,
                    smtp: SmtpOverrides::default(),
                }],
            };
            // Write back migrated format
            if let Err(e) = migrated.save() {
                log::warn!("Failed to write migrated config: {}", e);
            }
            return Ok(Some(migrated));
        }

        Err("Failed to parse config file (neither multi-account nor legacy format)".into())
    }

    pub fn save(&self) -> Result<(), String> {
        let path = config_path();
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).map_err(|e| format!("create config dir: {e}"))?;
        }
        let data =
            serde_json::to_string_pretty(self).map_err(|e| format!("serialize config: {e}"))?;
        fs::write(&path, data).map_err(|e| format!("write config: {e}"))
    }
}

// ---------------------------------------------------------------------------
// Config resolution
// ---------------------------------------------------------------------------

impl Config {
    /// Try env vars. Returns None if any required var is missing.
    fn from_env() -> Option<Self> {
        let imap_server = std::env::var("NEVERLIGHT_MAIL_SERVER").ok()?;
        let username = std::env::var("NEVERLIGHT_MAIL_USER").ok()?;
        let password = std::env::var("NEVERLIGHT_MAIL_PASSWORD").ok()?;
        let imap_port = std::env::var("NEVERLIGHT_MAIL_PORT")
            .ok()
            .and_then(|p| p.parse().ok())
            .unwrap_or(993);
        let use_starttls = std::env::var("NEVERLIGHT_MAIL_STARTTLS")
            .map(|v| v == "true" || v == "1")
            .unwrap_or(false);
        let email_addresses = std::env::var("NEVERLIGHT_MAIL_FROM")
            .ok()
            .map(|v| v.split(',').map(|s| s.trim().to_string()).filter(|s| !s.is_empty()).collect())
            .unwrap_or_default();

        Some(Config {
            imap_server,
            imap_port,
            username,
            password,
            use_starttls,
            email_addresses,
        })
    }

    /// Resolve all accounts from config.
    pub fn resolve_all_accounts() -> Result<Vec<AccountConfig>, ConfigNeedsInput> {
        // 1. Env vars → single env account
        if let Some(config) = Self::from_env() {
            log::info!("Config loaded from environment variables");
            let smtp = SmtpConfig::from_imap_config(&config);
            return Ok(vec![AccountConfig {
                id: ENV_ACCOUNT_ID.to_string(),
                label: config.username.clone(),
                imap_server: config.imap_server.clone(),
                imap_port: config.imap_port,
                username: config.username.clone(),
                password: config.password.clone(),
                use_starttls: config.use_starttls,
                email_addresses: config.email_addresses.clone(),
                smtp,
                smtp_overrides: SmtpOverrides::default(),
            }]);
        }

        // 2. Config file
        match MultiAccountFileConfig::load() {
            Ok(Some(multi)) => {
                let mut accounts = Vec::new();
                for fac in &multi.accounts {
                    match resolve_password(&fac.password, &fac.username, &fac.server) {
                        Ok(password) => {
                            accounts.push(AccountConfig::from_file_account(fac, password));
                        }
                        Err(e) => {
                            log::warn!(
                                "Failed to resolve password for account '{}': {}",
                                fac.label,
                                e
                            );
                            // Skip accounts with unresolvable passwords for now;
                            // they can be re-entered via setup dialog
                        }
                    }
                }
                if accounts.is_empty() && !multi.accounts.is_empty() {
                    // All accounts failed password resolution — show password dialog
                    let fac = &multi.accounts[0];
                    return Err(ConfigNeedsInput::PasswordOnly {
                        account_id: fac.id.clone(),
                        server: fac.server.clone(),
                        port: fac.port,
                        username: fac.username.clone(),
                        starttls: fac.starttls,
                        error: Some("Keyring unavailable for all accounts".into()),
                    });
                }
                if accounts.is_empty() {
                    return Err(ConfigNeedsInput::FullSetup);
                }
                Ok(accounts)
            }
            Ok(None) => {
                log::info!("No config file found, need full setup");
                Err(ConfigNeedsInput::FullSetup)
            }
            Err(e) => {
                log::warn!("Config file error: {}", e);
                Err(ConfigNeedsInput::FullSetup)
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn resolve_password(
    backend: &PasswordBackend,
    username: &str,
    server: &str,
) -> Result<String, String> {
    match backend {
        PasswordBackend::Plaintext { value } => Ok(value.clone()),
        PasswordBackend::Keyring => keyring::get_password(username, server),
    }
}