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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//! UI-agnostic setup state machine.
//!
//! Both TUI and GUI frontends render this model and map their input events
//! to [`SetupInput`]. Validation, field navigation, and config persistence
//! live here so bugs are fixed once.

use crate::config::{
    AccountId, ConfigNeedsInput, FileAccountConfig, MultiAccountFileConfig, PasswordBackend,
    SmtpOverrides, new_account_id,
};
use crate::keyring;

// ---------------------------------------------------------------------------
// Field identity
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FieldId {
    Label,
    Server,
    Port,
    Username,
    Password,
    Email,
    Starttls,
}

impl FieldId {
    /// All fields in tab order for full setup.
    pub const FULL: &[FieldId] = &[
        Self::Label,
        Self::Server,
        Self::Port,
        Self::Username,
        Self::Password,
        Self::Email,
        Self::Starttls,
    ];

    /// Editable fields in password-only mode.
    pub const PASSWORD_ONLY: &[FieldId] = &[Self::Password];

    /// Whether this field holds secret content (render masked).
    pub fn is_secret(self) -> bool {
        matches!(self, Self::Password)
    }

    /// Whether this field is a boolean toggle rather than text.
    pub fn is_toggle(self) -> bool {
        matches!(self, Self::Starttls)
    }
}

// ---------------------------------------------------------------------------
// Setup request (what the engine needs)
// ---------------------------------------------------------------------------

/// What prompted the setup — drives which fields are editable.
#[derive(Debug, Clone)]
pub enum SetupRequest {
    /// No config file exists — full account creation.
    Full,
    /// Config exists but password can't be resolved.
    PasswordOnly {
        account_id: AccountId,
        server: String,
        username: String,
        reason: Option<String>,
    },
    /// Editing an existing account (all fields, pre-filled).
    Edit { account_id: AccountId },
}

impl SetupRequest {
    /// Build from the error returned by `Config::resolve_all_accounts()`.
    pub fn from_config_needs(needs: &ConfigNeedsInput) -> Self {
        match needs {
            ConfigNeedsInput::FullSetup => Self::Full,
            ConfigNeedsInput::PasswordOnly {
                account_id,
                server,
                username,
                error,
                ..
            } => Self::PasswordOnly {
                account_id: account_id.clone(),
                server: server.clone(),
                username: username.clone(),
                reason: error.clone(),
            },
        }
    }

    /// Which fields the operator can edit.
    pub fn editable_fields(&self) -> &[FieldId] {
        match self {
            Self::Full | Self::Edit { .. } => FieldId::FULL,
            Self::PasswordOnly { .. } => FieldId::PASSWORD_ONLY,
        }
    }

    /// Whether a given field is read-only in this request mode.
    pub fn is_readonly(&self, field: FieldId) -> bool {
        !self.editable_fields().contains(&field)
    }
}

// ---------------------------------------------------------------------------
// Setup model (the state machine)
// ---------------------------------------------------------------------------

/// UI-agnostic setup state. Frontends read fields for rendering and call
/// [`update()`] with mapped input events.
pub struct SetupModel {
    pub request: SetupRequest,
    pub label: String,
    pub server: String,
    pub port: String,
    pub username: String,
    pub password: String,
    pub email: String,
    pub starttls: bool,
    pub active_field: FieldId,
    pub error: Option<String>,
}

impl SetupModel {
    /// Create a new setup model from a [`ConfigNeedsInput`] error.
    pub fn from_config_needs(needs: &ConfigNeedsInput) -> Self {
        let request = SetupRequest::from_config_needs(needs);
        match needs {
            ConfigNeedsInput::FullSetup => Self {
                request,
                label: String::new(),
                server: String::new(),
                port: "993".into(),
                username: String::new(),
                password: String::new(),
                email: String::new(),
                starttls: false,
                active_field: FieldId::Server,
                error: None,
            },
            ConfigNeedsInput::PasswordOnly {
                server,
                port,
                username,
                starttls,
                error,
                ..
            } => Self {
                request,
                label: String::new(),
                server: server.clone(),
                port: port.to_string(),
                username: username.clone(),
                password: String::new(),
                email: String::new(),
                starttls: *starttls,
                active_field: FieldId::Password,
                error: error.clone(),
            },
        }
    }

    /// Create a setup model for editing an existing account. The caller
    /// pre-fills fields from the account config. Password is intentionally
    /// left empty (must be re-entered).
    pub fn for_edit(account_id: AccountId, fields: SetupFields) -> Self {
        Self {
            request: SetupRequest::Edit { account_id },
            label: fields.label,
            server: fields.server,
            port: fields.port,
            username: fields.username,
            password: String::new(),
            email: fields.email,
            starttls: fields.starttls,
            active_field: FieldId::Server,
            error: None,
        }
    }

    /// Title string for the setup dialog/form.
    pub fn title(&self) -> &str {
        match &self.request {
            SetupRequest::Full => "Account Setup",
            SetupRequest::PasswordOnly { .. } => "Enter Password",
            SetupRequest::Edit { .. } => "Edit Account",
        }
    }

    /// Whether a specific field is read-only.
    pub fn is_readonly(&self, field: FieldId) -> bool {
        self.request.is_readonly(field)
    }

    /// Get the current value of a text field.
    pub fn field_value(&self, field: FieldId) -> &str {
        match field {
            FieldId::Label => &self.label,
            FieldId::Server => &self.server,
            FieldId::Port => &self.port,
            FieldId::Username => &self.username,
            FieldId::Password => &self.password,
            FieldId::Email => &self.email,
            FieldId::Starttls => unreachable!("starttls is a toggle, not text"),
        }
    }

    /// Mutable reference to a text field (None if toggle or readonly).
    fn field_mut(&mut self, field: FieldId) -> Option<&mut String> {
        if self.is_readonly(field) || field.is_toggle() {
            return None;
        }
        match field {
            FieldId::Label => Some(&mut self.label),
            FieldId::Server => Some(&mut self.server),
            FieldId::Port => Some(&mut self.port),
            FieldId::Username => Some(&mut self.username),
            FieldId::Password => Some(&mut self.password),
            FieldId::Email => Some(&mut self.email),
            FieldId::Starttls => None,
        }
    }

    /// Process an input event. Returns what the UI should do next.
    pub fn update(&mut self, input: SetupInput) -> SetupTransition {
        match input {
            SetupInput::NextField => self.cycle_field(1),
            SetupInput::PrevField => self.cycle_field(-1),
            SetupInput::Toggle => {
                if self.active_field == FieldId::Starttls && !self.is_readonly(FieldId::Starttls) {
                    self.starttls = !self.starttls;
                }
            }
            SetupInput::SetField(field, value) => {
                if !self.is_readonly(field) && !field.is_toggle() {
                    match field {
                        FieldId::Label => self.label = value,
                        FieldId::Server => self.server = value,
                        FieldId::Port => self.port = value,
                        FieldId::Username => self.username = value,
                        FieldId::Password => self.password = value,
                        FieldId::Email => self.email = value,
                        FieldId::Starttls => {}
                    }
                    self.error = None;
                }
            }
            SetupInput::SetToggle(field, value) => {
                if field == FieldId::Starttls && !self.is_readonly(field) {
                    self.starttls = value;
                }
            }
            SetupInput::InsertChar(c) => {
                if let Some(f) = self.field_mut(self.active_field) {
                    f.push(c);
                    self.error = None;
                }
            }
            SetupInput::Backspace => {
                if let Some(f) = self.field_mut(self.active_field) {
                    f.pop();
                }
            }
            SetupInput::Submit => {
                return self.try_submit();
            }
            SetupInput::Cancel => {
                return SetupTransition::Finished(SetupOutcome::Cancelled);
            }
        }
        SetupTransition::Continue
    }

    fn cycle_field(&mut self, direction: i32) {
        let fields = self.request.editable_fields();
        if fields.len() <= 1 {
            return;
        }
        if let Some(idx) = fields.iter().position(|&f| f == self.active_field) {
            let len = fields.len() as i32;
            let next = ((idx as i32 + direction).rem_euclid(len)) as usize;
            self.active_field = fields[next];
        }
    }

    fn try_submit(&mut self) -> SetupTransition {
        match &self.request {
            SetupRequest::PasswordOnly {
                account_id,
                server,
                username,
                ..
            } => {
                if self.password.is_empty() {
                    self.error = Some("Password is required".into());
                    return SetupTransition::Continue;
                }

                let password_backend = store_password(username, server, &self.password);

                let mut multi = match MultiAccountFileConfig::load() {
                    Ok(Some(m)) => m,
                    _ => {
                        self.error = Some("Could not load existing config".into());
                        return SetupTransition::Continue;
                    }
                };
                match multi.accounts.iter_mut().find(|a| a.id == *account_id) {
                    Some(acct) => acct.password = password_backend,
                    None => {
                        self.error = Some("Account not found in config".into());
                        return SetupTransition::Continue;
                    }
                }
                if let Err(e) = multi.save() {
                    self.error = Some(format!("Failed to save config: {e}"));
                    return SetupTransition::Continue;
                }
                SetupTransition::Finished(SetupOutcome::Configured)
            }

            SetupRequest::Full => {
                if let Some(err) = self.validate_full() {
                    self.error = Some(err);
                    return SetupTransition::Continue;
                }
                let port = self.port.trim().parse::<u16>().unwrap(); // validated above

                let server = self.server.trim().to_string();
                let username = self.username.trim().to_string();
                let email = self.email.trim().to_string();
                let label = if self.label.trim().is_empty() {
                    username.clone()
                } else {
                    self.label.trim().to_string()
                };
                let account_id = new_account_id();

                let password_backend = store_password(&username, &server, &self.password);

                let fac = FileAccountConfig {
                    id: account_id,
                    label,
                    server,
                    port,
                    username,
                    starttls: self.starttls,
                    password: password_backend,
                    email_addresses: vec![email],
                    smtp: SmtpOverrides::default(),
                };

                let mut multi = MultiAccountFileConfig::load()
                    .ok()
                    .flatten()
                    .unwrap_or(MultiAccountFileConfig {
                        accounts: Vec::new(),
                    });
                if multi
                    .accounts
                    .iter()
                    .any(|a| a.server == fac.server && a.username == fac.username)
                {
                    self.error = Some("Account already exists for this server/username".into());
                    return SetupTransition::Continue;
                }
                multi.accounts.push(fac);
                if let Err(e) = multi.save() {
                    self.error = Some(format!("Failed to save config: {e}"));
                    return SetupTransition::Continue;
                }
                SetupTransition::Finished(SetupOutcome::Configured)
            }

            SetupRequest::Edit { account_id } => {
                // Edit doesn't require password — empty means keep existing
                if let Some(err) = self.validate_edit() {
                    self.error = Some(err);
                    return SetupTransition::Continue;
                }
                let port = self.port.trim().parse::<u16>().unwrap();

                let server = self.server.trim().to_string();
                let username = self.username.trim().to_string();
                let email = self.email.trim().to_string();
                let label = if self.label.trim().is_empty() {
                    username.clone()
                } else {
                    self.label.trim().to_string()
                };

                let mut multi = MultiAccountFileConfig::load()
                    .ok()
                    .flatten()
                    .unwrap_or(MultiAccountFileConfig {
                        accounts: Vec::new(),
                    });

                let existing = match multi.accounts.iter().find(|a| a.id == *account_id) {
                    Some(a) => a,
                    None => {
                        self.error = Some("Account not found in config".into());
                        return SetupTransition::Continue;
                    }
                };

                // If server/username changed, keyring lookup key changes —
                // require password so we can store it under the new key
                let creds_changed =
                    existing.server != server || existing.username != username;
                if creds_changed && self.password.is_empty() {
                    self.error =
                        Some("Password required when changing server or username".into());
                    return SetupTransition::Continue;
                }

                let password_backend = if self.password.is_empty() {
                    existing.password.clone()
                } else {
                    store_password(&username, &server, &self.password)
                };

                let fac = FileAccountConfig {
                    id: account_id.clone(),
                    label,
                    server,
                    port,
                    username,
                    starttls: self.starttls,
                    password: password_backend,
                    email_addresses: vec![email],
                    smtp: SmtpOverrides::default(),
                };

                if let Some(pos) = multi.accounts.iter().position(|a| a.id == *account_id) {
                    multi.accounts[pos] = fac;
                } else {
                    multi.accounts.push(fac);
                }
                if let Err(e) = multi.save() {
                    self.error = Some(format!("Failed to save config: {e}"));
                    return SetupTransition::Continue;
                }
                SetupTransition::Finished(SetupOutcome::Configured)
            }
        }
    }

    /// Validate the current fields. Returns `None` if valid, `Some(error)` if not.
    /// Use this when the UI wants to validate before doing its own submit logic
    /// (e.g., COSMIC needs to spawn an IMAP connect task after persist).
    pub fn validate(&self) -> Option<String> {
        match &self.request {
            SetupRequest::PasswordOnly { .. } => {
                if self.password.is_empty() {
                    return Some("Password is required".into());
                }
                None
            }
            SetupRequest::Full => self.validate_full(),
            SetupRequest::Edit { .. } => self.validate_edit(),
        }
    }

    fn validate_full(&self) -> Option<String> {
        if self.server.trim().is_empty() {
            return Some("Server is required".into());
        }
        if self.username.trim().is_empty() {
            return Some("Username is required".into());
        }
        if self.password.is_empty() {
            return Some("Password is required".into());
        }
        if self.email.trim().is_empty() {
            return Some("Email address is required".into());
        }
        if self.port.trim().parse::<u16>().is_err() {
            return Some("Port must be a number (e.g. 993)".into());
        }
        None
    }

    /// Edit validation: same as full but password is optional (empty = keep existing).
    fn validate_edit(&self) -> Option<String> {
        if self.server.trim().is_empty() {
            return Some("Server is required".into());
        }
        if self.username.trim().is_empty() {
            return Some("Username is required".into());
        }
        if self.email.trim().is_empty() {
            return Some("Email address is required".into());
        }
        if self.port.trim().parse::<u16>().is_err() {
            return Some("Port must be a number (e.g. 993)".into());
        }
        None
    }
}

// ---------------------------------------------------------------------------
// Input / output types
// ---------------------------------------------------------------------------

/// Input events the UI maps its native events to.
#[derive(Debug, Clone)]
pub enum SetupInput {
    /// Tab / move to next editable field.
    NextField,
    /// Shift-Tab / move to previous editable field.
    PrevField,
    /// Toggle the currently active field (Starttls).
    Toggle,
    /// Set a field's entire value (for widget-based UIs like COSMIC).
    SetField(FieldId, String),
    /// Set a toggle field's value directly.
    SetToggle(FieldId, bool),
    /// Insert a character at cursor (for keystroke-based UIs like TUI).
    InsertChar(char),
    /// Delete last character from active text field.
    Backspace,
    /// Attempt to save and exit.
    Submit,
    /// Abort setup.
    Cancel,
}

/// What the UI should do after processing input.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SetupTransition {
    /// Keep showing the form.
    Continue,
    /// Setup is done — UI should exit the form.
    Finished(SetupOutcome),
}

/// Final result of the setup flow.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SetupOutcome {
    /// Config was written. Re-resolve accounts and proceed.
    Configured,
    /// Operator cancelled. Exit gracefully.
    Cancelled,
}

/// Pre-filled field values for the Edit flow.
pub struct SetupFields {
    pub label: String,
    pub server: String,
    pub port: String,
    pub username: String,
    pub email: String,
    pub starttls: bool,
}

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

/// Try keyring, fall back to plaintext. Public so UIs with custom submit
/// logic (e.g., COSMIC with SMTP overrides) can reuse the same strategy.
pub fn store_password(username: &str, server: &str, password: &str) -> PasswordBackend {
    match keyring::set_password(username, server, password) {
        Ok(()) => {
            log::info!("Password stored in keyring for {}@{}", username, server);
            PasswordBackend::Keyring
        }
        Err(e) => {
            log::warn!("Keyring unavailable ({}), using plaintext", e);
            PasswordBackend::Plaintext {
                value: password.to_string(),
            }
        }
    }
}