concord 2.1.13

A terminal user interface client for Discord
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
use ratatui::{
    Frame,
    layout::{Alignment, Constraint, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, BorderType, Borders, Clear, Paragraph, Wrap},
};

fn render_app_header(frame: &mut Frame) {
    let area = frame.area();
    if area.height == 0 {
        return;
    }
    let header = Rect {
        x: area.x,
        y: area.y,
        width: area.width,
        height: 1,
    };
    let title = format!(" Concord - v{} ", env!("CARGO_PKG_VERSION"));
    frame.render_widget(
        Paragraph::new(Line::from(Span::styled(
            title,
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
        )))
        .alignment(Alignment::Left),
        header,
    );
}

use crate::discord::password_auth::MfaMethod;

use super::{
    state::{LoginScreen, LoginState, PasswordField},
    terminal_events::mfa_supports,
};

pub(super) fn render(frame: &mut Frame, state: &LoginState) {
    match state.screen {
        LoginScreen::ModeSelect => render_mode_select(frame, state),
        LoginScreen::TokenInput => render_token_input(frame, state),
        LoginScreen::PasswordInput => render_password_input(frame, state),
        LoginScreen::MfaSelect => render_mfa_select(frame, state),
        LoginScreen::MfaCode => render_mfa_code(frame, state),
        LoginScreen::Qr => render_qr(frame, state),
    }
    render_app_header(frame);
}

fn render_mode_select(frame: &mut Frame, state: &LoginState) {
    let area = centered_rect(72, 18, frame.area());
    let key_bindings = &state.key_bindings;

    let mut lines = vec![
        Line::from(Span::styled("Discord login", accent_style())),
        Line::from(""),
        Line::from("Choose how you want to log in:"),
        Line::from(""),
        choice_line(
            key_bindings.login_token_choice_prefix(),
            "Use Discord token (paste an existing token)",
        ),
        choice_line(
            key_bindings.login_password_choice_prefix(),
            "Login with email/phone and password",
        ),
        choice_line(
            key_bindings.login_qr_choice_prefix(),
            "Login with QR code (scan with the mobile app)",
        ),
        Line::from(""),
    ];

    if let Some(notice) = &state.notice {
        lines.push(notice_line(notice));
        lines.push(Line::from(""));
    }
    if let Some(error) = &state.error {
        lines.push(error_line(error));
        lines.push(Line::from(""));
    }

    lines.push(Line::from(Span::styled(
        key_bindings.login_cancel_quit_label(),
        dim_style(),
    )));

    render_wrapped_login_panel(frame, area, " Login ", lines);
}

fn render_token_input(frame: &mut Frame, state: &LoginState) {
    let area = centered_rect(72, 14, frame.area());
    let masked = mask_chars(&state.token_input);
    let key_bindings = &state.key_bindings;

    let persistence_text = if state.notice.is_some() {
        "Paste your token below. It will be used for this session.".to_owned()
    } else {
        format!(
            "Paste your token below. It will be saved to {}.",
            crate::token_store::credential_path_display()
        )
    };

    let mut lines = vec![
        Line::from(Span::styled("Token login", accent_style())),
        Line::from(""),
        Line::from(persistence_text),
        Line::from(""),
        Line::from(vec![
            Span::styled("> Token  ", dim_style()),
            Span::styled(masked, Style::default().fg(Color::Green)),
        ]),
    ];

    if let Some(error) = &state.error {
        lines.push(error_line(error));
    } else {
        lines.push(Line::from(""));
    }

    if let Some(notice) = &state.notice {
        lines.push(notice_line(notice));
    }

    lines.push(Line::from(""));
    lines.push(Line::from(Span::styled(
        key_bindings.login_token_input_label(),
        dim_style(),
    )));

    render_wrapped_login_panel(frame, area, " Token ", lines);
}

fn render_password_input(frame: &mut Frame, state: &LoginState) {
    let area = centered_rect(82, 18, frame.area());
    let key_bindings = &state.key_bindings;
    let password_mask = mask_chars(&state.password.password);
    let login_active = state.password.active_field == PasswordField::Login;
    let password_active = state.password.active_field == PasswordField::Password;
    let login_style = if login_active {
        active_style()
    } else {
        plain_input_style()
    };
    let password_style = if password_active {
        active_style()
    } else {
        plain_input_style()
    };
    let login_marker = if login_active { "> " } else { "  " };
    let password_marker = if password_active { "> " } else { "  " };

    let mut lines = vec![
        Line::from(Span::styled("Email/password login", accent_style())),
        Line::from(""),
        Line::from("Credentials are used only to request a Discord token."),
        Line::from("They are not saved. Captcha is not supported here."),
        Line::from(""),
        Line::from(vec![
            Span::styled(format!("{login_marker}Email/phone  "), dim_style()),
            Span::styled(state.password.login.clone(), login_style),
        ]),
        Line::from(vec![
            Span::styled(format!("{password_marker}Password     "), dim_style()),
            Span::styled(password_mask, password_style),
        ]),
        Line::from(""),
    ];

    if state.password.in_progress && !state.password.status.is_empty() {
        lines.push(notice_line(&state.password.status));
    } else if let Some(error) = &state.error {
        lines.push(error_line(error));
    } else {
        lines.push(Line::from(""));
    }

    lines.push(Line::from(""));
    lines.push(Line::from(Span::styled(
        key_bindings.login_password_input_label(),
        dim_style(),
    )));

    render_wrapped_login_panel(frame, area, " Email Login ", lines);
}

fn render_mfa_select(frame: &mut Frame, state: &LoginState) {
    let area = centered_rect(82, 16, frame.area());
    let key_bindings = &state.key_bindings;
    let mut lines = vec![
        Line::from(Span::styled("Multi-factor authentication", accent_style())),
        Line::from(""),
        Line::from("Discord requires another verification step."),
        Line::from(""),
    ];

    if mfa_supports(&state.password.mfa, MfaMethod::Totp) {
        lines.push(choice_line(
            key_bindings.login_totp_choice_prefix(),
            "Use TOTP authenticator code",
        ));
    }
    if mfa_supports(&state.password.mfa, MfaMethod::Sms) {
        lines.push(choice_line(
            key_bindings.login_sms_choice_prefix(),
            "Send SMS verification code",
        ));
    }
    lines.push(Line::from(""));

    if state.password.in_progress && !state.password.status.is_empty() {
        lines.push(notice_line(&state.password.status));
    } else if let Some(error) = &state.error {
        lines.push(error_line(error));
    } else if !state.password.status.is_empty() {
        lines.push(Line::from(Span::raw(state.password.status.clone())));
    } else {
        lines.push(Line::from(""));
    }

    lines.push(Line::from(""));
    lines.push(Line::from(Span::styled(
        key_bindings.login_back_quit_label(),
        dim_style(),
    )));

    render_wrapped_login_panel(frame, area, " MFA ", lines);
}

pub(super) fn render_mfa_code(frame: &mut Frame, state: &LoginState) {
    let area = centered_rect(82, 15, frame.area());
    let key_bindings = &state.key_bindings;
    let method = match state.password.mfa_method {
        Some(MfaMethod::Totp) => "TOTP code",
        Some(MfaMethod::Sms) => "SMS code",
        None => "MFA code",
    };
    let mut lines = vec![
        Line::from(Span::styled("Multi-factor authentication", accent_style())),
        Line::from(""),
        Line::from(state.password.status.clone()),
        Line::from(""),
        Line::from(vec![
            Span::styled(format!("{method}  "), dim_style()),
            Span::styled(
                mask_chars(&state.password.mfa_code),
                Style::default().fg(Color::Green),
            ),
        ]),
        Line::from(""),
    ];

    if let Some(error) = &state.error {
        lines.push(error_line(error));
    } else {
        lines.push(Line::from(""));
    }

    lines.push(Line::from(""));
    lines.push(Line::from(Span::styled(
        key_bindings.login_mfa_code_label(),
        dim_style(),
    )));

    render_wrapped_login_panel(frame, area, " MFA Code ", lines);
}

fn render_qr(frame: &mut Frame, state: &LoginState) {
    let area = frame.area();
    let key_bindings = &state.key_bindings;

    let mut lines = vec![
        Line::from(Span::styled("Discord QR login", accent_style())),
        Line::from(""),
    ];

    if let Some(bitmap) = &state.qr.bitmap {
        for row_pair in bitmap.chunks(2) {
            let top = &row_pair[0];
            let bottom = row_pair.get(1);
            let mut line = String::with_capacity(top.len());
            for x in 0..top.len() {
                let upper = top[x];
                let lower = bottom.map(|row| row[x]).unwrap_or(false);
                let ch = match (upper, lower) {
                    (true, true) => '',
                    (true, false) => '',
                    (false, true) => '',
                    (false, false) => ' ',
                };
                line.push(ch);
            }
            lines.push(Line::from(Span::styled(
                line,
                Style::default().fg(Color::White),
            )));
        }
        lines.push(Line::from(""));
    }

    if !state.qr.status.is_empty() {
        lines.push(Line::from(Span::raw(state.qr.status.clone())));
    }
    if let Some(user) = &state.qr.pending_user {
        lines.push(Line::from(""));
        lines.push(Line::from(Span::styled(
            format!("Confirming login as {user}"),
            Style::default().fg(Color::Green),
        )));
    }

    lines.push(Line::from(""));
    lines.push(Line::from(Span::styled(
        key_bindings.login_cancel_quit_label(),
        dim_style(),
    )));

    frame.render_widget(Clear, area);
    frame.render_widget(
        Paragraph::new(lines)
            .alignment(Alignment::Center)
            .block(login_block(" QR Login ")),
        area,
    );
}

fn render_wrapped_login_panel(
    frame: &mut Frame,
    area: Rect,
    title: &'static str,
    lines: Vec<Line<'static>>,
) {
    frame.render_widget(Clear, area);
    frame.render_widget(
        Paragraph::new(lines)
            .alignment(Alignment::Left)
            .wrap(Wrap { trim: false })
            .block(login_block(title)),
        area,
    );
}

fn accent_style() -> Style {
    Style::default()
        .fg(Color::Cyan)
        .add_modifier(Modifier::BOLD)
}

fn dim_style() -> Style {
    Style::default().fg(Color::DarkGray)
}

fn active_style() -> Style {
    Style::default()
        .fg(Color::Green)
        .add_modifier(Modifier::BOLD)
}

fn plain_input_style() -> Style {
    Style::default().fg(Color::White)
}

fn error_line(value: impl AsRef<str>) -> Line<'static> {
    Line::from(Span::styled(
        value.as_ref().to_owned(),
        Style::default().fg(Color::Red),
    ))
}

fn notice_line(value: impl AsRef<str>) -> Line<'static> {
    Line::from(Span::styled(
        value.as_ref().to_owned(),
        Style::default().fg(Color::Yellow),
    ))
}

fn choice_line(key: &'static str, text: &'static str) -> Line<'static> {
    Line::from(vec![Span::styled(key, key_style()), Span::raw(text)])
}

fn key_style() -> Style {
    Style::default().fg(Color::Cyan)
}

fn login_block(title: &'static str) -> Block<'static> {
    Block::default()
        .title(title)
        .borders(Borders::ALL)
        .border_type(BorderType::Plain)
        .border_style(Style::default().fg(Color::Cyan))
        .title_style(
            Style::default()
                .fg(Color::White)
                .add_modifier(Modifier::BOLD),
        )
}

fn centered_rect(width: u16, height: u16, area: Rect) -> Rect {
    let [vertical] = Layout::vertical([Constraint::Length(height)])
        .flex(ratatui::layout::Flex::Center)
        .areas(area);

    let [horizontal] = Layout::horizontal([Constraint::Length(width)])
        .flex(ratatui::layout::Flex::Center)
        .areas(vertical);

    horizontal
}

fn mask_chars(value: &str) -> String {
    "".repeat(value.chars().count())
}