why2-chat 2.1.4

Lightweight, fast and secure chat application powered by WHY2 encryption.
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
/*
This is part of WHY2
Copyright (C) 2022-2026 Václav Šmejkal

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

//MODULES
pub mod consts;
pub mod draw;
pub mod event;
pub mod input;
pub mod login;
pub mod markup;
pub mod math;
pub mod palette;
pub mod settings;
pub mod state;
pub mod theme;
pub mod tofu;

use std::
{
    sync::Arc,
    io::
    {
        self,
        Stdout,
        Write,
        Result,
    },
};

use base64::prelude::{ Engine, BASE64_STANDARD };

use crossterm::
{
    cursor::Show,
    terminal::
    {
        self,
        EnterAlternateScreen,
        LeaveAlternateScreen,
    },
    event::
    {
        Event,
        EventStream,
        KeyCode,
        KeyEvent,
        KeyEventKind,
        KeyModifiers,
        KeyboardEnhancementFlags,
        MouseButton,
        MouseEventKind,
        DisableMouseCapture,
        EnableMouseCapture,
        PopKeyboardEnhancementFlags,
        PushKeyboardEnhancementFlags,
    },
};

use ratatui::
{
    Terminal,
    backend::CrosstermBackend,
};

use tokio::
{
    net::tcp::{ OwnedReadHalf, OwnedWriteHalf },
    time::{ self, MissedTickBehavior },
    sync::
    {
        mpsc::{ self, Receiver, Sender },
        Mutex as MutexAsync,
    },
};

use tokio_stream::StreamExt;

use crate::
{
    config,
    options,
    network::
    {
        self,
        codes::PacketCode,
        client::{ self, ClientEvent },
    },
    command::
    {
        self,
        Command,
    },
};

use login::{ Action, ConnectResult };

pub use state::App;

//TYPES
pub type Tui = Terminal<CrosstermBackend<Stdout>>;

//STRUCTS
pub struct TerminalGuard //RESTORES THE TERMINAL ON DROP
{
    mouse: bool,
    enhanced: bool,
}

//IMPLEMENTATIONS
impl TerminalGuard
{
    pub fn enter() -> Result<Self>
    {
        let mouse = config::read_config::<bool>("mouse_capture");

        terminal::enable_raw_mode()?;
        crossterm::execute!(io::stdout(), EnterAlternateScreen)?;

        if mouse { crossterm::execute!(io::stdout(), EnableMouseCapture)?; }

        //Shift+Enter NEEDS KITTY; Alt+Enter ALWAYS WORKS
        let enhanced = terminal::supports_keyboard_enhancement().unwrap_or(false);
        if enhanced
        {
            crossterm::execute!(io::stdout(), PushKeyboardEnhancementFlags(KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES))?;
        }

        Ok(Self { mouse, enhanced })
    }
}

impl Drop for TerminalGuard
{
    fn drop(&mut self)
    {
        let mut stdout = io::stdout();

        if self.enhanced { let _ = crossterm::execute!(stdout, PopKeyboardEnhancementFlags); }
        if self.mouse { let _ = crossterm::execute!(stdout, DisableMouseCapture); }

        let _ = crossterm::execute!(stdout, LeaveAlternateScreen, Show);
        let _ = terminal::disable_raw_mode();
        let _ = stdout.flush();
    }
}

//FUNCTIONS
//PUBLIC
//THE BRANCH GLYPHS FOR BLOCK-COMMAND LISTS
pub fn branch(last: bool) -> &'static str
{
    if last { "╰─ " } else { "├─ " }
}

pub fn install_panic_hook() //MANDATORY: THE RELEASE PROFILE USES panic = "abort"
{
    let previous = std::panic::take_hook();

    std::panic::set_hook(Box::new(move |info|
    {
        restore_terminal();
        previous(info);
    }));
}

pub fn restore_terminal() //BEST-EFFORT, IDEMPOTENT
{
    let mut stdout = io::stdout();

    let _ = crossterm::execute!(stdout, PopKeyboardEnhancementFlags);
    let _ = crossterm::execute!(stdout, DisableMouseCapture);
    let _ = crossterm::execute!(stdout, LeaveAlternateScreen, Show);
    let _ = terminal::disable_raw_mode();
    let _ = stdout.flush();
}

//NOTHING DRAGS WHILE AN OVERLAY IS UP
fn selectable(app: &App) -> bool
{
    app.tofu.is_none() && app.login.is_none() && !app.settings.open
}

//COPY WITH OSC 52, WHICH WORKS OVER SSH
fn copy_to_clipboard(text: &str)
{
    let mut stdout = io::stdout();

    let _ = write!(stdout, "\x1b]52;c;{}\x07", BASE64_STANDARD.encode(text));
    let _ = stdout.flush();
}

pub fn init() -> Result<Tui>
{
    //NO Terminal::clear() - IT QUERIES THE CURSOR
    Terminal::new(CrosstermBackend::new(io::stdout()))
}

//THE SINGLE EVENT LOOP AND EVERY TERMINAL WRITE
pub async fn run
(
    terminal: &mut Tui,
    app: &mut App,
    rx: &mut Receiver<ClientEvent>,
    tx: &Sender<ClientEvent>,
)
{
    let mut reader = EventStream::new();
    let mut tick = time::interval(consts::REDRAW_INTERVAL);
    tick.set_missed_tick_behavior(MissedTickBehavior::Skip);

    let mut events_open = true;

    //NONE UNTIL THE CONNECT PROMPT HAS A SOCKET
    let mut write_stream: Option<Arc<MutexAsync<OwnedWriteHalf>>> = None;

    let (connect_tx, mut connect_rx) = mpsc::channel::<ConnectResult>(1);

    //auto_connect DIALS WITHOUT A KEYSTROKE
    if app.login.as_ref().is_some_and(|prompt| prompt.busy) { login::connect(app, &connect_tx); }

    loop
    {
        tokio::select!
        {
            result = connect_rx.recv() =>
            {
                if let Some((attempt, result)) = result { connected(app, tx, &mut write_stream, attempt, result); }
            },

            event = rx.recv(), if events_open =>
            {
                match event
                {
                    Some(event) => app.apply(event),
                    None => events_open = false,
                }

                //DROP THE DEAD WRITE HALF
                if app.drop_stream
                {
                    app.drop_stream = false;
                    write_stream = None;
                }
            },

            event = reader.next() =>
            {
                match event
                {
                    Some(Ok(event)) => handle_terminal_event(app, event, write_stream.as_ref(), tx, &connect_tx, terminal).await,
                    Some(Err(_)) => app.quit(1, Some(String::from("Reading terminal input failed."))),
                    None => app.quit(0, None),
                }
            },

            _ = tick.tick() =>
            {
                //THE TICK IS THE ANIMATIONS' CLOCK
                app.advance_animations();

                //A DROPPED SESSION DIALS ITSELF BACK
                if app.reconnect.take_due() { login::connect(app, &connect_tx); }

                //AND ANSWERS THE IDENTITY STEPS ITSELF
                if app.reconnect.submit && let Some(write_stream) = write_stream.as_ref()
                {
                    app.reconnect.submit = false;

                    let answer = login::take_input(app);
                    let write_stream = write_stream.clone();

                    crate::submit(app, &write_stream, answer).await;
                }

                //SILENT ROSTER REFRESH
                if app.refresh_online && let Some(write_stream) = write_stream.as_ref()
                {
                    app.refresh_online = false;

                    network::send(&mut *write_stream.lock().await,
                        PacketCode::List { users: None }, options::get_keys().as_ref()).await;
                }

                //AND THE OFFERED PICTURES WE DO NOT HOLD
                if !app.image_requests.is_empty() && let Some(write_stream) = write_stream.as_ref()
                {
                    let keys = options::get_keys();
                    let mut stream = write_stream.lock().await;

                    for hash in app.image_requests.drain(..)
                    {
                        network::send(&mut *stream,
                            PacketCode::ImageData { hash, data: None }, keys.as_ref()).await;
                    }
                }

                app.expire_notice();

                if app.dirty
                {
                    app.dirty = false;
                    let _ = terminal.draw(|frame| draw::draw(frame, app));
                }
            },
        }

        if app.should_quit { break; }
    }
}

//PRIVATE
fn connected
(
    app: &mut App,
    tx: &Sender<ClientEvent>,
    write_stream: &mut Option<Arc<MutexAsync<OwnedWriteHalf>>>,
    attempt: u64,
    result: Result<(OwnedReadHalf, OwnedWriteHalf)>,
)
{
    if !app.login.as_ref().is_some_and(|prompt| prompt.accepts(attempt)) { return; }

    app.dirty = true;

    let (mut read_half, write_half) = match result
    {
        Ok(halves) => halves,

        //KEEP THE ADDRESS ON SCREEN WITH THE REASON
        Err(error) =>
        {
            if let Some(prompt) = app.login.as_mut() { prompt.failed(&error); }

            //KEEP TRYING WHILE THERE ARE TRIES LEFT
            if app.reconnect.arm() && let Some(prompt) = app.login.as_mut() { prompt.busy = true; }

            return;
        },
    };

    let stream = Arc::new(MutexAsync::new(write_half));

    //THE HANDSHAKE RUNS WITH THE TUI ALREADY UP
    let listen_stream = stream.clone();
    let listen_tx = tx.clone();

    tokio::spawn(async move
    {
        client::listen_server(&mut (&mut read_half, listen_stream), listen_tx).await;
    });

    *write_stream = Some(stream);

    if let Some(prompt) = app.login.as_mut() { prompt.connected = true; }

    //THE BOX STAYS UP UNTIL Authenticated
}

async fn handle_terminal_event
(
    app: &mut App,
    event: Event,
    write_stream: Option<&Arc<MutexAsync<OwnedWriteHalf>>>,
    tx: &Sender<ClientEvent>,
    connect_tx: &Sender<ConnectResult>,
    terminal: &Tui,
)
{
    match event
    {
        Event::Key(key) =>
        {
            if key.kind == KeyEventKind::Release { return; }

            handle_key(app, key, write_stream, connect_tx, message_viewport(terminal)).await;
        },

        Event::Mouse(mouse) =>
        {
            let viewport = message_viewport(terminal);

            match mouse.kind
            {
                //THE WHEEL DRIVES THE SETTINGS SELECTION
                MouseEventKind::ScrollUp if app.settings.open => settings::scroll(app, -1),
                MouseEventKind::ScrollDown if app.settings.open => settings::scroll(app, 1),

                MouseEventKind::ScrollUp => app.scroll_up(consts::SCROLL_STEP, viewport),
                MouseEventKind::ScrollDown => app.scroll_down(consts::SCROLL_STEP, viewport),

                //A PRESS ANCHORS A SELECTION
                MouseEventKind::Down(MouseButton::Left) if selectable(app) =>
                {
                    if !app.selection_start(mouse.column, mouse.row) { app.clear_selection(); }
                },

                MouseEventKind::Drag(MouseButton::Left) if selectable(app) =>
                {
                    app.selection_extend(mouse.column, mouse.row);
                },

                MouseEventKind::Up(MouseButton::Left) if selectable(app) =>
                {
                    //THE DRAG SAYS WHETHER THIS WAS A SELECTION
                    if app.selection.is_some_and(|selection| selection.dragged)
                    {
                        //COPY WHILE THE MOUSE IS CAPTURED
                        if let Some(text) = app.selection_text()
                        {
                            let lines = text.lines().count();

                            copy_to_clipboard(&text);
                            app.notify(format!("Copied {lines} line{} to the clipboard", if lines == 1 { "" } else { "s" }));
                        }
                    } else
                    {
                        app.clear_selection();

                        //A CLICK ON A CAPTION FETCHES THE PICTURE
                        if write_stream.is_some()
                            && let Some(entry) = app.image_at(mouse.column, mouse.row)
                            && let Some(hash) = app.request_image(entry)
                        {
                            client::fetch_image(hash, tx.clone());
                        }
                    }
                },

                _ => {},
            }

            app.dirty = true;
        },

        Event::Resize(..) | Event::FocusGained | Event::FocusLost => app.dirty = true,
        Event::Paste(text) =>
        {
            //A PASTE BELONGS TO THE CONNECT BOX
            if app.login.is_some()
            {
                login::insert_str(app, &text);
            } else
            {
                app.input.insert_str(&text);
                app.palette.update(&app.input.text(), app.role);
            }

            app.dirty = true;
        },
    }
}

async fn handle_key
(
    app: &mut App,
    key: KeyEvent,
    write_stream: Option<&Arc<MutexAsync<OwnedWriteHalf>>>,
    connect_tx: &Sender<ConnectResult>,
    viewport: u16,
)
{
    let control = key.modifiers.contains(KeyModifiers::CONTROL) && !key.modifiers.contains(KeyModifiers::ALT);
    let alt = key.modifiers.contains(KeyModifiers::ALT) && !key.modifiers.contains(KeyModifiers::CONTROL);
    let shift = key.modifiers.contains(KeyModifiers::SHIFT);

    app.dirty = true;

    //THE SERVER-KEY PROMPT OUTRANKS EVERYTHING
    if app.tofu.is_some()
    {
        tofu::handle_key(app, key);

        return;
    }

    //THEN THE CONNECT BOX
    if app.login.is_some()
    {
        match login::handle_key(app, key)
        {
            Action::Connect => login::connect(app, connect_tx),

            //AN ANSWERED STEP IS AN ORDINARY SUBMITTED LINE
            Action::Submit =>
            {
                if let Some(write_stream) = write_stream
                {
                    let answer = login::take_input(app);

                    crate::submit(app, write_stream, answer).await;
                }
            },

            Action::Quit => app.quit(0, None),
            Action::None => {},
        }

        return;
    }

    //THE SETTINGS OVERLAY OWNS THE KEYBOARD
    if app.settings.open
    {
        //Ctrl+S BELONGS TO THE SERVER ROWS
        if control && settings_shortcut(key.code) && !(app.settings.server && key.code == KeyCode::Char('s'))
        {
            app.settings.close();
        } else
        {
            settings::handle_key(app, key);
        }

        //A PRESSED Save LANDS HERE - THE SOCKET IS OURS
        if let Some(settings) = app.settings.take_save()
        {
            match write_stream
            {
                Some(write_stream) =>
                {
                    network::send(&mut *write_stream.lock().await,
                        PacketCode::ServerSettings { settings: Some(settings), save: true },
                        options::get_keys().as_ref()).await;

                    //STORED IS NOT IN USE FOR THESE
                    if let Some(keys) = app.settings.restart_note.take()
                    {
                        app.push_styled(format!("{keys} takes effect when the server is restarted."), theme::NOTICE);
                    }
                },

                //NOTHING WENT OUT, SO THE ROWS STAY EDITABLE
                None =>
                {
                    app.settings.saving = false;
                    app.settings.restart_note = None;
                },
            }
        }

        //AND SO DOES A CONFIRMED Restart
        if app.settings.take_restart() && let Some(write_stream) = write_stream
        {
            network::send(&mut *write_stream.lock().await,
                PacketCode::ServerRestart, options::get_keys().as_ref()).await;

            app.push_styled(String::from("Restarting the server..."), theme::NOTICE);
        }

        return;
    }

    //EVERYTHING BELOW SENDS OR EDITS THE LINE
    let Some(write_stream) = write_stream else { return };

    //NEWLINE (Alt+Enter, OR Shift+Enter IF REPORTED)
    if key.code == KeyCode::Enter && (alt || shift)
    {
        app.input.insert('\n');
        app.palette.dismiss();

        return;
    }

    if control
    {
        match key.code
        {
            KeyCode::Char('a') => app.input.home(),
            KeyCode::Char('e') => app.input.end(),
            KeyCode::Char('k') => app.input.kill_to_end(),
            KeyCode::Char('w') => app.input.delete_word(),
            KeyCode::Char('n') => app.palette.next(),
            KeyCode::Char('p') => app.palette.previous(),

            //COMMAND SHORTCUTS
            KeyCode::Char(c) =>
            {
                if let Some(info) = command::COMMAND_LIST.iter().find(|i| i.shortcut == Some(c))
                {
                    //CLEAR THE HALF-TYPED LINE FIRST
                    app.input.clear();
                    app.palette.dismiss();

                    let command = info.command.to_string();
                    crate::submit(app, write_stream, command).await;
                }
            },

            _ => {},
        }

        app.palette.update(&app.input.text(), app.role);

        return;
    }

    match key.code
    {
        KeyCode::Char(c) =>
        {
            app.input.insert(c);
            app.palette.update(&app.input.text(), app.role);
        },

        KeyCode::Backspace =>
        {
            app.input.backspace();
            app.palette.update(&app.input.text(), app.role);
        },

        KeyCode::Delete =>
        {
            app.input.delete();
            app.palette.update(&app.input.text(), app.role);
        },

        KeyCode::Left => if alt { app.input.word_left() } else { app.input.left() },
        KeyCode::Right => if alt { app.input.word_right() } else { app.input.right() },
        KeyCode::Home => app.input.home(),
        KeyCode::End => app.input.end(),

        KeyCode::Up => if app.palette.is_active() { app.palette.previous() } else { app.input.up() },
        KeyCode::Down => if app.palette.is_active() { app.palette.next() } else { app.input.down() },

        KeyCode::PageUp => app.scroll_up(viewport.saturating_sub(1).max(1), viewport),
        KeyCode::PageDown => app.scroll_down(viewport.saturating_sub(1).max(1), viewport),

        KeyCode::Esc =>
        {
            app.palette.dismiss();
            app.clear_selection();
        },

        KeyCode::Tab => { complete_selection(app, true); },

        KeyCode::Enter =>
        {
            //COMPLETE A HIGHLIGHTED PALETTE ENTRY FIRST
            if complete_selection(app, false) { return; }

            app.palette.dismiss();

            let input = app.input.take();
            crate::submit(app, write_stream, input).await;
        },

        _ => {},
    }
}

//WRITE THE HIGHLIGHTED ROW ONTO THE LINE
fn complete_selection(app: &mut App, force: bool) -> bool
{
    if let Some(values) = app.palette.values()
    {
        let input = app.input.text();

        let Some(value) = values.selection().filter(|_| force || !values.typed(&input)) else { return false };

        //KEEP EVERYTHING UP TO THE HALF-TYPED VALUE
        let kept = input.chars().take(values.start).collect::<String>();

        app.input.clear();
        app.input.insert_str(&format!("{kept}{value}"));
        app.palette.update(&app.input.text(), app.role);

        return true;
    }

    let Some(entry) = app.palette.selection().filter(|entry| force || !entry.typed(&app.input.text())) else { return false };

    complete(app, entry);

    true
}

fn complete(app: &mut App, entry: palette::Entry)
{
    app.input.clear();
    app.input.insert_str(&entry.name());

    //LEAVE ROOM FOR ARGUMENTS RIGHT AWAY
    if !entry.args().is_empty() { app.input.insert(' '); }

    app.palette.update(&app.input.text(), app.role);
}

fn settings_shortcut(code: KeyCode) -> bool //Ctrl+<SHORTCUT OF /settings>
{
    let KeyCode::Char(c) = code else { return false };

    command::COMMAND_LIST.iter()
        .find(|info| info.command == Command::Settings)
        .is_some_and(|info| info.shortcut == Some(c))
}

fn message_viewport(terminal: &Tui) -> u16 //ROWS OF ACTUAL MESSAGE TEXT, FOR SCROLL CLAMPING
{
    terminal.size().map(|s| s.height.saturating_sub(5)).unwrap_or(1).max(1)
}