-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProcessFile.cs
208 lines (170 loc) · 7.58 KB
/
ProcessFile.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MailRepeater
{
public class MailProcessor
{
List<string> keepHeaders;
Dictionary<string, string> destinationList;
string sender;
List<HeaderItem> inputHeaders;
string inputContent;
List<string> outgoingEmailList;
public bool HasRecipients
{
get
{
return outgoingEmailList.Count > 0;
}
}
public MailProcessor(List<string> keepHeaders, Dictionary<string, string> destinationList, string sender)
{
this.keepHeaders = keepHeaders;
this.destinationList = destinationList;
this.sender = sender;
}
public string Process(List<string> lines)
{
ParseInput(lines);
GetDestinations();
return BuildEmail();
}
void ParseInput(List<string> lines)
{
//split up the original into headers and content
inputHeaders = new List<HeaderItem>();
bool startContent = false;
HeaderItem currentItem = new HeaderItem();
for (int counter = 0; counter < lines.Count; counter ++)
{
if (!startContent) //still in header section?
{
if (string.IsNullOrEmpty(lines[counter].Trim()))
{
startContent = true; //indicate that header rows have ended
if (!string.IsNullOrEmpty(currentItem.Name))
inputHeaders.Add(currentItem.Copy());
}
else
{
if (lines[counter].StartsWith(" ") || lines[counter].StartsWith("\t"))
currentItem.Lines.Add(lines[counter].Trim()); //add line to existing header record
else
{
if (!string.IsNullOrEmpty(currentItem.Name))
{
inputHeaders.Add(currentItem.Copy());
currentItem = new HeaderItem();
}
if (lines[counter].Contains(':')) //split to get the name of the header, and first line value
{
currentItem.Name = lines[counter].Split(':')[0].Trim();
string line = lines[counter] + " ";
line = line.Substring(line.IndexOf(':') + 1).Trim();
currentItem.Lines.Add(line);
}
}
}
}
else
inputContent += lines[counter] + "\r\n";
}
}
void GetDestinations()
{
//get a list of destinations from the original email
string originalDestStr = "";
foreach (string line in inputHeaders.Where(a => a.Name.ToLower() == "to").Select(a => a.Unfold()))
originalDestStr += " " + line;
foreach (string line in inputHeaders.Where(a => a.Name.ToLower() == "cc").Select(a => a.Unfold()))
originalDestStr += " " + line;
foreach (string line in inputHeaders.Where(a => a.Name.ToLower() == "bcc").Select(a => a.Unfold()))
originalDestStr += " " + line;
originalDestStr = originalDestStr.Replace("<", " ").Replace(">", " ").Replace("\"", " ").Replace(",", " ").Replace(";", " ");
List<string> originalDest = originalDestStr.Split(' ').Where(a => a.Contains("@")).Select(a => a.ToLower().Trim()).ToList();
outgoingEmailList = new List<string>();
//try to map each of the original destinations
foreach (string original in originalDest)
{
//1. Is there an exact match?
if (destinationList.ContainsKey(original))
{
if (!string.IsNullOrEmpty(destinationList[original]))
outgoingEmailList.Add(destinationList[original]);
}
else
{
//2. Can we match on domain?
string matchDomain = "*" + original.Substring(original.IndexOf("@"));
if (destinationList.ContainsKey(matchDomain))
{
if (!string.IsNullOrEmpty(destinationList[matchDomain]))
outgoingEmailList.Add(destinationList[matchDomain]);
}
else
{
//3. If all else fails, use the global match
if (!string.IsNullOrEmpty(destinationList["*"]))
outgoingEmailList.Add(destinationList["*"]);
}
}
}
outgoingEmailList = outgoingEmailList.Distinct().ToList();
}
string BuildEmail()
{
//Build the headers
List<HeaderItem> outputHeaders = new List<HeaderItem>();
foreach (HeaderItem item in inputHeaders)
if (keepHeaders.Contains(item.Name.ToLower()))
outputHeaders.Add(item);
HeaderItem fromHeader = new HeaderItem();
fromHeader.Name = "From";
fromHeader.Lines.Add(sender);
outputHeaders.Add(fromHeader);
string originalTo = "";
foreach (HeaderItem item in inputHeaders)
if (item.Name.ToLower().Equals("to"))
originalTo = item.Unfold();
HeaderItem originalToHeader = new HeaderItem();
originalToHeader.Name = "X-Original-To";
originalToHeader.Lines.Add(originalTo);
outputHeaders.Add(originalToHeader);
string replyTo = "";
foreach (HeaderItem item in inputHeaders)
if (item.Name.ToLower().Equals("from"))
replyTo = item.Unfold();
HeaderItem replyToHeader = new HeaderItem();
replyToHeader.Name = "Reply-To";
replyToHeader.Lines.Add(replyTo);
outputHeaders.Add(replyToHeader);
HeaderItem toHeader = new HeaderItem();
toHeader.Name = "To";
for (int counter = 0; counter < outgoingEmailList.Count; counter ++)
{
if (counter < outgoingEmailList.Count - 1)
toHeader.Lines.Add(outgoingEmailList[counter] + ", ");
else
toHeader.Lines.Add(outgoingEmailList[counter]);
}
outputHeaders.Add(toHeader);
//Add the headers together
string output = "";
foreach (HeaderItem item in outputHeaders)
{
output += item.Name + ": ";
if (item.Lines.Count > 0)
output += item.Lines[0] + "\r\n";
if (item.Lines.Count > 1)
for (int counter = 1; counter < item.Lines.Count; counter++)
output += " " + item.Lines[counter] + "\r\n";
}
output += "\r\n";
output += inputContent;
return output;
}
}
}