aboutsummaryrefslogtreecommitdiffstats
path: root/examples/drhyde-news2mail-and-mail2news/mail2news
blob: 1f026a07c7d9aabf4bcefe77e55a333d8e91cf02 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/local/bin/perl

use warnings;
use strict;

use Data::Dumper;
use Net::NNTP;
use News::Article;

use constant DEBUG => 0;

select(STDERR); $| = 1; select(STDOUT);

die("mail2news hasn't been configured.\n") unless(-r '/etc/mail2newsrc');

my $subs = { do '/etc/mail2newsrc' };

my $group = shift;
die("mail2news must be told what group to post to\n") unless($group);

my $article = News::Article->new();
$article->read(\*STDIN);
$article->set_headers(Newsgroups => $group);
$article->drop_headers(qw(date to received));
$article->add_date();

my $posted_ok = 0;
foreach my $server (grep { $subs->{$_}->{$group} } keys %{$subs}) {
    my($auth, $host) = split('@', $server);
    ($auth, $host) = (':', $auth) if(!$host);
    my($user, $pass) = split(':', $auth);
    ($host, my $port) = split(':', $host);
    $port ||= 119;

    my $client = Net::NNTP->new($host, Port => $port);
    $client->authinfo($user => $pass) if($user);

    unless($client->postok()) {
        print STDERR "mail2news configured to post to $group via\n".
	   "$server, but it didn't say we can post.  Bother and damnation!\n";
	next;
    }
	
    eval { $article->post($client); };
    die("Error in posting to $server: $@\n") if($@);
    $posted_ok++;
}

if(!$posted_ok) {
    die "mail2news couldn't post to $group on any configured server.\n\n".
        "Your message read:\n\n".
        join("\n\n",
            join("\n", $article->headers()),
            join("\n", $article->body())
        )."\n\n".
	"And the environment was:\n\n".
	join("\n", map { join(":\t", $_, $ENV{$_}) } sort keys %ENV);
}