why2-chat 1.5.3

Lightweight, fast and secure chat application powered by WHY2 encryption.
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
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
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
/*
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/>.
*/

#![cfg(feature = "client")]

//MODULES
pub mod colors;
pub mod ui;

use std::
{
    env,
    thread,
    process,
    fs::File,
    path::Path,
    io::
    {
        self,
        Read,
        Write,
    },
    sync::
    {
        mpsc,
        LazyLock,
        Arc,
        Mutex,
    },
};

use sha2::{ Sha256, Digest };

use crossterm::
{
    terminal,
    event::
    {
        self,
        KeyCode,
        KeyModifiers,
        Event,
    },
};

use colored::Color;

use why2_chat::
{
    config,
    consts,
    options,
    misc,
    command::
    {
        self,
        Command,
    },
    network::
    {
        self,
        MessagePacket,
        MessageColors,
        FilePayload,
        voice::client::device,
        client::{ self, ClientEvent },
    },
};

//STRUCTS
struct RawModeGuard;

//GLOBAL VARIABLES
static INPUT_HISTORY: LazyLock<Arc<Mutex<(Vec<String>, usize)>>> = LazyLock::new(|| //INPUTS READ FROM CLIENT
{
    Arc::new(Mutex::new((Vec::new(), 0)))
});

//IMPLEMENTATIONS
impl RawModeGuard
{
    fn enable() -> Result<Self, std::io::Error> //ENABLES RAW MODE AND RETURNS A GUARD THAT WILL DISABLE IT ON DROP
    {
        terminal::enable_raw_mode()?;
        Ok(RawModeGuard)
    }
}

impl Drop for RawModeGuard
{
    fn drop(&mut self)
    {
        let _ = terminal::disable_raw_mode(); //IGNORE ERRORS
    }
}

//HANDLER FNS
fn redraw_removed(input: &Vec<char>, cursor_position: usize) //REDRAW TEXT AFTER CURSOR
{
    //REDRAW INPUT
    if !options::get_asking_password()
    {
        print!("{}", input[cursor_position..].iter().collect::<String>());
    } else
    {
        print!("{}", "*".repeat(input.len() - cursor_position));
    }

    //MOVE CURSOR BACK WHERE IS SHOULD BE
    let tail_len = input.len() - cursor_position;
    if tail_len > 0
    {
        print!("\x1B[{}D", tail_len);
    }
}

fn invalid_usage(subject: Option<&str>) //PRINT 'INVALID' MESSAGE
{
    print!("Invalid {}! Press Ctrl+H for help.\n\n\r>>> ", subject.unwrap_or("usage"))
}

fn mute(parameters: Option<String>) //MUTE LOCAL/PEER CLIENT
{
    ui::clear_lines(2);

    //GET ID PARAMETER
    let id = if let Some(parameters) = parameters
    {
        match parameters.parse::<usize>()
        {
            Ok(i) => Some(i),
            Err(_) => return invalid_usage(None)
        }
    } else { None };

    //INFO LOG
    print!
    (
        "Sucessfully {}muted{}.\n\n\r>>> ",
        if options::toggle_mute(id) { "" } else { "un" },
        if let Some(id) = id
        {
            format!(" ID {id}")
        } else { String::new() }
    );
}

fn read_input() -> String
{
    //CREATE/RESET PARTIAL INPUT VARIABLES
    *options::INPUT_READ.lock().unwrap() = Vec::new(); //RESET INPUT_READ
    let mut input: Vec<char> = Vec::new();
    let mut cursor_position = 0;

    loop
    {
        if let Event::Key(key_event) = event::read().unwrap()
        {
            //INGORE KEY RELEASE
            if key_event.is_release() { continue; }

            //CTRL SHORTCUTS
            if key_event.modifiers.contains(KeyModifiers::CONTROL) &&
                !key_event.modifiers.contains(KeyModifiers::ALT) //EXCLUDE ALTGR
            {
                match key_event.code
                {
                    //CTRL+A (MOVE TO LINE START)
                    KeyCode::Char('a') =>
                    {
                        if cursor_position > 0
                        {
                            print!("\x1B[{}D", cursor_position);
                            cursor_position = 0;
                        }
                    },

                    //CTRL+E (MOVE TO LINE END)
                    KeyCode::Char('e') =>
                    {
                        let input_len = input.len();

                        if cursor_position < input_len
                        {
                            print!("\x1B[{}C", input_len - cursor_position);
                            cursor_position = input_len;
                        }
                    },

                    //COMMAND SHORTCUTS
                    KeyCode::Char(c) =>
                    {
                        if let Some(command) = command::COMMAND_LIST.iter().find(|i| i.shortcut == Some(c))
                        {
                            match command.command
                            {
                                //CTRL+L (LIST)
                                Command::List =>
                                {
                                    ui::clear_lines(1);
                                },

                                //CTRL+C (EXIT)
                                Command::Exit =>
                                {
                                    ui::clear_lines(2);
                                },

                                _ => {} //NORMAL COMMAND
                            }

                            return command.command.to_string();
                        }
                    },

                    _ => {} //some random shortcut
                };
            } else
            {
                match key_event.code
                {
                    //CHAR INPUT, APPEND
                    KeyCode::Char(c) =>
                    {
                        input.insert(cursor_position, c); //LOCAL VARIABLE
                        options::INPUT_READ.lock().unwrap().insert(cursor_position, c); //GLOBAL VARIABLE
                        cursor_position += 1; //CURSOR

                        print!("\x1B[0K");

                        //PRINT ENTERED CHAR
                        if !options::get_asking_password() //DO NOT PRINT PASSWORD AS TEXT
                        {
                            print!("{}", input[(cursor_position - 1)..].iter().collect::<String>());
                        } else //PRINT PASSWORD AS ASTERISKS
                        {
                            print!("{}", "*".repeat((input.len() - cursor_position) + 1));
                        }

                        //MOVE CURSOR BACK WHERE IS SHOULD BE
                        let tail_len = input.len() - cursor_position;
                        if tail_len > 0
                        {
                            print!("\x1B[{}D", tail_len);
                        }
                    },

                    //CONTROL CHARACTERS
                    KeyCode::Backspace => //BACKSPACE - REMOVE LAST CHAR
                    {
                        if cursor_position > 0
                        {
                            cursor_position -= 1; //CURSOR
                            input.remove(cursor_position); //LOCAL VARIABLE
                            options::INPUT_READ.lock().unwrap().remove(cursor_position); //GLOBAL VARIABLE

                            //MOVE CURSOR TO LEFT AND DELETE REST OF THE LINE
                            print!("\x1B[1D\x1B[0K");

                            //PRINT REMAINING CHARS
                            redraw_removed(&input, cursor_position);
                        }
                    },

                    KeyCode::Delete => //DELETE - REMOVE NEXT CHAR
                    {
                        if cursor_position < input.len()
                        {
                            input.remove(cursor_position); //LOCAL VARIABLE
                            options::INPUT_READ.lock().unwrap().remove(cursor_position); //GLOBAL VARIABLE

                            //MOVE CURSOR TO LEFT AND DELETE REST OF THE LINE
                            print!("\x1B[0K");

                            //PRINT REMAINING CHARS
                            redraw_removed(&input, cursor_position);
                        }
                    },

                    KeyCode::Left => //ARROW LEFT - MOVE CURSOR
                    {
                        if cursor_position > 0
                        {
                            cursor_position -= 1;
                            print!("\x1B[1D");
                        }
                    },

                    KeyCode::Right => //ARROW RIGHT - MOVE CURSOR
                    {
                        if cursor_position < input.len()
                        {
                            cursor_position += 1;
                            print!("\x1B[1C");
                        }
                    },

                    KeyCode::Up => //ARROW UP - PAGE HISTORY
                    {
                        let mut history = INPUT_HISTORY.lock().unwrap();

                        //SKIP IF ON TOP OF HISTORY
                        if history.0.is_empty() || history.1 == 0 { continue; }

                        //CLEAR CURRENT INPUT
                        if cursor_position > 0
                        {
                            print!("\x1B[{}D\x1B[0K", cursor_position);
                        }

                        //MOVE IN HISTORY
                        history.1 -= 1;

                        let new_input = &history.0[history.1]; //SELECTED INPUT IN HISTORY

                        //REPLACE CURRENT INPUT
                        input = new_input.chars().collect(); //LOCAL VARIABLE
                        *options::INPUT_READ.lock().unwrap() = input.clone(); //GLOBAL VARIABLE
                        cursor_position = input.len(); //CURSOR

                        print!("{}", new_input); //PRINT
                    },

                    KeyCode::Down => //ARROW DOWN - PAGE HISTORY
                    { //TODO: Remove duplicity
                        let mut history = INPUT_HISTORY.lock().unwrap();

                        //SKIP IF ON TOP OF HISTORY
                        if history.1 == history.0.len() { continue; }

                        //CLEAR CURRENT INPUT
                        if cursor_position > 0
                        {
                            print!("\x1B[{}D\x1B[0K", cursor_position);
                        }

                        //MOVE IN HISTORY
                        history.1 += 1;

                        //SELECTED INPUT IN HISTORY
                        let new_input = if history.1 < history.0.len()
                        {
                            &history.0[history.1]
                        } else
                        {
                            ""
                        };

                        //REPLACE CURRENT INPUT
                        input = new_input.chars().collect(); //LOCAL VARIABLE
                        *options::INPUT_READ.lock().unwrap() = input.clone(); //GLOBAL VARIABLE
                        cursor_position = input.len(); //CURSOR

                        print!("{}", new_input); //PRINT
                    },

                    KeyCode::Enter => break, //ENTER PRESSED, FINALIZE
                    _ => {} //idk
                }
            }

            io::stdout().flush().unwrap();
        }
    }

    //COLLECT AND TRIM (NOT PASSWORDS)
    let read = input.iter().collect::<String>();
    if !options::get_asking_password()
    {
        read.trim().to_string()
    } else
    {
        read
    }
}

fn to_color(color: &str) -> Result<u8, ()> //CONVERT STRING TO COLOR CODE
{
    color.parse::<Color>()
        .map(|c| colors::color_to_u8(&c))
        .map_err(|_| ())
}

fn color_handler(config_key: &str, parameters: Option<String>) //HANDLE COLOR CHANGE
{
    ui::clear_lines(2);

    //CHECK FOR PARAMETERS
    if let Some(parameters) = parameters
    {
        //CHECK FOR COLOR VALIDITY
        print!("{}\n\n\r>>> ", if to_color(&parameters).is_ok()
        {
            //SAVE COLOR TO CONFIG
            config::client_write(config_key, &parameters.to_lowercase());

            "Color set successfully."
        } else
        {
            "Invalid color! See \x1b]8;;https://docs.rs/colored/latest/colored/enum.Color.html\x1b\\colored API\x1b]8;;\x1b\\ for help."
        });
    } else
    {
        invalid_usage(None);
    }
}

fn get_colors() -> MessageColors //READ COLORS FROM CONFIG
{
    MessageColors
    {
        username_color: to_color(&config::read_config::<String>("username_color")).ok(),
        message_color: to_color(&config::read_config::<String>("message_color")).ok(),
    }
}

fn main()
{
    //CREATE CHANNEL
    let (tx, rx) = mpsc::channel::<ClientEvent>();

    //SPAWN PRINTER THREAD
    thread::spawn(move ||
    {
        while let Ok(event) = rx.recv()
        {
            ui::draw_event(event);
        }
    });

    //CONFIGURATION
    misc::check_version(&tx); //CHECK WHY2 VERSION
    config::init_config(); //CREATE client.toml CONFIGURATION

    //CHECK FOR PARAMETERS
    if let Some(arg) = env::args().nth(1)
    {
        if arg == "--verify" && env::args().len() == 4 //SAVE SERVER PUBKEY
        {
            config::server_keys_save(&env::args().nth(2).unwrap(), &env::args().nth(3).unwrap());
            println!("Key saved.");
        } else if arg == "--audio-setup" && env::args().len() == 2
        {
            device::setup_devices();
        } else if arg == "--help" && env::args().len() == 2
        {
            println!
            (
                "WHY2 Chat Client\n\
                ================\n\n\
                Usage: why2 [options]\n\n\
                --verify (HOST) (PUBKEY HASH) - Whitelist server keys\n\
                --audio-setup                 - Select audio devices for voice chat\n\
                --help                        - Display this"
            );
        } else //INVALID CMD
        {
            println!("Invalid usage! Use 'why2 --help'.");
        }

        return;
    }

    println!("Welcome.\n");

    //GET CONNECTING ADDRESS
    let mut connecting_addr = if config::read_config::<bool>("auto_connect") //USER ENABLED AUTOMATIC CONNECTION
    {
        let addr = config::read_config("auto_connect_addr"); //USE CONFIG ADDR

        //PRINT OUT IP
        println!(">>> {addr}");
        io::stdout().flush().unwrap();

        addr
    } else //NO AUTO CONNECT
    {
        print!("Enter IP Address:\n>>> ");
        io::stdout().flush().unwrap();

        //GET IP FROM USER INPUT
        let mut input = String::new();
        io::stdin().read_line(&mut input).unwrap();

        input.trim().to_owned()
    };

    //SPACER ("=") TO BE ADDED
    let mut spacer_add_spaces = 4;

    //ADD PORT TO IP IF MISSING
    if !connecting_addr.contains(':')
    {
        //APPEND DEFAULT PORT TO connecting_ip
        connecting_addr.push_str(&format!(":{}", config::read_config::<u16>("default_port")));
    } else
    {
        spacer_add_spaces += connecting_addr.len() - connecting_addr.find(":").unwrap();
    }

    //PRINT SPACER
    println!("{}", "=".repeat(connecting_addr.find(":").unwrap() + spacer_add_spaces));

    //SET GLOBAL SERVER ADDR
    options::set_server_address(&connecting_addr);

    //CHECK IF SOCKS5 IS ENABLED
    if config::read_config("socks5_enabled")
    {
        options::enable_socks5();
    }

    //CONNECT TO SERVER
    let mut stream = match client::connect(connecting_addr)
    {
        Ok(s) => s,
        Err(e) =>
        {
            eprintln!("\nConnecting failed: {e}");
            process::exit(1);
        }
    };

    //CREATE STREAM LOCK
    let write_stream = Arc::new(Mutex::new(stream.try_clone().expect("Failed cloning stream")));

    //CLONE SOCKET FOR CLIENT INPUT
    let write_stream_listen = write_stream.clone();

    //LISTEN TO SERVER
    thread::spawn(move || client::listen_server(&mut (&mut stream, write_stream_listen), tx));

    //ENABLE RAW MODE
    let _raw_mode_guard = RawModeGuard::enable().unwrap();

    //LOOP FOR CLIENT-SIDE USER INPUT
    loop
    {
        //READ STDIN
        let input = read_input();

        //APPEND MESSAGE TO HISTORY
        if options::get_sending_messages()
        {
            if input.is_empty() { continue; } //DO NOT FORWARD EMPTY MESSAGES

            //USER COMMANDS
            let mut command_used = false;
            if let (Some(command), parameters) = command::get_command(&input)
            {
                //SEND CODE ON A SIMPLE COMMAND, CONTINUE OTHERWISE
                if !command::send_command_code(&mut write_stream.lock().unwrap(), &command, &parameters)
                {
                    match command
                    {
                        //HELP
                        Command::Help =>
                        {
                            ui::clear_lines(2);
                            options::set_extra_space(true); //ADD EXTRA NEWLINE ON NEXT RECEIVED MESSAGE

                            //PRINT COMMAND LIST
                            println!("\nCommands:");

                            for info in command::COMMAND_LIST //ITERATE OVER ALL COMMANDS
                            {
                                //[OPTIONAL], (REQUIRED)
                                let args = info.args.iter().map(|arg|
                                {
                                    if arg.required
                                    {
                                        format!("({})", arg.name)
                                    } else
                                    {
                                        format!("[{}]", arg.name)
                                    }
                                }).collect::<Vec<String>>().join(" ");

                                let separator = if info.args.is_empty() { "" } else { " " };

                                //PRINT COMMAND
                                println!
                                (
                                    "\r{prefix}{name}{separator}{args} - {description}",
                                    prefix = command::COMMAND_PREFIX,
                                    name = info.triggers[0].to_lowercase(),
                                    description = info.description,
                                );
                            }

                            //PRINT PROMPT BAR
                            print!("\n\r>>> ");
                        },

                        Command::Info =>
                        {
                            ui::clear_lines(2);
                            let mut valid = false;

                            //PARAMETERS PASSED
                            if let Some(parameters) = parameters
                            {
                                //CHECK IF COMMAND/ALIAS EXISTS
                                if let Some(command) = command::COMMAND_LIST.iter()
                                    .find(|c| c.triggers.iter()
                                        .find(|t| t.eq_ignore_ascii_case(&parameters)).is_some())
                                {
                                    options::set_extra_space(true); //ADD EXTRA NEWLINE ON NEXT RECEIVED MESSAGE
                                    print!
                                    (
                                        "\n\rCommand: {command}
                                        \rAliases: {aliases}
                                        \rShortcut: {shortcut}
                                        \rParameters: {args}
                                        \rDescription: {description}
                                        \n\r>>> ",

                                        command = command.triggers[0],
                                        aliases = command.triggers[1..].join(", "),
                                        shortcut = command.shortcut.map(|s| format!("Ctrl+{}", s.to_ascii_uppercase()))
                                            .unwrap_or_else(|| String::from("None")),
                                        description = command.description,
                                        args = if !command.args.is_empty()
                                        {
                                            command.args.iter().map(|arg|
                                            {
                                                if arg.required
                                                {
                                                    format!("({})", arg.name)
                                                } else
                                                {
                                                    format!("[{}]", arg.name)
                                                }
                                            }).collect::<Vec<String>>().join(" ")
                                        } else { String::from("None") },
                                    );

                                    valid = true;
                                }
                            }

                            if !valid { invalid_usage(None); }
                        },

                        Command::Upload =>
                        {
                            ui::clear_lines(2);

                            //CHECK PATH
                            if let Some(parameters) = parameters
                            {
                                let path = Path::new(parameters.trim());

                                //TRY TO OPEN FILE
                                if let Ok(metadata) = path.metadata() && path.is_file() &&
                                    let Ok(mut file) = File::open(path) &&
                                    let Some(filename) = path.file_name().and_then(|n| n.to_str())
                                {
                                    //GET SHA256 FILE HASH
                                    let mut hasher = Sha256::new();
                                    let mut buffer = vec![0; consts::UPLOAD_CHUNK_SIZE];

                                    //LOOP READING
                                    let success = loop
                                    {
                                        match file.read(&mut buffer)
                                        {
                                            Ok(0) => break true,
                                            Ok(bytes) => hasher.update(&buffer[..bytes]),
                                            Err(_) => break false,
                                        }
                                    };

                                    //REQUEST FILE UPLOAD
                                    if success
                                    {
                                        //FINALIZE HASH
                                        let hash: [u8; 32] = hasher.finalize().into();

                                        //STORE UPLOAD IN ACTIVE UPLOADS LIST
                                        client::ACTIVE_UPLOADS.lock().unwrap().insert(hash.clone(), path.canonicalize().unwrap());

                                        //SEND UPLOAD REQUEST
                                        network::send(&mut write_stream.lock().unwrap(), MessagePacket
                                        {
                                            code: command.to_code(),
                                            file: Some(FilePayload
                                            {
                                                size: Some(metadata.len()),
                                                filename: Some(filename.to_owned()),
                                                hash: Some(hash),
                                                ..Default::default()
                                            }),
                                            ..Default::default()
                                        }, options::get_keys().as_ref());
                                    } else //HASHING FAILED
                                    {
                                        print!("Error reading file!\n\n\r>>> ");
                                    }
                                } else //NON-EXISTING FILE
                                {
                                    print!("File not found!\n\n\r>>> ");
                                }
                            } else { invalid_usage(None); }
                        },

                        Command::UsernameColor =>
                        {
                            color_handler("username_color", parameters);
                        },

                        Command::MessageColor =>
                        {
                            color_handler("message_color", parameters);
                        },

                        Command::Mute =>
                        {
                            mute(parameters);
                        },

                        //INVALID COMMAND
                        Command::Invalid =>
                        {
                            ui::clear_lines(2);
                            invalid_usage(Some("command"));
                        },

                        //NON IMPLEMENTED COMMAND
                        _ => panic!("Invalid command")
                    }
                }

                io::stdout().flush().unwrap(); //FLUSH COMMAND OUTPUT
                command_used = true;
            }

            let mut history = INPUT_HISTORY.lock().unwrap();

            //ADD INPUT
            if history.0.last() != Some(&input)
            {
                history.0.push(input.clone());
            }

            //RESET HISTORY POSITION
            history.1 = history.0.len();

            if command_used { continue }; //DO NOT SEND COMMAND STRING
        }

        //DISABLE ASKING_PASSWORD
        options::set_asking_password(false);

        //SEND input TO SERVER
        network::send(&mut write_stream.lock().unwrap(), MessagePacket
        {
            text: Some(input),
            colors: get_colors(),
            ..Default::default()
        }, options::get_keys().as_ref());
    }
}