📄 mimedec.pas
字号:
{*_* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Author: Fran鏾is PIETTE
Object: TMimeDecode is a component whose job is to decode MIME encoded
EMail messages (file attach). You can use it for example to
decode messages received with a POP3 component.
MIME is described in RFC-1521. headers are described if RFC-822.
EMail: francois.piette@pophost.eunet.be francois.piette@rtfm.be
http://www.rtfm.be/fpiette
WebSite: http://www.rtfm.be/fpiette
Creation: March 08, 1998
Version: 1.11
Support: Use the mailing list twsocket@rtfm.be See website for details.
Legal issues: Copyright (C) 1997, 1998 by Fran鏾is PIETTE
Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
<francois.piette@pophost.eunet.be>
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
4. You must register this software by sending a picture postcard
to the author. Use a nice stamp and mention your name, street
address, EMail address and any comment you like to say.
QUICK REFERENCE:
----------------
TMimeDecode take a file or a stream as input and produce several event when
the message is parsed. each event can be used to display or to save to a file
the message parts.
Two methods can be called to decode either a file or a stream:
procedure DecodeFile(FileName : String);
procedure DecodeStream(aStream : TStream);
During the decode process, the component trigger several events. You have to
use those events to save data to a file or to display somehow on the
user interface.
Events are organized by groups of three for message header, part header and
part data:
Message header events: OnHeaderBegin OnHeaderLine OnHeaderEnd
Part header events: OnPartHeaderBegin OnPartHeaderLine OnPartHeaderEnd
Part data events: OnPartDataBegin OnPartDataLine OnPartDataEnd
The 'Begin' event is triggered once just before the first item will occur.
The 'Line' event is triggered for each item of the given type.
The 'End' event is triggered once after the last item.
For a multi-part message, we have this sequence:
a) The message header
OnHeaderBegin, then many OnHeaderLine, one for each line in the header. Lines
can be continuated in the message. The event here is triggered with continuated
lines concatenated (so it can be quite large !). After the last header line
has been processed, the OnHeaderEnd is triggered once.
b) The non-significant message part which can be empty. This is part 0. We
get OnPartBegin once, then OnPartLine for each line and finally OnPartEnd once.
c) The first significant part header with his three events, just like the
message header: OnPartHeaderBegin, OnPartHeaderLine and OnPartHeaderEnd.
d) The first significant part data with his three events: OnPartBegin once,
OnPartLine for each line and OnPartEnd once at the end of the part.
It's possible to have an empty part. This gives the OnPartBegin and OnPartEnd
events and NO OnPartLine event.
e) We can have many other parts. The sequence is always the same. We restart
at point (b) here above for each part (header, then data). Note that there is
often en empty part at the end of a message.
TMimeDecode decode encoded parts using 'base64' and 'quoted-printable' methods.
For those parts, the OnPartLine event will gives DECODED data. Other methods
are passed not decoded. You can use the property ContentTransferEncoding to
know which encoding method is used and add your own decoding mechanism.
For each OnHeaderLine, OnPartHeaderLine and OnPartLine, you can find the
actual data at the address pointed by the property CurrentData (a PChar).
The reason for a PChar is that the data can be quite large. The data pointed
is a null terminated string. You can get the length using StrLen, or convert
to a string with StrPas. It is more efficient to process the data using a
pointer. Using strings tends to copy the data several times.
The OnPartLine event passes a PChar and a length to the handler. This actully
point to the internal buffer and overwrite the original data (base64 and
quote-printable method produce decoded data smaller tha encoded one).
From the message header, the component extract the following values:
From The message author. Not necessary the real author...
Looks like "Francois Piette" <francois.piette@pophost.eunet.be>
Dest The message destination (To field, but To is a reserved word)
Looks like "Francois Piette" <francois.piette@pophost.eunet.be>
Subject The message subject. Free text.
Date The message date.
Look like: Mon, 16 Feb 1998 12:45:11 -0800
ContentType 'multipart/mixed' or empty.
For details about those header fields and others, read RFC-822
For each part, we have the following properties updated (the header is parsed
on the fly):
PartNumber Starting from 0 for the non-significant part
PartLine Starting 1 for the first line of each part or header
PartContentType Such as 'text/plain' or 'application/x-zip-compressed'
PartCharset This is a complement for the PartContentType.
ApplicationType When PartContentType is 'application/something', we
get the 'something' extracted
PartName This is the value for 'name=something' in the
Content-Type header line.
PartEncoding Encoding method (Content-Transfer-Encoding).
Can be used to decode unsupported
methods (supported methods are 'base64' and
'quoted-printable'. '7bit' and '8bit' does'nt
generally require processing.
PartDisposition Can be 'inline' or 'attachement' and is generally
followed by a 'filename=something'
PartFileName The specified filename in Content-Disposition header
line. Be aware that the file name is not necessary
suitable for windows ! Use it with caution...
For details about those header fields and others, read RFC-1521.
To write part data to files, you can either implement your own writing in
the OnPartLine event handler, or use the DestStream property. If assigned,
this property will be used to write the data. If not assigned, it will be
ignore.
To select a file name for each part, you can use the PartFileName property or
the 'PartName' property or a comnination of both. But be aware that those value
can be either missing or even invalid as a filename because the message was
generated with another opertaing system which has different filename
conventions.
Updates:
Apr 13, 1998 V1.01 Corrected a bug in ProcessLineBase64 which decoded one
byte too much. Thanks to Rune Fredriksen <runefr@mail.link.no>.
Apr 15, 1998 V1.02 Corrected bug in ProcessHeaderLine which retreived only
the first word for each item.
Added the ReturnPath property.
Apr 24, 1998 V1.03 Removed the modification made in version 1.01 !
Apr 26, 1998 V1.04 Corrected a bug in ReallocMem with Delphi 1
Aug 27, 1998 V1.05 Corrected a bug in decoding which incorrectly merge
the first message line with the header when the line begon
by a space. Thanks to Mitch Cant <mitchcant@hotmail.com> for
finding the bug and correction.
Sep 13, 1998 V1.06 Correctly handled unterminated messages.
Correctly handled parts without header.
Dec 26, 1998 V1.07 Added features coded by Eric Fortier <efortier@videotron.ca>
(Embedded mime parts, UUDecode).
Dec 30, 1998 V1.08 Check for header end when a header line begin with a
space or tab character. (Normally a header end with a blank
line, we also accept invalid header line).
Feb 01, 1999 V1.09 Corrected a bug ProcessLineUUDecode where 'end' was not
checked. Thanks to Eric Fortier.
Feb 16, 1999 V1.10 Added UUEncoded embedded parts. Thanks to Eric Fortier.
Corrected a line termination problem in ProcessLineBase64.
Jul 21, 1999 V1.11 Added support for encoded message without multipart.
Added Encoding property with the encoding value.
Thanks to Marcelo S Massuda <massuda@4web.com.br> for pinting this
lack of feature.
Aug 20, 1999 V1.12 Added compile time options. Revised for BCB4.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit MimeDec;
{$B-} { Enable partial boolean evaluation }
{$T-} { Untyped pointers }
{$X+} { Enable extended syntax }
{$IFNDEF VER80} { Not for Delphi 1 }
{$H+} { Use long strings }
{$J+} { Allow typed constant to be modified }
{$ENDIF}
{$IFDEF VER110} { C++ Builder V3.0 }
{$ObjExportAll On}
{$ENDIF}
{$IFDEF VER125} { C++ Builder V4.0 }
{$ObjExportAll On}
{$ENDIF}
interface
uses
WinTypes, WinProcs, SysUtils, Classes;
const
MimeDecodeVersion = 112;
CopyRight : String = ' TMimeDecode (c) 1998, 1999 F. Piette V1.12 ';
type
TMimeDecodePartLine = procedure (Sender : TObject;
Data : PChar;
DataLen : Integer) of object;
TInlineDecodeBegin = procedure (Sender: TObject; Filename: string) of object;
TInlineDecodeLine = procedure (Sender: TObject; Line: pchar) of object;
TInlineDecodeEnd = procedure (Sender: TObject; Filename: string) of object;
TMimeDecode = class(TComponent)
private
FFrom : String;
FDest : String;
FSubject : String;
FDate : String;
FReturnPath : String;
FEncoding : String;
FContentType : String;
FMimeVersion : String;
FPartContentType : String;
FPartEncoding : String;
FPartNumber : Integer;
FPartHeaderBeginSignaled : Boolean;
FPartName : String;
FPartDisposition : String;
FPartFileName : String;
FPartCharset : String;
FApplicationType : String;
FPartOpened : Boolean;
FHeaderFlag : Boolean;
FLineNum : Integer;
FBuffer : PChar;
FBufferSize : Integer;
FCurrentData : PChar;
FBoundary : String;
FNext : procedure of object;
FDestStream : TStream;
FOnHeaderBegin : TNotifyEvent;
FOnHeaderLine : TNotifyEvent;
FOnHeaderEnd : TNotifyEvent;
FOnPartHeaderBegin : TNotifyEvent;
FOnPartHeaderLine : TNotifyEvent;
FOnPartHeaderEnd : TNotifyEvent;
FOnPartBegin : TNotifyEvent;
FOnPartLine : TMimeDecodePartLine;
FOnPartEnd : TNotifyEvent;
cUUFilename : String; { ##ERIC }
FEmbeddedBoundary : TStringList; { ##ERIC }
cIsEmbedded : Boolean; { ##ERIC }
FInlineBegin: TInlineDecodeBegin;
FInlineLine: TInlineDecodeLine;
FInlineEnd: TInlineDecodeEnd;
procedure TriggerHeaderBegin; virtual;
procedure TriggerHeaderLine; virtual;
procedure TriggerHeaderEnd; virtual;
procedure TriggerPartHeaderBegin; virtual;
procedure TriggerPartHeaderLine; virtual;
procedure TriggerPartHeaderEnd; virtual;
procedure TriggerPartBegin; virtual;
procedure TriggerPartLine(Data : PChar; DataLen : Integer); virtual;
procedure TriggerPartEnd; virtual;
procedure ProcessLineBase64;
procedure ProcessLineUUDecode;
function UUProcessLine(FCurrentData: pchar): boolean;
procedure ProcessLineQuotedPrintable;
procedure ProcessHeaderLine;
procedure ProcessPartHeaderLine;
procedure ProcessPartLine;
procedure ProcessWaitBoundary;
procedure ProcessMessageLine;
procedure PreparePart;
procedure PrepareNextPart;
procedure ProcessDecodedLine(Line : PChar; Len : Integer);
procedure InternalDecodeStream(aStream : TStream);
procedure MessageBegin;
procedure MessageEnd;
public
procedure DecodeFile(FileName : String);
procedure DecodeStream(aStream : TStream);
property From : String read FFrom;
property Dest : String read FDest;
property Subject : String read FSubject;
property Date : String read FDate;
property ReturnPath : String read FReturnPath;
property ContentType : String read FContentType;
property Encoding : String read FEncoding;
property MimeVersion : String read FMimeVersion;
property PartContentType : String read FPartContentType;
property PartEncoding : String read FPartEncoding;
property PartName : String read FPartName;
property PartDisposition : String read FPartDisposition;
property PartFileName : String read FPartFileName;
property PartCharset : String read FPartCharset;
property ApplicationType : String read FApplicationType;
property PartNumber : Integer read FPartNumber;
property CurrentData : PChar read FCurrentData;
property DestStream : TStream read FDestStream
write FDestStream;
published
property OnHeaderBegin : TNotifyEvent read FOnHeaderBegin
write FOnHeaderBegin;
property OnHeaderLine : TNotifyEvent read FOnHeaderLine
write FOnHeaderLine;
property OnHeaderEnd : TNotifyEvent read FOnHeaderEnd
write FOnHeaderEnd;
property OnPartHeaderBegin : TNotifyEvent read FOnPartHeaderBegin
write FOnPartHeaderBegin;
property OnPartHeaderLine : TNotifyEvent read FOnPartHeaderLine
write FOnPartHeaderLine;
property OnPartHeaderEnd : TNotifyEvent read FOnPartHeaderEnd
write FOnPartHeaderEnd;
property OnPartBegin : TNotifyEvent read FOnPartBegin
write FOnPartBegin;
property OnPartLine : TMimeDecodePartLine read FOnPartLine
write FOnPartLine;
property OnPartEnd : TNotifyEvent read FOnPartEnd
write FOnPartEnd;
property OnInlineDecodeBegin : TInlineDecodeBegin
read FInlineBegin
write FInlineBegin;
property OnInlineDecodeLine : TInlineDecodeLine
read FInlineLine
write FInlineLine;
property OnInlineDecodeEnd : TInlineDecodeEnd
read FInlineEnd
write FInlineEnd;
end;
procedure Register;
implementation
type
TLookup = array [0..127] of Byte;
const
Base64In: TLookup = (
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 62, 255, 255, 255, 63, 52, 53, 54, 55,
56, 57, 58, 59, 60, 61, 255, 255, 255, 64, 255, 255, 255,
0, 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,
255, 255, 255, 255, 255, 255, 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, 255, 255, 255, 255, 255
);
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure Register;
begin
RegisterComponents('FPiette', [TMimeDecode]);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function HexConv(Ch : Char) : Integer;
begin
if Ch in ['0'..'9'] then
Result := Ord(Ch) - Ord('0')
else
Result := (Ord(Ch) and 15) + 9;
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TMimeDecode.TriggerHeaderBegin;
begin
if Assigned(FOnHeaderBegin) then
FOnHeaderBegin(Self);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TMimeDecode.TriggerHeaderLine;
begin
if Assigned(FOnHeaderLine) then
FOnHeaderLine(Self);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TMimeDecode.TriggerHeaderEnd;
begin
if Assigned(FOnHeaderEnd) then
FOnHeaderEnd(Self);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
procedure TMimeDecode.TriggerPartHeaderBegin;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -