tuimail 0.2.0

Email in your terminal: a vim-style IMAP, POP3 and SMTP client
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
//! Adding an account: a form with the address and password, the servers
//! filled in for providers tuimail knows (and guessed for others), checked
//! by logging in before the account is saved.

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui_textarea::{CursorMove, TextArea};

use crate::account::{self, Account, Auth, PasswordFrom, Protocol, Security};
use crate::{oauth, text};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Row {
    Email,
    Password,
    Name,
    Protocol,
    Host,
    Port,
    Security,
    SmtpHost,
    SmtpPort,
    SmtpSecurity,
    Username,
}

pub const ROWS: [Row; 11] = [
    Row::Email,
    Row::Password,
    Row::Name,
    Row::Protocol,
    Row::Host,
    Row::Port,
    Row::Security,
    Row::SmtpHost,
    Row::SmtpPort,
    Row::SmtpSecurity,
    Row::Username,
];

impl Row {
    pub fn label(self) -> &'static str {
        match self {
            Row::Email => "Email address",
            Row::Password => "Password",
            Row::Name => "Your name",
            Row::Protocol => "Receive with",
            Row::Host => "Incoming server",
            Row::Port => "Port",
            Row::Security => "Security",
            Row::SmtpHost => "Outgoing server",
            Row::SmtpPort => "Port",
            Row::SmtpSecurity => "Security",
            Row::Username => "Username",
        }
    }

    /// Rows toggled with Space or the arrows, not typed in.
    pub fn is_choice(self) -> bool {
        matches!(self, Row::Protocol | Row::Security | Row::SmtpSecurity)
    }
}

pub struct Setup {
    pub email: TextArea<'static>,
    pub password: TextArea<'static>,
    pub name: TextArea<'static>,
    pub protocol: Protocol,
    pub host: TextArea<'static>,
    pub port: TextArea<'static>,
    pub security: Security,
    pub smtp_host: TextArea<'static>,
    pub smtp_port: TextArea<'static>,
    pub smtp_security: Security,
    pub username: TextArea<'static>,
    pub row: usize,
    /// What to know about the provider, or that the servers are a guess.
    pub note: String,
    pub error: Option<String>,
    /// Logging in to check, waiting for the answer.
    pub checking: bool,
    /// The address the servers were filled in for.
    filled_for: String,
    /// The servers were changed by hand: typing the address again doesn't
    /// replace them.
    servers_edited: bool,
}

#[derive(Debug, PartialEq, Eq)]
pub enum Action {
    None,
    /// Check the login, then save.
    Check,
    /// Save without checking.
    SaveAnyway,
    Cancel,
}

fn input(text: &str) -> TextArea<'static> {
    let mut input = TextArea::new(vec![text.to_string()]);
    input.move_cursor(CursorMove::End);
    input.set_cursor_line_style(ratatui::style::Style::default());
    input
}

fn value(input: &TextArea) -> String {
    input.lines().join("").trim().to_string()
}

impl Default for Setup {
    fn default() -> Self {
        let mut password = input("");
        password.set_mask_char('•');
        Self {
            email: input(""),
            password,
            name: input(""),
            protocol: Protocol::Imap,
            host: input(""),
            port: input(""),
            security: Security::Tls,
            smtp_host: input(""),
            smtp_port: input(""),
            smtp_security: Security::Tls,
            username: input(""),
            row: 0,
            note: String::new(),
            error: None,
            checking: false,
            filled_for: String::new(),
            servers_edited: false,
        }
    }
}

impl Setup {
    pub fn current(&self) -> Row {
        ROWS[self.row]
    }

    fn input_mut(&mut self, row: Row) -> Option<&mut TextArea<'static>> {
        Some(match row {
            Row::Email => &mut self.email,
            Row::Password => &mut self.password,
            Row::Name => &mut self.name,
            Row::Host => &mut self.host,
            Row::Port => &mut self.port,
            Row::SmtpHost => &mut self.smtp_host,
            Row::SmtpPort => &mut self.smtp_port,
            Row::Username => &mut self.username,
            _ => return None,
        })
    }

    /// Fills in the servers for the address typed, unless they were
    /// changed by hand.
    fn fill(&mut self) {
        let email = value(&self.email);
        if email == self.filled_for || !email.contains('@') {
            return;
        }
        self.filled_for = email.clone();
        if self.servers_edited {
            return;
        }
        let (guess, note) = account::guess(&email, self.protocol);
        self.host = input(&guess.host);
        self.port = input(&guess.port().to_string());
        self.security = guess.security;
        self.smtp_host = input(&guess.smtp_host);
        self.smtp_port = input(&guess.smtp_port().to_string());
        self.smtp_security = guess.smtp_security;
        self.note = match oauth::client_for(&guess).map(|c| c.provider) {
            Some(oauth::Provider::Microsoft) => {
                "Signs in with Microsoft in your browser: leave the password empty".into()
            }
            Some(oauth::Provider::Google) => {
                "An app password, or leave it empty to sign in with Google in your browser".into()
            }
            None => note.to_string(),
        };
    }

    fn toggle(&mut self, row: Row, by: isize) {
        let cycle = |s: Security, by: isize| {
            let all = [Security::Tls, Security::Starttls, Security::None];
            let at = all.iter().position(|x| *x == s).unwrap_or(0) as isize;
            all[(at + by).rem_euclid(3) as usize]
        };
        match row {
            Row::Protocol => {
                self.protocol = match self.protocol {
                    Protocol::Imap => Protocol::Pop3,
                    Protocol::Pop3 => Protocol::Imap,
                };
                // Different servers for the other protocol.
                self.filled_for.clear();
                self.servers_edited = false;
                self.fill();
            }
            Row::Security => {
                self.security = cycle(self.security, by);
                self.servers_edited = true;
            }
            Row::SmtpSecurity => {
                self.smtp_security = cycle(self.smtp_security, by);
                self.servers_edited = true;
            }
            _ => {}
        }
    }

    fn step(&mut self, by: isize) {
        if self.current() == Row::Email {
            self.fill();
        }
        self.row = (self.row as isize + by).rem_euclid(ROWS.len() as isize) as usize;
    }

    pub fn paste(&mut self, text: &str) {
        // While the login is checked, the form is what's checked: what's
        // saved is that, not something pasted meanwhile.
        if self.checking {
            return;
        }
        let row = self.current();
        if let Some(input) = self.input_mut(row) {
            let line = if row == Row::Password {
                text.trim_matches(['\r', '\n']).to_string()
            } else {
                text::one_line(text, 300)
            };
            input.insert_str(line);
        }
    }

    pub fn on_key(&mut self, key: KeyEvent) -> Action {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        if self.checking {
            return Action::None;
        }
        let row = self.current();
        match key.code {
            KeyCode::Esc => return Action::Cancel,
            KeyCode::Char('s') if ctrl => {
                self.fill();
                return Action::Check;
            }
            KeyCode::Char('f') if ctrl => {
                self.fill();
                return Action::SaveAnyway;
            }
            KeyCode::Tab | KeyCode::Down => self.step(1),
            KeyCode::BackTab | KeyCode::Up => self.step(-1),
            KeyCode::Enter => {
                if self.row + 1 == ROWS.len()
                    || (row == Row::Password && !self.filled_for.is_empty())
                {
                    self.fill();
                    return Action::Check;
                }
                self.step(1);
            }
            KeyCode::Left | KeyCode::Char('h') if row.is_choice() => self.toggle(row, -1),
            KeyCode::Right | KeyCode::Char('l' | ' ') if row.is_choice() => self.toggle(row, 1),
            _ => {
                let edits_server =
                    matches!(row, Row::Host | Row::Port | Row::SmtpHost | Row::SmtpPort);
                let numeric = matches!(row, Row::Port | Row::SmtpPort);
                if let KeyCode::Char(c) = key.code
                    && !ctrl
                    && (text::is_hidden(c) || (numeric && !c.is_ascii_digit()))
                {
                    return Action::None;
                }
                if let Some(input) = self.input_mut(row) {
                    let full = input.lines().join("").chars().count() >= 300
                        && matches!(key.code, KeyCode::Char(_));
                    let line_break = ctrl && matches!(key.code, KeyCode::Char('m' | 'j'));
                    if !full && !line_break {
                        input.input(key);
                    }
                    if edits_server {
                        self.servers_edited = true;
                    }
                }
                self.error = None;
            }
        }
        Action::None
    }

    pub fn password(&self) -> String {
        self.password.lines().join("")
    }

    /// The account the form describes, or what's wrong with it.
    pub fn account(&self, keychain: bool) -> Result<Account, String> {
        let email = value(&self.email);
        if !account::valid_address(&email) {
            return Err("Type your email address".into());
        }
        let port = |input: &TextArea, what: &str| -> Result<Option<u16>, String> {
            let text = value(input);
            if text.is_empty() {
                return Ok(None);
            }
            text.parse::<u16>()
                .ok()
                .filter(|p| *p > 0)
                .map(Some)
                .ok_or_else(|| format!("The {what} port isn't a port number"))
        };
        let (guess, _) = account::guess(&email, self.protocol);
        let name = value(&self.name);
        // No `name`: the sidebar shows the address, which tells two
        // accounts at one provider apart.
        let mut account = Account {
            email: email.clone(),
            display_name: name,
            protocol: self.protocol,
            host: value(&self.host),
            port: port(&self.port, "incoming")?,
            security: self.security,
            username: value(&self.username),
            auth: guess.auth,
            password: if keychain {
                PasswordFrom::Keychain
            } else {
                PasswordFrom::Ask
            },
            smtp_host: value(&self.smtp_host),
            smtp_port: port(&self.smtp_port, "outgoing")?,
            smtp_security: self.smtp_security,
            ..Account::default()
        };
        // Microsoft and Google sign in in the browser when no password is
        // typed and there's an app to sign in as (Outlook.com takes
        // nothing else); others need a password.
        let typed = !self.password().is_empty();
        let can_sign_in = oauth::client_for(&account).is_some();
        account.auth = if !typed && (can_sign_in || account.auth == Auth::Oauth2) {
            Auth::Oauth2
        } else {
            Auth::Password
        };
        if account.auth == Auth::Password && !typed {
            return Err("Type your password (or app password)".into());
        }
        if account.auth == Auth::Oauth2 && oauth::client_for(&account).is_none() {
            return Err(match oauth::provider_for(&account) {
                Some(p) => format!(
                    "Signing in with {} needs an app registered with it: see OAuth2 in \
                     tuimail's README",
                    p.name()
                ),
                None => "This provider needs OAuth2: see OAuth2 in tuimail's README".into(),
            });
        }
        if let Some(problem) = account.problem() {
            return Err(format!("The account can't be used: {problem}"));
        }
        Ok(account)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    fn typed(setup: &mut Setup, text: &str) {
        for c in text.chars() {
            setup.on_key(key(KeyCode::Char(c)));
        }
    }

    #[test]
    fn the_servers_fill_in_from_the_address_and_the_form_makes_an_account() {
        let mut setup = Setup::default();
        typed(&mut setup, "me@gmail.com");
        setup.on_key(key(KeyCode::Tab));
        assert_eq!(value(&setup.host), "imap.gmail.com");
        assert_eq!(value(&setup.smtp_port), "465");
        assert!(setup.note.contains("app password"));
        // No app registered for signing in: an app password it is.
        if oauth::GOOGLE_CLIENT_ID.is_empty() {
            assert!(setup.account(true).unwrap_err().contains("password"));
        }
        typed(&mut setup, "abcd efgh");
        assert_eq!(
            setup.on_key(key(KeyCode::Enter)),
            Action::Check,
            "Enter on the password checks"
        );
        let account = setup.account(true).unwrap();
        assert_eq!(account.email, "me@gmail.com");
        assert_eq!(account.host, "imap.gmail.com");
        assert_eq!(account.port(), 993);
        assert_eq!(account.password, PasswordFrom::Keychain);
        assert_eq!(setup.account(false).unwrap().password, PasswordFrom::Ask);

        // POP3 instead: its servers.
        setup.row = ROWS.iter().position(|r| *r == Row::Protocol).unwrap();
        setup.on_key(key(KeyCode::Char(' ')));
        assert_eq!(value(&setup.host), "pop.gmail.com");
        // Ports take digits only.
        setup.row = ROWS.iter().position(|r| *r == Row::Port).unwrap();
        typed(&mut setup, "x");
        assert_eq!(value(&setup.port), "995");
    }

    #[test]
    fn outlook_signs_in_in_the_browser_or_says_what_it_needs() {
        let mut setup = Setup::default();
        typed(&mut setup, "me@outlook.com");
        setup.on_key(key(KeyCode::Tab));
        assert_eq!(value(&setup.host), "outlook.office365.com");
        match setup.account(true) {
            Ok(account) => {
                assert!(!oauth::MICROSOFT_CLIENT_ID.is_empty());
                assert_eq!(account.auth, Auth::Oauth2);
                assert!(oauth::built_in(&account));
                assert!(setup.note.contains("Signs in with Microsoft"));
            }
            Err(e) => {
                assert!(oauth::MICROSOFT_CLIENT_ID.is_empty());
                assert!(e.contains("registered with it"), "{e}");
            }
        }
    }

    #[test]
    fn servers_changed_by_hand_stay() {
        let mut setup = Setup::default();
        typed(&mut setup, "me@example.org");
        setup.on_key(key(KeyCode::Tab));
        assert_eq!(value(&setup.host), "imap.example.org");
        setup.row = ROWS.iter().position(|r| *r == Row::Host).unwrap();
        setup.host = input("mail.example.org");
        setup.on_key(key(KeyCode::Char('x')));
        setup.on_key(key(KeyCode::Backspace));
        setup.row = 0;
        typed(&mut setup, "m");
        setup.on_key(key(KeyCode::Tab));
        assert_eq!(value(&setup.host), "mail.example.org");
    }
}