home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / unity / d23456 / SYNAPSE.ZIP / source / lib / POP3send.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2001-08-13  |  7.7 KB  |  263 lines

  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/ |
  9. |                                                                              |
  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. |==============================================================================|
  20. | Contributor(s):                                                              |
  21. |==============================================================================|
  22. | History: see HISTORY.HTM from distribution package                           |
  23. |          (Found at URL: http://www.ararat.cz/synapse/)                       |
  24. |==============================================================================}
  25.  
  26. {$WEAKPACKAGEUNIT ON}
  27.  
  28. unit POP3send;
  29.  
  30. interface
  31.  
  32. uses
  33.   SysUtils, Classes,
  34.   blcksock, SynaUtil, SynaCode;
  35.  
  36. const
  37.   cPop3Protocol = 'pop3';
  38.  
  39. type
  40.   TPOP3AuthType = (POP3AuthAll, POP3AuthLogin, POP3AuthAPOP);
  41.  
  42.   TPOP3Send = class(TObject)
  43.   private
  44.     FSock: TTCPBlockSocket;
  45.     FTimeout: Integer;
  46.     FPOP3Host: string;
  47.     FPOP3Port: string;
  48.     FResultCode: Integer;
  49.     FResultString: string;
  50.     FFullResult: TStringList;
  51.     FUsername: string;
  52.     FPassword: string;
  53.     FStatCount: Integer;
  54.     FStatSize: Integer;
  55.     FTimeStamp: string;
  56.     FAuthType: TPOP3AuthType;
  57.     function ReadResult(Full: Boolean): Integer;
  58.     function Connect: Boolean;
  59.     function AuthLogin: Boolean;
  60.     function AuthApop: Boolean;
  61.   public
  62.     constructor Create;
  63.     destructor Destroy; override;
  64.     function Login: Boolean;
  65.     procedure Logout;
  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.   published
  75.     property Timeout: Integer read FTimeout Write FTimeout;
  76.     property POP3Host: string read FPOP3Host Write FPOP3Host;
  77.     property POP3Port: string read FPOP3Port Write FPOP3Port;
  78.     property ResultCode: Integer read FResultCode;
  79.     property ResultString: string read FResultString;
  80.     property FullResult: TStringList read FFullResult;
  81.     property Username: string read FUsername Write FUsername;
  82.     property Password: string read FPassword Write FPassword;
  83.     property StatCount: Integer read FStatCount;
  84.     property StatSize: Integer read  FStatSize;
  85.     property TimeStamp: string read FTimeStamp;
  86.     property AuthType: TPOP3AuthType read FAuthType Write FAuthType;
  87.     property Sock: TTCPBlockSocket read FSock;
  88.   end;
  89.  
  90. implementation
  91.  
  92. const
  93.   CRLF = #13#10;
  94.  
  95. constructor TPOP3Send.Create;
  96. begin
  97.   inherited Create;
  98.   FFullResult := TStringList.Create;
  99.   FSock := TTCPBlockSocket.Create;
  100.   FSock.CreateSocket;
  101.   FTimeout := 300000;
  102.   FPOP3host := cLocalhost;
  103.   FPOP3Port := cPop3Protocol;
  104.   FUsername := '';
  105.   FPassword := '';
  106.   FStatCount := 0;
  107.   FStatSize := 0;
  108.   FAuthType := POP3AuthAll;
  109. end;
  110.  
  111. destructor TPOP3Send.Destroy;
  112. begin
  113.   FSock.Free;
  114.   FullResult.Free;
  115.   inherited Destroy;
  116. end;
  117.  
  118. function TPOP3Send.ReadResult(Full: Boolean): Integer;
  119. var
  120.   s: string;
  121. begin
  122.   Result := 0;
  123.   FFullResult.Clear;
  124.   s := FSock.RecvString(FTimeout);
  125.   if Pos('+OK', s) = 1 then
  126.     Result := 1;
  127.   FResultString := s;
  128.   if Full and (Result = 1) then
  129.     repeat
  130.       s := FSock.RecvString(FTimeout);
  131.       if s = '.' then
  132.         Break;
  133.       FFullResult.Add(s);
  134.     until FSock.LastError <> 0;
  135.   FResultCode := Result;
  136. end;
  137.  
  138. function TPOP3Send.AuthLogin: Boolean;
  139. begin
  140.   Result := False;
  141.   FSock.SendString('USER ' + FUserName + CRLF);
  142.   if ReadResult(False) <> 1 then
  143.     Exit;
  144.   FSock.SendString('PASS ' + FPassword + CRLF);
  145.   Result := ReadResult(False) = 1;
  146. end;
  147.  
  148. function TPOP3Send.AuthAPOP: Boolean;
  149. var
  150.   s: string;
  151. begin
  152.   s := StrToHex(MD5(FTimeStamp + FPassWord));
  153.   FSock.SendString('APOP ' + FUserName + ' ' + s + CRLF);
  154.   Result := ReadResult(False) = 1;
  155. end;
  156.  
  157. function TPOP3Send.Connect: Boolean;
  158. begin
  159.   // Do not call this function! It is calling by LOGIN method!
  160.   FStatCount := 0;
  161.   FStatSize := 0;
  162.   FSock.CloseSocket;
  163.   FSock.LineBuffer := '';
  164.   FSock.CreateSocket;
  165.   FSock.Connect(POP3Host, POP3Port);
  166.   Result := FSock.LastError = 0;
  167. end;
  168.  
  169. function TPOP3Send.Login: Boolean;
  170. var
  171.   s, s1: string;
  172. begin
  173.   Result := False;
  174.   FTimeStamp := '';
  175.   if not Connect then
  176.     Exit;
  177.   if ReadResult(False) <> 1 then
  178.     Exit;
  179.   s := SeparateRight(FResultString, '<');
  180.   if s <> FResultString then
  181.   begin
  182.     s1 := SeparateLeft(s, '>');
  183.     if s1 <> s then
  184.       FTimeStamp := '<' + s1 + '>';
  185.   end;
  186.   Result := False;
  187.   if (FTimeStamp <> '') and not (FAuthType = POP3AuthLogin) then
  188.     Result := AuthApop;
  189.   if not Result and not (FAuthType = POP3AuthAPOP) then
  190.     Result := AuthLogin;
  191. end;
  192.  
  193. procedure TPOP3Send.Logout;
  194. begin
  195.   FSock.SendString('QUIT' + CRLF);
  196.   ReadResult(False);
  197.   FSock.CloseSocket;
  198. end;
  199.  
  200. function TPOP3Send.Reset: Boolean;
  201. begin
  202.   FSock.SendString('RSET' + CRLF);
  203.   Result := ReadResult(False) = 1;
  204. end;
  205.  
  206. function TPOP3Send.NoOp: Boolean;
  207. begin
  208.   FSock.SendString('NOOP' + CRLF);
  209.   Result := ReadResult(False) = 1;
  210. end;
  211.  
  212. function TPOP3Send.Stat: Boolean;
  213. var
  214.   s: string;
  215. begin
  216.   Result := False;
  217.   FSock.SendString('STAT' + CRLF);
  218.   if ReadResult(False) <> 1 then
  219.     Exit;
  220.   s := SeparateRight(ResultString, '+OK ');
  221.   FStatCount := StrToIntDef(SeparateLeft(s, ' '), 0);
  222.   FStatSize := StrToIntDef(SeparateRight(s, ' '), 0);
  223.   Result := True;
  224. end;
  225.  
  226. function TPOP3Send.List(Value: Integer): Boolean;
  227. begin
  228.   if Value = 0 then
  229.     FSock.SendString('LIST' + CRLF)
  230.   else
  231.     FSock.SendString('LIST ' + IntToStr(Value) + CRLF);
  232.   Result := ReadResult(Value = 0) = 1;
  233. end;
  234.  
  235. function TPOP3Send.Retr(Value: Integer): Boolean;
  236. begin
  237.   FSock.SendString('RETR ' + IntToStr(Value) + CRLF);
  238.   Result := ReadResult(True) = 1;
  239. end;
  240.  
  241. function TPOP3Send.Dele(Value: Integer): Boolean;
  242. begin
  243.   FSock.SendString('DELE ' + IntToStr(Value) + CRLF);
  244.   Result := ReadResult(False) = 1;
  245. end;
  246.  
  247. function TPOP3Send.Top(Value, Maxlines: Integer): Boolean;
  248. begin
  249.   FSock.SendString('TOP ' + IntToStr(Value) + ' ' + IntToStr(Maxlines) + CRLF);
  250.   Result := ReadResult(True) = 1;
  251. end;
  252.  
  253. function TPOP3Send.Uidl(Value: Integer): Boolean;
  254. begin
  255.   if Value = 0 then
  256.     FSock.SendString('UIDL' + CRLF)
  257.   else
  258.     FSock.SendString('UIDL ' + IntToStr(Value) + CRLF);
  259.   Result := ReadResult(Value = 0) = 1;
  260. end;
  261.  
  262. end.
  263.