#!/usr/bin/env perl
use vars qw( $VERSION ); $VERSION = "1.1";

use strict;

###############################################################################
### Default Configuration #####################################################
###############################################################################

use vars qw( $BASE_URL $AGENT @SERVERS );

## Where we're getting messages from on Usenet
$BASE_URL = 'http://groups.google.com/groups?dmode=source';     

## What user agent should we say we're using?
$AGENT = "Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/0.8.12";

@SERVERS = qw( news.readfreenews.net );
unshift @SERVERS, $ENV{'NNTPSERVER'} ;

###############################################################################
### main() ####################################################################
###############################################################################

use Net::NNTP;
use LWP::UserAgent;

my $ua = new LWP::UserAgent ( 'agent' => $AGENT );

$0 =~ s%.*/%%;	# Clean up path info

my $id = shift @ARGV;
die "Usage: $0 ID\n" unless $id;

my $article;
foreach (@SERVERS) { 
  my $nntp = new Net::NNTP($_);  next unless $nntp;
  $id = join('', '<', $id, '>');
  $id =~ s/^<</</g;  $id =~ s/>>$/>/g;
  $article = $nntp->article($id);
  $nntp->quit;
  last if $article;
} 

if ($article) { print @$article; exit 0}

else { 
  my $newd = $id;  
  $newd =~ s/%/%25/g; $newd =~ s/@/%40/g;  $newd =~ s/^<|>$//g;
  my $url = join('&', $BASE_URL, "selm=$newd");
  warn "U: $url\n";
  my $response = $ua->get($url);
  my (@lines, $isarticle, $lastline);
  if ($response->is_success) { 
    foreach my $line (split("\n", $response->content)) { 
      chomp $line;
      $line =~ s/&gt;/>/g;
      $line =~ s/&lt;/</g;
      $line =~ s/&quot;/"/g;
      # $line =~ s/<pre>//g;
      $lastline = $line;
      # $isarticle++ if $line =~ /^.*(Path|From): /;
      $isarticle++ if $line =~ /^(\s*<pre>)/;
       $line =~ s/^\s*<pre>// if $1;
      $isarticle = 0 if $line =~ m%^<\/pre>$%; 
      push @lines, $line if $isarticle;
      $lastline = $line;
    }
  } else { die "Couldn't get $newd: " . $response->status_line, "\n"; }

  print join("\n", @lines, "");
 
  close STRING;
}


###############################################################################
### Documentation #############################################################
###############################################################################

=head1 NAME

article - a script that downloads a Usenet news article by Message-ID

=head1 SYNOPSIS

  article MSGID

=head1 DESCRIPTION

'article' is a simple script for looking for news articles from either
your local news spool (using Net::NNTP and your NNTPSERVER variable) or,
failing that, Google Groups.  Prints the article and exits if it finds
something, exits without printing anything if nothing is found.

=head1 NOTES

This is fairly dependent on how Google is interfacing with their articles
on any given day...  This works as of 13 Jun 2006.

=head1 REQUIREMENTS

=head1 SEE ALSO

B<News::Article>, B<Net::NNTP>

=head1 AUTHOR

Tim Skirvin <tskirvin@killfile.org>

=head1 LICENSE

This code may be redistributed under the same terms as Perl itself.

=head1 COPYRIGHT 

Copyright 2002-2006, Tim Skirvin <tskirvin@killfile.org> 

=cut

###############################################################################
### Version History ###########################################################
###############################################################################
# 1.2           Tue Jun 13 15:28:02 2006        tskirvin
### Documenting it for distribution with the urlview-news package.
# 1.1           Sun May 21 15:16:39 2006        tskirvin
### Updated how Google downloads work, when they changed their scheme.
# 1.0           2004-05                         tskirvin
### Original, working version.  I didn't really document it at all back then.
