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
//! IRC numeric replies and error codes
//!
//! This module contains the standard IRC numeric reply codes and error messages
//! as defined in RFC 1459, RFC 2812, and various IRCv3 specifications.

use crate::message::IrcMessage;

/// IRC numeric replies and error codes
#[derive(Debug, Clone)]
pub enum Reply {
    // Welcome sequence (001-005)
    /// 001 RPL_WELCOME
    Welcome { nick: String, network: String },
    /// 002 RPL_YOURHOST
    YourHost { nick: String, servername: String, version: String },
    /// 003 RPL_CREATED
    Created { nick: String, date: String },
    /// 004 RPL_MYINFO
    MyInfo { nick: String, servername: String, version: String, usermodes: String, chanmodes: String },
    /// 005 RPL_ISUPPORT
    ISupport { nick: String, tokens: Vec<String> },
    
    // Channel operations (331-366)
    /// 331 RPL_NOTOPIC
    NoTopic { nick: String, channel: String },
    /// 332 RPL_TOPIC
    Topic { nick: String, channel: String, topic: String },
    /// 353 RPL_NAMREPLY
    NamReply { nick: String, symbol: char, channel: String, names: Vec<String> },
    /// 366 RPL_ENDOFNAMES
    EndOfNames { nick: String, channel: String },
    
    // MOTD (372-376, 422)
    /// 375 RPL_MOTDSTART
    MotdStart { nick: String, server: String },
    /// 372 RPL_MOTD
    Motd { nick: String, line: String },
    /// 376 RPL_ENDOFMOTD
    EndOfMotd { nick: String },
    /// 422 ERR_NOMOTD
    NoMotd { nick: String },
    
    // Error replies (400-599)
    /// 401 ERR_NOSUCHNICK
    NoSuchNick { nick: String, target: String },
    /// 403 ERR_NOSUCHCHANNEL
    NoSuchChannel { nick: String, channel: String },
    /// 404 ERR_CANNOTSENDTOCHAN
    CannotSendToChan { nick: String, channel: String },
    /// 442 ERR_NOTONCHANNEL
    NotOnChannel { nick: String, channel: String },
    /// 433 ERR_NICKNAMEINUSE
    NicknameInUse { nick: String, attempted: String },
    /// 461 ERR_NEEDMOREPARAMS
    NeedMoreParams { nick: String, command: String },
    /// 462 ERR_ALREADYREGISTERED
    AlreadyRegistered { nick: String },
    /// 421 ERR_UNKNOWNCOMMAND
    UnknownCommand { nick: String, command: String },
    /// 464 ERR_PASSWDMISMATCH
    PasswdMismatch { nick: String },
    /// 451 ERR_NOTREGISTERED
    NotRegistered { nick: String },
    
    // Additional replies for compatibility
    /// 432 ERR_ERRONEUSNICKNAME
    ErroneousNickname { nick: String, attempted: String },
    /// 475 ERR_BADCHANNELKEY
    BadChannelKey { nick: String, channel: String },
    /// 471 ERR_CHANNELISFULL
    ChannelIsFull { nick: String, channel: String },
    /// 482 ERR_CHANOPRIVSNEEDED
    ChanOpPrivsNeeded { nick: String, channel: String },
    /// 441 ERR_USERNOTINCHANNEL
    UserNotInChannel { nick: String, target: String, channel: String },
    /// 324 RPL_CHANNELMODEIS
    ChannelModeIs { nick: String, channel: String, modes: String, params: Vec<String> },
    /// 322 RPL_LIST
    List { nick: String, channel: String, visible: usize, topic: String },
    /// 315 RPL_ENDOFWHO
    EndOfWho { nick: String, target: String },
    /// 311 RPL_WHOISUSER
    WhoisUser { nick: String, target: String, username: String, host: String, realname: String },
    /// 312 RPL_WHOISSERVER
    WhoisServer { nick: String, target: String, server: String, info: String },
    /// 318 RPL_ENDOFWHOIS
    EndOfWhois { nick: String, target: String },
    /// 321 RPL_LISTSTART
    ListStart { nick: String },
    /// 323 RPL_LISTEND
    ListEnd { nick: String },
}

impl Reply {
    /// Convert reply to IRC message
    pub fn to_message(&self, server_name: &str) -> IrcMessage {
        match self {
            Reply::Welcome { nick, network } => {
                IrcMessage::new("001")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        format!("Welcome to the {} IRC Network, {}", network, nick),
                    ])
            }
            Reply::YourHost { nick, servername, version } => {
                IrcMessage::new("002")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        format!("Your host is {}, running version {}", servername, version),
                    ])
            }
            Reply::Created { nick, date } => {
                IrcMessage::new("003")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        format!("This server was created {}", date),
                    ])
            }
            Reply::MyInfo { nick, servername, version, usermodes, chanmodes } => {
                IrcMessage::new("004")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        servername.clone(),
                        version.clone(),
                        usermodes.clone(),
                        chanmodes.clone(),
                    ])
            }
            Reply::ISupport { nick, tokens } => {
                let mut params = vec![nick.clone()];
                params.extend(tokens.clone());
                params.push("are supported by this server".to_string());
                IrcMessage::new("005")
                    .with_prefix(server_name)
                    .with_params(params)
            }
            Reply::NoTopic { nick, channel } => {
                IrcMessage::new("331")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        channel.clone(),
                        "No topic is set".to_string(),
                    ])
            }
            Reply::Topic { nick, channel, topic } => {
                IrcMessage::new("332")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        channel.clone(),
                        topic.clone(),
                    ])
            }
            Reply::NamReply { nick, symbol, channel, names } => {
                let mut params = vec![
                    nick.clone(),
                    symbol.to_string(),
                    channel.clone(),
                ];
                let names_str = names.join(" ");
                params.push(names_str);
                IrcMessage::new("353")
                    .with_prefix(server_name)
                    .with_params(params)
            }
            Reply::EndOfNames { nick, channel } => {
                IrcMessage::new("366")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        channel.clone(),
                        "End of /NAMES list".to_string(),
                    ])
            }
            Reply::MotdStart { nick, server } => {
                IrcMessage::new("375")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        format!("- {} Message of the day -", server),
                    ])
            }
            Reply::Motd { nick, line } => {
                IrcMessage::new("372")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        format!("- {}", line),
                    ])
            }
            Reply::EndOfMotd { nick } => {
                IrcMessage::new("376")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        "End of /MOTD command".to_string(),
                    ])
            }
            Reply::NoMotd { nick } => {
                IrcMessage::new("422")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        "MOTD File is missing".to_string(),
                    ])
            }
            Reply::NoSuchNick { nick, target } => {
                IrcMessage::new("401")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        target.clone(),
                        "No such nick/channel".to_string(),
                    ])
            }
            Reply::NoSuchChannel { nick, channel } => {
                IrcMessage::new("403")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        channel.clone(),
                        "No such channel".to_string(),
                    ])
            }
            Reply::CannotSendToChan { nick, channel } => {
                IrcMessage::new("404")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        channel.clone(),
                        "Cannot send to channel".to_string(),
                    ])
            }
            Reply::NotOnChannel { nick, channel } => {
                IrcMessage::new("442")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        channel.clone(),
                        "You're not on that channel".to_string(),
                    ])
            }
            Reply::NicknameInUse { nick, attempted } => {
                IrcMessage::new("433")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        attempted.clone(),
                        "Nickname is already in use".to_string(),
                    ])
            }
            Reply::NeedMoreParams { nick, command } => {
                IrcMessage::new("461")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        command.clone(),
                        "Not enough parameters".to_string(),
                    ])
            }
            Reply::AlreadyRegistered { nick } => {
                IrcMessage::new("462")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        "You may not reregister".to_string(),
                    ])
            }
            Reply::UnknownCommand { nick, command } => {
                IrcMessage::new("421")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        command.clone(),
                        "Unknown command".to_string(),
                    ])
            }
            Reply::PasswdMismatch { nick } => {
                IrcMessage::new("464")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        "Password incorrect".to_string(),
                    ])
            }
            Reply::NotRegistered { nick } => {
                IrcMessage::new("451")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        "You have not registered".to_string(),
                    ])
            }
            Reply::ErroneousNickname { nick, attempted } => {
                IrcMessage::new("432")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        attempted.clone(),
                        "Erroneous nickname".to_string(),
                    ])
            }
            Reply::BadChannelKey { nick, channel } => {
                IrcMessage::new("475")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        channel.clone(),
                        "Cannot join channel (+k)".to_string(),
                    ])
            }
            Reply::ChannelIsFull { nick, channel } => {
                IrcMessage::new("471")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        channel.clone(),
                        "Cannot join channel (+l)".to_string(),
                    ])
            }
            Reply::ChanOpPrivsNeeded { nick, channel } => {
                IrcMessage::new("482")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        channel.clone(),
                        "You're not channel operator".to_string(),
                    ])
            }
            Reply::UserNotInChannel { nick, target, channel } => {
                IrcMessage::new("441")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        target.clone(),
                        channel.clone(),
                        "They aren't on that channel".to_string(),
                    ])
            }
            Reply::ChannelModeIs { nick, channel, modes, params } => {
                let mut msg_params = vec![nick.clone(), channel.clone(), modes.clone()];
                msg_params.extend(params.clone());
                IrcMessage::new("324")
                    .with_prefix(server_name)
                    .with_params(msg_params)
            }
            Reply::List { nick, channel, visible, topic } => {
                IrcMessage::new("322")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        channel.clone(),
                        visible.to_string(),
                        topic.clone(),
                    ])
            }
            Reply::EndOfWho { nick, target } => {
                IrcMessage::new("315")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        target.clone(),
                        "End of /WHO list".to_string(),
                    ])
            }
            Reply::WhoisUser { nick, target, username, host, realname } => {
                IrcMessage::new("311")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        target.clone(),
                        username.clone(),
                        host.clone(),
                        "*".to_string(),
                        realname.clone(),
                    ])
            }
            Reply::WhoisServer { nick, target, server, info } => {
                IrcMessage::new("312")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        target.clone(),
                        server.clone(),
                        info.clone(),
                    ])
            }
            Reply::EndOfWhois { nick, target } => {
                IrcMessage::new("318")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        target.clone(),
                        "End of /WHOIS list".to_string(),
                    ])
            }
            Reply::ListStart { nick } => {
                IrcMessage::new("321")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        "Channel".to_string(),
                        "Users  Name".to_string(),
                    ])
            }
            Reply::ListEnd { nick } => {
                IrcMessage::new("323")
                    .with_prefix(server_name)
                    .with_params(vec![
                        nick.clone(),
                        "End of /LIST".to_string(),
                    ])
            }
        }
    }
}

impl From<Reply> for crate::IrcMessage {
    fn from(reply: Reply) -> Self {
        reply.to_message("ironchatd.local")
    }
}