Next Previous Contents

5. The posting script & perl

5.1 What is Perl?

Perl stands for Practical Extension and Report Language. It is very popular for small scripts which manipulate text which is exactly what we need.

Perl is installed by default on almost every Unix system.

5.2 Location of perl

If you perl intepreter is in an unusual place (not /usr/bin/) then you will have to modify the first line of the script. If this line is wrong, on my 2.2 kernel system I get "bash: /usr/local/bin/mail2news.pl: No such file or directory" Can we please change this to "bash: /usr/local/bin/mail2news.pl: Interpreter not found. Check first line of script." ?

5.3 The mail2news.pl script


#!/usr/bin/perl

($program = $0) =~ s%.*/%%;

#( $version  ) = $] =~ /(\d+\.\d+).*\nPatch level/;
#die "$program: requires at least version 3 of perl\n"
#        if $version < 3;

# $news_poster_program = "/usr/bin/inews";
# $news_poster_options = "-h -o \"mail2news gateway\"";
$news_poster_program = "/usr/bin/rnews";
$news_poster_options = "-r localhost";
$postinghost = "localhost";

if ($#ARGV < 0) {
    # $newsgroup = "test";
    # we'll expect the newsgroup line in the body
} elsif ($#ARGV == 0) {
    $newsgroup = $ARGV[0];
} else {
    die "usage: $program [newsgroup]\n";
}

# in case inews dumps core or something crazy
$SIG{'PIPE'} = "plumber";
sub plumber { die "$program: \"$news_poster_program\" died prematurely!\n"; }

open (INEWS, "| $news_poster_program $news_poster_options") ||
    die "$program: can't run $news_poster_program\n";

# header munging loop
while (<STDIN>) {
   last if /^$/;

   # transform real from: line back to icky style
   s/^From:\s+(.*) <(.*)>/From: $2 ($1)/;

   s/Message-Id/Message-ID/;
 
   # transform from_ line to path header; also works locally
   s/^From\s+(\S+)@(\S+).*/Path: $2!$1/
     || s/^From\s+(\S+)[^@]*$/Path: $1\n/;

   print INEWS
#       if /^(Date|From|Subject|Path|Newsgroups|Organization|Message-ID):/i;
   if /^(Date|From|Subject|Path|Newsgroups|Message-ID):/i;
   $saw_subject |= ( $+ eq 'Subject' );

   $saw_msgid |= ( $+ eq 'Message-ID' );

#   $saw_newsgroup |= ( $+ eq 'Newsgroups' );
}

warn "$program: didn't expect newsgroup in both headers and ARGV\n"
    if $newsgroup && $saw_newsgroup;
 
die "$program: didn't get newsgroup from either headers or ARGV\n"
    unless $newsgroup || $saw_newsgroup;
     
$approved = $newsgroup;
$approved =~ s/\./'-'/eg;

($sec,$min,$hour,$mday,$mon,$year)=localtime(time);
$madeupid = "\<$year$mon$mday.$hour$min$sec.$$\@kepler.hedland.edu.au\>";

printf INEWS "Newsgroups: %s\n", $newsgroup if $newsgroup;
printf INEWS "Approved: %s\@kepler.hedland.edu.au\n", $approved;
print  INEWS "Subject: Untitled\n" unless $saw_subject;
printf INEWS "Message-ID: %s\n", $madeupid unless $saw_msgid;
printf INEWS "NNTP-Posting-Host: %s\n", $postinghost;
print  INEWS "Organisation: (mail2news gateway)\n";
print  INEWS "\n";
 
print INEWS while <STDIN>;   # gobble rest of message
    
close INEWS;
exit $?;

I saved the script in /usr/local/bin (and will use this path throughout the HOWTO).

Be sure to make the script executable by all, but not writable by group or other. Sendmail is picky.

chmod a+x /usr/local/bin/mail2news.pl
chmod go-w /usr/local/bin/mail2news.pl
or
chmod 555 /usr/local/bin/mail2news.pl
for short.

5.4 How do I know if the script is running?

I tested this script by changed my news poster from /usr/bin/rnews to /bin/cat. I then saved an e-mail send to myself in a file. Finally I ran the mail2news.pl on the saved mail and captured the output to a file.

/usr/local/bin/mail2news.pl ietf.confctrl </tmp/savedMailFile >/tmp/article

5.5 What is with the Aussie?

Yes, the output of your posting script should contain the e-mail address of an austrailian. My guess is that the Austrailian's address is trusted address in your news configuration (althogh I could not find it in mine) for approving moderated postings.

5.6 What do you mean $PATH?

If the mail2news.pl script is not in my path, I get the error bash: mail2news.pl: command not found. You will need to either add this directory to your path

PATH=$PATH:/usr/local/bin
(which only works for the current login) or give an absolute path when you run the script
/usr/local/bin/mail2news.pl
To view your current path type
echo $PATH


Next Previous Contents