microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/email/BodyParser.cs
63lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | using TypeAgent.Core; |
| 5 | |
| 6 | namespace TypeAgent; |
| 7 | |
| 8 | public class BodyParser |
| 9 | { |
| 10 | public static readonly BodyParser Default = new BodyParser(); |
| 11 | |
| 12 | List<string> _delimiters; |
| 13 | Regex _splitBody; |
| 14 | |
| 15 | public BodyParser() |
| 16 | { |
| 17 | _delimiters = new List<string> |
| 18 | { |
| 19 | "From:", |
| 20 | "Sent:", |
| 21 | "To:", |
| 22 | "Subject:", |
| 23 | "-----Original Message-----", |
| 24 | "----- Forwarded by", |
| 25 | "________________________________________" |
| 26 | }; |
| 27 | _splitBody = new Regex("(?=From:)", RegexOptions.IgnoreCase); |
| 28 | |
| 29 | } |
| 30 | |
| 31 | public List<string> Delimiters => _delimiters; |
| 32 | |
| 33 | public string GetLatest(string body) |
| 34 | { |
| 35 | if (string.IsNullOrEmpty(body)) |
| 36 | { |
| 37 | return string.Empty; |
| 38 | } |
| 39 | int firstDelimiterAt = -1; |
| 40 | foreach (var delimiter in _delimiters) |
| 41 | { |
| 42 | int index = body.IndexOf(delimiter); |
| 43 | if (index >= 0 && (firstDelimiterAt == -1 || index < firstDelimiterAt)) |
| 44 | { |
| 45 | firstDelimiterAt = index; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | if (firstDelimiterAt >= 0) |
| 50 | { |
| 51 | return body[..firstDelimiterAt].Trim(); |
| 52 | } |
| 53 | |
| 54 | return body; |
| 55 | } |
| 56 | |
| 57 | public string[] SplitForwardedEmail(string email) |
| 58 | { |
| 59 | string[] parts = _splitBody.Split(email); |
| 60 | return parts.FilterEmpty().ToArray(); |
| 61 | } |
| 62 | |
| 63 | } |
| 64 | |