18.4. Reading and Posting Usenet News MessagesProblemYou want to connect to a Usenet news server to read and post messages. Your program could send a periodic posting to a newsgroup,[ 1 ] summarize a newsgroup, or identify first-time contributors in a newsgroup so you can send them a helpful welcome message.
SolutionUse the CPAN module Net::NNTP:
use Net::NNTP;
$server = Net::NNTP->new("news.host.dom")
or die "Can't connect to news server: $@\n";
($narticles, $first, $last, $name) = $server->group( "misc.test" )
or die "Can't select misc.test\n";
$headers = $server->head($first)
or die "Can't get headers from article $first in $name\n";
$bodytext = $server->body($first)
or die "Can't get body from article $first in $name\n";
$article = $server->article($first)
or die "Can't get article $first from $name\n";
$server->
DiscussionUsenet is a distributed news system. Servers exchange messages to ensure that each server gets all the messages for the newsgroups it carries. Each server sets its own expiration criteria to decide how long messages stay on the server. Client newsreaders connect to their designated server (usually belonging to their company, ISP, or university) and can read existing postings and contribute new ones. Each message (or article, as they're also known) has a set of headers and a body, separated by a blank line. Articles are identified in two ways: the message ID header and an article number within a newsgroup. An article's message ID is stored in the message itself and is guaranteed to be unique no matter which news server the article was read from. When an article references others, it does so by message ID. A message ID is a string like: <0401@jpl-devvax.JPL.NASA.GOV> An article can also be identified by a newsgroup and an article number within the group. Each news server assigns its own article numbers to the articles it has, so they're only guaranteed to be good for the news server you got them from.
The Net::NNTP constructor connects to the specified news server. If the connection couldn't be made, it returns $server = Net::NNTP->new("news.mycompany.com") or die "Couldn't connect to news.mycompany.com: $@\n";
Once connected, you can get a list of newsgroups with the
$grouplist = $server->
Much as FTP has the concept of a current directory, the Network News Transfer Protocol (NNTP) has the concept of a current group. Make a group the current group with the ($narticles, $first, $last, $name) = $server->group("comp.lang.perl.misc") or die "Can't select comp.lang.perl.misc\n";
The
There are two ways to retrieve articles: call @lines = $server->article($message_id) or die "Can't fetch article $message_id: $!\n";
You can fetch an article's header or body with the @group = $server->group("comp.lang.perl.misc") or die "Can't select group comp.lang.perl.misc\n"; @lines = $server->head($group[1]) or die "Can't get headers from first article in comp.lang.perl.misc\n";
To post an article, use the $server->post(@message) or die "Can't post\n";
Use the
unless ($server->
Read the manpage for Net::NNTP for a complete list of methods. See AlsoThe documentation for the Net::NNTP module from CPAN; RFC 977, Network News Transfer Protocol ; your system's trn (1) and innd (8) manpages (if you have them) Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|