-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmtpProtocol.cs
172 lines (152 loc) · 5.48 KB
/
SmtpProtocol.cs
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
using System;
using System.Net.Sockets;
namespace smtpServer
{
public class SmtpProtocol
{
private string Bye = "221 Bye";
private string Ok = "250 OK";
private string BadCommand = "500 Syntax error, command unrecognized";
private string BadParams = "501 Syntax error in parameters or arguments";
private string BadSequence = "503 Bad sequence of commands";
private string BadUser = "550 User not available";
private string Help = "250 OK. Supported commands: HELO, EHLO, MAIL, RCPT, DATA, NOOP, RSET, VRFY, QUIT";
private string InitialMessage = "220 Service ready";
private string StartInput = "354 Start mail input; end with <CRLF>.";
private bool InitialMessageSent = false;
ServerSocket Server;
string Domain;
public SmtpProtocol(ServerSocket server, string domain)
{
Server = server;
Domain = domain;
}
private AddressString SplitAddress(string message)
{
string beginning = message.Substring(0, message.IndexOf("<") + 1);
string ending = message[message.Length - 1].ToString();
string address = message.Remove(message.Length - 1, ending.Length).Remove(0, beginning.Length);
return new AddressString(beginning, ending, address);
}
public void TestAddressSplitting(string message, AddressString expected)
{
var gotten = SplitAddress(message);
if (!expected.Equals(gotten))
{
System.Console.WriteLine(gotten.Address);
System.Console.WriteLine(gotten.Start);
System.Console.WriteLine(gotten.End);
System.Environment.Exit(1);
}
System.Console.WriteLine("Splitting good");
return;
}
public void HandleClient()
{
if (!InitialMessageSent)
{
InitialMessageSent = true;
Server.SendClientMessage(InitialMessage);
}
var message = Server.GetClientMessage();
if (message.Contains("noop"))
{
Server.SendClientMessage(Ok);
}
else if (message.Contains("ehlo"))
{
Server.SendClientMessage(Help);
}
else if (message.Contains("helo"))
{
Server.SendClientMessage(Ok);
}
else if (message.Contains("QUIT"))
{
Server.SendClientMessage(Bye);
Server.CloseClient();
System.Environment.Exit(1);
}
else if (message.Contains("rset"))
{
Server.SendClientMessage(Ok);
}
else if (message.Contains("vrfy"))
{
Server.SendClientMessage(VrfyCommand(message));
}
else if (message.Contains("mail FROM:<"))
{
var email = HandleEmailRequest(message);
if (email != null)
{
EmailSaver.Save((Email)email);
}
}
else
{
Server.SendClientMessage(BadCommand);
}
HandleClient();
}
private string VerifyMessage(string message, string start, string end)
{
AddressString splitMessage = SplitAddress(message);
if (splitMessage.Start.Equals(start) && splitMessage.End.Equals(end))
{
return splitMessage.Address;
}
return BadSequence;
}
private string VrfyCommand(string email)
{
var domainEnding = $"@{Domain}.com";
if (!(email.Contains("@") && email.Contains(".")))
{
return BadParams;
}
else if (!email.EndsWith(domainEnding))
{
return BadUser;
}
return Ok;
}
private Email? HandleEmailRequest(string message)
{
var sender = VerifyMessage(message, "mail FROM:<", ">");
if (sender.Equals(BadSequence))
{
Server.SendClientMessage(BadSequence);
return null;
}
Server.SendClientMessage(Ok);
List<string> recipients = new List<string>();
while (true)
{
var rcptMessage = Server.GetClientMessage();
if (rcptMessage.Contains("data"))
{
if (recipients.Count() == 0)
{
Server.SendClientMessage(BadSequence);
return null;
}
break;
}
var recipient = VerifyMessage(rcptMessage, "rcpt TO:<", ">");
if (recipient.Equals(BadSequence))
{
Server.SendClientMessage(BadSequence);
return null;
}
recipients.Add(recipient);
Server.SendClientMessage(Ok);
}
Server.SendClientMessage(StartInput);
var clientMessage = Server.GetClientMessage();
Server.SendClientMessage(Ok);
var headers = EmailParser.ParseHeaders(clientMessage);
return new Email(sender, recipients, EmailParser.ParseMessage(clientMessage), headers.Cc, headers.Bcc);
}
}
}