📄 ch18_05.htm
字号:
<HTML><HEAD><TITLE>Recipe 18.4. Reading and Posting Usenet News Messages (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen & Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:45:03Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch18_01.htm"TITLE="18. Internet Services"><LINKREL="prev"HREF="ch18_04.htm"TITLE="18.3. Sending Mail"><LINKREL="next"HREF="ch18_06.htm"TITLE="18.5. Reading Mail with POP3"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch18_04.htm"TITLE="18.3. Sending Mail"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 18.3. Sending Mail"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch18_01.htm"TITLE="18. Internet Services"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch18_06.htm"TITLE="18.5. Reading Mail with POP3"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 18.5. Reading Mail with POP3"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch18-chap18_reading_0">18.4. Reading and Posting Usenet News Messages</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-575">Problem<ACLASS="indexterm"NAME="ch18-idx-1000004299-0"></A><ACLASS="indexterm"NAME="ch18-idx-1000004299-1"></A><ACLASS="indexterm"NAME="ch18-idx-1000004299-2"></A><ACLASS="indexterm"NAME="ch18-idx-1000004299-3"></A><ACLASS="indexterm"NAME="ch18-idx-1000004299-4"></A><ACLASS="indexterm"NAME="ch18-idx-1000004299-5"></A><ACLASS="indexterm"NAME="ch18-idx-1000004299-6"></A></A></H3><PCLASS="para">You want to connect to a Usenet news server to read and post messages. Your program could send a periodic posting to a newsgroup,[<ACLASS="footnote"HREF="#ch18-pgfId-1000000812">1</A>] summarize a newsgroup, or identify first-time contributors in a newsgroup so you can send them a helpful welcome message.</P><BLOCKQUOTECLASS="footnote"><DIVCLASS="footnote"><PCLASS="para"><ACLASS="footnote"NAME="ch18-pgfId-1000000812">[1]</A> If so, be sure to check out Ian Kluft's <EMCLASS="emphasis">auto-faq</EM> program at <ACLASS="systemitem.url"HREF="http://www.novia.net/~pschleck/auto-faq/">http://www.novia.net/~pschleck/auto-faq/</A>.</P></DIV></BLOCKQUOTE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-581">Solution</A></H3><PCLASS="para">Use the CPAN module Net::NNTP:</P><PRECLASS="programlisting">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-><CODECLASS="literal">postok()</CODE> or warn "Server didn't tell me I could post.\n";$server->post( [ @lines ] ) or die "Can't post: $!\n";</PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-623">Discussion</A></H3><PCLASS="para">Usenet 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.</P><PCLASS="para">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 <EMCLASS="emphasis">message ID</EM> header and an <EMCLASS="emphasis">article number</EM> 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:</P><PRECLASS="programlisting"><0401@jpl-devvax.JPL.NASA.GOV></PRE><PCLASS="para">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.</P><PCLASS="para">The Net::NNTP constructor connects to the specified news server. If the connection couldn't be made, it returns <CODECLASS="literal">undef</CODE> and sets <CODECLASS="literal">$@</CODE> to an error message. If the connection was successfully made, <CODECLASS="literal">new</CODE> returns a new Net::NNTP object:</P><PRECLASS="programlisting">$server = Net::NNTP->new("news.mycompany.com") or die "Couldn't connect to news.mycompany.com: $@\n";</PRE><PCLASS="para">Once connected, you can get a list of newsgroups with the <CODECLASS="literal">list</CODE> method. This returns a reference to a hash whose keys are newsgroup names. Each value is a reference to an array consisting of the first valid article number in the group, the last valid article number in the group, and a string of flags. The flags are typically <CODECLASS="literal">"y"</CODE>, meaning you may post, but could be <CODECLASS="literal">"m"</CODE> for moderated or <CODECLASS="literal">=NAME</CODE>, meaning that the group is an alias for the newsgroup <CODECLASS="literal">NAME</CODE>. There are over 17,000 newsgroups that your server might carry, so fetching a list of all the groups can take a while.</P><PRECLASS="programlisting">$grouplist = $server-><CODECLASS="literal">list()</CODE> or die "Couldn't fetch group list\n";foreach $group (keys %$grouplist) { if ($grouplist->{$group}->[2] eq 'y') { # I can post to $group }}</PRE><PCLASS="para">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 <CODECLASS="literal">group</CODE> method:</P><PRECLASS="programlisting">($narticles, $first, $last, $name) = $server->group("comp.lang.perl.misc") or die "Can't select comp.lang.perl.misc\n";</PRE><PCLASS="para">The <CODECLASS="literal">group</CODE> method returns a four-element list: the number of articles in the group, the first article number, the last article number, and the name of the group. If the group does not exist, it returns an empty list.</P><PCLASS="para">There are two ways to retrieve articles: call <CODECLASS="literal">article</CODE> with a message ID, or select a group with <CODECLASS="literal">group</CODE> and then call <CODECLASS="literal">article</CODE> with an article number. In scalar context, it returns a reference to an array of lines. In list context, <CODECLASS="literal">article</CODE> returns a list of lines. If an error occurs, <CODECLASS="literal">article</CODE> returns false:</P><PRECLASS="programlisting">@lines = $server->article($message_id) or die "Can't fetch article $message_id: $!\n";</PRE><PCLASS="para">You can fetch an article's header or body with the <CODECLASS="literal">head</CODE> and <CODECLASS="literal">body</CODE> methods. Like <CODECLASS="literal">article</CODE>, these methods take an article number or message ID, and return a list of lines or an array reference.</P><PRECLASS="programlisting">@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";</PRE><PCLASS="para">To post an article, use the <CODECLASS="literal">post</CODE> method. Give it a list of lines or a reference to an array of lines, and it returns true if the post succeeded, false if the article couldn't be posted.</P><PRECLASS="programlisting">$server->post(@message) or die "Can't post\n";</PRE><PCLASS="para">Use the <CODECLASS="literal">postok</CODE> method to find out whether the server said that you may post:</P><PRECLASS="programlisting">unless ($server-><CODECLASS="literal">postok()</CODE>) { warn "You may not post.\n";}</PRE><PCLASS="para">Read the manpage for Net::NNTP for a complete list of methods.<ACLASS="indexterm"NAME="ch18-idx-1000004301-0"></A><ACLASS="indexterm"NAME="ch18-idx-1000004301-1"></A><ACLASS="indexterm"NAME="ch18-idx-1000004301-2"></A><ACLASS="indexterm"NAME="ch18-idx-1000004301-3"></A><ACLASS="indexterm"NAME="ch18-idx-1000004301-4"></A><ACLASS="indexterm"NAME="ch18-idx-1000004301-5"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch18-pgfId-699">See Also</A></H3><PCLASS="para">The documentation for the Net::NNTP module from CPAN; RFC 977, <CITECLASS="citetitle">Network News Transfer Protocol </CITE>; your system's <ICLASS="filename">trn </I>(1) and <ICLASS="filename">innd</I> (8) manpages (if you have them)</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch18_04.htm"TITLE="18.3. Sending Mail"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 18.3. Sending Mail"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch18_06.htm"TITLE="18.5. Reading Mail with POP3"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 18.5. Reading Mail with POP3"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">18.3. Sending Mail</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">18.5. Reading Mail with POP3</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -