home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / zkuste / delphi / kompon / d3456 / SYNAPSE.ZIP / source / lib / NNTPsend.pas < prev    next >
Pascal/Delphi Source File  |  2002-07-07  |  10KB  |  323 lines

  1. {==============================================================================|
  2. | Project : Delphree - Synapse                                   | 001.001.000 |
  3. |==============================================================================|
  4. | Content: NNTP client                                                         |
  5. |==============================================================================|
  6. | Copyright (c)1999-2002, 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) 1999,2000,2001.          |
  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.  
  45. {$WEAKPACKAGEUNIT ON}
  46.  
  47. unit NNTPsend;
  48.  
  49. interface
  50.  
  51. uses
  52.   SysUtils, Classes,
  53.   blcksock, SynaUtil, SynaCode;
  54.  
  55. const
  56.   cNNTPProtocol = 'nntp';
  57.  
  58. type
  59.   TNNTPSend = class(TSynaClient)
  60.   private
  61.     FSock: TTCPBlockSocket;
  62.     FResultCode: Integer;
  63.     FResultString: string;
  64.     FData: TStringList;
  65.     function ReadResult: Integer;
  66.     function ReadData: boolean;
  67.     function SendData: boolean;
  68.     function Connect: Boolean;
  69.   public
  70.     constructor Create;
  71.     destructor Destroy; override;
  72.     function Login: Boolean;
  73.     procedure Logout;
  74.     function GetArticle(const Value: string): Boolean;
  75.     function GetBody(const Value: string): Boolean;
  76.     function GetHead(const Value: string): Boolean;
  77.     function GetStat(const Value: string): Boolean;
  78.     function SelectGroup(const Value: string): Boolean;
  79.     function IHave(const MessID: string): Boolean;
  80.     function GotoLast: Boolean;
  81.     function GotoNext: Boolean;
  82.     function ListGroups: Boolean;
  83.     function ListNewGroups(Since: TDateTime): Boolean;
  84.     function NewArticles(const Group: string; Since: TDateTime): Boolean;
  85.     function PostArticle: Boolean;
  86.     function SwitchToSlave: Boolean;
  87.   published
  88.     property ResultCode: Integer read FResultCode;
  89.     property ResultString: string read FResultString;
  90.     property Data: TStringList read FData;
  91.     property Sock: TTCPBlockSocket read FSock;
  92.   end;
  93.  
  94. implementation
  95.  
  96. const
  97.   CRLF = #13#10;
  98.  
  99. constructor TNNTPSend.Create;
  100. begin
  101.   inherited Create;
  102.   FData := TStringList.Create;
  103.   FSock := TTCPBlockSocket.Create;
  104.   FSock.CreateSocket;
  105.   FSock.ConvertLineEnd := True;
  106.   FTimeout := 300000;
  107.   FTargetPort := cNNTPProtocol;
  108. end;
  109.  
  110. destructor TNNTPSend.Destroy;
  111. begin
  112.   FSock.Free;
  113.   FData.Free;
  114.   inherited Destroy;
  115. end;
  116.  
  117. function TNNTPSend.ReadResult: Integer;
  118. var
  119.   s: string;
  120. begin
  121.   Result := 0;
  122.   FData.Clear;
  123.   s := FSock.RecvString(FTimeout);
  124.   FResultString := Copy(s, 5, Length(s) - 4);
  125.   if FSock.LastError <> 0 then
  126.     Exit;
  127.   if Length(s) >= 3 then
  128.     Result := StrToIntDef(Copy(s, 1, 3), 0);
  129.   FResultCode := Result;
  130. end;
  131.  
  132. function TNNTPSend.ReadData: boolean;
  133. var
  134.   s: string;
  135. begin
  136.   repeat
  137.     s := FSock.RecvString(FTimeout);
  138.     if s = '.' then
  139.       break;
  140.     if (s <> '') and (s[1] = '.') then
  141.       s := Copy(s, 2, Length(s) - 1);
  142.     FData.Add(s);
  143.   until FSock.LastError <> 0;
  144.   Result := FSock.LastError = 0;
  145. end;
  146.  
  147. function TNNTPSend.SendData: boolean;
  148. var
  149.   s: string;
  150.   n: integer;
  151. begin
  152.   for n := 0 to FData.Count -1 do
  153.   begin
  154.     s := FData[n];
  155.     if (s <> '') and (s[1]='.') then
  156.       s := s + '.';
  157.     FSock.SendString(s + CRLF);
  158.     if FSock.LastError <> 0 then
  159.       break;
  160.   end;
  161.   Result := FSock.LastError = 0;
  162. end;
  163.  
  164. function TNNTPSend.Connect: Boolean;
  165. begin
  166.   FSock.CloseSocket;
  167.   FSock.CreateSocket;
  168.   FSock.Bind(FIPInterface, cAnyPort);
  169.   FSock.Connect(FTargetHost, FTargetPort);
  170.   Result := FSock.LastError = 0;
  171. end;
  172.  
  173. function TNNTPSend.Login: Boolean;
  174. begin
  175.   Result := False;
  176.   if not Connect then
  177.     Exit;
  178.   Result := (ReadResult div 100) = 2;
  179. end;
  180.  
  181. procedure TNNTPSend.Logout;
  182. begin
  183.   FSock.SendString('QUIT' + CRLF);
  184.   ReadResult;
  185.   FSock.CloseSocket;
  186. end;
  187.  
  188. function TNNTPSend.GetArticle(const Value: string): Boolean;
  189. var
  190.   s: string;
  191. begin
  192.   Result := False;
  193.   s := 'ARTICLE';
  194.   if Value <> '' then
  195.     s := s + ' ' + Value;
  196.   FSock.SendString(s + CRLF);
  197.   if (ReadResult div 100) <> 2 then
  198.     Exit;
  199.   Result := ReadData;
  200. end;
  201.  
  202. function TNNTPSend.GetBody(const Value: string): Boolean;
  203. var
  204.   s: string;
  205. begin
  206.   Result := False;
  207.   s := 'BODY';
  208.   if Value <> '' then
  209.     s := s + ' ' + Value;
  210.   FSock.SendString(s + CRLF);
  211.   if (ReadResult div 100) <> 2 then
  212.     Exit;
  213.   Result := ReadData;
  214. end;
  215.  
  216. function TNNTPSend.GetHead(const Value: string): Boolean;
  217. var
  218.   s: string;
  219. begin
  220.   Result := False;
  221.   s := 'HEAD';
  222.   if Value <> '' then
  223.     s := s + ' ' + Value;
  224.   FSock.SendString(s + CRLF);
  225.   if (ReadResult div 100) <> 2 then
  226.     Exit;
  227.   Result := ReadData;
  228. end;
  229.  
  230. function TNNTPSend.GetStat(const Value: string): Boolean;
  231. var
  232.   s: string;
  233. begin
  234.   Result := False;
  235.   s := 'STAT';
  236.   if Value <> '' then
  237.     s := s + ' ' + Value;
  238.   FSock.SendString(s + CRLF);
  239.   if (ReadResult div 100) <> 2 then
  240.     Exit;
  241.   Result := FSock.LastError = 0;
  242. end;
  243.  
  244. function TNNTPSend.SelectGroup(const Value: string): Boolean;
  245. begin
  246.   FSock.SendString('GROUP ' + Value + CRLF);
  247.   Result := (ReadResult div 100) = 2;
  248. end;
  249.  
  250. function TNNTPSend.IHave(const MessID: string): Boolean;
  251. var
  252.   x: integer;
  253. begin
  254.   FSock.SendString('IHAVE ' + MessID + CRLF);
  255.   x := (ReadResult div 100);
  256.   if x = 3 then
  257.   begin
  258.     SendData;
  259.     x := (ReadResult div 100);
  260.   end;
  261.   Result := x = 2;
  262. end;
  263.  
  264. function TNNTPSend.GotoLast: Boolean;
  265. begin
  266.   FSock.SendString('LAST' + CRLF);
  267.   Result := (ReadResult div 100) = 2;
  268. end;
  269.  
  270. function TNNTPSend.GotoNext: Boolean;
  271. begin
  272.   FSock.SendString('NEXT' + CRLF);
  273.   Result := (ReadResult div 100) = 2;
  274. end;
  275.  
  276. function TNNTPSend.ListGroups: Boolean;
  277. begin
  278.   FSock.SendString('LIST' + CRLF);
  279.   Result := (ReadResult div 100) = 2;
  280.   if Result then
  281.     Result := ReadData;
  282. end;
  283.  
  284. function TNNTPSend.ListNewGroups(Since: TDateTime): Boolean;
  285. begin
  286.   FSock.SendString('NEWGROUPS ' + SimpleDateTime(Since) + ' GMT' + CRLF);
  287.   Result := (ReadResult div 100) = 2;
  288.   if Result then
  289.     Result := ReadData;
  290. end;
  291.  
  292. function TNNTPSend.NewArticles(const Group: string; Since: TDateTime): Boolean;
  293. begin
  294.   FSock.SendString('NEWNEWS ' + Group + ' ' + SimpleDateTime(Since) + ' GMT' + CRLF);
  295.   Result := (ReadResult div 100) = 2;
  296.   if Result then
  297.     Result := ReadData;
  298. end;
  299.  
  300. function TNNTPSend.PostArticle: Boolean;
  301. var
  302.   x: integer;
  303. begin
  304.   FSock.SendString('POST' + CRLF);
  305.   x := (ReadResult div 100);
  306.   if x = 3 then
  307.   begin
  308.     SendData;
  309.     x := (ReadResult div 100);
  310.   end;
  311.   Result := x = 2;
  312. end;
  313.  
  314. function TNNTPSend.SwitchToSlave: Boolean;
  315. begin
  316.   FSock.SendString('SLAVE' + CRLF);
  317.   Result := (ReadResult div 100) = 2;
  318. end;
  319.  
  320. {==============================================================================}
  321.  
  322. end.
  323.