📄 idmessageclient.pas
字号:
{ $HDR$}
{**********************************************************************}
{ Unit archived using Team Coherence }
{ Team Coherence is Copyright 2002 by Quality Software Components }
{ }
{ For further information / comments, visit our WEB site at }
{ http://www.TeamCoherence.com }
{**********************************************************************}
{}
{ $Log: 10253: IdMessageClient.pas
{
{ Rev 1.5 2003.06.15 3:00:34 PM czhower
{ -Fixed IdIOHandlerStream to function as originally designed and needed.
{ -Change ReadStream, WriteStream to Input/Output to be consistent with other
{ areas.
}
{
{ Rev 1.4 21/2/2003 1:53:10 PM SGrobety
{ Fixed a problem when the message contained only a single text part
}
{
{ Rev 1.3 11-30-2002 11:49:50 BGooijen
{ Fixed double if keywork in if-statement, which caused to file not to compile
}
{
{ Rev 1.2 11/23/2002 03:23:08 AM JPMugaas
{ Reverted back to old way because the fix turned out to be problematic.
}
{
{ Rev 1.1 11/19/2002 05:24:10 PM JPMugaas
{ Fixed problem with a . starting a line causing a duplicate period where it
{ shouldn't.
}
{
{ Rev 1.0 2002.11.12 10:45:48 PM czhower
}
unit IdMessageClient;
{
2001-Oct-29 Don Siders
Modified TIdMessageClient.SendMsg to use AHeadersOnly argument.
2001-Dec-1 Don Siders
Save ContentDisposition in TIdMessageClient.ProcessAttachment
}
interface
uses
Classes,
IdGlobal, IdMessage, IdTCPClient, IdHeaderList;
type
TIdMessageClient = class(TIdTCPClient)
protected
// The length of the folded line
FMsgLineLength: integer;
// The string to be pre-pended to the next line
FMsgLineFold: string;
//
procedure ReceiveBody(AMsg: TIdMessage; const ADelim: string = '.'); virtual;
function ReceiveHeader(AMsg: TIdMessage; const AAltTerm: string = ''): string; virtual;
procedure SendBody(AMsg: TIdMessage); virtual;
procedure SendHeader(AMsg: TIdMessage); virtual;
procedure WriteBodyText(AMsg: TIdMessage); virtual;
procedure WriteFoldedLine(const ALine : string);
public
constructor Create(AOwner : TComponent); override;
procedure ProcessMessage(AMsg: TIdMessage; AHeaderOnly: Boolean = False); overload;
procedure ProcessMessage(AMsg: TIdMessage; const AStream: TStream; AHeaderOnly: Boolean = False); overload;
procedure ProcessMessage(AMsg: TIdMessage; const AFilename: string; AHeaderOnly: Boolean = False); overload;
procedure SendMsg(AMsg: TIdMessage; const AHeadersOnly: Boolean = False); virtual;
//
property MsgLineLength: integer read FMsgLineLength write FMsgLineLength;
property MsgLineFold: string read FMsgLineFold write FMsgLineFold;
end;
implementation
uses
//TODO: Remove these references and make it completely pluggable. Check other spots in Indy as well
IdCoderQuotedPrintable, IdMessageCoderMIME, IdMessageCoderUUE, IdMessageCoderXXE,
//
IdCoder, IdCoder3to4,
IdCoderHeader, IdMessageCoder, IdComponent, IdException, IdResourceStrings, IdTCPConnection,
IdTCPStream, IdIOHandlerStream, IdIOHandler,
SysUtils;
function GetLongestLine(var ALine : String; ADelim : String) : String;
var
i, fnd, lineLen, delimLen : Integer;
begin
i := 0;
fnd := -1;
delimLen := length(ADelim);
lineLen := length(ALine);
while i < lineLen do
begin
if ALine[i] = ADelim[1] then
begin
if Copy(ALine, i, delimLen) = ADelim then
begin
fnd := i;
end;
end;
Inc(i);
end;
if fnd = -1 then
begin
result := '';
end
else begin
result := Copy(ALine, 1, fnd - 1);
ALine := Copy(ALine, fnd + delimLen, lineLen);
end;
end;
///////////////////
// TIdMessageClient
///////////////////
constructor TIdMessageClient.Create;
begin
inherited;
FMsgLineLength := 79;
FMsgLineFold := TAB;
end;
procedure TIdMessageClient.WriteFoldedLine;
var
ins, s, line, spare : String;
msgLen, insLen : Word;
begin
s := ALine;
// To give an amount of thread-safety
ins := FMsgLineFold;
insLen := Length(ins);
msgLen := FMsgLineLength;
// Do first line
if length(s) > FMsgLineLength then
begin
spare := Copy(s, 1, msgLen);
line := GetLongestLine(spare, ' ');
s := spare + Copy(s, msgLen + 1, length(s));
WriteLn(line);
// continue with the folded lines
while length(s) > (msgLen - insLen) do
begin
spare := Copy(s, 1, (msgLen - insLen));
line := GetLongestLine(spare, ' ');
s := ins + spare + Copy(s, (msgLen - insLen) + 1, length(s));
WriteLn(line);
end;
// complete the output with what's left
if Trim(s) <> '' then
begin
WriteLn(ins + s);
end;
end
else begin
WriteLn(s);
end;
end;
procedure TIdMessageClient.ReceiveBody(AMsg: TIdMessage; const ADelim: string = '.');
var
LMsgEnd: Boolean;
LActiveDecoder: TIdMessageDecoder;
LLine: string;
function ProcessTextPart(ADecoder: TIdMessageDecoder): TIdMessageDecoder;
var
LDestStream: TStringStream;
begin
LDestStream := TStringStream.Create('');
try
Result := ADecoder.ReadBody(LDestStream, LMsgEnd);
with TIdText.Create(AMsg.MessageParts) do
begin
ContentType := ADecoder.Headers.Values['Content-Type'];
ContentTransfer := ADecoder.Headers.Values['Content-Transfer-Encoding'];
Body.Text := LDestStream.DataString;
end;
ADecoder.Free;
finally
FreeAndNil(LDestStream);
end;
end;
function ProcessAttachment(ADecoder: TIdMessageDecoder): TIdMessageDecoder;
var
LDestStream: TFileStream;
LTempPathname: string;
begin
LTempPathname := MakeTempFilename;
LDestStream := TFileStream.Create(LTempPathname, fmCreate);
try
Result := ADecoder.ReadBody(LDestStream, LMsgEnd);
with TIdAttachment.Create(AMsg.MessageParts) do
begin
ContentType := ADecoder.Headers.Values['Content-Type'];
ContentTransfer := ADecoder.Headers.Values['Content-Transfer-Encoding'];
// dsiders 2001.12.01
ContentDisposition := ADecoder.Headers.Values['Content-Disposition'];
Filename := ADecoder.Filename;
StoredPathname := LTempPathname;
end;
ADecoder.Free;
finally
FreeAndNil(LDestStream);
end;
end;
const
wDoublePoint = ord('.') shl 8 + ord('.');
Begin
LMsgEnd := False;
if AMsg.NoDecode then
begin
Capture(AMsg.Body, ADelim);
end
else begin
BeginWork(wmRead);
try
LActiveDecoder := nil;
repeat
LLine := ReadLn;
if LLine = ADelim then
begin
Break;
end;
if LActiveDecoder = nil then
begin
LActiveDecoder := TIdMessageDecoderList.CheckForStart(AMsg, LLine);
end;
if LActiveDecoder = nil then begin
if PWord(PChar(LLine))^= wDoublePoint then begin
Delete(LLine,1,1);
end;//if '..'
AMsg.Body.Add(LLine);
end else begin
while LActiveDecoder <> nil do begin
LActiveDecoder.SourceStream := TIdTCPStream.Create(Self);
LActiveDecoder.ReadHeader;
case LActiveDecoder.PartType of
mcptUnknown:
begin
raise EIdException.Create(RSMsgClientUnkownMessagePartType);
end;
mcptText:
begin
LActiveDecoder := ProcessTextPart(LActiveDecoder);
end;
mcptAttachment:
begin
LActiveDecoder := ProcessAttachment(LActiveDecoder);
end;
end;
end;
end;
until LMsgEnd;
finally
EndWork(wmRead);
end;
end;
end;
procedure TIdMessageClient.SendHeader(AMsg: TIdMessage);
var
LHeaders: TIdHeaderList;
begin
LHeaders := AMsg.GenerateHeader;
try
WriteStrings(LHeaders);
finally
FreeAndNil(LHeaders);
end;
end;
procedure TIdMessageClient.SendBody(AMsg: TIdMEssage);
var
i: Integer;
LAttachment: TIdAttachment;
LBoundary: string;
LDestStream: TIdTCPStream;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -