#!/usr/bin/env perl
# -* Perl -*- Fri 21 Sep 10:37:28 CDT 2007 
###############################################################################
# emp.pl - multi-post articles to different hierarchies, and save copies
# Written by Tim Skirvin <tskirvin@killfile.org>
# Copyright 2007, Tim Skirvin.  Redistribution terms are below.
###############################################################################
use vars qw( $VERSION );  $VERSION = "0.01";

use vars qw( $OUTPUT @GROUPS %HEADERS @SIGNATURE $PGPSIGN $PGPCONF $PERSON 
             $ID $PHRASE );
use News::Article;
use strict;

###############################################################################
### User Variables ############################################################
###############################################################################

## What headers do we consistently want to set in these articles?
%HEADERS = (
        'From'          =>      'tskirvin@killfile.org (Tim Skirvin)',
           );

## Which newsgroups do we want to post to?
@GROUPS = qw( bofh.vidiots cmi.movies );
# @GROUPS = qw( bofh.test cmi.test );

## Where do we want to write copies of the articles?
$OUTPUT = "/home/tskirvin/news/sentnews";
# $OUTPUT = "/home/tskirvin/news/sentnews.test";

## Where do we have the PGP-signing configuration
$PGPCONF = "/tskirvin/newssign.conf";

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

@SIGNATURE = <DATA>;

do $PGPCONF;
our $nntpuser ||= (`whoami`);
chomp($nntpuser);

my $article = new News::Article(\*STDIN);

foreach (keys %HEADERS) { 
    $article->set_headers($_, $HEADERS{$_});
}

foreach my $group ( @GROUPS ) {
    my $newart = $article->clone;
    $newart->set_headers('newsgroups', $group);
    $newart->add_date;  
    $newart->add_message_id( "$nntpuser." );

    $newart->sign_pgpmoose($PERSON, $PHRASE, $ID);
    $newart->rename_header('x-auth', 'x-signature');
    $newart->add_body(@SIGNATURE);

    # post article
    print "Posting article to $group\n";
    # $newart->write(\*STDOUT); print "\n";
    $newart->post or (warn "Couldn't post to $group: $!\n" && next);
    print "Posted article to $group\n";
    
    write_output($newart, $OUTPUT) or warn "Couldn't write to $OUTPUT: $!\n";
}

exit 0;

sub write_output {
    my ($article, $file) = @_;
    open(FILE, ">>$file") or ( warn "Couldn't open $file: $!\n" && return 0 );
    print FILE "From $nntpuser " . scalar localtime(time) . "\n";
    $article->write( \*FILE );
    print FILE "\n";
    close FILE;
    1; 
}

__DATA__
                           - Tim Skirvin (tskirvin@killfile.org)
-- 
http://wiki.killfile.org/           Skirv's Homepage <FISH>< <*>
http://wiki.killfile.org/reviews/movies    Skirv's Movie Reviews
