-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathMailParser.cs
More file actions
76 lines (70 loc) · 2.2 KB
/
Copy pathMailParser.cs
File metadata and controls
76 lines (70 loc) · 2.2 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using TypeAgent.Core;
namespace TypeAgent;
public class MailParser
{
public static readonly MailParser Default = new MailParser();
MailParser() { }
public IEnumerable<KeyValuePair<string, string>> ParseParts(string message)
{
var lines = message.Split("\r\n");
string fieldName = null;
string fieldVal = null;
string curFieldName = null;
string curFieldVal = string.Empty;
int i = 0;
for (; i < lines.Length; ++i)
{
var line = lines[i];
var bodyStart = line.Trim();
if (string.IsNullOrEmpty(bodyStart))
{
++i;
break;
}
fieldName = null;
fieldVal = null;
int nameEndPos = line.IndexOf(':');
int valueStartPos = 0;
if (nameEndPos >= 0)
{
fieldName = line[..nameEndPos];
valueStartPos = nameEndPos + 1;
}
if (valueStartPos < line.Length)
{
fieldVal = line[(nameEndPos + 1)..];
}
if (!string.IsNullOrEmpty(fieldName))
{
if (!string.IsNullOrEmpty(curFieldName))
{
yield return new KeyValuePair<string, string>(curFieldName, curFieldVal);
}
curFieldName = fieldName;
curFieldVal = !string.IsNullOrEmpty(fieldVal) ? fieldVal.TrimStart() : fieldVal;
}
else if (!string.IsNullOrEmpty(fieldVal))
{
curFieldVal += fieldVal;
}
}
if (!string.IsNullOrEmpty(curFieldName))
{
yield return new KeyValuePair<string, string>(curFieldName, curFieldVal);
}
for (; i < lines.Length; ++i)
{
if (!string.IsNullOrEmpty(lines[i]))
{
break;
}
}
if (i < lines.Length)
{
string body = string.Join("\r\n", lines, i, lines.Length - i);
yield return new KeyValuePair<string, string>("Body", body);
}
}
}