-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSharpTnefMessage.cs
453 lines (433 loc) · 18.3 KB
/
SharpTnefMessage.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
// -----------------------------------------------------------------------
//
// Copyright (C) 2003-2006 Angel Marin
//
// This file is part of SharpMimeTools
//
// SharpMimeTools is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// SharpMimeTools is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with SharpMimeTools; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor,
// Boston, MA 02110-1301 USA
//
// -----------------------------------------------------------------------
using System;
using System.IO;
using System.Collections.Generic;
using System.Diagnostics;
namespace anmar.SharpMimeTools
{
/// <summary>
/// Decodes <a href="http://msdn.microsoft.com/library/en-us/mapi/html/ca148ec3-8586-4c74-8ff8-cd542256e385.asp">ms-tnef</a> streams (those winmail.dat attachments).
/// </summary>
/// <remarks>Only tnef attributes related to attachments are decoded right now. All the MAPI properties encoded in the stream (rtf body, ...) are ignored. <br />
/// While decoding, the cheksum of each attribute is verified to ensure the tnef stream is not corupt.</remarks>
public class SharpTnefMessage : IDisposable
{
private enum TnefLvlType : byte
{
Message = 0x01,
Attachment = 0x02,
Unknown = Byte.MaxValue
}
// TNEF attributes
private enum TnefAttribute : ushort
{
Owner = 0x0000,
SentFor = 0x0001,
Delegate = 0x0002,
DateStart = 0x0006,
DateEnd = 0x0007,
AIDOwner = 0x0008,
Requestres = 0x0009,
From = 0x8000,
Subject = 0x8004,
DateSent = 0x8005,
DateRecd = 0x8006,
MessageStatus = 0x8007,
MessageClass = 0x8008,
MessageId = 0x8009,
ParentId = 0x800a,
ConversationId = 0x800b,
Body = 0x800c,
Priority = 0x800d,
AttachData = 0x800f,
AttachTitle = 0x8010,
AttachMetafile = 0x8011,
AttachCreateDate = 0x8012,
AttachModifyDate = 0x8013,
DateModify = 0x8020,
AttachTransportFilename = 0x9001,
AttachRendData = 0x9002,
MapiProps = 0x9003,
RecipTable = 0x9004,
Attachment = 0x9005,
TnefVersion = 0x9006,
OEMCodepage = 0x9007,
OriginalMessageClass = 0x9008,
Unknown = UInt16.MaxValue
}
// TNEF data types
private enum TnefDataType : ushort
{
atpTriples = 0x0000,
atpString = 0x0001,
atpText = 0x0002,
atpDate = 0x0003,
atpShort = 0x0004,
atpLong = 0x0005,
atpByte = 0x0006,
atpWord = 0x0007,
atpDword = 0x0008,
atpMax = 0x0009,
Unknown = UInt16.MaxValue
}
// TNEF signature
private const int TnefSignature = 0x223e9f78;
private BinaryReader _reader;
private String _body;
/// <summary>
/// Initializes a new instance of the <see cref="anmar.SharpMimeTools.SharpTnefMessage" /> class based on the supplied <see cref="System.IO.Stream" />.
/// </summary>
/// <param name="input"><see cref="System.IO.Stream" /> that contains the ms-tnef strream.</param>
/// <remarks>The tnef stream isn't automatically parsed, you must call <see cref="Parse()" /> or <see cref="Parse(string)" />.</remarks>
public SharpTnefMessage(Stream input)
{
if (input != null && input.CanRead)
{
if (input is BufferedStream)
_reader = new BinaryReader(input);
else
_reader = new BinaryReader(new BufferedStream(input));
}
}
/// <summary>
/// Gets a list that contains the attachments found in the tnef stream.
/// </summary>
/// <value>A list that contains the <see cref="SharpAttachment" /> found in the tnef stream. The <b>null</b> reference is retuned when no attachments found.</value>
/// <remarks>Each attachment is a <see cref="SharpAttachment" /> instance.</remarks>
public List<SharpAttachment> Attachments { get; private set; }
/// <summary>
/// Gets a the text body from the ms-tnef stream (<b>BODY</b> tnef attribute).
/// </summary>
/// <value>Text body from the ms-tnef stream (<b>BODY</b> tnef attribute). Or the <b>null</b> reference if the attribute is not part of the stream.</value>
public String TextBody
{
get { return _body; }
}
/// <summary>
/// Closes and releases the reading resources associated with this instance.
/// </summary>
/// <remarks>Be carefull before calling this method, as it also close the underlying <see cref="System.IO.Stream" />.</remarks>
public void Close()
{
if (_reader != null)
_reader.Close();
_reader = null;
}
/// <summary>
/// Parses the ms-tnef stream from the provided <see cref="System.IO.Stream" />.
/// </summary>
/// <returns><b>true</b> if parsing is successful. <b>false</b> otherwise.</returns>
/// <remarks>The attachments found are saved in memory as <see cref="System.IO.MemoryStream" /> instances.</remarks>
public bool Parse()
{
return Parse(null);
}
/// <summary>
/// Parses the ms-tnef stream from the provided <see cref="System.IO.Stream" />.
/// </summary>
/// <param name="path">A <see cref="System.String" /> specifying the path on which to save the attachments found. Specify the <b>null</b> reference to save them in memory as <see cref="System.IO.MemoryStream" /> instances instead.</param>
/// <returns><b>true</b> if parsing is successful. <b>false</b> otherwise.</returns>
public bool Parse(String path)
{
if (_reader == null || !_reader.BaseStream.CanRead)
return false;
int sig = ReadInt();
if (sig != TnefSignature)
{
return false;
}
bool error = false;
Attachments = new List<SharpAttachment>();
ushort key = ReadUInt16();
System.Text.Encoding enc = SharpMimeHeader.EncodingDefault;
SharpAttachment attachment_cur = null;
for (Byte cur = ReadByte(); cur != Byte.MinValue; cur = ReadByte())
{
TnefLvlType lvl = (TnefLvlType)SharpMimeTools.ParseEnum(typeof(TnefLvlType), cur, TnefLvlType.Unknown);
// Type
int type = ReadInt();
// Size
int size = ReadInt();
// Attibute name and type
TnefAttribute att_n = (TnefAttribute)SharpMimeTools.ParseEnum(typeof(TnefAttribute), (ushort)((type << 16) >> 16), TnefAttribute.Unknown);
TnefDataType att_t = (TnefDataType)SharpMimeTools.ParseEnum(typeof(TnefDataType), (ushort)(type >> 16), TnefDataType.Unknown);
if (lvl == TnefLvlType.Unknown || att_n == TnefAttribute.Unknown || att_t == TnefDataType.Unknown)
{
error = true;
break;
}
// Read data
Byte[] buffer = ReadBytes(size);
// Read checkSum
ushort checksum = ReadUInt16();
// Verify checksum
if (!VerifyChecksum(buffer, checksum))
{
error = true;
break;
}
size = buffer.Length;
if (lvl == TnefLvlType.Message)
{
// Text body
if (att_n == TnefAttribute.Body)
{
if (att_t == TnefDataType.atpString)
{
_body = enc.GetString(buffer, 0, size);
}
// Message mapi props (html body, rtf body, ...)
}
else if (att_n == TnefAttribute.MapiProps)
{
ReadMapi(buffer);
// Stream Codepage
}
else if (att_n == TnefAttribute.OEMCodepage)
{
uint codepage1 = (uint)(buffer[0] + (buffer[1] << 8) + (buffer[2] << 16) + (buffer[3] << 24));
if (codepage1 > 0)
{
try
{
enc = System.Text.Encoding.GetEncoding((int)codepage1);
}
catch (Exception e)
{
Trace.Fail(e.Message, e.StackTrace);
}
}
}
}
else if (lvl == TnefLvlType.Attachment)
{
// Attachment start
if (att_n == TnefAttribute.AttachRendData)
{
String name = String.Concat("generated_", key, "_", (Attachments.Count + 1), ".binary");
if (path == null)
{
attachment_cur = new SharpAttachment();
}
else
{
attachment_cur = new SharpAttachment(new FileInfo(Path.Combine(path, name)));
}
// Attachment name
attachment_cur.Name = name;
}
else if (att_n == TnefAttribute.AttachTitle)
{
if (attachment_cur != null && att_t == TnefDataType.atpString && buffer != null)
{
// NULL terminated string
if (buffer[size - 1] == '\0')
{
size--;
}
if (size > 0)
{
String name = enc.GetString(buffer, 0, size);
if (name.Length > 0)
{
attachment_cur.Name = name;
// Content already saved, so we have to rename
if (attachment_cur.SavedFile != null && attachment_cur.SavedFile.Exists)
{
try
{
attachment_cur.SavedFile.MoveTo(Path.Combine(path, attachment_cur.Name));
}
catch (Exception e)
{
Trace.Fail(e.Message, e.StackTrace);
}
}
}
}
}
// Modification and creation date
}
else if (att_n == TnefAttribute.AttachModifyDate || att_n == TnefAttribute.AttachCreateDate)
{
if (attachment_cur != null && att_t == TnefDataType.atpDate && buffer != null && size == 14)
{
int pos = 0;
DateTime date = new DateTime((buffer[pos++] + (buffer[pos++] << 8)), (buffer[pos++] + (buffer[pos++] << 8)), (buffer[pos++] + (buffer[pos++] << 8)), (buffer[pos++] + (buffer[pos++] << 8)), (buffer[pos++] + (buffer[pos++] << 8)), (buffer[pos++] + (buffer[pos++] << 8)));
if (att_n == TnefAttribute.AttachModifyDate)
{
attachment_cur.LastWriteTime = date;
}
else if (att_n == TnefAttribute.AttachCreateDate)
{
attachment_cur.CreationTime = date;
}
}
// Attachment data
}
else if (att_n == TnefAttribute.AttachData)
{
if (attachment_cur != null && att_t == TnefDataType.atpByte && buffer != null)
{
if (attachment_cur.SavedFile != null)
{
FileStream stream = null;
try
{
stream = attachment_cur.SavedFile.OpenWrite();
}
catch (Exception e)
{
error = true;
break;
}
stream.Write(buffer, 0, size);
stream.Flush();
attachment_cur.Size = stream.Length;
stream.Close();
stream = null;
attachment_cur.SavedFile.Refresh();
// Is name has changed, we have to rename
if (attachment_cur.SavedFile.Name != attachment_cur.Name)
try
{
attachment_cur.SavedFile.MoveTo(Path.Combine(path, attachment_cur.Name));
}
catch (Exception e)
{
Trace.Fail(e.Message, e.StackTrace);
}
}
else
{
attachment_cur.Stream.Write(buffer, 0, size);
attachment_cur.Stream.Flush();
if (attachment_cur.Stream.CanSeek)
attachment_cur.Stream.Seek(0, SeekOrigin.Begin);
attachment_cur.Size = attachment_cur.Stream.Length;
}
Attachments.Add(attachment_cur);
}
// Attachment mapi props
}
else if (att_n == TnefAttribute.Attachment)
{
ReadMapi(buffer);
}
}
}
if (Attachments.Count == 0)
Attachments = null;
return !error;
}
private static void ReadMapi(Byte[] data)
{
int pos = 0;
ushort count = (ushort)(data[pos++] + (data[pos++] << 8));
if (count == 0)
return;
//FIXME: Read each mapi prop
}
private static bool VerifyChecksum(Byte[] data, ushort checksum)
{
if (data == null)
return false;
ushort checksum_calc = 0;
for (int i = 0, count = data.Length; i < count; i++)
{
checksum_calc += data[i];
}
checksum_calc = (ushort)(checksum_calc % 65536);
return (checksum_calc == checksum);
}
private Byte ReadByte()
{
Byte cur;
try
{
cur = _reader.ReadByte();
}
catch (Exception)
{
cur = Byte.MinValue;
}
return cur;
}
private Byte[] ReadBytes(int length)
{
if (length <= 0)
return null;
Byte[] buffer = null;
try
{
buffer = _reader.ReadBytes(length);
}
catch (Exception e)
{
Trace.Fail(e.Message, e.StackTrace);
}
return buffer;
}
private int ReadInt()
{
int cur;
try
{
cur = _reader.ReadInt32();
}
catch (Exception)
{
cur = Int32.MinValue;
}
return cur;
}
private ushort ReadUInt16()
{
ushort cur;
try
{
cur = _reader.ReadUInt16();
}
catch (Exception)
{
cur = UInt16.MinValue;
}
return cur;
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (_reader != null)
{
_reader.Close();
}
}
}
}
}