2008-04-24 10:12:01 +03:00
|
|
|
{==============================================================================|
|
2008-04-24 10:22:17 +03:00
|
|
|
| Project : Delphree - Synapse | 001.002.003 |
|
2008-04-24 10:12:01 +03:00
|
|
|
|==============================================================================|
|
|
|
|
| Content: NNTP client |
|
|
|
|
|==============================================================================|
|
2008-04-24 10:22:17 +03:00
|
|
|
| Copyright (c)1999-2003, Lukas Gebauer |
|
2008-04-24 10:20:39 +03:00
|
|
|
| All rights reserved. |
|
2008-04-24 10:12:01 +03:00
|
|
|
| |
|
2008-04-24 10:20:39 +03:00
|
|
|
| Redistribution and use in source and binary forms, with or without |
|
|
|
|
| modification, are permitted provided that the following conditions are met: |
|
|
|
|
| |
|
|
|
|
| Redistributions of source code must retain the above copyright notice, this |
|
|
|
|
| list of conditions and the following disclaimer. |
|
|
|
|
| |
|
|
|
|
| Redistributions in binary form must reproduce the above copyright notice, |
|
|
|
|
| this list of conditions and the following disclaimer in the documentation |
|
|
|
|
| and/or other materials provided with the distribution. |
|
|
|
|
| |
|
|
|
|
| Neither the name of Lukas Gebauer nor the names of its contributors may |
|
|
|
|
| be used to endorse or promote products derived from this software without |
|
|
|
|
| specific prior written permission. |
|
|
|
|
| |
|
|
|
|
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
|
|
|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
|
|
|
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
|
|
|
| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
|
|
|
|
| ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|
|
|
|
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|
|
|
|
| SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|
|
|
|
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
|
|
|
|
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
|
|
|
|
| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
|
|
|
|
| DAMAGE. |
|
2008-04-24 10:12:01 +03:00
|
|
|
|==============================================================================|
|
|
|
|
| The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
|
2008-04-24 10:22:17 +03:00
|
|
|
| Portions created by Lukas Gebauer are Copyright (c) 1999-2003. |
|
2008-04-24 10:12:01 +03:00
|
|
|
| All Rights Reserved. |
|
|
|
|
|==============================================================================|
|
|
|
|
| Contributor(s): |
|
|
|
|
|==============================================================================|
|
|
|
|
| History: see HISTORY.HTM from distribution package |
|
|
|
|
| (Found at URL: http://www.ararat.cz/synapse/) |
|
|
|
|
|==============================================================================}
|
|
|
|
|
|
|
|
{$WEAKPACKAGEUNIT ON}
|
|
|
|
|
|
|
|
unit NNTPsend;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
SysUtils, Classes,
|
|
|
|
blcksock, SynaUtil, SynaCode;
|
|
|
|
|
|
|
|
const
|
|
|
|
cNNTPProtocol = 'nntp';
|
|
|
|
|
|
|
|
type
|
2008-04-24 10:20:39 +03:00
|
|
|
TNNTPSend = class(TSynaClient)
|
2008-04-24 10:12:01 +03:00
|
|
|
private
|
|
|
|
FSock: TTCPBlockSocket;
|
|
|
|
FResultCode: Integer;
|
|
|
|
FResultString: string;
|
|
|
|
FData: TStringList;
|
2008-04-24 10:22:17 +03:00
|
|
|
FDataToSend: TStringList;
|
|
|
|
FUsername: string;
|
|
|
|
FPassword: string;
|
2008-04-24 10:12:01 +03:00
|
|
|
function ReadResult: Integer;
|
|
|
|
function ReadData: boolean;
|
|
|
|
function SendData: boolean;
|
|
|
|
function Connect: Boolean;
|
|
|
|
public
|
|
|
|
constructor Create;
|
|
|
|
destructor Destroy; override;
|
|
|
|
function Login: Boolean;
|
|
|
|
procedure Logout;
|
2008-04-24 10:22:17 +03:00
|
|
|
function DoCommand(const Command: string): boolean;
|
|
|
|
function DoCommandRead(const Command: string): boolean;
|
|
|
|
function DoCommandWrite(const Command: string): boolean;
|
2008-04-24 10:12:01 +03:00
|
|
|
function GetArticle(const Value: string): Boolean;
|
|
|
|
function GetBody(const Value: string): Boolean;
|
|
|
|
function GetHead(const Value: string): Boolean;
|
|
|
|
function GetStat(const Value: string): Boolean;
|
|
|
|
function SelectGroup(const Value: string): Boolean;
|
|
|
|
function IHave(const MessID: string): Boolean;
|
|
|
|
function GotoLast: Boolean;
|
|
|
|
function GotoNext: Boolean;
|
|
|
|
function ListGroups: Boolean;
|
|
|
|
function ListNewGroups(Since: TDateTime): Boolean;
|
|
|
|
function NewArticles(const Group: string; Since: TDateTime): Boolean;
|
|
|
|
function PostArticle: Boolean;
|
|
|
|
function SwitchToSlave: Boolean;
|
2008-04-24 10:22:17 +03:00
|
|
|
function Xover(xoStart, xoEnd: string): boolean;
|
2008-04-24 10:12:01 +03:00
|
|
|
published
|
2008-04-24 10:22:17 +03:00
|
|
|
property Username: string read FUsername write FUsername;
|
|
|
|
property Password: string read FPassword write FPassword;
|
2008-04-24 10:12:01 +03:00
|
|
|
property ResultCode: Integer read FResultCode;
|
|
|
|
property ResultString: string read FResultString;
|
|
|
|
property Data: TStringList read FData;
|
|
|
|
property Sock: TTCPBlockSocket read FSock;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
constructor TNNTPSend.Create;
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
FData := TStringList.Create;
|
2008-04-24 10:22:17 +03:00
|
|
|
FDataToSend := TStringList.Create;
|
2008-04-24 10:12:01 +03:00
|
|
|
FSock := TTCPBlockSocket.Create;
|
2008-04-24 10:20:39 +03:00
|
|
|
FSock.ConvertLineEnd := True;
|
2008-04-24 10:12:01 +03:00
|
|
|
FTimeout := 300000;
|
2008-04-24 10:20:39 +03:00
|
|
|
FTargetPort := cNNTPProtocol;
|
2008-04-24 10:22:17 +03:00
|
|
|
FUsername := '';
|
|
|
|
FPassword := '';
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TNNTPSend.Destroy;
|
|
|
|
begin
|
|
|
|
FSock.Free;
|
2008-04-24 10:22:17 +03:00
|
|
|
FDataToSend.Free;
|
2008-04-24 10:12:01 +03:00
|
|
|
FData.Free;
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.ReadResult: Integer;
|
|
|
|
var
|
|
|
|
s: string;
|
|
|
|
begin
|
|
|
|
Result := 0;
|
|
|
|
FData.Clear;
|
|
|
|
s := FSock.RecvString(FTimeout);
|
|
|
|
FResultString := Copy(s, 5, Length(s) - 4);
|
|
|
|
if FSock.LastError <> 0 then
|
|
|
|
Exit;
|
|
|
|
if Length(s) >= 3 then
|
|
|
|
Result := StrToIntDef(Copy(s, 1, 3), 0);
|
|
|
|
FResultCode := Result;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.ReadData: boolean;
|
|
|
|
var
|
|
|
|
s: string;
|
|
|
|
begin
|
|
|
|
repeat
|
|
|
|
s := FSock.RecvString(FTimeout);
|
|
|
|
if s = '.' then
|
|
|
|
break;
|
|
|
|
if (s <> '') and (s[1] = '.') then
|
|
|
|
s := Copy(s, 2, Length(s) - 1);
|
|
|
|
FData.Add(s);
|
|
|
|
until FSock.LastError <> 0;
|
|
|
|
Result := FSock.LastError = 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.SendData: boolean;
|
|
|
|
var
|
|
|
|
s: string;
|
|
|
|
n: integer;
|
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
for n := 0 to FDataToSend.Count - 1 do
|
2008-04-24 10:12:01 +03:00
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
s := FDataToSend[n];
|
|
|
|
if (s <> '') and (s[1] = '.') then
|
2008-04-24 10:12:01 +03:00
|
|
|
s := s + '.';
|
|
|
|
FSock.SendString(s + CRLF);
|
|
|
|
if FSock.LastError <> 0 then
|
|
|
|
break;
|
|
|
|
end;
|
2008-04-24 10:22:17 +03:00
|
|
|
if FDataToSend.Count = 0 then
|
|
|
|
FSock.SendString(CRLF);
|
|
|
|
if FSock.LastError = 0 then
|
|
|
|
FSock.SendString('.' + CRLF);
|
|
|
|
FDataToSend.Clear;
|
2008-04-24 10:12:01 +03:00
|
|
|
Result := FSock.LastError = 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.Connect: Boolean;
|
|
|
|
begin
|
|
|
|
FSock.CloseSocket;
|
2008-04-24 10:20:39 +03:00
|
|
|
FSock.Bind(FIPInterface, cAnyPort);
|
|
|
|
FSock.Connect(FTargetHost, FTargetPort);
|
2008-04-24 10:12:01 +03:00
|
|
|
Result := FSock.LastError = 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.Login: Boolean;
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
if not Connect then
|
|
|
|
Exit;
|
|
|
|
Result := (ReadResult div 100) = 2;
|
2008-04-24 10:22:17 +03:00
|
|
|
if (FUsername <> '') and Result then
|
|
|
|
begin
|
|
|
|
FSock.SendString('AUTHINFO USER ' + FUsername + CRLF);
|
|
|
|
if (ReadResult div 100) = 3 then
|
|
|
|
begin
|
|
|
|
FSock.SendString('AUTHINFO PASS ' + FPassword + CRLF);
|
|
|
|
Result := (ReadResult div 100) = 2;
|
|
|
|
end;
|
|
|
|
end;
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TNNTPSend.Logout;
|
|
|
|
begin
|
|
|
|
FSock.SendString('QUIT' + CRLF);
|
|
|
|
ReadResult;
|
|
|
|
FSock.CloseSocket;
|
|
|
|
end;
|
|
|
|
|
2008-04-24 10:22:17 +03:00
|
|
|
function TNNTPSend.DoCommand(const Command: string): Boolean;
|
|
|
|
begin
|
|
|
|
FSock.SendString(Command + CRLF);
|
|
|
|
Result := (ReadResult div 100) = 2;
|
|
|
|
Result := Result and (FSock.LastError = 0);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.DoCommandRead(const Command: string): Boolean;
|
|
|
|
begin
|
|
|
|
Result := DoCommand(Command);
|
|
|
|
if Result then
|
|
|
|
begin
|
|
|
|
Result := ReadData;
|
|
|
|
Result := Result and (FSock.LastError = 0);
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.DoCommandWrite(const Command: string): Boolean;
|
|
|
|
var
|
|
|
|
x: integer;
|
|
|
|
begin
|
|
|
|
FDataToSend.Assign(FData);
|
|
|
|
FSock.SendString(Command + CRLF);
|
|
|
|
x := (ReadResult div 100);
|
|
|
|
if x = 3 then
|
|
|
|
begin
|
|
|
|
SendData;
|
|
|
|
x := (ReadResult div 100);
|
|
|
|
end;
|
|
|
|
Result := x = 2;
|
|
|
|
Result := Result and (FSock.LastError = 0);
|
|
|
|
end;
|
|
|
|
|
2008-04-24 10:12:01 +03:00
|
|
|
function TNNTPSend.GetArticle(const Value: string): Boolean;
|
|
|
|
var
|
|
|
|
s: string;
|
|
|
|
begin
|
|
|
|
s := 'ARTICLE';
|
|
|
|
if Value <> '' then
|
|
|
|
s := s + ' ' + Value;
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := DoCommandRead(s);
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.GetBody(const Value: string): Boolean;
|
|
|
|
var
|
|
|
|
s: string;
|
|
|
|
begin
|
|
|
|
s := 'BODY';
|
|
|
|
if Value <> '' then
|
|
|
|
s := s + ' ' + Value;
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := DoCommandRead(s);
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.GetHead(const Value: string): Boolean;
|
|
|
|
var
|
|
|
|
s: string;
|
|
|
|
begin
|
|
|
|
s := 'HEAD';
|
|
|
|
if Value <> '' then
|
|
|
|
s := s + ' ' + Value;
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := DoCommandRead(s);
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.GetStat(const Value: string): Boolean;
|
|
|
|
var
|
|
|
|
s: string;
|
|
|
|
begin
|
|
|
|
s := 'STAT';
|
|
|
|
if Value <> '' then
|
|
|
|
s := s + ' ' + Value;
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := DoCommandRead(s);
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.SelectGroup(const Value: string): Boolean;
|
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := DoCommand('GROUP ' + Value);
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.IHave(const MessID: string): Boolean;
|
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := DoCommandWrite('IHAVE ' + MessID);
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.GotoLast: Boolean;
|
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := DoCommand('LAST');
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.GotoNext: Boolean;
|
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := DoCommand('NEXT');
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.ListGroups: Boolean;
|
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := DoCommandRead('LIST');
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.ListNewGroups(Since: TDateTime): Boolean;
|
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := DoCommandRead('NEWGROUPS ' + SimpleDateTime(Since) + ' GMT');
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.NewArticles(const Group: string; Since: TDateTime): Boolean;
|
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := DoCommandRead('NEWNEWS ' + Group + ' ' + SimpleDateTime(Since) + ' GMT');
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.PostArticle: Boolean;
|
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := DoCommandWrite('POST');
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.SwitchToSlave: Boolean;
|
|
|
|
begin
|
2008-04-24 10:22:17 +03:00
|
|
|
Result := DoCommand('SLAVE');
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TNNTPSend.Xover(xoStart, xoEnd: string): Boolean;
|
|
|
|
var
|
|
|
|
s: string;
|
|
|
|
begin
|
|
|
|
s := 'XOVER ' + xoStart;
|
|
|
|
if xoEnd <> xoStart then
|
|
|
|
s := s + '-' + xoEnd;
|
|
|
|
Result := DoCommandRead(s);
|
2008-04-24 10:12:01 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
{==============================================================================}
|
|
|
|
|
|
|
|
end.
|