-
Notifications
You must be signed in to change notification settings - Fork 3
/
FrmMain2.cs
453 lines (417 loc) · 20.6 KB
/
FrmMain2.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using ScintillaNET;
using System.Xml;
namespace EmailViewer
{
public partial class FrmMain2 : Form
{
private string MsgFilePath = null;
private string MailBody = null;
private List<string> Attachments = new List<string>();
private QuoteproLexer qpLexer = new QuoteproLexer("Type Agent Code State Quoted Insurer Web Login Password ProducerCode Comment has been added as Autoquote Homequote");
public FrmMain2(string path)
{
this.MsgFilePath = path;
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
DialogResult msgFileSelectResult = this.openFileDialog1.ShowDialog();
if (msgFileSelectResult == DialogResult.OK)
{
string msgfile = this.openFileDialog1.FileName;
ReadMsgFile(msgfile);
}
}
private void ApplyStyling()
{
//txtMail.StartStyling(0);
//txtMail.SetStyling(5, 1);
//txtMail.SetStyling(2, 2);
}
private void FrmMain_Load(object sender, EventArgs e)
{
txtMail.StyleResetDefault();
txtMail.Styles[Style.Default].Font = "Consolas";
txtMail.Styles[Style.Default].Size = 10;
txtMail.StyleClearAll();
txtMail.Styles[QuoteproLexer.StyleDefault].ForeColor = Color.Black;
txtMail.Styles[QuoteproLexer.StyleKeyword].ForeColor = Color.Blue;
txtMail.Styles[QuoteproLexer.StyleIdentifier].ForeColor = Color.Teal;
txtMail.Styles[QuoteproLexer.StyleNumber].ForeColor = Color.Purple;
txtMail.Styles[QuoteproLexer.StyleString].ForeColor = Color.Red;
txtMail.Styles[QuoteproLexer.StyleAutoOrHome].ForeColor = Color.Black;
txtMail.Styles[QuoteproLexer.StyleAutoOrHome].BackColor = Color.Yellow;
txtMail.Lexer = Lexer.Container;
//txtMail.Lexer = Lexer.Null;
//txtMail.Styles[1].ForeColor = Color.Red;
//txtMail.Styles[2].ForeColor = Color.Blue;
//txtMail.SetKeywords(0, "Insurer Web Login:");
if (!string.IsNullOrWhiteSpace(MsgFilePath))
{
if (System.IO.File.Exists(MsgFilePath))
{
ReadMsgFile(MsgFilePath);
}
else
{
MessageBox.Show("文件["+ MsgFilePath +"]不存在","Oops", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
private void ReadMsgFile(string msgfile)
{
//清空附件内容列表,删除附件菜单项
MailBody = string.Empty;
Attachments.Clear();
btnFiles.DropDownItems.Clear();
lblStatus.Text = "-";
FileInfo fi = new FileInfo(msgfile);
this.Text = string.Format("EmailViewer of {0}", fi.Name);
using (Stream messageStream = File.Open(msgfile, FileMode.Open, FileAccess.Read))
{
//fileName = string.Empty;
OutlookStorage.Message message = new OutlookStorage.Message(messageStream);
messageStream.Close();
try
{
//获取comment#
//Comment has been added as: 34227
//string comment = null;
MailBody = message.BodyText;
this.txtMail.Text = MailBody;
//附件
if (message.Attachments.Count > 0)
{
int i = 0;
foreach (OutlookStorage.Attachment item in message.Attachments)
{
string fileContent = System.Text.Encoding.UTF8.GetString(item.Data);
Attachments.Add(fileContent);
ToolStripMenuItem ddItem = new ToolStripMenuItem(item.Filename);
ddItem.Name = "btnFile" + i.ToString();
ddItem.Width = 300;
ddItem.Click += ddItem_Click;
btnFiles.DropDownItems.Add(ddItem);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "不好,出了点儿问题", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
message.Dispose();
}
}
}
void ddItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem ddItem = sender as ToolStripMenuItem;
if (ddItem == null)
{
return;
}
string btnName = ddItem.Name;
int idx = ddItem.Name.IndexOf("btnFile");
if (idx==-1)
{
lblStatus.Text = "Error";
return;
}
string AttachIndex = ddItem.Name.Substring(idx+7);
int? index = ToInt(AttachIndex);
if (index.HasValue)
{
this.txtMail.Text = Attachments[index.Value];
}
else
{
lblStatus.Text = "Error";
}
}
private void txtMail_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void txtMail_DragDrop(object sender, DragEventArgs e)
{
var files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files.Length > 0)
{
string file = files[0];
if (System.IO.File.Exists(file))
{
MsgFilePath = file;
//目前仅支持 msg 文件
ReadMsgFile(MsgFilePath);
}
}
}
private void btnMailBody_Click(object sender, EventArgs e)
{
this.txtMail.Text = MailBody;
}
private int? ToInt(string str)
{
if (string.IsNullOrWhiteSpace(str))
{
return null;
}
int i = -1;
if (int.TryParse(str, out i))
{
return i;
}
return null;
}
//int needed = 1;
private void txtMail_StyleNeeded(object sender, StyleNeededEventArgs e)
{
var startPos = txtMail.GetEndStyled();
var endPos = e.Position;
qpLexer.Style(txtMail, startPos, endPos);
//lblStatus.Text = "needed" + needed.ToString();
//needed++;
}
private void btnDump_Click(object sender, EventArgs e)
{
this.openFileDialog1.Multiselect = true;
DialogResult msgFileSelectResult = this.openFileDialog1.ShowDialog();
if (msgFileSelectResult == DialogResult.OK)
{
string targetFolder = this.openFileDialog1.FileNames[0];
targetFolder = System.IO.Directory.GetParent(targetFolder).FullName;
foreach (string msgfile in this.openFileDialog1.FileNames)
{
bool attachGenerated = false;
string fileName = null;
using (Stream messageStream = File.Open(msgfile, FileMode.Open, FileAccess.Read))
{
fileName = string.Empty;
OutlookStorage.Message message = new OutlookStorage.Message(messageStream);
messageStream.Close();
try
{
//获取comment#
//Comment has been added as: 34227
string comment = null;
string body = message.BodyText;
comment = body.Substring(body.IndexOf("Comment has been added as:") + 26).Trim();
//Insurer: Aggressive Insurance Texas Elite
string insurer = null;
string state = null;
string title = message.Subject;
if (title.Contains("Reminder Email for debugging purpose"))
{
//CarrierName: Progressive Auto
insurer = ExtractHtml(body, "CarrierName:", "\r\n");
if (string.IsNullOrEmpty(insurer))
{
throw new Exception("Failed to extract CarrierName");
}
insurer = insurer.Trim().Split(' ')[0];
//Quoting State: VT
state = body.Substring(body.IndexOf("Quoting State:") + 14).Trim();
state = state.Substring(0, state.IndexOf("\r\n"));
}
else
{
//insurer = body.Substring(body.IndexOf("Insurer:")+8).Trim();
//insurer = insurer.Substring(0, insurer.IndexOf("\r\n"));
//insurer = insurer.Substring(0, insurer.IndexOf(" "));
//qpquote\stdInsurers\HartfordScrape.vb
insurer = body.Substring(body.LastIndexOf("qpquote\\stdInsurers\\") + 20);
if (insurer.Contains(".vb"))
{
insurer = insurer.Substring(0, insurer.IndexOf(".vb"));
if (insurer.Contains("Scrape"))
{
//insurer = insurer.Replace("Scrape", "");
insurer = insurer.Substring(0, insurer.IndexOf("Scrape"));
}
}
else
{
//Insurer: Progressive Insurance Company
insurer = ExtractHtml(insurer, "Insurer:", "Insurer Web Login:");
if (string.IsNullOrEmpty(insurer))
{
throw new Exception("Failed to extract Insurer Company Name");
}
insurer = insurer.Trim().Split(' ')[0];
}
//State Quoted: MA
state = body.Substring(body.IndexOf("State Quoted:") + 13).Trim();
state = state.Substring(0, state.IndexOf("\r\n"));
}
//2017-08-10,iimax:state rule changed
if (state.StartsWith("State"))
{
state = state.Substring(5);
}
fileName = string.Format("#{0}-{1} ({2}).txt", comment, insurer, state);
//附件
if (message.Attachments.Count > 0)
{
OutlookStorage.Attachment file = message.Attachments[0];
string fileContent = System.Text.Encoding.UTF8.GetString(file.Data);
//2016-06-29:修改last name为 testing,修改 effdate
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.PreserveWhitespace = true;
doc.LoadXml(fileContent);
if (fileContent.Contains("<AutoQuote>"))
{
var CusLastName = doc.DocumentElement.SelectSingleNode("/AutoQuote/Quotation/CusLastName");
if (CusLastName != null)
{
//Create a comment.
XmlComment newComment = doc.CreateComment(CusLastName.OuterXml);
CusLastName.ParentNode.InsertAfter(newComment, CusLastName);
CusLastName.InnerText = "Testing";
}
var EffDate = doc.DocumentElement.SelectSingleNode("/AutoQuote/Quotation/EffDate");
if (EffDate != null)
{
string eff = EffDate.InnerText;
DateTime dt = DateTime.Today;
DateTime.TryParse(eff, out dt);
if (dt < DateTime.Today)
{
XmlComment newComment = doc.CreateComment(EffDate.OuterXml);
EffDate.ParentNode.InsertAfter(newComment, EffDate);
EffDate.InnerText = DateTime.Today.ToString("MM/dd/yyyy");
}
}
var drivers = doc.DocumentElement.SelectNodes("/AutoQuote/Quotation/Drivers/Driver");
if (drivers != null)
{
foreach (XmlNode item in drivers)
{
var LastName = item.SelectSingleNode("LastName");
if (LastName != null)
{
XmlComment newComment = doc.CreateComment(LastName.OuterXml);
LastName.ParentNode.InsertAfter(newComment, LastName);
LastName.InnerText = "Testing";
}
}
}
}
else if (fileContent.Contains("<HomeQuote>"))
{
var CusLastName = doc.DocumentElement.SelectSingleNode("/HomeQuote/InsuredInfo/CusLastName");
if (CusLastName != null)
{
//Create a comment.
XmlComment newComment = doc.CreateComment(CusLastName.OuterXml);
CusLastName.ParentNode.InsertAfter(newComment, CusLastName);
CusLastName.InnerText = "Testing";
}
var hEffDate = doc.DocumentElement.SelectSingleNode("/HomeQuote/InsuredInfo/hEffDate");
if (hEffDate != null)
{
string eff = hEffDate.InnerText;
DateTime dt = DateTime.Today;
if (DateTime.TryParse(eff, out dt))
{
}
if (dt < DateTime.Today)
{
XmlComment newComment = doc.CreateComment(hEffDate.OuterXml);
hEffDate.ParentNode.InsertAfter(newComment, hEffDate);
hEffDate.InnerText = DateTime.Today.ToString("MM/dd/yyyy");
var hExpDate = doc.DocumentElement.SelectSingleNode("/HomeQuote/InsuredInfo/hExpDate");
if (hExpDate != null)
{
XmlComment hExpDateComment = doc.CreateComment(hExpDate.OuterXml);
hExpDate.ParentNode.InsertAfter(hExpDateComment, hExpDate);
hExpDate.InnerText = DateTime.Today.AddYears(1).ToString("MM/dd/yyyy");
}
//修改 RiskInfo
var EffDate = doc.DocumentElement.SelectSingleNode("/HomeQuote/RiskInfo/ThisRisk/EffDate");
var ExpDate = doc.DocumentElement.SelectSingleNode("/HomeQuote/RiskInfo/ThisRisk/ExpDate");
if (EffDate != null)
{
XmlComment xComment = doc.CreateComment(EffDate.OuterXml);
EffDate.ParentNode.InsertAfter(xComment, EffDate);
EffDate.InnerText = DateTime.Today.ToString("MM/dd/yyyy");
}
if (ExpDate != null)
{
XmlComment xComment = doc.CreateComment(ExpDate.OuterXml);
ExpDate.ParentNode.InsertAfter(xComment, ExpDate);
ExpDate.InnerText = DateTime.Today.AddYears(1).ToString("MM/dd/yyyy");
}
}
}
}
fileContent = doc.OuterXml;
System.IO.File.WriteAllText(Path.Combine(targetFolder, fileName), fileContent);
}
attachGenerated = true;
//MessageBox.Show(fileName);
}
catch (Exception)
{
throw;
}
finally
{
message.Dispose();
}
if (attachGenerated)
{
//重命名msg
FileInfo fi = new FileInfo(msgfile);
//目标文件名
string msgfileName = fi.Name;
string ourfileName = fi.Name.Replace(".msg", string.Format("{0}.msg", fileName));
ourfileName = ourfileName.Replace("comment#", "#").Replace(".txt.", ".");
System.IO.File.Move(msgfile, Path.Combine(fi.DirectoryName, ourfileName));
}
}
}
}
}
private string ExtractHtml(string rs, string prefix, string suffix)
{
if (!rs.Contains(prefix))
{
return string.Empty;
}
if (string.IsNullOrWhiteSpace(prefix))
{
throw new ArgumentNullException("prefix is required");
}
int start = rs.IndexOf(prefix);
rs = rs.Substring(start + prefix.Length);
//if (string.IsNullOrWhiteSpace(suffix))
//{
// return rs;
//}
//if (!rs.Contains(suffix))
//{
// return string.Empty;
//}
return rs.Substring(0, rs.IndexOf(suffix));
}
private void btnAbout_Click(object sender, EventArgs e)
{
txtMail.InsertText(0, "//mailto:[email protected]" + Environment.NewLine);
}
}
}