repartee 0.8.0

A modern terminal IRC client built with Ratatui and Tokio
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
use std::sync::LazyLock;

use super::handlers_admin::{
    cmd_autoconnect, cmd_ignore, cmd_image, cmd_kill, cmd_log, cmd_oper, cmd_preview, cmd_reload,
    cmd_mentions, cmd_script, cmd_server, cmd_spellcheck, cmd_stats, cmd_unignore, cmd_wallops,
};
use super::handlers_dcc::cmd_dcc;
use super::handlers_irc::{
    cmd_admin, cmd_away, cmd_ban, cmd_connect, cmd_cycle, cmd_deop, cmd_devoice, cmd_disconnect,
    cmd_except, cmd_info, cmd_invex, cmd_invite, cmd_join, cmd_kick, cmd_kickban, cmd_links,
    cmd_list, cmd_lusers, cmd_me, cmd_mode, cmd_msg, cmd_names, cmd_nick, cmd_notice, cmd_op,
    cmd_part, cmd_query, cmd_quote, cmd_reop, cmd_time, cmd_topic, cmd_unban, cmd_unexcept,
    cmd_uninvex, cmd_unreop, cmd_version, cmd_voice, cmd_who, cmd_whois, cmd_whowas, cmd_wii,
};
use super::handlers_ui::{
    cmd_alias, cmd_clear, cmd_close, cmd_detach, cmd_help, cmd_items, cmd_quit, cmd_shell,
    cmd_unalias,
};
use super::types::{CommandCategory, CommandDef};

static COMMANDS: LazyLock<Vec<(&'static str, CommandDef)>> = LazyLock::new(|| {
    vec![
        // === Connection ===
        (
            "connect",
            CommandDef {
                handler: cmd_connect,
                description: "Connect to a server",
                aliases: &["c"],
                category: CommandCategory::Connection,
            },
        ),
        (
            "disconnect",
            CommandDef {
                handler: cmd_disconnect,
                description: "Disconnect from current server",
                aliases: &[],
                category: CommandCategory::Connection,
            },
        ),
        (
            "quit",
            CommandDef {
                handler: cmd_quit,
                description: "Quit the client",
                aliases: &["exit"],
                category: CommandCategory::Connection,
            },
        ),
        (
            "detach",
            CommandDef {
                handler: cmd_detach,
                description: "Detach from terminal (keep running)",
                aliases: &["dt"],
                category: CommandCategory::Connection,
            },
        ),
        (
            "server",
            CommandDef {
                handler: cmd_server,
                description: "Manage server configurations",
                aliases: &[],
                category: CommandCategory::Connection,
            },
        ),
        (
            "dcc",
            CommandDef {
                handler: cmd_dcc,
                description: "DCC CHAT commands (chat, close, list, reject)",
                aliases: &[],
                category: CommandCategory::Connection,
            },
        ),
        // === Channel ===
        (
            "join",
            CommandDef {
                handler: cmd_join,
                description: "Join a channel",
                aliases: &["j"],
                category: CommandCategory::Channel,
            },
        ),
        (
            "part",
            CommandDef {
                handler: cmd_part,
                description: "Leave a channel",
                aliases: &["leave"],
                category: CommandCategory::Channel,
            },
        ),
        (
            "cycle",
            CommandDef {
                handler: cmd_cycle,
                description: "Part and rejoin a channel",
                aliases: &["rejoin"],
                category: CommandCategory::Channel,
            },
        ),
        (
            "topic",
            CommandDef {
                handler: cmd_topic,
                description: "View or set channel topic",
                aliases: &["t"],
                category: CommandCategory::Channel,
            },
        ),
        (
            "kick",
            CommandDef {
                handler: cmd_kick,
                description: "Kick a user from channel",
                aliases: &["k"],
                category: CommandCategory::Channel,
            },
        ),
        (
            "invite",
            CommandDef {
                handler: cmd_invite,
                description: "Invite a user to a channel",
                aliases: &[],
                category: CommandCategory::Channel,
            },
        ),
        (
            "mode",
            CommandDef {
                handler: cmd_mode,
                description: "Query or set modes",
                aliases: &[],
                category: CommandCategory::Channel,
            },
        ),
        (
            "op",
            CommandDef {
                handler: cmd_op,
                description: "Give operator status",
                aliases: &[],
                category: CommandCategory::Channel,
            },
        ),
        (
            "deop",
            CommandDef {
                handler: cmd_deop,
                description: "Remove operator status",
                aliases: &[],
                category: CommandCategory::Channel,
            },
        ),
        (
            "voice",
            CommandDef {
                handler: cmd_voice,
                description: "Give voice status",
                aliases: &["v"],
                category: CommandCategory::Channel,
            },
        ),
        (
            "devoice",
            CommandDef {
                handler: cmd_devoice,
                description: "Remove voice status",
                aliases: &[],
                category: CommandCategory::Channel,
            },
        ),
        (
            "ban",
            CommandDef {
                handler: cmd_ban,
                description: "Add ban or show ban list",
                aliases: &["b"],
                category: CommandCategory::Channel,
            },
        ),
        (
            "unban",
            CommandDef {
                handler: cmd_unban,
                description: "Remove ban(s) by number, mask, or wildcard (* = all)",
                aliases: &[],
                category: CommandCategory::Channel,
            },
        ),
        (
            "kb",
            CommandDef {
                handler: cmd_kickban,
                description: "Kickban a user (*!*ident@host)",
                aliases: &["kickban"],
                category: CommandCategory::Channel,
            },
        ),
        (
            "except",
            CommandDef {
                handler: cmd_except,
                description: "Add exception or show list",
                aliases: &[],
                category: CommandCategory::Channel,
            },
        ),
        (
            "unexcept",
            CommandDef {
                handler: cmd_unexcept,
                description: "Remove an exception",
                aliases: &[],
                category: CommandCategory::Channel,
            },
        ),
        (
            "invex",
            CommandDef {
                handler: cmd_invex,
                description: "Add invite exception or show list",
                aliases: &[],
                category: CommandCategory::Channel,
            },
        ),
        (
            "uninvex",
            CommandDef {
                handler: cmd_uninvex,
                description: "Remove an invite exception",
                aliases: &[],
                category: CommandCategory::Channel,
            },
        ),
        (
            "reop",
            CommandDef {
                handler: cmd_reop,
                description: "Add reop entry or show list",
                aliases: &[],
                category: CommandCategory::Channel,
            },
        ),
        (
            "unreop",
            CommandDef {
                handler: cmd_unreop,
                description: "Remove a reop entry",
                aliases: &[],
                category: CommandCategory::Channel,
            },
        ),
        (
            "names",
            CommandDef {
                handler: cmd_names,
                description: "Request NAMES list",
                aliases: &[],
                category: CommandCategory::Channel,
            },
        ),
        // === Messaging ===
        (
            "msg",
            CommandDef {
                handler: cmd_msg,
                description: "Send a private message",
                aliases: &["m"],
                category: CommandCategory::Messaging,
            },
        ),
        (
            "query",
            CommandDef {
                handler: cmd_query,
                description: "Open a query with a user",
                aliases: &["q"],
                category: CommandCategory::Messaging,
            },
        ),
        (
            "me",
            CommandDef {
                handler: cmd_me,
                description: "Send a CTCP ACTION",
                aliases: &["action"],
                category: CommandCategory::Messaging,
            },
        ),
        (
            "notice",
            CommandDef {
                handler: cmd_notice,
                description: "Send a notice",
                aliases: &[],
                category: CommandCategory::Messaging,
            },
        ),
        (
            "nick",
            CommandDef {
                handler: cmd_nick,
                description: "Change nickname",
                aliases: &[],
                category: CommandCategory::Messaging,
            },
        ),
        (
            "away",
            CommandDef {
                handler: cmd_away,
                description: "Set or clear away status",
                aliases: &[],
                category: CommandCategory::Messaging,
            },
        ),
        // === Info ===
        (
            "help",
            CommandDef {
                handler: cmd_help,
                description: "Show help for commands",
                aliases: &["?"],
                category: CommandCategory::Info,
            },
        ),
        (
            "whois",
            CommandDef {
                handler: cmd_whois,
                description: "WHOIS query on a user",
                aliases: &["wi"],
                category: CommandCategory::Info,
            },
        ),
        (
            "wii",
            CommandDef {
                handler: cmd_wii,
                description: "WHOIS with idle time",
                aliases: &[],
                category: CommandCategory::Info,
            },
        ),
        (
            "version",
            CommandDef {
                handler: cmd_version,
                description: "CTCP VERSION or server version",
                aliases: &["ver"],
                category: CommandCategory::Info,
            },
        ),
        (
            "list",
            CommandDef {
                handler: cmd_list,
                description: "List channels on server",
                aliases: &[],
                category: CommandCategory::Info,
            },
        ),
        (
            "who",
            CommandDef {
                handler: cmd_who,
                description: "WHO query",
                aliases: &[],
                category: CommandCategory::Info,
            },
        ),
        (
            "whowas",
            CommandDef {
                handler: cmd_whowas,
                description: "WHOWAS query on a nick",
                aliases: &[],
                category: CommandCategory::Info,
            },
        ),
        (
            "info",
            CommandDef {
                handler: cmd_info,
                description: "Request server info",
                aliases: &[],
                category: CommandCategory::Info,
            },
        ),
        (
            "admin",
            CommandDef {
                handler: cmd_admin,
                description: "Request server admin info",
                aliases: &[],
                category: CommandCategory::Info,
            },
        ),
        (
            "lusers",
            CommandDef {
                handler: cmd_lusers,
                description: "Request server user statistics",
                aliases: &[],
                category: CommandCategory::Info,
            },
        ),
        (
            "time",
            CommandDef {
                handler: cmd_time,
                description: "Request server time",
                aliases: &[],
                category: CommandCategory::Info,
            },
        ),
        (
            "links",
            CommandDef {
                handler: cmd_links,
                description: "List server links",
                aliases: &[],
                category: CommandCategory::Info,
            },
        ),
        // === Configuration ===
        (
            "set",
            CommandDef {
                handler: super::settings::cmd_set,
                description: "View or change settings",
                aliases: &[],
                category: CommandCategory::Configuration,
            },
        ),
        (
            "reload",
            CommandDef {
                handler: cmd_reload,
                description: "Reload theme and config",
                aliases: &[],
                category: CommandCategory::Configuration,
            },
        ),
        (
            "spellcheck",
            CommandDef {
                handler: cmd_spellcheck,
                description: "Spell checker status and control",
                aliases: &["spell"],
                category: CommandCategory::Configuration,
            },
        ),
        (
            "mentions",
            CommandDef {
                handler: cmd_mentions,
                description: "Switch to the mentions buffer",
                aliases: &[],
                category: CommandCategory::Info,
            },
        ),
        (
            "ignore",
            CommandDef {
                handler: cmd_ignore,
                description: "Add or list ignore rules",
                aliases: &[],
                category: CommandCategory::Configuration,
            },
        ),
        (
            "unignore",
            CommandDef {
                handler: cmd_unignore,
                description: "Remove an ignore rule",
                aliases: &[],
                category: CommandCategory::Configuration,
            },
        ),
        (
            "alias",
            CommandDef {
                handler: cmd_alias,
                description: "Define or list command aliases",
                aliases: &[],
                category: CommandCategory::Configuration,
            },
        ),
        (
            "unalias",
            CommandDef {
                handler: cmd_unalias,
                description: "Remove a command alias",
                aliases: &[],
                category: CommandCategory::Configuration,
            },
        ),
        (
            "autoconnect",
            CommandDef {
                handler: cmd_autoconnect,
                description: "Toggle server autoconnect",
                aliases: &[],
                category: CommandCategory::Configuration,
            },
        ),
        (
            "items",
            CommandDef {
                handler: cmd_items,
                description: "Manage statusbar items",
                aliases: &[],
                category: CommandCategory::Configuration,
            },
        ),
        (
            "log",
            CommandDef {
                handler: cmd_log,
                description: "Log status and search",
                aliases: &[],
                category: CommandCategory::Configuration,
            },
        ),
        (
            "script",
            CommandDef {
                handler: cmd_script,
                description: "Manage Lua scripts",
                aliases: &[],
                category: CommandCategory::Configuration,
            },
        ),
        // === Other ===
        (
            "oper",
            CommandDef {
                handler: cmd_oper,
                description: "Authenticate as IRC operator",
                aliases: &[],
                category: CommandCategory::Other,
            },
        ),
        (
            "kill",
            CommandDef {
                handler: cmd_kill,
                description: "Disconnect a user (oper only)",
                aliases: &[],
                category: CommandCategory::Other,
            },
        ),
        (
            "wallops",
            CommandDef {
                handler: cmd_wallops,
                description: "Send message to all opers",
                aliases: &[],
                category: CommandCategory::Other,
            },
        ),
        (
            "stats",
            CommandDef {
                handler: cmd_stats,
                description: "Request server statistics",
                aliases: &[],
                category: CommandCategory::Other,
            },
        ),
        (
            "clear",
            CommandDef {
                handler: cmd_clear,
                description: "Clear active buffer",
                aliases: &[],
                category: CommandCategory::Other,
            },
        ),
        (
            "close",
            CommandDef {
                handler: cmd_close,
                description: "Close active buffer",
                aliases: &["wc"],
                category: CommandCategory::Other,
            },
        ),
        (
            "quote",
            CommandDef {
                handler: cmd_quote,
                description: "Send a raw IRC command",
                aliases: &["raw"],
                category: CommandCategory::Other,
            },
        ),
        // === Media ===
        (
            "preview",
            CommandDef {
                handler: cmd_preview,
                description: "Preview an image URL",
                aliases: &[],
                category: CommandCategory::Other,
            },
        ),
        (
            "image",
            CommandDef {
                handler: cmd_image,
                description: "Image cache management",
                aliases: &["img"],
                category: CommandCategory::Other,
            },
        ),
        // === Shell ===
        (
            "shell",
            CommandDef {
                handler: cmd_shell,
                description: "Open an embedded terminal",
                aliases: &["sh"],
                category: CommandCategory::Other,
            },
        ),
    ]
});

/// Command table — handler + short description + aliases + category.
/// Detailed help lives in docs/commands/*.md, accessed via the docs module.
pub fn get_commands() -> &'static [(&'static str, CommandDef)] {
    &COMMANDS
}

/// Get all command names including aliases.
pub fn get_command_names() -> &'static [&'static str] {
    static NAMES: LazyLock<Vec<&'static str>> = LazyLock::new(|| {
        let commands = get_commands();
        let mut names: Vec<&'static str> = Vec::new();
        for &(name, ref def) in commands {
            names.push(name);
            for alias in def.aliases {
                names.push(alias);
            }
        }
        names.sort_unstable();
        names.dedup();
        names
    });
    &NAMES
}

/// Resolve an alias to its canonical command name.
#[allow(dead_code)]
pub fn resolve_alias(name: &str) -> Option<&'static str> {
    let commands = get_commands();
    for &(cmd_name, ref def) in commands {
        if cmd_name == name {
            return Some(cmd_name);
        }
        for alias in def.aliases {
            if *alias == name {
                return Some(cmd_name);
            }
        }
    }
    None
}

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

    #[test]
    fn all_commands_have_description() {
        for &(name, ref def) in get_commands() {
            assert!(!def.description.is_empty(), "/{name} missing description");
        }
    }

    #[test]
    fn command_names_includes_aliases() {
        let names = get_command_names();
        assert!(names.contains(&"connect"));
        assert!(names.contains(&"c"));
        assert!(names.contains(&"j"));
        assert!(names.contains(&"?"));
        assert!(names.contains(&"exit"));
    }

    #[test]
    fn resolve_alias_works() {
        assert_eq!(resolve_alias("c"), Some("connect"));
        assert_eq!(resolve_alias("j"), Some("join"));
        assert_eq!(resolve_alias("?"), Some("help"));
        assert_eq!(resolve_alias("connect"), Some("connect"));
        assert_eq!(resolve_alias("nonexistent"), None);
    }

    #[test]
    fn categories_cover_all_commands() {
        for &(name, ref def) in get_commands() {
            assert!(
                !def.category.label().is_empty(),
                "/{name} has empty category label"
            );
        }
    }

    #[test]
    fn server_query_commands_registered() {
        let commands = get_commands();
        let names: Vec<&str> = commands.iter().map(|(name, _)| *name).collect();
        assert!(names.contains(&"info"), "/info command not registered");
        assert!(names.contains(&"admin"), "/admin command not registered");
        assert!(names.contains(&"lusers"), "/lusers command not registered");
        assert!(names.contains(&"time"), "/time command not registered");
        assert!(names.contains(&"stats"), "/stats command not registered");
        assert!(names.contains(&"links"), "/links command not registered");
    }

    #[test]
    fn server_query_commands_in_correct_category() {
        let commands = get_commands();
        for &(name, ref def) in commands {
            match name {
                "info" | "admin" | "lusers" | "time" | "links" => {
                    assert!(
                        matches!(def.category, CommandCategory::Info),
                        "/{name} should be in Info category, got {:?}",
                        def.category
                    );
                }
                _ => {}
            }
        }
    }

    #[test]
    fn no_duplicate_aliases() {
        let mut all_names: Vec<&str> = Vec::new();
        for &(name, ref def) in get_commands() {
            assert!(!all_names.contains(&name), "Duplicate command name: {name}");
            all_names.push(name);
            for alias in def.aliases {
                assert!(!all_names.contains(alias), "Duplicate alias: {alias}");
                all_names.push(alias);
            }
        }
    }
}