📄 newsrdr1.cpp
字号:
/*---------------------------------------------------------------------------
Author: Fran鏾is PIETTE
Description: This sample program show how to use TNntpCli to write a news
enabled application.
EMail: francois.piette@pophost.eunet.be http://www.rtfm.be/fpiette
francois.piette@rtfm.be
Creation: January 14, 1997
Version: 1.02
WebSite: http://www.rtfm.be/fpiette/indexuk.htm
Support: Use the mailing list twsocket@rtfm.be See website for details.
Legal issues: Copyright (C) 1997, 1998 by Fran鏾is PIETTE
<francois.piette@pophost.eunet.be>
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Updates:
Feb 21, 1998 Ported form Delphi code
Apr 11, 1998 V1.01 Adapted to BCB3
Aug 15, 1999 V1.02 Adapted for BCB4
Added support for XHDR and MODE READER.
Corrected a bug that let Connect and Abort button
disabled when DNS lookup failed.
---------------------------------------------------------------------------*/
#if __BORLANDC__ == 0x520 // BCB1 is BC5.20 BCB3 is BC5.30
#define _WINSOCKAPI_ // Prevent winsock.h from being included
#endif
#include <vcl\vcl.h>
#include <vcl\inifiles.hpp>
#include <vcl/registry.hpp>
#pragma hdrstop
#include "NewsRdr1.h"
//---------------------------------------------------------------------------
#pragma link "NntpCli"
#pragma resource "*.dfm"
#define IniFileName "NEWSRDR.INI"
TNNTPForm *NNTPForm;
//---------------------------------------------------------------------------
__fastcall TNNTPForm::TNNTPForm(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TNNTPForm::FormShow(TObject *Sender)
{
TIniFile *IniFile;
AnsiString EMail;
AnsiString UserName;
TRegistry *Reg;
AnsiString Key;
if (FInitialized)
return;
EMail = "your.name@yourcompany.domain";
UserName = "Your Name";
// Get username and EMail from the Internet Explorer settings
// Should add code for Netscape Navigator...
Reg = new TRegistry;
Reg->RootKey = HKEY_CURRENT_USER;
Key = "\Software\Microsoft\Internet Mail and News\Mail";
if (Reg->OpenKey(Key, FALSE)) {
EMail = Reg->ReadString("Sender EMail");
UserName = Reg->ReadString("Sender Name");
}
Reg->CloseKey();
delete Reg;
FInitialized = TRUE;
IniFile = new TIniFile(IniFileName);
Top = IniFile->ReadInteger("Window", "Top", Top);
Left = IniFile->ReadInteger("Window", "Left", Left);
Width = IniFile->ReadInteger("Window", "Width", Width);
Height = IniFile->ReadInteger("Window", "Height", Height);
ServerEdit->Text = IniFile->ReadString("Data", "Server", "");
ArticleNumEdit->Text = IniFile->ReadString("Data", "ArticleNum", "");
ArticleIDEdit->Text = IniFile->ReadString("Data", "ArticleID", "");
FileEdit->Text = IniFile->ReadString("Data", "File", "nntprdr.txt");
UserNameEdit->Text = IniFile->ReadString("Data", "UserName", "");
PasswordEdit->Text = IniFile->ReadString("Data", "Password", "");
UserEdit->Text = IniFile->ReadString("Data", "User",
"\"" + UserName +
"\" <" + EMail + ">");
GroupEdit->Text = IniFile->ReadString("Data", "Group",
"borland->public->delphi->internet");
delete IniFile;
DisplayMemo->Clear();
}
//---------------------------------------------------------------------------
void __fastcall TNNTPForm::FormClose(TObject *Sender, TCloseAction &Action)
{
TIniFile *IniFile;
IniFile = new TIniFile(IniFileName);
IniFile->WriteString("Data", "Server", ServerEdit->Text);
IniFile->WriteString("Data", "Group", GroupEdit->Text);
IniFile->WriteString("Data", "ArticleNum", ArticleNumEdit->Text);
IniFile->WriteString("Data", "ArticleID", ArticleIDEdit->Text);
IniFile->WriteString("Data", "File", FileEdit->Text);
IniFile->WriteString("Data", "User", UserEdit->Text);
IniFile->WriteString("Data", "UserName", UserNameEdit->Text);
IniFile->WriteString("Data", "Password", PasswordEdit->Text);
IniFile->WriteInteger("Window", "Top", Top);
IniFile->WriteInteger("Window", "Left", Left);
IniFile->WriteInteger("Window", "Width", Width);
IniFile->WriteInteger("Window", "Height", Height);
delete IniFile;
}
//---------------------------------------------------------------------------
void __fastcall TNNTPForm::Display(const AnsiString Msg)
{
// Limit the memo to 100 lines
while (DisplayMemo->Lines->Count > 100)
DisplayMemo->Lines->Delete(1);
DisplayMemo->Lines->Add(Msg);
};
//---------------------------------------------------------------------------
void __fastcall TNNTPForm::NntpCli1SessionConnected(TObject *Sender, WORD Error)
{
AbortButton->Enabled = TRUE;
Display("Connected, StatusCode = " + IntToStr(NntpCli1->StatusCode));
if (NntpCli1->PostingPermited)
Display("Posting permited");
else
Display("Posting not permited");
Display(NntpCli1->LastResponse);
}
//---------------------------------------------------------------------------
void __fastcall TNNTPForm::NntpCli1SessionClosed(TObject *Sender, WORD Error)
{
AbortButton->Enabled = FALSE;
ConnectButton->Enabled = TRUE;
Display("Connection closed");
}
//---------------------------------------------------------------------------
// This event handler is called for each NNTP command when the command has
// been exected (correctly or not).
void __fastcall TNNTPForm::NntpCli1RequestDone(TObject *Sender,
TNntpRequest RqType, WORD Error)
{
Display("Request done. LastResponse = " + NntpCli1->LastResponse);
if (Error == 0)
Display("No error");
else
Display("Error #" + IntToStr(Error));
switch (RqType) {
case nntpConnect:
if (Error != 0) {
AbortButton->Enabled = FALSE;
ConnectButton->Enabled = TRUE;
Display("Connect failed");
}
break;
case nntpGroup:
Display("ArticleEstimated = " + IntToStr(NntpCli1->ArticleEstimated));
Display("ArticleFirst = " + IntToStr(NntpCli1->ArticleFirst));
Display("ArticleLast = " + IntToStr(NntpCli1->ArticleLast));
ArticleNumEdit->Text = IntToStr(NntpCli1->ArticleFirst);
break;
case nntpPost:
case nntpQuit:
case nntpAbort:
case nntpHelp:
case nntpNewGroups:
case nntpNewNews:
case nntpXOver:
case nntpListOverViewFmt:
case nntpAuthenticate:
case nntpXHdr:
case nntpModeReader:
// Nothing to do
break;
case nntpDate:
Display("Server Date is " + DateTimeToStr(NntpCli1->ServerDate));
break;
case nntpStatByNumber:
case nntpStatByID:
case nntpHeadByNumber:
case nntpHeadByID:
case nntpBodyByNumber:
case nntpBodyByID:
case nntpArticleByNumber:
case nntpArticleByID:
case nntpNext:
case nntpLast:
Display("ArticleNumber = " + IntToStr(NntpCli1->ArticleNumber));
Display("ArticleID = <" + NntpCli1->ArticleID + ">");
if (Error == 0) {
ArticleNumEdit->Text = IntToStr(NntpCli1->ArticleNumber);
ArticleIDEdit->Text = NntpCli1->ArticleID;
};
break;
default:
Display("Unknown request type.");
}
// If any stream where used, destroy it
if (FDataStream) {
delete FDataStream;
FDataStream = NULL;
}
}
//---------------------------------------------------------------------------
// This event handler is called by TNntpCli when it has received data and
// don't know what to do with it. It should normally not occur !
void __fastcall TNNTPForm::NntpCli1DataAvailable(TObject *Sender, WORD Error)
{
Display("Data: " + NntpCli1->LastResponse);
}
//---------------------------------------------------------------------------
// This event handler is called by TNntpCli component just before the
// component will begin receiving a message. It's a good place to open a
// file or start a progress bar.
void __fastcall TNNTPForm::NntpCli1MessageBegin(TObject *Sender)
{
Display("Message begin");
}
//---------------------------------------------------------------------------
// This event handler is called by TNntpCli component for each line of an
// incomming message. Header line as well as body lines are comming here.
// It's a good place to write to a file or update screen or progress bar.
// It's also the place to intercept header lines.
void __fastcall TNNTPForm::NntpCli1MessageLine(TObject *Sender)
{
AnsiString NewsGroupName;
int LastArticle;
int FirstArticle;
char PostingFlag;
Display("Line: " + NntpCli1->LastResponse);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -