View NG with Net::NNTP

M

Mike

I tried posting a few minutes ago via Google, and got an ISE, so
please forgive me if I'm repeating.

I have a client that would really benefit from allowing his visitors
to read a specific newsgroup on his site. I've been looking through
some modules, and even though a lot of the standard modules mention
this, I haven't been able to find any actual references so I think I
might be misunderstanding the wording in the modules.

Specifically, I want to be able to show the most recent, say, 20
article headers from a specific newsgroup, then when the visitor
selects an article then they can read the body. Then, I want to give
the visitor an option to post to that NG.

So far, I've researched LWP::UserAgent and Net::NNTP. Both seem like
they're talking about viewing and posting, but all of the references
I'm finding are about posting. Can either of these let me view the
articles themselves? I don't think I'll have a problem with posting,
just viewing.

TIA,

Mike
 
J

James Willmore

On 2 Sep 2003 21:39:39 -0700
So far, I've researched LWP::UserAgent and Net::NNTP. Both seem like
they're talking about viewing and posting, but all of the references
I'm finding are about posting. Can either of these let me view the
articles themselves? I don't think I'll have a problem with posting,
just viewing.

This is just a sample (I cut up a script I just recently started
working on, so _don't_ consider it production quaility).

==untested==
#!/usr/bin/perl -w

use strict;
use Net::NNTP;

my($SERVER, $nntp, $articles, $first, $last, $ng_name);

$SERVER = 'localhost';
$nntp = Net::NNTP->new($SERVER, Debug=>0)
or die "Can't connect to server $SERVER: $!\n";

($articles,$first,$last,$ng_name) =
$nntp->group('comp.lang.perl.misc');
printf("%s: %5d articles (%-5d to
%-5d)\n",$ng_name,$articles,$first,$last);

while(my $fh = $nntp->articlefh){
while(<$fh>){
print;
}
$nntp->next;
}

$nntp->quit;
==untested==

This will print each message in the newsgrop indicated. It takes some
re-reading of the Net::NNTP documentation to get a full understanding
of it.

HTH
 
T

Tad McClellan

Mike said:
I have a client that would really benefit from allowing his visitors
to read a specific newsgroup on his site.

Specifically, I want to be able to show the most recent, say, 20
article headers from a specific newsgroup, then when the visitor
selects an article then they can read the body. Then, I want to give
the visitor an option to post to that NG.


Net::NNTP can be used to do that.

So far, I've researched LWP::UserAgent and Net::NNTP.


Eh?

Those are two totally different beasties, they don't even
use the same protocol.

UserAgent is for automating web forms (so it uses HTTP).

Can either of these let me view the
articles themselves?


Yes, Net::NNTP::article() can retrieve the headers and body
of a newsgroup article.
 
T

Tom

I have a client that would really benefit from allowing his visitors
to read a specific newsgroup on his site.

Specifically, I want to be able to show the most recent, say, 20
article headers from a specific newsgroup, then when the visitor
selects an article then they can read the body. Then, I want to give
the visitor an option to post to that NG.

You can use Net::NNTP to do the above. I'm currently developing a
feature similar to what you described into a template system that I
developed called TML (Template Markup Language). You can view the
sample outputs by going to
http://ztml.com/cgi-bin/demo/mb.pl?disp+perl+NGtopics.admin.dsp

Tom
 
R

Randal L. Schwartz

Mike> I have a client that would really benefit from allowing his visitors
Mike> to read a specific newsgroup on his site. I've been looking through
Mike> some modules, and even though a lot of the standard modules mention
Mike> this, I haven't been able to find any actual references so I think I
Mike> might be misunderstanding the wording in the modules.

Consider the code given in my column as a starting point:

<http://www.stonehenge.com/merlyn/WebTechniques/col62.html>

print "Just another Perl hacker,"
 
J

James Willmore

On 2 Sep 2003 21:39:39 -0700
(e-mail address removed) (Mike) wrote:
==untested==
#!/usr/bin/perl -w

use strict;
use Net::NNTP;

my($SERVER, $nntp, $articles, $first, $last, $ng_name);

$SERVER = 'localhost';
$nntp = Net::NNTP->new($SERVER, Debug=>0)
or die "Can't connect to server $SERVER: $!\n";

($articles,$first,$last,$ng_name) =
$nntp->group('comp.lang.perl.misc');
printf("%s: %5d articles (%-5d to
%-5d)\n",$ng_name,$articles,$first,$last);

while(my $fh = $nntp->articlefh){
while(<$fh>){
print;
}
$nntp->next;
}

$nntp->quit;
==untested==

This will print each message in the newsgrop indicated. It takes
some re-reading of the Net::NNTP documentation to get a full
understanding of it.

I should follow my own advise :)
The above code will cause an infinite loop - opps! The below code
(tested and with comments) works :)

#!/usr/bin/perl -w

use strict;
use Net::NNTP;

#set lexical variables
my($SERVER, $nntp, $articles, $first, $last, $ng_name);

#define NNTP server
$SERVER = 'localhost';
#declare new Net::NNTP object - or die with a connection failure
#message
$nntp = Net::NNTP->new($SERVER, Debug=>0)
or die "Can't connect to server $SERVER: $!\n";

#define what newsgroup to use -
#get the newsgroup name, amount of article, the first article number
#and last article number
($articles,$first,$last,$ng_name) =
$nntp->group('comp.lang.perl.misc');
#print the information out
printf("%s: %5d articles (%-5d to
%-5d)\n",$ng_name,$articles,$first,$last);

#declare a messages counter
my $x = 1;
#set the nntpstat marker to the last message in the newsgroup
$nntp->last;
#while we can go to the next message ....
while($nntp->next){
#print what number message we're on
print "num: $x\n";
#declare a file handle for the article
my $fh = $nntp->articlefh;
#print the file handle
while(<$fh>){
print;
}
#limit how many messages you want
last if $x == 1;
#increment messages counter
$x++;
}

#close the connection to the NNTP server
$nntp->quit;



HTH
 
J

James Willmore

James> I should follow my own advise :)
James> The above code will cause an infinite loop - opps! The below code
James> (tested and with comments) works :)

And to keep from seeing the same messages twice, you can even combine
it with as I did in my column article at:

<http://www.stonehenge.com/merlyn/UnixReview/col22.html>

Thanks for the tip. I was using a variation of the overview_fmt,
xover, and a hash that, well, got very messy.

Thanks.

Jim
 
M

Mike

I should follow my own advise :)
The above code will cause an infinite loop - opps! The below code
(tested and with comments) works :)

#!/usr/bin/perl -w

use strict;
use Net::NNTP;
#set the nntpstat marker to the last message in the newsgroup
$nntp->last;
#while we can go to the next message ....
while($nntp->next){
<snip>


Hey, all, thanks for the help. I really do appreciate it!

I had originally started my own program that was quite similar to
this, but the only information I was getting was the newsgroup name,
first, last, and number of articles. I couldn't get it to pull up the
article itself, which is why I thought I was doing something wrong.

But when I used your program above, Jim, I get the same thing. It's
like "$nntp->last" isn't returning anything. I tried setting a
variable to it, like "$lastmessage = $nntp->last" and according to
CPAN that's supposed to return the message ID, but it didn't return
anything at all.

Is there a syntax error here that I'm overlooking? Following this
program verbatim, the only results I got were:

comp.lang.perl.misc: 29851 articles (448306 to 478156)
num: 1


Nothing in the "while" statement at all. Thanks again, all,

Mike
 
J

James Willmore

On 3 Sep 2003 17:34:26 -0700
But when I used your program above, Jim, I get the same thing. It's
like "$nntp->last" isn't returning anything. I tried setting a
variable to it, like "$lastmessage = $nntp->last" and according to
CPAN that's supposed to return the message ID, but it didn't return
anything at all.

Is there a syntax error here that I'm overlooking? Following this
program verbatim, the only results I got were:

comp.lang.perl.misc: 29851 articles (448306 to 478156)
num: 1


Nothing in the "while" statement at all. Thanks again, all,

I'm at a loss. I tested against leafnode as the NNTP server and it
worked. According to the documentation, using the 'last' method sets
the marker to the last message.

In theory, if you were to uncomment that line, you may have better
results - depending upon what NNTP server you use it against. Or,
you could get the first message ID, use the 'nntpstat' method to set
the message marker for that message, then use next to iterate through
all the messages. It's just a guess.

I don't want to test against a "live" NNTP server until I feel
comfortable using Net::NNTP yet. I use a dialup connection and don't
wish to be on all night if something goes south. Sorry I don't have
a
better answer for you at this moment. If I come up with anything,
I'll post it -or- if someone else has run into this situation, maybe
they'll post a solution.

You should check out Randal's solution (posted in another post). That
may give you some more ideas. I haven't had a chance to read it fully
yet..
 
J

James Willmore

In theory, if you were to uncomment that line, you may have better

comment out, not uncomment. :(
!@#? fingers - wish the fingers and the brain were in sync once in a
while :)
 
T

Tom

James Willmore said:
I'm at a loss. I tested against leafnode as the NNTP server and it
worked. According to the documentation, using the 'last' method sets
the marker to the last message.

Perhaps you might have misinterpreted the documentation. The LAST
method does not set the marker to the last message; rather it moves
the pointer to the PREVIOUS entry.

Tom
ztml.com
 
J

James Willmore

On 4 Sep 2003 03:50:01 -0700
Perhaps you might have misinterpreted the documentation. The LAST
method does not set the marker to the last message; rather it moves
the pointer to the PREVIOUS entry.

Yes, you're right. :(

Does it work for you (the code I posted)? Anything else in error?
Comments welcomed.

--
Jim
---
Copyright notice: all code written by the author in this post is
released under the GPL. http://www.gnu.org/licenses/gpl.txt
for more information.
---
a real quote ...
Linus Torvalids: "They are somking crack ...."
(http://www.eweek.com/article2/0,3959,1227150,00.asp)
---
a fortune quote ...
One nice thing about egotists: they don't talk about other
people.
 
T

Tom

James Willmore said:
Yes, you're right. :(

Does it work for you (the code I posted)? Anything else in error?
Comments welcomed.

#!/usr/bin/perl -w

use strict;
use Net::NNTP;

#set lexical variables
my($SERVER, $nntp, $articles, $first, $last, $ng_name);

#define NNTP server
$SERVER = 'localhost';
#declare new Net::NNTP object - or die with a connection failure
#message
$nntp = Net::NNTP->new($SERVER, Debug=>0)
or die "Can't connect to server $SERVER: $!\n";

#define what newsgroup to use -
#get the newsgroup name, amount of article, the first article number
#and last article number
($articles,$first,$last,$ng_name) =
$nntp->group('comp.lang.perl.misc');
#print the information out
printf("%s: %5d articles (%-5d to
%-5d)\n",$ng_name,$articles,$first,$last);

#declare a messages counter
my $x = 1;
#set the nntpstat marker to the last message in the newsgroup
#
# ***************

# $nntp->last;
$nntp->nntpstat($last);

# ***************
#while we can go to the next message ....
# ***************

#while($nntp->next){
while(my $msgid=$nntp->last) {

# ***************
#print what number message we're on
print "num: $x\n";
#declare a file handle for the article
# ***************

# not all Net::NNTP packages include this function
# my $fh = $nntp->articlefh;
$nntp->article($msgid,*STDOUT);

# ***************
#print the file handle
# while(<$fh>){

print;

# }
#limit how many messages you want
last if $x == 1;
#increment messages counter
$x++;
}

------------------------------------------

The changes made will output the last (most recent) message first. To
output the oldest message first then nntpstat would be
$nntp->nntpstat($first). The method in the while loop now should be
$nttp->next.

Tom
ztml.com
 
J

James Willmore

The changes made will output the last (most recent) message first. To
output the oldest message first then nntpstat would be
$nntp->nntpstat($first). The method in the while loop now should be
$nttp->next.


I'm hoping the OP is still around, because I feel like I "stole" his
thread. But hey, I'm always open to new experiences :)

Thanks again.

Jim
 
M

Mike

#!/usr/bin/perl -w
use strict;
use Net::NNTP;
while(my $msgid=$nntp->last) {
# not all Net::NNTP packages include this function
# my $fh = $nntp->articlefh;
$nntp->article($msgid,*STDOUT);

print;
last if $x == 1;
$x++;
}


You guys are being an incredible help, I really appreciate that.
Apparently, my package simply doesn't recognize "$fh =
$nntp->articlefh;", like you said Tom, because changing it worked
perfectly.

Last question (I think). I noticed that, in my own script, I didn't
need to include the last "print;" statement; the statement
"$nntp->article($msgid,*STDOUT);" was printing for me. Based on this,
how would I modify the output? I can't seem to set a variable equal to
the output, but I need to do things like changing /n to <br>, and I
was hoping to create a blacklist to take out profanity.

Can I do something like this (this didn't work, but it describes what
I'm needing)?
$nntp->article($msgid,*STDOUT) =~ tr/\n/<br>/;

Thanks again,

Mike
 
M

Mike

I posted about an hour ago, and just discovered that I have to ask
another question about this. Sorry about that!

I'm using a remote host for the final site, but I've been writing and
testing on my PC using localhost. The news server I'm using is through
my dial-up ISP, but the remote host doesn't offer a news server.

Based on this, once I upload to a final server, will I still be able
to retrieve and post through my ISP? If not, is there an alternative?

Thanks again,

Mike
 
T

Tom

You guys are being an incredible help, I really appreciate that.
Apparently, my package simply doesn't recognize "$fh =
$nntp->articlefh;", like you said Tom, because changing it worked
perfectly.

Last question (I think). I noticed that, in my own script, I didn't
need to include the last "print;" statement; the statement

PERL will assume alots of stuff if you don't specify something to operate
on. In the example, you can enter print $_ or just print, or in what
you have discovered by leaving it out all together, and PERL will take care
the rest.
"$nntp->article($msgid,*STDOUT);" was printing for me. Based on this,
how would I modify the output? I can't seem to set a variable equal to
the output, but I need to do things like changing /n to <br>, and I
was hoping to create a blacklist to take out profanity.

Can I do something like this (this didn't work, but it describes what
I'm needing)?
$nntp->article($msgid,*STDOUT) =~ tr/\n/<br>/;

$msgid and *STDOUT are optional arguments. To capture the output from the
function, you simply enter:
my $text = $nntp->article;
{ do something with $text }

Tom
ztml.com
 
R

Ryan Shondell

PERL will assume alots of stuff if you don't specify something to operate
on. In the example, you can enter print $_ or just print, or in what
you have discovered by leaving it out all together, and PERL will take care
the rest.

Actually, PERL won't do any of that. Using perl will allow you to
parse Perl, but all PERL does is tell everyone you didn't read the
perlfaq. :)

perldoc -q '"perl" and "Perl"'

<end picking of nits> :)

Ryan
 
M

Mike

$msgid and *STDOUT are optional arguments. To capture the output from the
function, you simply enter:
my $text = $nntp->article;
{ do something with $text }


When I do that, the output I get is:
ARRAY(0x15f1a0c)

My exact code snippet is:
my $text = $nntp->article;
$text =~ tr/\n/<br>/;
# also tried using:
# my @body =~ split(/\n/, $text);
print "$text";

In fact, it actually prints the entire article without changing the \n
to <br>, as if it's ignoring the "my $text=" section, then after that
it prints "ARRAY(0x15f1a0c)".

I'm thinking that this would work if I could use articlefh instead,
but my server doesn't seem to recognize that. Or is there something
else that I'm noticing?

Thanks again for your help,

Mike
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top