1 {==============================================================================|
2 | Project : Delphree - Synapse | 001.001.002 |
3 |==============================================================================|
4 | Content: POP3 client |
5 |==============================================================================|
6 | The contents of this file are subject to the Mozilla Public License Ver. 1.1 |
7 | (the "License"); you may not use this file except in compliance with the |
8 | License. You may obtain a copy of the License at http://www.mozilla.org/MPL/ |
10 | Software distributed under the License is distributed on an "AS IS" basis, |
11 | WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for |
12 | the specific language governing rights and limitations under the License. |
13 |==============================================================================|
14 | The Original Code is Synapse Delphi Library. |
15 |==============================================================================|
16 | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
17 | Portions created by Lukas Gebauer are Copyright (c)2001. |
18 | All Rights Reserved. |
19 |==============================================================================|
21 |==============================================================================|
22 | History: see HISTORY.HTM from distribution package |
23 | (Found at URL: http://www.ararat.cz/synapse/) |
24 |==============================================================================}
34 blcksock
, SynaUtil
, SynaCode
;
37 cPop3Protocol
= 'pop3';
40 TPOP3AuthType
= (POP3AuthAll
, POP3AuthLogin
, POP3AuthAPOP
);
42 PPOP3Send
= ^TPOP3Send
;
43 TPOP3Send
= object(TObj
)
45 FSock
: PTCPBlockSocket
;
50 FResultString
: string;
51 FFullResult
: PStrList
;
57 FAuthType
: TPOP3AuthType
;
58 function ReadResult(Full
: Boolean): Integer;
59 function Connect
: Boolean;
60 function AuthLogin
: Boolean;
61 function AuthApop
: Boolean;
63 destructor Destroy
; virtual;
64 function Login
: Boolean;
66 function Reset
: Boolean;
67 function NoOp
: Boolean;
68 function Stat
: Boolean;
69 function List(Value
: Integer): Boolean;
70 function Retr(Value
: Integer): Boolean;
71 function Dele(Value
: Integer): Boolean;
72 function Top(Value
, Maxlines
: Integer): Boolean;
73 function Uidl(Value
: Integer): Boolean;
74 property Timeout
: Integer read FTimeout Write FTimeout
;
75 property POP3Host
: string read FPOP3Host Write FPOP3Host
;
76 property POP3Port
: string read FPOP3Port Write FPOP3Port
;
77 property ResultCode
: Integer read FResultCode
;
78 property ResultString
: string read FResultString
;
79 property FullResult
: PStrList read FFullResult
;
80 property Username
: string read FUsername Write FUsername
;
81 property Password
: string read FPassword Write FPassword
;
82 property StatCount
: Integer read FStatCount
;
83 property StatSize
: Integer read FStatSize
;
84 property TimeStamp
: string read FTimeStamp
;
85 property AuthType
: TPOP3AuthType read FAuthType Write FAuthType
;
86 property Sock
: PTCPBlockSocket read FSock
;
90 function NewPOP3Send
: PPOP3Send
;
98 function NewPOP3Send
: PPOP3Send
;
101 Result
.FFullResult
:= NewStrList
;
102 Result
.FSock
:= NewTCPBlockSocket
;
103 Result
.FSock
.CreateSocket
;
104 Result
.FTimeout
:= 300000;
105 Result
.FPOP3host
:= cLocalhost
;
106 Result
.FPOP3Port
:= cPop3Protocol
;
107 Result
.FUsername
:= '';
108 Result
.FPassword
:= '';
109 Result
.FStatCount
:= 0;
110 Result
.FStatSize
:= 0;
111 Result
.FAuthType
:= POP3AuthAll
;
114 destructor TPOP3Send
.Destroy
;
121 function TPOP3Send
.ReadResult(Full
: Boolean): Integer;
127 s
:= FSock
.RecvString(FTimeout
);
128 if Pos('+OK', s
) = 1 then
131 if Full
and (Result
= 1) then
133 s
:= FSock
.RecvString(FTimeout
);
137 until FSock
.LastError
<> 0;
138 FResultCode
:= Result
;
141 function TPOP3Send
.AuthLogin
: Boolean;
144 FSock
.SendString('USER ' + FUserName
+ CRLF
);
145 if ReadResult(False) <> 1 then
147 FSock
.SendString('PASS ' + FPassword
+ CRLF
);
148 Result
:= ReadResult(False) = 1;
151 function TPOP3Send
.AuthAPOP
: Boolean;
155 s
:= StrToHex(MD5(FTimeStamp
+ FPassWord
));
156 FSock
.SendString('APOP ' + FUserName
+ ' ' + s
+ CRLF
);
157 Result
:= ReadResult(False) = 1;
160 function TPOP3Send
.Connect
: Boolean;
162 // Do not call this function! It is calling by LOGIN method!
166 FSock
.LineBuffer
:= '';
168 FSock
.Connect(POP3Host
, POP3Port
);
169 Result
:= FSock
.LastError
= 0;
172 function TPOP3Send
.Login
: Boolean;
180 if ReadResult(False) <> 1 then
182 s
:= SeparateRight(FResultString
, '<');
183 if s
<> FResultString
then
185 s1
:= SeparateLeft(s
, '>');
187 FTimeStamp
:= '<' + s1
+ '>';
190 if (FTimeStamp
<> '') and not (FAuthType
= POP3AuthLogin
) then
192 if not Result
and not (FAuthType
= POP3AuthAPOP
) then
196 procedure TPOP3Send
.Logout
;
198 FSock
.SendString('QUIT' + CRLF
);
203 function TPOP3Send
.Reset
: Boolean;
205 FSock
.SendString('RSET' + CRLF
);
206 Result
:= ReadResult(False) = 1;
209 function TPOP3Send
.NoOp
: Boolean;
211 FSock
.SendString('NOOP' + CRLF
);
212 Result
:= ReadResult(False) = 1;
215 function TPOP3Send
.Stat
: Boolean;
220 FSock
.SendString('STAT' + CRLF
);
221 if ReadResult(False) <> 1 then
223 s
:= SeparateRight(ResultString
, '+OK ');
224 FStatCount
:= StrToIntDef(SeparateLeft(s
, ' '), 0);
225 FStatSize
:= StrToIntDef(SeparateRight(s
, ' '), 0);
229 function TPOP3Send
.List(Value
: Integer): Boolean;
232 FSock
.SendString('LIST' + CRLF
)
234 FSock
.SendString('LIST ' + Int2Str(Value
) + CRLF
);
235 Result
:= ReadResult(Value
= 0) = 1;
238 function TPOP3Send
.Retr(Value
: Integer): Boolean;
240 FSock
.SendString('RETR ' + Int2Str(Value
) + CRLF
);
241 Result
:= ReadResult(True) = 1;
244 function TPOP3Send
.Dele(Value
: Integer): Boolean;
246 FSock
.SendString('DELE ' + Int2Str(Value
) + CRLF
);
247 Result
:= ReadResult(False) = 1;
250 function TPOP3Send
.Top(Value
, Maxlines
: Integer): Boolean;
252 FSock
.SendString('TOP ' + Int2Str(Value
) + ' ' + Int2Str(Maxlines
) + CRLF
);
253 Result
:= ReadResult(True) = 1;
256 function TPOP3Send
.Uidl(Value
: Integer): Boolean;
259 FSock
.SendString('UIDL' + CRLF
)
261 FSock
.SendString('UIDL ' + Int2Str(Value
) + CRLF
);
262 Result
:= ReadResult(Value
= 0) = 1;