Posting to nntp newsgroup with Perl (Net::NNTP)

S

sadie-no-reply

I am trying to post a message to a newsgroup using Net::NNTP, but I keep
getting an error message. Help!
I can read from the newsgroup without problems and $nntp->postok() gives OK.
I suspect there's something wrong with way I've constructed my message
array.
Here's the code.

use Net::NNTP;

$server="news.yourserver.com"; #replace this with your nntp server name

#connect to the nntp server (this works fine)
$nntp = Net::NNTP->new($server);

#open a newsgroup (this works fine)
$nntp->group("nl.test") or die "cannot open nl.test";

#download and print the last message (this works fine)
$articleId = $nntp->last();
@tekst = $nntp->article($articleId);
for ($i=1; $t=$tekst[0][$i]; $i++)
{print $t."<BR>\n";}

#check if I'm alowed to post (returns: ok)
if ($nntp->postok())
{print "Posting would be ok\n";}else
{print "Posting not ok\n";}

#construct a message - I'm not sure about this.
#For example, should there be \r\n at the end of each line??
@message=(
"Subject: this is test 1\r\n",
"From: cllq\@noreply.nil\r\n",
"Newsgroups: nl.test\r\n",
"Date: Sat, 3 Mar 2007 20:14:13 +0100\r\n",
"\r\n",
"This is my message\r\n",
"Yours sincerely\r\n",
"Bla\r\n");

#now post the message
$nntp->post(@message) or die "post failed"; #this one fails, but why?
$nntp->quit();

#alternative code that also fails:
$nntp->post() or die "post failed"; #this one ok
$nntp->datasend(@message) or die "datasend nok"; #this one ok
$nntp->dataend() or die "dataend nok"; #this one fails but why?
$nntp->quit();
 
P

Peter J. Holzer

I am trying to post a message to a newsgroup using Net::NNTP, but I keep
getting an error message. Help! [...]
#now post the message
$nntp->post(@message) or die "post failed"; #this one fails, but why?

Why ask us? Ask perl:

$nntp->post(@message) or die "post failed: " . $nntp->message();

hp
 
S

sadie-no-reply

Thanks for the tip. It returns:
340 <[email protected]> (desired) Article has no
body -- just headers

After checking RFC977&850, I changed @message into:

@message=(
"Subject: dit is test 2
From: cllq\@noreply.nil
Newsgroups: nl.test
Date: Sat, 4 Mar 2007 20:45:13 +0100

Hier staat de eerste regel
Hier de tweede
..");

And it seems to work. At least returns ok status. Haven't seen the result
back, though...

Peter J. Holzer said:
I am trying to post a message to a newsgroup using Net::NNTP, but I keep
getting an error message. Help! [...]
#now post the message
$nntp->post(@message) or die "post failed"; #this one fails, but why?

Why ask us? Ask perl:

$nntp->post(@message) or die "post failed: " . $nntp->message();

hp


--
_ | Peter J. Holzer | Es ist ganz einfach ihn zu verstehen, wenn
|_|_) | Sysadmin WSR | man nur alle wichtigen Worte im Satz durch
| | | (e-mail address removed) | andere ersetzt.
__/ | http://www.hjp.at/ | -- Nils Ketelsen in danr
 
J

Jamie

In said:
Thanks for the tip. It returns:
340 <[email protected]> (desired) Article has no
body -- just headers

After checking RFC977&850, I changed @message into:

@message=(
"Subject: dit is test 2
From: cllq\@noreply.nil
Newsgroups: nl.test
Date: Sat, 4 Mar 2007 20:45:13 +0100

Hier staat de eerste regel
Hier de tweede
.");

And it seems to work. At least returns ok status. Haven't seen the result
back, though...

You might look into the datasend(), dataend() methods of Net::Cmd (these
are inherited by Net::NNTP, so you can use them withing a $nntp object
as well)

Not saying it's proper, but I generally just use telnet combined with
Net::Cmd and Net::NNTP. (actually, you could easily just use Net::Cmd if
you wanted) telnet for debugging and getting a feel for what the perl
modules are doing.


# The critical bits from one of my "inews" clone scripts:
#
# I left out the connect and alarm wrappers.
#
$nntp->post();
while(my $line = <>){
$nntp->datasend($line);
}
$nntp->dataend();
unless($nntp->ok()){
die sprintf("NNTP[%03d] %s\n",$nntp->code(),$nntp->message());
}

Try posting a small article (to a test group) with telnet to get a feel
for how NNTP works. It'll make using the perl modules easier.

Jamie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top