pop3send.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. {==============================================================================|
  2. | Project : Ararat Synapse | 002.006.000 |
  3. |==============================================================================|
  4. | Content: POP3 client |
  5. |==============================================================================|
  6. | Copyright (c)1999-2007, Lukas Gebauer |
  7. | All rights reserved. |
  8. | |
  9. | Redistribution and use in source and binary forms, with or without |
  10. | modification, are permitted provided that the following conditions are met: |
  11. | |
  12. | Redistributions of source code must retain the above copyright notice, this |
  13. | list of conditions and the following disclaimer. |
  14. | |
  15. | Redistributions in binary form must reproduce the above copyright notice, |
  16. | this list of conditions and the following disclaimer in the documentation |
  17. | and/or other materials provided with the distribution. |
  18. | |
  19. | Neither the name of Lukas Gebauer nor the names of its contributors may |
  20. | be used to endorse or promote products derived from this software without |
  21. | specific prior written permission. |
  22. | |
  23. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
  24. | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
  25. | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
  26. | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
  27. | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
  28. | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
  29. | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
  30. | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  31. | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
  32. | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
  33. | DAMAGE. |
  34. |==============================================================================|
  35. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  36. | Portions created by Lukas Gebauer are Copyright (c)2001-2007. |
  37. | All Rights Reserved. |
  38. |==============================================================================|
  39. | Contributor(s): |
  40. |==============================================================================|
  41. | History: see HISTORY.HTM from distribution package |
  42. | (Found at URL: http://www.ararat.cz/synapse/) |
  43. |==============================================================================}
  44. {:@abstract(POP3 protocol client)
  45. Used RFC: RFC-1734, RFC-1939, RFC-2195, RFC-2449, RFC-2595
  46. }
  47. {$IFDEF FPC}
  48. {$MODE DELPHI}
  49. {$ENDIF}
  50. {$H+}
  51. unit pop3send;
  52. interface
  53. uses
  54. SysUtils, Classes,
  55. blcksock, synautil, synacode;
  56. const
  57. cPop3Protocol = '110';
  58. type
  59. {:The three types of possible authorization methods for "logging in" to a POP3
  60. server.}
  61. TPOP3AuthType = (POP3AuthAll, POP3AuthLogin, POP3AuthAPOP);
  62. {:@abstract(Implementation of POP3 client protocol.)
  63. Note: Are you missing properties for setting Username and Password? Look to
  64. parent @link(TSynaClient) object!
  65. Are you missing properties for specify server address and port? Look to
  66. parent @link(TSynaClient) too!}
  67. TPOP3Send = class(TSynaClient)
  68. private
  69. FSock: TTCPBlockSocket;
  70. FResultCode: Integer;
  71. FResultString: string;
  72. FFullResult: TStringList;
  73. FStatCount: Integer;
  74. FStatSize: Integer;
  75. FListSize: Integer;
  76. FTimeStamp: string;
  77. FAuthType: TPOP3AuthType;
  78. FPOP3cap: TStringList;
  79. FAutoTLS: Boolean;
  80. FFullSSL: Boolean;
  81. function ReadResult(Full: Boolean): Integer;
  82. function Connect: Boolean;
  83. function AuthLogin: Boolean;
  84. function AuthApop: Boolean;
  85. public
  86. constructor Create;
  87. destructor Destroy; override;
  88. {:You can call any custom by this method. Call Command without trailing CRLF.
  89. If MultiLine parameter is @true, multilined response are expected.
  90. Result is @true on sucess.}
  91. function CustomCommand(const Command: string; MultiLine: Boolean): boolean;
  92. {:Call CAPA command for get POP3 server capabilites.
  93. note: not all servers support this command!}
  94. function Capability: Boolean;
  95. {:Connect to remote POP3 host. If all OK, result is @true.}
  96. function Login: Boolean;
  97. {:Disconnects from POP3 server.}
  98. function Logout: Boolean;
  99. {:Send RSET command. If all OK, result is @true.}
  100. function Reset: Boolean;
  101. {:Send NOOP command. If all OK, result is @true.}
  102. function NoOp: Boolean;
  103. {:Send STAT command and fill @link(StatCount) and @link(StatSize) property.
  104. If all OK, result is @true.}
  105. function Stat: Boolean;
  106. {:Send LIST command. If Value is 0, LIST is for all messages. After
  107. successful operation is listing in FullResult. If all OK, result is @True.}
  108. function List(Value: Integer): Boolean;
  109. {:Send RETR command. After successful operation dowloaded message in
  110. @link(FullResult). If all OK, result is @true.}
  111. function Retr(Value: Integer): Boolean;
  112. {:Send RETR command. After successful operation dowloaded message in
  113. @link(Stream). If all OK, result is @true.}
  114. function RetrStream(Value: Integer; Stream: TStream): Boolean;
  115. {:Send DELE command for delete specified message. If all OK, result is @true.}
  116. function Dele(Value: Integer): Boolean;
  117. {:Send TOP command. After successful operation dowloaded headers of message
  118. and maxlines count of message in @link(FullResult). If all OK, result is
  119. @true.}
  120. function Top(Value, Maxlines: Integer): Boolean;
  121. {:Send UIDL command. If Value is 0, UIDL is for all messages. After
  122. successful operation is listing in FullResult. If all OK, result is @True.}
  123. function Uidl(Value: Integer): Boolean;
  124. {:Call STLS command for upgrade connection to SSL/TLS mode.}
  125. function StartTLS: Boolean;
  126. {:Try to find given capabily in capabilty string returned from POP3 server
  127. by CAPA command.}
  128. function FindCap(const Value: string): string;
  129. published
  130. {:Result code of last POP3 operation. 0 - error, 1 - OK.}
  131. property ResultCode: Integer read FResultCode;
  132. {:Result string of last POP3 operation.}
  133. property ResultString: string read FResultString;
  134. {:Stringlist with full lines returned as result of POP3 operation. I.e. if
  135. operation is LIST, this property is filled by list of messages. If
  136. operation is RETR, this property have downloaded message.}
  137. property FullResult: TStringList read FFullResult;
  138. {:After STAT command is there count of messages in inbox.}
  139. property StatCount: Integer read FStatCount;
  140. {:After STAT command is there size of all messages in inbox.}
  141. property StatSize: Integer read FStatSize;
  142. {:After LIST 0 command size of all messages on server, After LIST x size of message x on server}
  143. property ListSize: Integer read FListSize;
  144. {:If server support this, after comnnect is in this property timestamp of
  145. remote server.}
  146. property TimeStamp: string read FTimeStamp;
  147. {:Type of authorisation for login to POP3 server. Dafault is autodetect one
  148. of possible authorisation. Autodetect do this:
  149. If remote POP3 server support APOP, try login by APOP method. If APOP is
  150. not supported, or if APOP login failed, try classic USER+PASS login method.}
  151. property AuthType: TPOP3AuthType read FAuthType Write FAuthType;
  152. {:If is set to @true, then upgrade to SSL/TLS mode if remote server support it.}
  153. property AutoTLS: Boolean read FAutoTLS Write FAutoTLS;
  154. {:SSL/TLS mode is used from first contact to server. Servers with full
  155. SSL/TLS mode usualy using non-standard TCP port!}
  156. property FullSSL: Boolean read FFullSSL Write FFullSSL;
  157. {:Socket object used for TCP/IP operation. Good for seting OnStatus hook, etc.}
  158. property Sock: TTCPBlockSocket read FSock;
  159. end;
  160. implementation
  161. constructor TPOP3Send.Create;
  162. begin
  163. inherited Create;
  164. FFullResult := TStringList.Create;
  165. FPOP3cap := TStringList.Create;
  166. FSock := TTCPBlockSocket.Create;
  167. FSock.ConvertLineEnd := true;
  168. FTimeout := 60000;
  169. FTargetPort := cPop3Protocol;
  170. FStatCount := 0;
  171. FStatSize := 0;
  172. FListSize := 0;
  173. FAuthType := POP3AuthAll;
  174. FAutoTLS := False;
  175. FFullSSL := False;
  176. end;
  177. destructor TPOP3Send.Destroy;
  178. begin
  179. FSock.Free;
  180. FPOP3cap.Free;
  181. FullResult.Free;
  182. inherited Destroy;
  183. end;
  184. function TPOP3Send.ReadResult(Full: Boolean): Integer;
  185. var
  186. s: string;
  187. begin
  188. Result := 0;
  189. FFullResult.Clear;
  190. s := FSock.RecvString(FTimeout);
  191. if Pos('+OK', s) = 1 then
  192. Result := 1;
  193. FResultString := s;
  194. if Full and (Result = 1) then
  195. repeat
  196. s := FSock.RecvString(FTimeout);
  197. if s = '.' then
  198. Break;
  199. if s <> '' then
  200. if s[1] = '.' then
  201. Delete(s, 1, 1);
  202. FFullResult.Add(s);
  203. until FSock.LastError <> 0;
  204. if not Full and (Result = 1) then
  205. FFullResult.Add(SeparateRight(FResultString, ' '));
  206. if FSock.LastError <> 0 then
  207. Result := 0;
  208. FResultCode := Result;
  209. end;
  210. function TPOP3Send.CustomCommand(const Command: string; MultiLine: Boolean): boolean;
  211. begin
  212. FSock.SendString(Command + CRLF);
  213. Result := ReadResult(MultiLine) <> 0;
  214. end;
  215. function TPOP3Send.AuthLogin: Boolean;
  216. begin
  217. Result := False;
  218. if not CustomCommand('USER ' + FUserName, False) then
  219. exit;
  220. Result := CustomCommand('PASS ' + FPassword, False)
  221. end;
  222. function TPOP3Send.AuthAPOP: Boolean;
  223. var
  224. s: string;
  225. begin
  226. s := StrToHex(MD5(FTimeStamp + FPassWord));
  227. Result := CustomCommand('APOP ' + FUserName + ' ' + s, False);
  228. end;
  229. function TPOP3Send.Connect: Boolean;
  230. begin
  231. // Do not call this function! It is calling by LOGIN method!
  232. FStatCount := 0;
  233. FStatSize := 0;
  234. FSock.CloseSocket;
  235. FSock.LineBuffer := '';
  236. FSock.Bind(FIPInterface, cAnyPort);
  237. if FSock.LastError = 0 then
  238. FSock.Connect(FTargetHost, FTargetPort);
  239. if FSock.LastError = 0 then
  240. if FFullSSL then
  241. FSock.SSLDoConnect;
  242. Result := FSock.LastError = 0;
  243. end;
  244. function TPOP3Send.Capability: Boolean;
  245. begin
  246. FPOP3cap.Clear;
  247. Result := CustomCommand('CAPA', True);
  248. if Result then
  249. FPOP3cap.AddStrings(FFullResult);
  250. end;
  251. function TPOP3Send.Login: Boolean;
  252. var
  253. s, s1: string;
  254. begin
  255. Result := False;
  256. FTimeStamp := '';
  257. if not Connect then
  258. Exit;
  259. if ReadResult(False) <> 1 then
  260. Exit;
  261. s := SeparateRight(FResultString, '<');
  262. if s <> FResultString then
  263. begin
  264. s1 := Trim(SeparateLeft(s, '>'));
  265. if s1 <> s then
  266. FTimeStamp := '<' + s1 + '>';
  267. end;
  268. Result := False;
  269. if Capability then
  270. if FAutoTLS and (Findcap('STLS') <> '') then
  271. if StartTLS then
  272. Capability
  273. else
  274. begin
  275. Result := False;
  276. Exit;
  277. end;
  278. if (FTimeStamp <> '') and not (FAuthType = POP3AuthLogin) then
  279. begin
  280. Result := AuthApop;
  281. if not Result then
  282. begin
  283. if not Connect then
  284. Exit;
  285. if ReadResult(False) <> 1 then
  286. Exit;
  287. end;
  288. end;
  289. if not Result and not (FAuthType = POP3AuthAPOP) then
  290. Result := AuthLogin;
  291. end;
  292. function TPOP3Send.Logout: Boolean;
  293. begin
  294. Result := CustomCommand('QUIT', False);
  295. FSock.CloseSocket;
  296. end;
  297. function TPOP3Send.Reset: Boolean;
  298. begin
  299. Result := CustomCommand('RSET', False);
  300. end;
  301. function TPOP3Send.NoOp: Boolean;
  302. begin
  303. Result := CustomCommand('NOOP', False);
  304. end;
  305. function TPOP3Send.Stat: Boolean;
  306. var
  307. s: string;
  308. begin
  309. Result := CustomCommand('STAT', False);
  310. if Result then
  311. begin
  312. s := SeparateRight(ResultString, '+OK ');
  313. FStatCount := StrToIntDef(Trim(SeparateLeft(s, ' ')), 0);
  314. FStatSize := StrToIntDef(Trim(SeparateRight(s, ' ')), 0);
  315. end;
  316. end;
  317. function TPOP3Send.List(Value: Integer): Boolean;
  318. var
  319. s: string;
  320. n: integer;
  321. begin
  322. if Value = 0 then
  323. s := 'LIST'
  324. else
  325. s := 'LIST ' + IntToStr(Value);
  326. Result := CustomCommand(s, Value = 0);
  327. FListSize := 0;
  328. if Result then
  329. if Value <> 0 then
  330. begin
  331. s := SeparateRight(ResultString, '+OK ');
  332. FListSize := StrToIntDef(SeparateLeft(SeparateRight(s, ' '), ' '), 0);
  333. end
  334. else
  335. for n := 0 to FFullResult.Count - 1 do
  336. FListSize := FListSize + StrToIntDef(SeparateLeft(SeparateRight(s, ' '), ' '), 0);
  337. end;
  338. function TPOP3Send.Retr(Value: Integer): Boolean;
  339. begin
  340. Result := CustomCommand('RETR ' + IntToStr(Value), True);
  341. end;
  342. //based on code by Miha Vrhovnik
  343. function TPOP3Send.RetrStream(Value: Integer; Stream: TStream): Boolean;
  344. var
  345. s: string;
  346. begin
  347. Result := False;
  348. FFullResult.Clear;
  349. Stream.Size := 0;
  350. FSock.SendString('RETR ' + IntToStr(Value) + CRLF);
  351. s := FSock.RecvString(FTimeout);
  352. if Pos('+OK', s) = 1 then
  353. Result := True;
  354. FResultString := s;
  355. if Result then begin
  356. repeat
  357. s := FSock.RecvString(FTimeout);
  358. if s = '.' then
  359. Break;
  360. if s <> '' then begin
  361. if s[1] = '.' then
  362. Delete(s, 1, 1);
  363. end;
  364. WriteStrToStream(Stream, s);
  365. WriteStrToStream(Stream, CRLF);
  366. until FSock.LastError <> 0;
  367. end;
  368. if Result then
  369. FResultCode := 1
  370. else
  371. FResultCode := 0;
  372. end;
  373. function TPOP3Send.Dele(Value: Integer): Boolean;
  374. begin
  375. Result := CustomCommand('DELE ' + IntToStr(Value), False);
  376. end;
  377. function TPOP3Send.Top(Value, Maxlines: Integer): Boolean;
  378. begin
  379. Result := CustomCommand('TOP ' + IntToStr(Value) + ' ' + IntToStr(Maxlines), True);
  380. end;
  381. function TPOP3Send.Uidl(Value: Integer): Boolean;
  382. var
  383. s: string;
  384. begin
  385. if Value = 0 then
  386. s := 'UIDL'
  387. else
  388. s := 'UIDL ' + IntToStr(Value);
  389. Result := CustomCommand(s, Value = 0);
  390. end;
  391. function TPOP3Send.StartTLS: Boolean;
  392. begin
  393. Result := False;
  394. if CustomCommand('STLS', False) then
  395. begin
  396. Fsock.SSLDoConnect;
  397. Result := FSock.LastError = 0;
  398. end;
  399. end;
  400. function TPOP3Send.FindCap(const Value: string): string;
  401. var
  402. n: Integer;
  403. s: string;
  404. begin
  405. s := UpperCase(Value);
  406. Result := '';
  407. for n := 0 to FPOP3cap.Count - 1 do
  408. if Pos(s, UpperCase(FPOP3cap[n])) = 1 then
  409. begin
  410. Result := FPOP3cap[n];
  411. Break;
  412. end;
  413. end;
  414. end.