-
Notifications
You must be signed in to change notification settings - Fork 5
/
ThisAddIn.cs
271 lines (240 loc) · 10.4 KB
/
ThisAddIn.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using FlexConfirmMail.Dialog;
using System.Windows.Forms;
using System.Windows.Media;
using System.Windows.Interop;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Collections.Generic;
using System;
namespace FlexConfirmMail
{
public partial class ThisAddIn
{
private Outlook.Explorers Explorers { get; set; } = null;
private List<Outlook.Explorer> ExplorerList { get; set; } = new List<Outlook.Explorer>();
private Outlook.Inspectors Inspectors { get; set; } = null;
private Dictionary<string, Outlook.MailItem> SelectedMailDictionary { get; set; } = new Dictionary<string, Outlook.MailItem>();
private Dictionary<string, List<RecipientInfo>> EntryIdToOriginalRecipientsDictionary { get; set; } = new Dictionary<string, List<RecipientInfo>>();
private string OutBoxFolderPath { get; set; } = null;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(ThisAddIn_ItemSend);
Explorers = Application.Explorers;
foreach (Outlook.Explorer explorer in Explorers)
{
explorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(ThisAddIn_SelectionChange);
ExplorerList.Add(explorer);
}
Explorers.NewExplorer += new Outlook.ExplorersEvents_NewExplorerEventHandler(ThisAddIn_NewExplorer);
Inspectors = Application.Inspectors;
Inspectors.NewInspector += new Outlook.InspectorsEvents_NewInspectorEventHandler(ThisAddIn_NewInspector);
try
{
OutBoxFolderPath = Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox).FolderPath;
}
catch
{
// If the target folder type does not exist, Outlook raises an error.
// It is a possible situation, so ignore it.
}
ShowBanner();
}
private void ThisAddIn_NewExplorer(Outlook.Explorer explorer)
{
explorer.SelectionChange += new Outlook.ExplorerEvents_10_SelectionChangeEventHandler(ThisAddIn_SelectionChange);
ExplorerList.Add(explorer);
}
private void SaveOriginalRecipients(object response, ref bool cancel)
{
Outlook.MailItem mailItem = response as Outlook.MailItem;
var mailID = GenerateMailID(mailItem);
if (string.IsNullOrEmpty(mailID))
{
return;
}
var originalRecipients = new List<RecipientInfo>();
foreach (Outlook.Recipient recp in mailItem.Recipients)
{
originalRecipients.Add(new RecipientInfo(recp));
}
EntryIdToOriginalRecipientsDictionary[mailID] = originalRecipients;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
}
private void ThisAddIn_NewInspector(Outlook.Inspector inspector)
{
object item = inspector.CurrentItem;
if (item is Outlook.MailItem mailItem)
{
SaveToMailCacheDictionary(mailItem);
}
}
private void ThisAddIn_SelectionChange()
{
Outlook.Explorer acriveExplorer = Application.ActiveExplorer();
if (!string.IsNullOrEmpty(OutBoxFolderPath) &&
StringComparer.InvariantCultureIgnoreCase.Equals(acriveExplorer.CurrentFolder.FolderPath, OutBoxFolderPath))
{
// When deferred delivery is enabled, a mail is moved to the outbox and delivered after spending the delay time.
// If we access any of properties of the mail in the outbox, the mail is regarded as "edited" and not delivered
// even after spending the delay time.
// So if the folder is the outbox, return before accessing properties of the mail.
// Note that if a mail in the outbox is opened with a new inspector, it is regarded as "edited", which is Outlook's
// specification, so we don't add this check for ThisAddIn_NewInspector.
return;
}
if (acriveExplorer.Selection.Count > 0)
{
object item = acriveExplorer.Selection[1];
if (item is Outlook.MailItem mailItem)
{
SaveToMailCacheDictionary(mailItem);
}
}
}
private void SaveToMailCacheDictionary(Outlook.MailItem mailItem)
{
if (mailItem.Sender == null)
{
//NewMail, can't reply
return;
}
var id = mailItem.EntryID;
if (SelectedMailDictionary.ContainsKey(id))
{
return;
}
Outlook.ItemEvents_10_Event mailEvent = mailItem;
mailEvent.Reply += new Outlook.ItemEvents_10_ReplyEventHandler(SaveOriginalRecipients);
mailEvent.ReplyAll += new Outlook.ItemEvents_10_ReplyAllEventHandler(SaveOriginalRecipients);
SelectedMailDictionary[id] = mailItem;
}
private void ThisAddIn_ItemSend(object Item, ref bool Cancel)
{
Outlook.MailItem mail = (Outlook.MailItem)Item;
// Some users reported that Intel Graphic + Win10 causes
// a blank screen. Diable Hardware Accerelation.
RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;
// First of all, enable Cancel flag. This makes Outlook
// NOT to send the mail even if something goes awry.
Cancel = true;
try
{
if (DoCheck(mail))
{
Cancel = false;
QueueLogger.Log("Check finished [send=yes]");
}
else
{
QueueLogger.Log("Check finished [send=no]");
}
}
catch (System.Exception e)
{
QueueLogger.Log(e);
}
// Never send a message with a debug build!
#if DEBUG
Cancel = true;
#endif
}
private void ShowBanner()
{
try
{
QueueLogger.Log($"Start {Global.AppName}");
QueueLogger.Log($"* Version {Global.Version} {Global.Edition}");
QueueLogger.Log($"* Outlook {Application.Version}");
QueueLogger.Log($"* {System.Environment.OSVersion.VersionString}");
}
catch (System.Exception e)
{
QueueLogger.Log(e);
}
}
private bool DoCheck(Outlook.MailItem mail)
{
QueueLogger.Log($"===== Start DoCheck() ======");
Config config = new Config();
if (Global.EnableGPO)
{
config.Merge(Loader.LoadFromReg(RegistryPath.DefaultPolicy));
}
config.Merge(Loader.LoadFromDir(StandardPath.GetDefaultConfigDir()));
config.Merge(Loader.LoadFromDir(StandardPath.GetUserDir()));
var mailID = GenerateMailID(mail);
List<RecipientInfo> originalRecipients = GetOriginalRecipientsFromDictionary(mailID);
MainDialog mainDialog = new MainDialog(config, mail, originalRecipients);
if (mainDialog.SkipConfirm())
{
RemoveRecipientsFromDictionary(mail.EntryID);
return true;
}
if (mainDialog.ShowDialog() == true)
{
if (!config.CountEnabled)
{
RemoveRecipientsFromDictionary(mailID);
return true;
}
if (new CountDialog(config).ShowDialog() == true)
{
RemoveRecipientsFromDictionary(mailID);
return true;
}
}
return false;
}
private List<RecipientInfo> GetOriginalRecipientsFromDictionary(string mailID)
{
if (string.IsNullOrEmpty(mailID))
{
return null;
}
return EntryIdToOriginalRecipientsDictionary.ContainsKey(mailID) ? EntryIdToOriginalRecipientsDictionary[mailID] : null;
}
private void RemoveRecipientsFromDictionary(string mailID)
{
if (string.IsNullOrEmpty(mailID))
{
return;
}
EntryIdToOriginalRecipientsDictionary.Remove(mailID);
}
private string GenerateMailID(Outlook.MailItem mailItem)
{
if(mailItem == null)
{
return null;
}
// Create an uniq id for this mail(response). mailItem.EntryID can use as uniq id, but
// mailItem.EntryID is created when saving, but this mail(response) is not saved yet.
// We can generate the entry id by executing mailItem.Save(), but mailItem.Save()
// may do unexpected behavior when a mail item is an inline reply, so we can't use it.
//
// https://learn.microsoft.com/en-us/office/vba/api/outlook.mailitem.save
// > If a mail item is an inline reply, calling Save on that mail item may fail and result in unexpected behavior.
var index = mailItem.ConversationIndex ?? "";
var topic = mailItem.ConversationTopic ?? "";
// For mails that have no index and no topic, we give up to specify an uniq id, and mailId for them becomes "_".
// Among those mails, recipients of the last mail that passes this method is regarded as original recipients.
return $"{topic}_{index}";
}
protected override Microsoft.Office.Core.IRibbonExtensibility CreateRibbonExtensibilityObject()
{
return new Ribbon();
}
#region VSTO で生成されたコード
/// <summary>
/// デザイナーのサポートに必要なメソッドです。
/// このメソッドの内容をコード エディターで変更しないでください。
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}