-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Email Processing - first steps (#204)
First cuts: * Email knowledge processing * NET code to use COM interop to parse and export .msg files * Bugs fixes
- Loading branch information
Showing
28 changed files
with
1,310 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace TypeAgent; | ||
|
||
public class BodyParser | ||
{ | ||
public static readonly BodyParser Default = new BodyParser(); | ||
|
||
List<string> _delimiters; | ||
public BodyParser() | ||
{ | ||
_delimiters = new List<string> | ||
{ | ||
"From:", | ||
"Sent:", | ||
"To:", | ||
"Subject:", | ||
"-----Original Message-----", | ||
"----- Forwarded by", | ||
"________________________________________" | ||
}; | ||
} | ||
|
||
public List<string> Delimiters => _delimiters; | ||
|
||
public string GetLatest(string body) | ||
{ | ||
ArgumentException.ThrowIfNullOrEmpty(body); | ||
|
||
int firstDelimiterAt = -1; | ||
foreach (var delimiter in _delimiters) | ||
{ | ||
int index = body.IndexOf(delimiter); | ||
if (index >= 0 && (firstDelimiterAt == -1 || index < firstDelimiterAt)) | ||
{ | ||
firstDelimiterAt = index; | ||
} | ||
} | ||
|
||
if (firstDelimiterAt >= 0) | ||
{ | ||
return body[..firstDelimiterAt].Trim(); | ||
} | ||
|
||
return body; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
using System.Runtime.InteropServices; | ||
|
||
namespace TypeAgent; | ||
|
||
public class COMObject : IDisposable | ||
{ | ||
bool _disposed; | ||
|
||
public COMObject() | ||
{ | ||
} | ||
|
||
~COMObject() | ||
{ | ||
Dispose(false); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
#pragma warning disable CA1063 | ||
void Dispose(bool fromDispose) | ||
{ | ||
if (!_disposed) | ||
{ | ||
OnDispose(); | ||
_disposed = true; | ||
} | ||
} | ||
|
||
protected virtual void OnDispose() {} | ||
|
||
public static void Release(object value) | ||
{ | ||
#pragma warning disable CA1416 | ||
if (value != null) | ||
{ | ||
Marshal.ReleaseComObject(value); | ||
} | ||
} | ||
|
||
public static void ReleaseAll() | ||
{ | ||
GC.Collect(); | ||
GC.WaitForFullGCComplete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace TypeAgent.Core; | ||
|
||
public static class DirectoryEx | ||
{ | ||
public static void Ensure(string path) | ||
{ | ||
ArgumentException.ThrowIfNullOrEmpty(path, nameof(path)); | ||
|
||
if (!Directory.Exists(path)) | ||
{ | ||
DirectoryInfo info = Directory.CreateDirectory(path); | ||
if (!info.Exists) | ||
{ | ||
throw new DirectoryNotFoundException(path); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace TypeAgent.Core; | ||
|
||
/// <summary> | ||
/// Json makes the idiomatic Javascript Json.Stringify and Json.Parse APIs available to .NET by | ||
/// wrapping the .NET System.Text.Json serialization | ||
/// </summary> | ||
public class Json | ||
{ | ||
public static JsonSerializerOptions DefaultOptions() | ||
{ | ||
var options = new JsonSerializerOptions | ||
{ | ||
IncludeFields = true, | ||
IgnoreReadOnlyFields = true, | ||
AllowTrailingCommas = true, | ||
ReadCommentHandling = JsonCommentHandling.Skip, | ||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, | ||
}; | ||
|
||
options.Converters.Add(new JsonStringEnumConverter()); | ||
return options; | ||
} | ||
|
||
static readonly Json s_default = new Json(); | ||
static readonly Json s_indented = new Json(true); | ||
|
||
JsonSerializerOptions _options; | ||
|
||
public Json(bool indented = false) | ||
{ | ||
_options = DefaultOptions(); | ||
_options.WriteIndented = indented; | ||
} | ||
|
||
/// <summary> | ||
/// Turn the given object into a JSON string | ||
/// </summary> | ||
/// <param name="value"></param> | ||
/// <param name="indented"></param> | ||
/// <returns></returns> | ||
public static string Stringify(object value, bool indented = true) | ||
{ | ||
return indented ? | ||
s_indented.Serialize(value) : | ||
s_default.Serialize(value); | ||
} | ||
|
||
/// <summary> | ||
/// Stringify value of type T | ||
/// </summary> | ||
/// <typeparam name="T">value type</typeparam> | ||
/// <param name="value">value to stringify</param> | ||
/// <param name="indented">if true, produce indented json</param> | ||
/// <returns></returns> | ||
public static string Stringify<T>(T value, bool indented = true) | ||
{ | ||
return indented ? | ||
s_indented.Serialize(value) : | ||
s_default.Serialize(value); | ||
} | ||
|
||
/// <summary> | ||
/// Parse Json into an object of the given type | ||
/// </summary> | ||
/// <param name="json">json string</param> | ||
/// <param name="type">Deserialize json to this type</param> | ||
/// <returns></returns> | ||
public static object? Parse(string json, Type type) | ||
{ | ||
return s_default.Deserialize(json, type); | ||
} | ||
|
||
/// <summary> | ||
/// Parse Json into an object of the given type | ||
/// </summary> | ||
/// <typeparam name="T">destination type</typeparam> | ||
/// <param name="json">json string</param> | ||
/// <returns></returns> | ||
public static T Parse<T>(string json) | ||
{ | ||
return (T)Parse(json, typeof(T)); | ||
} | ||
|
||
/// <summary> | ||
/// Parse Json from a stream into an object of the given type | ||
/// </summary> | ||
/// <typeparam name="T">destination type</typeparam> | ||
/// <param name="jsonStream">stream to read from</param> | ||
/// <returns></returns> | ||
public static T Parse<T>(Stream jsonStream) | ||
{ | ||
return (T)s_default.Deserialize(jsonStream, typeof(T)); | ||
} | ||
|
||
string Serialize<T>(T value) | ||
{ | ||
return JsonSerializer.Serialize(value, _options); | ||
} | ||
|
||
object? Deserialize(string json, Type type) | ||
{ | ||
return JsonSerializer.Deserialize(json, type, _options); | ||
} | ||
|
||
object? Deserialize(Stream jsonStream, Type type) | ||
{ | ||
return JsonSerializer.Deserialize(jsonStream, type, _options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace TypeAgent.Core; | ||
|
||
public static class PathEx | ||
{ | ||
public static bool IsDirectory(string filePath) | ||
{ | ||
try | ||
{ | ||
return File.GetAttributes(filePath).HasFlag(FileAttributes.Directory); | ||
} | ||
catch (FileNotFoundException) | ||
{ | ||
} | ||
catch (DirectoryNotFoundException) | ||
{ | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace TypeAgent.Core; | ||
|
||
public static class StringEx | ||
{ | ||
public static StringBuilder AppendHeader(this StringBuilder sb, string name, string? value) | ||
{ | ||
if (!string.IsNullOrEmpty(value)) | ||
{ | ||
sb.Append(name); | ||
sb.Append(": "); | ||
sb.AppendLine(value); | ||
} | ||
return sb; | ||
} | ||
|
||
public static string[] ParseCommandLine(this string cmdLine) | ||
{ | ||
var regex = new Regex("\"[^\"]+\"|[^\"\\s]+"); | ||
var matches = regex.Matches(cmdLine); | ||
var args = new List<string>(); | ||
foreach (Match match in matches) | ||
{ | ||
// Remove the enclosing quotes from the matched strings | ||
args.Add(match.Value.Trim('"')); | ||
} | ||
|
||
return args.ToArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
namespace TypeAgent.Core; | ||
|
||
public static class Verify | ||
{ | ||
public static void FileExists(string path) | ||
{ | ||
if (!File.Exists(path)) | ||
{ | ||
throw new FileNotFoundException(path); | ||
} | ||
} | ||
|
||
public static void DirectoryExists(string path) | ||
{ | ||
if (!Directory.Exists(path)) | ||
{ | ||
throw new DirectoryNotFoundException(path); | ||
} | ||
} | ||
} |
Oops, something went wrong.