microsoft/TypeAgent
Publicmirrored fromhttps://github.com/microsoft/TypeAgentAvailable
dotnet/email/App.cs
139lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. |
| 2 | // Licensed under the MIT License. |
| 3 | using System.Diagnostics; |
| 4 | using TypeAgent.Core; |
| 5 | |
| 6 | namespace TypeAgent; |
| 7 | |
| 8 | public class App |
| 9 | { |
| 10 | RootCommand _commands; |
| 11 | EmailExporter _exporter; |
| 12 | MailStats _stats; |
| 13 | |
| 14 | public App(Outlook outlook) |
| 15 | { |
| 16 | _exporter = new EmailExporter(outlook); |
| 17 | _stats = new MailStats(outlook); |
| 18 | |
| 19 | _commands = new RootCommand("Mail commands"); |
| 20 | _commands.AddCommand(Command_Quit()); |
| 21 | _commands.AddCommand(Command_Distribution()); |
| 22 | _commands.AddCommand(Command_ExportAll()); |
| 23 | } |
| 24 | |
| 25 | public EmailExporter Exporter => _exporter; |
| 26 | |
| 27 | public Command Command_Distribution() |
| 28 | { |
| 29 | Command command = new Command("distribution"); |
| 30 | var pathOption = new Option<string>("--outPath", "Output path"); |
| 31 | command.AddOption(pathOption); |
| 32 | command.SetHandler<string>((string outPath) => |
| 33 | { |
| 34 | var (counter, histogram) = _stats.GetSizeDistribution(); |
| 35 | ConsoleEx.WriteLineColor(ConsoleColor.Green, $"{counter} items"); |
| 36 | string csv = MailStats.PrintHistogram(histogram); |
| 37 | if (!string.IsNullOrEmpty(outPath)) |
| 38 | { |
| 39 | File.WriteAllText(outPath, csv); |
| 40 | } |
| 41 | Console.WriteLine(csv); |
| 42 | }, pathOption); |
| 43 | return command; |
| 44 | } |
| 45 | |
| 46 | public Command Command_ExportAll() |
| 47 | { |
| 48 | Command command = new Command("exportAll"); |
| 49 | var dirPath = new Option<string>("--destDir", "Output path"); |
| 50 | var maxMessages = new Option<int>("--maxMessages", () => -1, "Max messages to export"); |
| 51 | var bucket = new Option<bool>("--bucket", () => true, "Bucket messages by latest body size"); |
| 52 | var includeJson = new Option<bool>("--includeJson", () => true, "Also export to Json"); |
| 53 | command.AddOption(dirPath); |
| 54 | command.AddOption(maxMessages); |
| 55 | command.AddOption(bucket); |
| 56 | command.AddOption(includeJson); |
| 57 | command.SetHandler<string, int, bool, bool>((string dirPath, int maxMessages, bool bucket, bool includeJson) => |
| 58 | { |
| 59 | _exporter.ExportAll(dirPath, maxMessages, bucket, includeJson); |
| 60 | |
| 61 | }, dirPath, maxMessages, bucket, includeJson); |
| 62 | |
| 63 | return command; |
| 64 | } |
| 65 | |
| 66 | Command Command_Quit() |
| 67 | { |
| 68 | Command cmd = new Command("quit"); |
| 69 | cmd.SetHandler(() => Environment.Exit(0)); |
| 70 | return cmd; |
| 71 | } |
| 72 | |
| 73 | static void Main(string[] args) |
| 74 | { |
| 75 | Console.OutputEncoding = Encoding.UTF8; |
| 76 | args = EnsureArgs(args); |
| 77 | if (args == null || args.Length == 0) |
| 78 | { |
| 79 | return; |
| 80 | } |
| 81 | try |
| 82 | { |
| 83 | using Outlook outlook = new Outlook(); |
| 84 | var app = new App(outlook); |
| 85 | switch (args[0]) |
| 86 | { |
| 87 | default: |
| 88 | if (args[0].StartsWith('@')) |
| 89 | { |
| 90 | RunInteractive(app, args); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | app.Exporter.Export(args.ElementAtOrDefault(0), args.ElementAtOrDefault(1)); |
| 95 | } |
| 96 | break; |
| 97 | |
| 98 | case "--sender": |
| 99 | app.Exporter.ExportFrom(args.GetArg(1)); |
| 100 | break; |
| 101 | |
| 102 | case "--print": |
| 103 | app.Exporter.PrintEmail(args.GetArg(1)); |
| 104 | Console.ReadLine(); |
| 105 | return; |
| 106 | } |
| 107 | } |
| 108 | catch (System.Exception ex) |
| 109 | { |
| 110 | ConsoleEx.LogError(ex); |
| 111 | } |
| 112 | finally |
| 113 | { |
| 114 | COMObject.ReleaseAll(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static void RunInteractive(App app, string[] args) |
| 119 | { |
| 120 | while (true) |
| 121 | { |
| 122 | if (args.Length > 0) |
| 123 | { |
| 124 | args[0] = args[0][1..]; |
| 125 | var result = app._commands.Invoke(args); |
| 126 | if (result != 0) |
| 127 | { |
| 128 | Console.WriteLine($"Command returned {result}"); |
| 129 | } |
| 130 | } |
| 131 | args = ConsoleEx.GetInput("📬>"); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | static string[]? EnsureArgs(string[] args) |
| 136 | { |
| 137 | return args != null && args.Length > 0 ? args : ConsoleEx.GetInput("📬>"); |
| 138 | } |
| 139 | } |
| 140 | |