2008-04-24 09:47:56 +03:00
|
|
|
{==============================================================================|
|
2008-04-24 10:05:26 +03:00
|
|
|
| Project : Delphree - Synapse | 001.001.001 |
|
2008-04-24 09:47:56 +03:00
|
|
|
|==============================================================================|
|
|
|
|
| Content: POP3 client |
|
|
|
|
|==============================================================================|
|
2008-04-24 10:05:26 +03:00
|
|
|
| The contents of this file are subject to the Mozilla Public License Ver. 1.1 |
|
2008-04-24 09:47:56 +03:00
|
|
|
| (the "License"); you may not use this file except in compliance with the |
|
|
|
|
| License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ |
|
|
|
|
| |
|
|
|
|
| Software distributed under the License is distributed on an "AS IS" basis, |
|
|
|
|
| WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
|
|
|
|
| the specific language governing rights and limitations under the License. |
|
|
|
|
|==============================================================================|
|
|
|
|
| The Original Code is Synapse Delphi Library. |
|
|
|
|
|==============================================================================|
|
|
|
|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
|
|
|
|
| Portions created by Lukas Gebauer are Copyright (c)2001. |
|
|
|
|
| All Rights Reserved. |
|
|
|
|
|==============================================================================|
|
|
|
|
| Contributor(s): |
|
|
|
|
|==============================================================================|
|
|
|
|
| History: see HISTORY.HTM from distribution package |
|
|
|
|
| (Found at URL: http://www.ararat.cz/synapse/) |
|
|
|
|
|==============================================================================}
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
{$WEAKPACKAGEUNIT ON}
|
|
|
|
|
2008-04-24 09:47:56 +03:00
|
|
|
unit POP3send;
|
|
|
|
|
|
|
|
interface
|
2008-04-24 10:05:26 +03:00
|
|
|
|
2008-04-24 09:47:56 +03:00
|
|
|
uses
|
2008-04-24 10:05:26 +03:00
|
|
|
SysUtils, Classes,
|
|
|
|
blcksock, SynaUtil, SynaCode;
|
2008-04-24 09:47:56 +03:00
|
|
|
|
|
|
|
const
|
2008-04-24 10:05:26 +03:00
|
|
|
cPop3Protocol = 'pop3';
|
2008-04-24 09:47:56 +03:00
|
|
|
|
|
|
|
type
|
2008-04-24 10:05:26 +03:00
|
|
|
TPOP3AuthType = (POP3AuthAll, POP3AuthLogin, POP3AuthAPOP);
|
2008-04-24 09:59:26 +03:00
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
TPOP3Send = class(TObject)
|
2008-04-24 09:47:56 +03:00
|
|
|
private
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock: TTCPBlockSocket;
|
|
|
|
FTimeout: Integer;
|
|
|
|
FPOP3Host: string;
|
|
|
|
FPOP3Port: string;
|
|
|
|
FResultCode: Integer;
|
|
|
|
FResultString: string;
|
|
|
|
FFullResult: TStringList;
|
|
|
|
FUsername: string;
|
|
|
|
FPassword: string;
|
|
|
|
FStatCount: Integer;
|
|
|
|
FStatSize: Integer;
|
|
|
|
FTimeStamp: string;
|
|
|
|
FAuthType: TPOP3AuthType;
|
|
|
|
function ReadResult(Full: Boolean): Integer;
|
|
|
|
function Connect: Boolean;
|
|
|
|
function AuthLogin: Boolean;
|
|
|
|
function AuthApop: Boolean;
|
2008-04-24 09:47:56 +03:00
|
|
|
public
|
2008-04-24 10:05:26 +03:00
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
function Login: Boolean;
|
|
|
|
procedure Logout;
|
|
|
|
function Reset: Boolean;
|
|
|
|
function NoOp: Boolean;
|
|
|
|
function Stat: Boolean;
|
|
|
|
function List(Value: Integer): Boolean;
|
|
|
|
function Retr(Value: Integer): Boolean;
|
|
|
|
function Dele(Value: Integer): Boolean;
|
|
|
|
function Top(Value, Maxlines: Integer): Boolean;
|
|
|
|
function Uidl(Value: Integer): Boolean;
|
|
|
|
published
|
|
|
|
property Timeout: Integer read FTimeout Write FTimeout;
|
|
|
|
property POP3Host: string read FPOP3Host Write FPOP3Host;
|
|
|
|
property POP3Port: string read FPOP3Port Write FPOP3Port;
|
|
|
|
property ResultCode: Integer read FResultCode;
|
|
|
|
property ResultString: string read FResultString;
|
|
|
|
property FullResult: TStringList read FFullResult;
|
|
|
|
property Username: string read FUsername Write FUsername;
|
|
|
|
property Password: string read FPassword Write FPassword;
|
|
|
|
property StatCount: Integer read FStatCount;
|
|
|
|
property StatSize: Integer read FStatSize;
|
|
|
|
property TimeStamp: string read FTimeStamp;
|
|
|
|
property AuthType: TPOP3AuthType read FAuthType Write FAuthType;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
const
|
|
|
|
CRLF = #13#10;
|
|
|
|
|
|
|
|
constructor TPOP3Send.Create;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
|
|
|
inherited Create;
|
2008-04-24 10:05:26 +03:00
|
|
|
FFullResult := TStringList.Create;
|
|
|
|
FSock := TTCPBlockSocket.Create;
|
|
|
|
FSock.CreateSocket;
|
|
|
|
FTimeout := 300000;
|
|
|
|
FPOP3host := cLocalhost;
|
|
|
|
FPOP3Port := cPop3Protocol;
|
|
|
|
FUsername := '';
|
|
|
|
FPassword := '';
|
|
|
|
FStatCount := 0;
|
|
|
|
FStatSize := 0;
|
|
|
|
FAuthType := POP3AuthAll;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
destructor TPOP3Send.Destroy;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock.Free;
|
|
|
|
FullResult.Free;
|
|
|
|
inherited Destroy;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TPOP3Send.ReadResult(Full: Boolean): Integer;
|
2008-04-24 09:47:56 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
s: string;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := 0;
|
|
|
|
FFullResult.Clear;
|
|
|
|
s := FSock.RecvString(FTimeout);
|
|
|
|
if Pos('+OK', s) = 1 then
|
|
|
|
Result := 1;
|
|
|
|
FResultString := s;
|
|
|
|
if Full and (Result = 1) then
|
2008-04-24 09:47:56 +03:00
|
|
|
repeat
|
2008-04-24 10:05:26 +03:00
|
|
|
s := FSock.RecvString(FTimeout);
|
|
|
|
if s = '.' then
|
|
|
|
Break;
|
|
|
|
FFullResult.Add(s);
|
|
|
|
until FSock.LastError <> 0;
|
|
|
|
FResultCode := Result;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TPOP3Send.AuthLogin: Boolean;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := False;
|
|
|
|
FSock.SendString('USER ' + FUserName + CRLF);
|
|
|
|
if ReadResult(False) <> 1 then
|
|
|
|
Exit;
|
|
|
|
FSock.SendString('PASS ' + FPassword + CRLF);
|
|
|
|
Result := ReadResult(False) = 1;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TPOP3Send.AuthAPOP: Boolean;
|
2008-04-24 09:47:56 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
s: string;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
s := StrToHex(MD5(FTimeStamp + FPassWord));
|
|
|
|
FSock.SendString('APOP ' + FUserName + ' ' + s + CRLF);
|
|
|
|
Result := ReadResult(False) = 1;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TPOP3Send.Connect: Boolean;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
// Do not call this function! It is calling by LOGIN method!
|
|
|
|
FStatCount := 0;
|
|
|
|
FStatSize := 0;
|
|
|
|
FSock.CloseSocket;
|
|
|
|
FSock.LineBuffer := '';
|
|
|
|
FSock.CreateSocket;
|
|
|
|
FSock.Connect(POP3Host, POP3Port);
|
|
|
|
Result := FSock.LastError = 0;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TPOP3Send.Login: Boolean;
|
2008-04-24 09:47:56 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
s, s1: string;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := False;
|
|
|
|
FTimeStamp := '';
|
|
|
|
if not Connect then
|
|
|
|
Exit;
|
|
|
|
if ReadResult(False) <> 1 then
|
|
|
|
Exit;
|
|
|
|
s := SeparateRight(FResultString, '<');
|
|
|
|
if s <> FResultString then
|
|
|
|
begin
|
|
|
|
s1 := SeparateLeft(s, '>');
|
|
|
|
if s1 <> s then
|
|
|
|
FTimeStamp := '<' + s1 + '>';
|
|
|
|
end;
|
|
|
|
Result := False;
|
|
|
|
if (FTimeStamp <> '') and not (FAuthType = POP3AuthLogin) then
|
|
|
|
Result := AuthApop;
|
|
|
|
if not Result and not (FAuthType = POP3AuthAPOP) then
|
|
|
|
Result := AuthLogin;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
procedure TPOP3Send.Logout;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock.SendString('QUIT' + CRLF);
|
|
|
|
ReadResult(False);
|
|
|
|
FSock.CloseSocket;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TPOP3Send.Reset: Boolean;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock.SendString('RSET' + CRLF);
|
|
|
|
Result := ReadResult(False) = 1;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TPOP3Send.NoOp: Boolean;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock.SendString('NOOP' + CRLF);
|
|
|
|
Result := ReadResult(False) = 1;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TPOP3Send.Stat: Boolean;
|
2008-04-24 09:47:56 +03:00
|
|
|
var
|
2008-04-24 10:05:26 +03:00
|
|
|
s: string;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
Result := False;
|
|
|
|
FSock.SendString('STAT' + CRLF);
|
|
|
|
if ReadResult(False) <> 1 then
|
|
|
|
Exit;
|
|
|
|
s := SeparateRight(ResultString, '+OK ');
|
|
|
|
FStatCount := StrToIntDef(SeparateLeft(s, ' '), 0);
|
|
|
|
FStatSize := StrToIntDef(SeparateRight(s, ' '), 0);
|
|
|
|
Result := True;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TPOP3Send.List(Value: Integer): Boolean;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
if Value = 0 then
|
|
|
|
FSock.SendString('LIST' + CRLF)
|
|
|
|
else
|
|
|
|
FSock.SendString('LIST ' + IntToStr(Value) + CRLF);
|
|
|
|
Result := ReadResult(Value = 0) = 1;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TPOP3Send.Retr(Value: Integer): Boolean;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock.SendString('RETR ' + IntToStr(Value) + CRLF);
|
|
|
|
Result := ReadResult(True) = 1;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TPOP3Send.Dele(Value: Integer): Boolean;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock.SendString('DELE ' + IntToStr(Value) + CRLF);
|
|
|
|
Result := ReadResult(False) = 1;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TPOP3Send.Top(Value, Maxlines: Integer): Boolean;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
FSock.SendString('TOP ' + IntToStr(Value) + ' ' + IntToStr(Maxlines) + CRLF);
|
|
|
|
Result := ReadResult(True) = 1;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
2008-04-24 10:05:26 +03:00
|
|
|
function TPOP3Send.Uidl(Value: Integer): Boolean;
|
2008-04-24 09:47:56 +03:00
|
|
|
begin
|
2008-04-24 10:05:26 +03:00
|
|
|
if Value = 0 then
|
|
|
|
FSock.SendString('UIDL' + CRLF)
|
|
|
|
else
|
|
|
|
FSock.SendString('UIDL ' + IntToStr(Value) + CRLF);
|
|
|
|
Result := ReadResult(Value = 0) = 1;
|
2008-04-24 09:47:56 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|