legion-protocol 0.1.0

🏛️ Legion Protocol - Secure, IRC-compatible communication protocol with E2E 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
//! IRC command parsing and representation
//!
//! This module provides types and functionality for working with IRC commands,
//! including both standard IRC commands and IRCv3 extensions.

// use std::str::FromStr; // Not currently used

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Represents various IRC commands with their parameters
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Command {
    // Connection registration
    /// NICK command - set nickname
    Nick(String),
    /// USER command - user registration
    User { username: String, realname: String },
    /// PASS command - connection password
    Pass(String),
    /// QUIT command - disconnect
    Quit(Option<String>),
    /// PING command - server ping
    Ping(String),
    /// PONG command - ping response
    Pong(String),
    
    // Channel operations
    /// JOIN command - join channels
    Join(Vec<String>, Vec<String>), // channels, keys
    /// PART command - leave channels
    Part(Vec<String>, Option<String>), // channels, message
    /// TOPIC command - get/set channel topic
    Topic { channel: String, topic: Option<String> },
    /// NAMES command - list channel members
    Names(Vec<String>),
    /// LIST command - list channels
    List(Option<Vec<String>>),
    
    // Messaging
    /// PRIVMSG command - send message
    Privmsg { target: String, message: String },
    /// NOTICE command - send notice
    Notice { target: String, message: String },
    
    // User queries
    /// WHO command - query user information
    Who(Option<String>),
    /// WHOIS command - detailed user information
    Whois(Vec<String>),
    /// WHOWAS command - historical user information
    Whowas(String, Option<i32>),
    /// QUERY command - open private message window
    Query(String),
    
    // Channel management
    /// KICK command - remove user from channel
    Kick { channel: String, user: String, reason: Option<String> },
    /// MODE command - change modes
    Mode { target: String, modes: Option<String>, params: Vec<String> },
    /// INVITE command - invite user to channel
    Invite { nick: String, channel: String },
    
    // Server queries
    /// MOTD command - message of the day
    Motd(Option<String>),
    /// VERSION command - server version
    Version(Option<String>),
    /// STATS command - server statistics
    Stats(Option<String>, Option<String>),
    /// TIME command - server time
    Time(Option<String>),
    /// INFO command - server information
    Info(Option<String>),
    
    // IRCv3 commands
    /// CAP command - capability negotiation
    Cap { subcommand: String, params: Vec<String> },
    /// AUTHENTICATE command - SASL authentication
    Authenticate(String),
    /// ACCOUNT command - account notification
    Account(String),
    /// MONITOR command - nickname monitoring
    Monitor { subcommand: String, targets: Vec<String> },
    /// METADATA command - user metadata
    Metadata { target: String, subcommand: String, params: Vec<String> },
    /// TAGMSG command - tag-only message
    TagMsg { target: String },
    /// BATCH command - message batching
    Batch { reference: String, batch_type: Option<String>, params: Vec<String> },
    
    // 2024 Bleeding-edge IRCv3 commands
    /// REDACT command - message redaction
    Redact { target: String, msgid: String, reason: Option<String> },
    /// MARKREAD command - mark messages as read
    MarkRead { target: String, timestamp: Option<String> },
    /// SETNAME command - change real name
    SetName { realname: String },
    /// CHATHISTORY command - request chat history
    ChatHistory { subcommand: String, target: String, params: Vec<String> },
    
    // Operator commands
    /// OPER command - gain operator privileges
    Oper { name: String, password: String },
    /// KILL command - forcibly disconnect user
    Kill { nick: String, reason: String },
    /// REHASH command - reload server configuration
    Rehash,
    /// RESTART command - restart server
    Restart,
    /// DIE command - shutdown server
    Die,
    
    // CTCP commands
    /// CTCP request
    CtcpRequest { target: String, command: String, params: String },
    /// CTCP response
    CtcpResponse { target: String, command: String, params: String },
    
    // Fallback for unknown commands
    /// Unknown command
    Unknown(String, Vec<String>),
}

impl Command {
    /// Parse a command from its string representation and parameters
    pub fn parse(command: &str, params: Vec<String>) -> Self {
        match command.to_uppercase().as_str() {
            "NICK" => {
                if let Some(nick) = params.first() {
                    Command::Nick(nick.clone())
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "USER" => {
                if params.len() >= 4 {
                    Command::User {
                        username: params[0].clone(),
                        realname: params[3].clone(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "PASS" => {
                if let Some(pass) = params.first() {
                    Command::Pass(pass.clone())
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "QUIT" => Command::Quit(params.first().cloned()),
            "PING" => {
                if let Some(token) = params.first() {
                    Command::Ping(token.clone())
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "PONG" => {
                if let Some(token) = params.first() {
                    Command::Pong(token.clone())
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "JOIN" => {
                if let Some(channels) = params.first() {
                    let channels: Vec<String> = channels.split(',').map(|s| s.to_string()).collect();
                    let keys: Vec<String> = params.get(1)
                        .map(|k| k.split(',').map(|s| s.to_string()).collect())
                        .unwrap_or_default();
                    Command::Join(channels, keys)
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "PART" => {
                if let Some(channels) = params.first() {
                    let channels: Vec<String> = channels.split(',').map(|s| s.to_string()).collect();
                    let message = params.get(1).cloned();
                    Command::Part(channels, message)
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "TOPIC" => {
                if let Some(channel) = params.first() {
                    Command::Topic {
                        channel: channel.clone(),
                        topic: params.get(1).cloned(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "NAMES" => {
                if let Some(channels) = params.first() {
                    let channels: Vec<String> = channels.split(',').map(|s| s.to_string()).collect();
                    Command::Names(channels)
                } else {
                    Command::Names(Vec::new())
                }
            }
            "LIST" => {
                if let Some(channels) = params.first() {
                    let channels: Vec<String> = channels.split(',').map(|s| s.to_string()).collect();
                    Command::List(Some(channels))
                } else {
                    Command::List(None)
                }
            }
            "PRIVMSG" => {
                if params.len() >= 2 {
                    Command::Privmsg {
                        target: params[0].clone(),
                        message: params[1].clone(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "NOTICE" => {
                if params.len() >= 2 {
                    Command::Notice {
                        target: params[0].clone(),
                        message: params[1].clone(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "WHO" => Command::Who(params.first().cloned()),
            "WHOIS" => {
                if !params.is_empty() {
                    Command::Whois(params)
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "WHOWAS" => {
                if let Some(nick) = params.first() {
                    let count = params.get(1).and_then(|s| s.parse().ok());
                    Command::Whowas(nick.clone(), count)
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "QUERY" => {
                if let Some(target) = params.first() {
                    Command::Query(target.clone())
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "KICK" => {
                if params.len() >= 2 {
                    Command::Kick {
                        channel: params[0].clone(),
                        user: params[1].clone(),
                        reason: params.get(2).cloned(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "MODE" => {
                if let Some(target) = params.first() {
                    Command::Mode {
                        target: target.clone(),
                        modes: params.get(1).cloned(),
                        params: params[2..].to_vec(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "INVITE" => {
                if params.len() >= 2 {
                    Command::Invite {
                        nick: params[0].clone(),
                        channel: params[1].clone(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "MOTD" => Command::Motd(params.first().cloned()),
            "VERSION" => Command::Version(params.first().cloned()),
            "STATS" => Command::Stats(params.first().cloned(), params.get(1).cloned()),
            "TIME" => Command::Time(params.first().cloned()),
            "INFO" => Command::Info(params.first().cloned()),
            
            // IRCv3 commands
            "CAP" => {
                if let Some(subcommand) = params.first() {
                    Command::Cap {
                        subcommand: subcommand.clone(),
                        params: params[1..].to_vec(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "AUTHENTICATE" => {
                if let Some(data) = params.first() {
                    Command::Authenticate(data.clone())
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "ACCOUNT" => {
                if let Some(account) = params.first() {
                    Command::Account(account.clone())
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "MONITOR" => {
                if let Some(subcommand) = params.first() {
                    Command::Monitor {
                        subcommand: subcommand.clone(),
                        targets: params[1..].to_vec(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "METADATA" => {
                if params.len() >= 2 {
                    Command::Metadata {
                        target: params[0].clone(),
                        subcommand: params[1].clone(),
                        params: params[2..].to_vec(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "TAGMSG" => {
                if let Some(target) = params.first() {
                    Command::TagMsg {
                        target: target.clone(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "BATCH" => {
                if let Some(reference) = params.first() {
                    Command::Batch {
                        reference: reference.clone(),
                        batch_type: params.get(1).cloned(),
                        params: params[2..].to_vec(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            
            // 2024 Bleeding-edge IRCv3 commands
            "REDACT" => {
                if params.len() >= 2 {
                    Command::Redact {
                        target: params[0].clone(),
                        msgid: params[1].clone(),
                        reason: params.get(2).cloned(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "MARKREAD" => {
                if !params.is_empty() {
                    Command::MarkRead {
                        target: params[0].clone(),
                        timestamp: params.get(1).cloned(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "SETNAME" => {
                if let Some(realname) = params.first() {
                    Command::SetName {
                        realname: realname.clone(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "CHATHISTORY" => {
                if params.len() >= 2 {
                    Command::ChatHistory {
                        subcommand: params[0].clone(),
                        target: params[1].clone(),
                        params: params[2..].to_vec(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            
            // Operator commands
            "OPER" => {
                if params.len() >= 2 {
                    Command::Oper {
                        name: params[0].clone(),
                        password: params[1].clone(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "KILL" => {
                if params.len() >= 2 {
                    Command::Kill {
                        nick: params[0].clone(),
                        reason: params[1].clone(),
                    }
                } else {
                    Command::Unknown(command.to_string(), params)
                }
            }
            "REHASH" => Command::Rehash,
            "RESTART" => Command::Restart,
            "DIE" => Command::Die,
            
            _ => Command::Unknown(command.to_string(), params),
        }
    }

    /// Get the command name as a string
    pub fn command_name(&self) -> &str {
        match self {
            Command::Nick(_) => "NICK",
            Command::User { .. } => "USER",
            Command::Pass(_) => "PASS",
            Command::Quit(_) => "QUIT",
            Command::Ping(_) => "PING",
            Command::Pong(_) => "PONG",
            Command::Join(_, _) => "JOIN",
            Command::Part(_, _) => "PART",
            Command::Topic { .. } => "TOPIC",
            Command::Names(_) => "NAMES",
            Command::List(_) => "LIST",
            Command::Privmsg { .. } => "PRIVMSG",
            Command::Notice { .. } => "NOTICE",
            Command::Who(_) => "WHO",
            Command::Whois(_) => "WHOIS",
            Command::Whowas(_, _) => "WHOWAS",
            Command::Query(_) => "QUERY",
            Command::Kick { .. } => "KICK",
            Command::Mode { .. } => "MODE",
            Command::Invite { .. } => "INVITE",
            Command::Motd(_) => "MOTD",
            Command::Version(_) => "VERSION",
            Command::Stats(_, _) => "STATS",
            Command::Time(_) => "TIME",
            Command::Info(_) => "INFO",
            Command::Cap { .. } => "CAP",
            Command::Authenticate(_) => "AUTHENTICATE",
            Command::Account(_) => "ACCOUNT",
            Command::Monitor { .. } => "MONITOR",
            Command::Metadata { .. } => "METADATA",
            Command::TagMsg { .. } => "TAGMSG",
            Command::Batch { .. } => "BATCH",
            Command::Redact { .. } => "REDACT",
            Command::MarkRead { .. } => "MARKREAD",
            Command::SetName { .. } => "SETNAME",
            Command::ChatHistory { .. } => "CHATHISTORY",
            Command::Oper { .. } => "OPER",
            Command::Kill { .. } => "KILL",
            Command::Rehash => "REHASH",
            Command::Restart => "RESTART",
            Command::Die => "DIE",
            Command::CtcpRequest { .. } => "PRIVMSG", // CTCP is sent via PRIVMSG
            Command::CtcpResponse { .. } => "NOTICE", // CTCP response via NOTICE
            Command::Unknown(cmd, _) => cmd,
        }
    }

    /// Check if this is a channel-related command
    pub fn is_channel_command(&self) -> bool {
        match self {
            Command::Join(_, _) |
            Command::Part(_, _) |
            Command::Topic { .. } |
            Command::Names(_) |
            Command::Kick { .. } => true,
            Command::Mode { target, .. } => target.starts_with('#') || target.starts_with('&'),
            _ => false,
        }
    }

    /// Check if this is a messaging command
    pub fn is_message_command(&self) -> bool {
        matches!(self, Command::Privmsg { .. } | Command::Notice { .. })
    }

    /// Check if this is an IRCv3 command
    pub fn is_ircv3_command(&self) -> bool {
        matches!(self,
            Command::Cap { .. } |
            Command::Authenticate(_) |
            Command::Account(_) |
            Command::Monitor { .. } |
            Command::Metadata { .. } |
            Command::TagMsg { .. } |
            Command::Batch { .. } |
            Command::Redact { .. } |
            Command::MarkRead { .. } |
            Command::SetName { .. } |
            Command::ChatHistory { .. }
        )
    }
}

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

    #[test]
    fn test_basic_command_parsing() {
        let cmd = Command::parse("PRIVMSG", vec!["#channel".to_string(), "Hello world".to_string()]);
        match cmd {
            Command::Privmsg { target, message } => {
                assert_eq!(target, "#channel");
                assert_eq!(message, "Hello world");
            }
            _ => panic!("Expected Privmsg command"),
        }
    }

    #[test]
    fn test_join_command_parsing() {
        let cmd = Command::parse("JOIN", vec!["#chan1,#chan2".to_string(), "key1,key2".to_string()]);
        match cmd {
            Command::Join(channels, keys) => {
                assert_eq!(channels, vec!["#chan1", "#chan2"]);
                assert_eq!(keys, vec!["key1", "key2"]);
            }
            _ => panic!("Expected Join command"),
        }
    }

    #[test]
    fn test_cap_command_parsing() {
        let cmd = Command::parse("CAP", vec!["LS".to_string(), "302".to_string()]);
        match cmd {
            Command::Cap { subcommand, params } => {
                assert_eq!(subcommand, "LS");
                assert_eq!(params, vec!["302"]);
            }
            _ => panic!("Expected Cap command"),
        }
    }

    #[test]
    fn test_command_name() {
        let cmd = Command::Privmsg { target: "#test".to_string(), message: "hello".to_string() };
        assert_eq!(cmd.command_name(), "PRIVMSG");
    }

    #[test]
    fn test_command_categories() {
        let privmsg = Command::Privmsg { target: "#test".to_string(), message: "hello".to_string() };
        let join = Command::Join(vec!["#test".to_string()], vec![]);
        let cap = Command::Cap { subcommand: "LS".to_string(), params: vec![] };

        assert!(privmsg.is_message_command());
        assert!(join.is_channel_command());
        assert!(cap.is_ircv3_command());
    }

    #[test]
    fn test_unknown_command() {
        let cmd = Command::parse("UNKNOWN", vec!["param1".to_string()]);
        match cmd {
            Command::Unknown(name, params) => {
                assert_eq!(name, "UNKNOWN");
                assert_eq!(params, vec!["param1"]);
            }
            _ => panic!("Expected Unknown command"),
        }
    }
}