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
use serde::{Deserialize, Serialize};
/// # SMTP Commands
///
/// This enum represents the commands that the SMTP server can receive.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum Commands {
/// HELO Command
///
/// This command is used to identify the client to the server.
HELO,
/// Extended HELO
///
/// Usually used for getting the server capabilities.
EHLO,
/// MAIL Command
///
/// This command is used to specify the sender of the email.
MAIL,
/// RCPT Command
///
/// This command is used to specify the recipient of the email.
RCPT,
/// DATA Command
/// This command is used to send the email data.
DATA,
/// RSET Command
///
/// This command is used to reset the session.
RSET,
/// VRFY Command
///
/// This command is used to verify the email address.
VRFY,
/// EXPN Command
///
/// This command is used to expand the mailing list.
EXPN,
/// HELP Command
///
/// This command is used to get help from the server.
HELP,
/// NOOP Command
///
/// This command is used to do nothing.
NOOP,
/// QUIT Command
///
/// This command is used to quit the session.
QUIT,
/// AUTH Command
///
/// This command is used to authenticate the user.
AUTH,
/// STARTTLS Command
///
/// This command is used to start the TLS session.
STARTTLS,
/// Unknown Command
///
/// This command is used when the command is not recognized.
UNKNOWN(String),
}
impl Commands {
/// # From Bytes
///
/// This function converts a byte array to a Commands enum.
pub fn from_bytes(bytes: &[u8]) -> Self {
// Convert bytes to string, uppercase, trim and convert to string
let bytes_to_string = String::from_utf8_lossy(bytes)
.to_uppercase()
.trim()
.to_string();
match bytes_to_string.as_str() {
"HELO" => Commands::HELO,
"EHLO" => Commands::EHLO,
"MAIL" => Commands::MAIL,
"RCPT" => Commands::RCPT,
"DATA" => Commands::DATA,
"RSET" => Commands::RSET,
"VRFY" => Commands::VRFY,
"EXPN" => Commands::EXPN,
"HELP" => Commands::HELP,
"NOOP" => Commands::NOOP,
"QUIT" => Commands::QUIT,
"AUTH" => Commands::AUTH,
"STARTTLS" => Commands::STARTTLS,
_ => Commands::UNKNOWN(bytes_to_string),
}
}
}