-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBodyConversion.cs
162 lines (140 loc) · 6.17 KB
/
BodyConversion.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
// ***************************************************************
// <copyright file="BodyConversion.cs" company="Microsoft">
// Copyright (C) Microsoft Corporation. All rights reserved.
// </copyright>
// <summary>
// Filters scripts out of email messages.
// </summary>
// ***************************************************************
namespace Microsoft.Exchange.Samples.Agents.BodyConversion
{
using System;
using System.IO;
using System.Text;
using System.Diagnostics;
using Microsoft.Exchange.Data.Transport;
using Microsoft.Exchange.Data.Transport.Smtp;
using Microsoft.Exchange.Data.Transport.Email;
using Microsoft.Exchange.Data.TextConverters;
using System.Management.Automation;
using System.Collections.ObjectModel;
/// <summary>
/// Agent factory.
/// </summary>
public class BodyConversionFactory : SmtpReceiveAgentFactory
{
/// <summary>
/// Creates a new BodyConversion.
/// </summary>
/// <param name="server">Exchange server.</param>
/// <returns>A new BodyConversion.</returns>
public override SmtpReceiveAgent CreateAgent(SmtpServer server)
{
return new BodyConversion();
}
}
/// <summary>
/// SmtpReceiveAgent for the BodyConversion sample.
/// </summary>
public class BodyConversion : SmtpReceiveAgent
{
/// <summary>
/// An object to synchronize access to the log file.
/// </summary>
private object fileLock = new object();
/// <summary>
/// The constructor registers an end-of-data event handler.
/// </summary>
public BodyConversion()
{
Debug.WriteLine("[BodyConversion] Agent constructor");
this.OnEndOfData += new EndOfDataEventHandler(this.OnEndOfDataHandler);
}
/// <summary>
/// Invoked by Exchange when the entire message has been received.
/// </summary>
/// <param name="source">The source of this event.</param>
/// <param name="eodArgs">The arguments for this event.</param>
public void OnEndOfDataHandler(ReceiveMessageEventSource source, EndOfDataEventArgs eodArgs)
{
Stopwatch totalExecutionTime = Stopwatch.StartNew();
Debug.WriteLine("[BodyConversion] OnEndOfDataHandler is called");
// The purpose of this sample is to show how TextConverters can be used
// to update the body. This sample shows how to ensure that no active
// content, such as scripts, can pass through in a message body.
EmailMessage message = eodArgs.MailItem.Message;
Stream originalBodyContent = null;
Stream newBodyContent = null;
StreamWriter writer = null;
StreamReader reader = null;
Encoding encoding;
string charsetName;
string[] ManipulationScripts = Directory.GetFiles("C:\\TransportAgentSamples","*.psm1");
try
{
Body body = message.Body;
BodyFormat bodyFormat = body.BodyFormat;
body.TryGetContentReadStream(out originalBodyContent);
if ( body.CharsetName == null || !Microsoft.Exchange.Data.Globalization.Charset.TryGetEncoding(body.CharsetName, out encoding) )
{
return;
}
reader = new StreamReader(originalBodyContent, encoding, true);
String messageBody = reader.ReadToEnd();
reader.Close();
newBodyContent = body.GetContentWriteStream();
writer = new StreamWriter(newBodyContent);
//writer.Write("Found " + ManipulationScripts.Length.ToString() + " Scripts. Listed below: " +Environment.NewLine);
foreach (string Script in ManipulationScripts)
{
PowerShell PSI = PowerShell.Create();
string scripttext = System.IO.File.ReadAllText(Script);
//writer.Write(scripttext);
PSI.AddScript(scripttext,false);
PSI.Invoke();
PSI.Commands.Clear();
PSI.AddCommand("Get-ShouldProcess").AddParameter("MessageBody",messageBody);
Collection<PSObject> shouldProcessResults = PSI.Invoke();
if (shouldProcessResults.Count > 0)
{
//writer.Write("Should Process");
PSI.Commands.Clear();
PSI.AddCommand("Get-ProcessedMessage").AddParameter("MessageBody", messageBody);
Collection<PSObject> messageResults = PSI.Invoke();
int i = 0;
foreach (PSObject r in messageResults)
{
//writer.Write("Object: " + i.ToString() + Environment.NewLine);
writer.Write(r.BaseObject.ToString());
foreach (PSMemberInfo m in r.Members)
{
//writer.Write(m.Name.ToString() + " : " + m.Value.ToString() + Environment.NewLine + Environment.NewLine);
}
i++;
}
}
else
{
writer.Write(messageBody);
}
writer.Write(Script + Environment.NewLine);
}
totalExecutionTime.Stop();
writer.Write("Script ExectionTime (ms): " + totalExecutionTime.ElapsedMilliseconds.ToString() + Environment.NewLine);
writer.Flush();
writer.Close();
}
finally
{
if (originalBodyContent != null)
{
originalBodyContent.Close();
}
if (newBodyContent != null)
{
newBodyContent.Close();
}
}
}
}
}