#!/usr/local/bin/perl
# File: nnelm
# Purposes:
# - expand elm aliases
# - transform to mail-safe encoding
# nnelm's behaviour can be influenced by specifying an X-nnelm: header
# with the following options:
# ascii		text is ASCII, don't encode
# noenc		don't encode (assumes 8bit clean channel)
# qp		convert to quoted-printable
# b64		convert to base64
# This version doesn't use elm or fastmail to actually send the mail
# An elm utility is used to expand aliases only.
#  1993, 1994 by Robert Joop <rj@rainbow.in-berlin.de>
$version = '3.1.3';
# Put the following line into your ~/.nn/init:
#	set mailer nnelm
# The line
#	unset mailer-pipe-input
# is *not* needed.
# This program automatically generates a Bcc: to oneself that can be
# redirected using procmail; put something like the following into your
# ~/.procmailrc:
#	:2
#	^X-Mailer: nnelm
#	^From: rj 
#	nn-sent
# (you've got to replace `rj' with your userid.)

# --- configuration: ---

# command that expands elm aliases:
$expand = '/usr/local/bin/checkalias';
$commatospace = 1; # checkalias doesn't like commas :-(

$charset = 'ISO-8859-1';

# MIME encoding:
$encoding = 'quoted-printable';
# command that generates MIME $encoding:
if ($ENV{'DOMAIN'} eq 'tubcs')
{
  $encoder = '/usr/elm/bin/mmencode';
}
else
{
  $encoder = '/usr/local/bin/mmencode';
}
%encoders = ('quoted-printable', "$encoder -q", 'base64', "$encoder -b");
$encode = 1;
$encoding = 'quoted-printable';

# MTA:
$sendmail = '/usr/lib/sendmail -t';

$tmp = "/tmp/nnelm.$$";

# --- let's do it! ---

open (TMP, "> $tmp");

$hdr = '';
hdr_line: while (<>)
{
  if ((/^$/ || !/^[ \t]/) && $hdr ne '')
  {
    # process header line:
    if ($hdr =~ /^(X-)?nnelm:/i)
    {
      $hdr =~ s/^(X-)?nnelm:\s*//i;
      @args = split (/[\s,]+/, $hdr);
      foreach $arg (@args)
      {
	if ($arg eq 'ascii')
	{
	  $encode = 0;
	  $encoding = '7bit';
	  $charset = 'US-ASCII';
	}
	elsif ($arg eq 'noenc' || $arg eq 'no')
	{
	  $encode = 0;
	  $encoding = '8bit';
	}
	elsif ($arg eq 'qp' || $arg =~ /^quote/)
	{
	  $encode = 1;
	  $encoding = 'quoted-printable';
	}
	elsif ($arg eq 'b64' || $arg =~ /^base/)
	{
	  $encode = 1;
	  $encoding = 'base64';
	}
      }
    }
    else
    {
      foreach $key (('To','Cc','Bcc','Reply-To'))
      {
	if ($hdr =~ /^$key: /i)
	{
	  $hdr =~ s/^$key: //i;
	  $hdr =~ s/,/ /g if $commatospace;
	  $hdr = `$expand "$hdr"`;
	  $hdr =~ s/^Expands to: (\n\t)?//i;
	  print TMP "$key: $hdr";
	  $hdr = '';
	  goto start;
	}
      }
      print TMP "$hdr";
      $hdr = '';
    }
  }
start:
  last if /^$/;
  $hdr .= "$_";
}
print TMP "Bcc: ", (getpwuid ($<))[0], " (instead of writing into =sent)\n";
print TMP "X-Mailer: nnelm $version (c) 1993, 1994 <rj\@rainbow.in-berlin.de>\n";

print TMP "MIME-Version: 1.0\n";
print TMP "Content-Type: text/plain; charset=$charset\n";
print TMP "Content-Transfer-Encoding: $encoding\n";
print TMP "\n";

if ($encode)
{
  close (TMP);
  $encoder = $encoders{$encoding};
  open (QUOTE, "| $encoder >> $tmp");
  print QUOTE while (<>);
  close (QUOTE);
}
else
{
  print TMP while (<>);
  close (TMP);
}

open (TMP, "< $tmp");
open (SENDMAIL, "| $sendmail");
while (<TMP>)
{
  s/^\.$/. /;
  print SENDMAIL;
}
close (SENDMAIL);
unlink ("$tmp");
