🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #79748

news2mail script.

#79748
From: D. Stussy
Date: Sun, 23 Nov 2008 02:51
380 lines
13680 bytes
I use a modified version of the script which has a configuration file that
has a THIRD parameter - SMTP envelope from address.  Would this be useful
to everyone/anyone?

Why do that?  Because some mailing lists (perhaps many, maybe eventually
all) will only allow messages to the list that come from list members.
Therefore, the sending address needs to be a list member.  Therefore, what
I have is for "~news/etc/news2mail.conf" (not ".cf" - for consistency) is:

Newsfeeds-name      List-to-address     [List-sender-address]

Newsfeeds-name:    Name used in the newsfeeds file for the gateway feed
(required).
List-to-address:        List address to send to (required).
List-sender-address:    Envelope-From address to use (a list member's
address; optional, NEW).

Mailing lists gated to/from the server should have their own mailbox
address to avoid confusion with a real participant's mailbox.

The SMTP envelope address is optional in the configuration file.  However,
if it is specified, then only one mailing list may be the recipient of the
message.

To do:  I wasn't skilled enough with perl to check for multiple entries all
the same for those lists that specify the parameter, but that is the next
logical step.  Obviously, messages to multiple lists that have different
envelope-from values are currently a problem and should be handled by
sending multiple messages or generating an error.  Currently, the "news"
user is used as the envelope-from sender when multiple lists receive a
posting, or when one list omits the third parameter.

Careful:  I may have made other changes for my own preference.  See the
diff at the end to identify those.

Changes include (besides the obvious):
1)  Change of "~news/etc/news2mail.cf" => "~news/etc/news2mail.conf"
2)  Hiding "Newsgroups:" header from mailing list, et. al.
-----
Manual page:  news2mail(8)

It should probably contain a warning about looping messages between the
list and a news server (especially if the list doesn't protect itself
against looping).  The newsfeeds entry for any news-list gateway should
include in the "exclude" field the domain of the list owner and a control
flag of "Ap".  The examples should probably be updated to account for this.


news2mail script (complete first, diff second):

#! /usr/bin/perl
require '/var/lib/news/lib/innshellvars.pl';

# news to mail channel backend
#
# INN gives us
#       @token@ addrs
# for each article that needs to be mailed.  We invoke sm on the
# localhost to get the actual article and stuff it down sendmail's throat.
#
# This program expect to find a file that maps listname to listaddrs,
#       @prefix@/etc/news2mail.cf
# which must contain address mapping pairs such as
#
#       big-red-ants@ucsd.edu   big-red-ants-digest@ucsd.edu
#
# where the first token is the name fed to us from INN, and which is
# also placed in the To: header of the outgoing mail.  It's probably
# the subscriber's list submittal address so that replies go to the
# right place.  The second token is the actual address sendmail ships
# the article to.
#
# 2008-06-26:  Added a third, optional token for envelope-from address if
# mailing to only one destination/list.  Also changed configuration file
# extension from ".cf" to ".conf" for INN naming consistency.  - D. Stussy
#
# In the INN newsfeeds file, you need to have a channel feed:
#       n2m!:!*:Ac,Tc,Wn*:@prefix@/bin/news2mail
# and a site for each of the various mailing lists you're feeding,
# such as
#       big-red-ants@ucsd.edu/ucsd.edu:rec.pets.redants.*:Ap,Tm:n2m!
#
# Error handling is nearly nonexistent.
#
#       - Brian Kantor, UCSD Aug 1998

require 5.006;

use FileHandle;
use Sys::Syslog;
use strict;

my $cfFile = $inn::pathetc . "/news2mail.conf" ;
my $sendmail = $inn::mta ;
my $sm = $inn::pathbin . "/sm" ;
my %maddr = ();
my %saddr = ();

openlog('news2mail', 'pid', 'mail');
syslog('info', 'begin');

#
# load the list names and their mail addresses from conf file
# #comments and blank lines are ignored
#
unless (open CF, "< $cfFile") {
                syslog('notice', 'CF open failed %m');
                die "bad CF";
}

while ( <CF> ) {
        next if /^#|^\s+$/;
        my ( $ln, $ma, $sa ) = split /\s+/;
        $maddr{ $ln } = $ma;
        $saddr{ $ln } = $sa if $sa =~ /.+/;
        $sa = $inn::newsuser if !defined $saddr{ $ln };
        syslog('debug',"List=$ln, To=<$ma>, From=<$sa>");
        }
close CF;

#
# for each incoming line from the INN channel
#
while ( <STDIN> ) {
        chomp;
        syslog('info', $_);
        my ($token, $lnames) = split /\s+/, $_, 2;
        my @addrs = split /\s+/, $lnames;
        my @good = grep { defined $maddr{$_} } @addrs;
        my @bad = grep { !defined $maddr{$_} } @addrs;

        if (! @good) {
                syslog('notice', "unknown list(s): $lnames");
                next;
        }
        if (@bad) {
                syslog('info', 'skipping list(s): ', join(' ', @bad));
        }
        my $sa = $inn::newsuser;
        my @src = grep { defined $saddr{$_} } @good;
        $sa = @saddr{@src} if @src == 1;

# If multiple lists have different addresses in the saddr{} array, we
# should send multiple copies, each with its different $sa=saddr{}
# mapping.  Note that NULL entries should be ignored, so that
# if saddr{src} for all sources yields only one non-null entry, use it.
# Otherwise, use the specific address for each list.  Someone who
# knows perl better than I do needs to write this.  - D. Stussy.

        mailto($token, $sa, @maddr{@good});
}
syslog ("info", "end") ;
exit 0;

sub mailto {
        my($t, $s, @a) = @_ ;

        my $sendmail = $inn::mta ;
        $sendmail =~ s!\s*%s!! ;
        my @command = (split (' ', $sendmail), '-ee', '-odq', "-f$s",
                "-pNNTP:$inn::pathhost", @a);

        syslog('debug', join(' ', @command));

        unless (open(SM, '|-', @command)) {
                syslog('error', join(' ', '|', @command), 'failed!');
                die "bad $sendmail";
        }

        my $smgr = "$sm -q $t |";
        unless (open(SMGR, $smgr)) {
            syslog('error', "$smgr failed!");
            die "bad $smgr";
        }

        # header
        while ( <SMGR> ) {
                chomp;

                # empty line signals end of header
                if ( /^$/ ) {
                        print SM "To: @a\n\n";
                        last;
                }
                s/^X-Received:/Received:/i;
                next if /^NNTP-Posting-Date:/i;
                next if /^NNTP-Posting-Host:/i;
                next if /^Newsgroups:/i;
                next if /^Lines:/i;
                next if /^Xref:/i;
                next if /^Path:/i;
                next if /^To:/i;
                next if /^X-/i;
                print SM "$_\n";
        }

        # body
        while ( <SMGR> ) {
                print SM $_;
        }
        close(SMGR);
        close(SM);
}

!diff -ub /usr/src/inn-STABLE-20081021/backends/news2mail
~news/bin/news2mail
--- /usr/src/inn-STABLE-20081021/backends/news2mail     2008-10-23
03:29:32.000000000 +0000
+++ /var/lib/news/bin/news2mail 2008-11-20 00:00:00.000000000 +0000
@@ -6,8 +6,7 @@
 # INN gives us
 #      @token@ addrs
 # for each article that needs to be mailed.  We invoke sm on the
-# localhost to get the actual article and stuff
-# it down sendmail's throat.
+# localhost to get the actual article and stuff it down sendmail's throat.
 #
 # This program expect to find a file that maps listname to listaddrs,
 #      @prefix@/etc/news2mail.cf
@@ -21,8 +20,12 @@
 # right place.  The second token is the actual address sendmail ships
 # the article to.
 #
+# 2008-06-26:  Added a third, optional token for envelope-from address if
+# mailing to only one destination/list.  Also changed configuration file
+# extension from ".cf" to ".conf" for INN naming consistency.  - D. Stussy
+#
 # In the INN newsfeeds file, you need to have a channel feed:
-#      n2m!:!*:Tc,Ac,Wn*:@prefix@/bin/news2mail
+#      n2m!:!*:Ac,Tc,Wn*:@prefix@/bin/news2mail
 # and a site for each of the various mailing lists you're feeding,
 # such as
 #      big-red-ants@ucsd.edu:rec.pets.redants.*:Tm:n2m!
@@ -37,31 +40,31 @@
 use Sys::Syslog;
 use strict;

-my $cfFile = $inn::pathetc . "/news2mail.cf" ;
+my $cfFile = $inn::pathetc . "/news2mail.conf" ;
 my $sendmail = $inn::mta ;
 my $sm = $inn::pathbin . "/sm" ;
 my %maddr = ();
+my %saddr = ();

-#
-# the syslog calls are here but don't work on my system
-#
 openlog('news2mail', 'pid', 'mail');
-
 syslog('info', 'begin');

 #
-# load the list names and their mail addresses from cf file
+# load the list names and their mail addresses from conf file
 # #comments and blank lines are ignored
 #
 unless (open CF, "< $cfFile") {
                syslog('notice', 'CF open failed %m');
                die "bad CF";
-               }
+}

 while ( <CF> ) {
        next if /^#|^\s+$/;
-       my ( $ln, $ma ) = split /\s+/;
+       my ( $ln, $ma, $sa ) = split /\s+/;
        $maddr{ $ln } = $ma;
+       $saddr{ $ln } = $sa if $sa =~ /.+/;
+       $sa = $inn::newsuser if !defined $saddr{ $ln };
+       syslog('debug',"List=$ln, To=<$ma>, From=<$sa>");
        }
 close CF;

@@ -70,49 +73,45 @@
 #
 while ( <STDIN> ) {
        chomp;
-
        syslog('info', $_);
-
        my ($token, $lnames) = split /\s+/, $_, 2;
        my @addrs = split /\s+/, $lnames;
-
        my @good = grep {  defined $maddr{$_} } @addrs;
        my @bad  = grep { !defined $maddr{$_} } @addrs;

        if (! @good) {
-               syslog('notice', "unknown listname $_");
+               syslog('notice', "unknown list(s): $lnames");
                next;
                }
-
        if (@bad) {
-               syslog('info', 'skipping unknown lists: ', join(' ',
@bad));
+               syslog('info', 'skipping list(s): ', join(' ', @bad));
                }
-       mailto($token, $lnames, @maddr{@good});
-       }
-
+       my $sa = $inn::newsuser;
+       my @src = grep { defined $saddr{$_} } @good;
+       $sa = @saddr{@src} if @src == 1;
+       mailto($token, $sa, @maddr{@good});
+}
 syslog ("info", "end") ;
-
 exit 0;

 sub mailto {
-       my($t, $l, @a) = @_ ;
+       my($t, $s, @a) = @_ ;

        my $sendmail = $inn::mta ;
        $sendmail =~ s!\s*%s!! ;
-       my @command = (split (' ', $sendmail), '-ee', '-fnews', '-odq',
@a);
-#      @command[0] = '/usr/local/bin/debug';
+       my @command = (split (' ', $sendmail), '-ee', '-odq', "-f$s",
+               "-pNNTP:$inn::pathhost", @a);

-       syslog('info', join(' ', @command));
+       syslog('debug', join(' ', @command));

        unless (open(SM, '|-', @command)) {
-               syslog('notice', join(' ', '|', @command), 'failed!');
+               syslog('error', join(' ', '|', @command), 'failed!');
                die "bad $sendmail";
                }

        my $smgr = "$sm -q $t |";
-
        unless (open(SMGR, $smgr)) {
-           syslog('notice', "$smgr failed!");
+           syslog('error', "$smgr failed!");
            die "bad $smgr";
        }

@@ -122,24 +121,18 @@

                # empty line signals end of header
                if ( /^$/ ) {
-                       print SM "To: $l\n\n";
+                       print SM "To: @a\n\n";
                        last;
                        }
-
-               #
-               # skip unnecessary headers
-               #
+               s/^X-Received:/Received:/i;
                next if /^NNTP-Posting-Date:/i;
                next if /^NNTP-Posting-Host:/i;
-               next if /^X-Trace:/i;
+               next if /^Newsgroups:/i;
+               next if /^Lines:/i;
                next if /^Xref:/i;
                next if /^Path:/i;
-
-               #
-               # convert Newsgroups header into X-Newsgroups
-               #
-               s/^Newsgroups:/X-Newsgroups:/i;
-
+               next if /^To:/i;
+               next if /^X-/i;
                print SM "$_\n";
                }

@@ -147,7 +140,6 @@
        while ( <SMGR> ) {
                print SM $_;
        }
-
        close(SMGR);
        close(SM);
-       }
+}

Technical Details
Message-ID: <ggagf0$2ga$1@snarked.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.20141118.mbox.gz!border1.nntp.ams.giganews.com!nntp.giganews.com!feeder.news-service.com!postnews.google.com!news2.google.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!news-in-02.newsfeed.easynews.com!easynews.com!easynews!news.he.net!news.snarked.org!not-for-mail

Show Full Headers
Complete Article Headers
Message-ID: <ggagf0$2ga$1@snarked.org>
Subject: news2mail script.
Date: Sat, 22 Nov 2008 18:51:11 -0800
From: "D. Stussy" <spam@bde-arc.ampr.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.20141118.mbox.gz!border1.nntp.ams.giganews.com!nntp.giganews.com!feeder.news-service.com!postnews.google.com!news2.google.com!npeer03.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!news-in-02.newsfeed.easynews.com!easynews.com!easynews!news.he.net!news.snarked.org!not-for-mail
Newsgroups: news.software.nntp
Lines: 380
Reply-To: "D. Stussy" <replies@newsgroups.kd6lvw.ampr.org>
NNTP-Posting-Host: pool-71-116-244-151.lsanca.dsl-w.verizon.net
X-Trace: snarked.org 1227408672 2570 71.116.244.151 (23 Nov 2008 02:51:12 GMT)
X-Complaints-To: newsmaster@snarked.org
NNTP-Posting-Date: Sun, 23 Nov 2008 02:51:12 +0000 (UTC)
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1478
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1478