#! /usr/local/bin/perl

##---------------------------------------------------------------------------##
##  File:
##      HtmlMail
##  Author:
##      Earl Hood       ehood@convex.com
##	Copyright (C) 1994, Wed Oct 26 14:56:26 CDT 1994
##
##  Description:
##      HtmlMail is a simple Perl program that loads a single mail
##	message from STDIN into a WWW client.  Even though, this
##	program can be used to with NCSA Mosaic, it is better to
##	use MosaicMail since it tries to load the message in an
##	existent Mosaic session.
##
##	MHonArc is called to do the actual conversion of the e-mail
##	message.  Therefore, MIME messages will be converted, and
##	text/plain messages will have URLs hyperlinked.
##
##	Change the values in the CONFIG section below to suit your
##	needs.
##
##  Notes:
##	o  For MH users, the following can be used to process the current
##	   mail message:
##
##		% show -noshowproc -noheader | HtmlMail
##
##	o  For Trn users, you can load a newsgroup post into a WWW client
##	   by piping the post to HtmlMail:
##
##		| HtmlMail
##
##	o  MHonArc needs to be in your search path
##
##	o  If extra files are created by MHonArc (i.e. for graphics,
##	   binaries, etc), they will not get removed.  You'll have
##	   to manually remove them.
##
##---------------------------------------------------------------------------##
##    Copyright (C) 1995        Earl Hood, ehood@convex.com
##
##    This program is free software; you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation; either version 2 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program; if not, write to the Free Software
##    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##---------------------------------------------------------------------------##

##---------------------------------------------------------------------------
#############################################################################
##---------------------------------------------------------------------------
##	CONFIG: Change the following variables to reflect your needs

## WWW client to invoke (client must be able to take a file://localhost/...
## URL for loading converted message).
$WWWClient = "lynx -force_html";

## Location of converted message; directory must exist and be a FULL
## pathname.
$DESTDIR = "$ENV{'HOME'}/tmp";

## Uncomment and set the following variable to a MHonArc resource file if
## you want to customize the format of the converted message
#$ENV{'M2H_RCFILE'} = "mhonarc.rc";

##	CONFIG: End change section
##---------------------------------------------------------------------------
#############################################################################
##---------------------------------------------------------------------------

##---------------------------------------------------------------------------
##	Actual code to do the conversion (Should not have to edit)

## Set html filename
$htmlfile  = "$DESTDIR/" . time() . ".$$.html";

## Set handler to clean up if program is interrupted
$SIG{'ABRT'} =
$SIG{'HUP'}  =
$SIG{'INT'}  =
$SIG{'QUIT'} =
$SIG{'TERM'} = 'cleanup';

## Set web client command
$webcmd = "$WWWClient file://localhost$htmlfile";

## Convert message to HTML by using MHonArc
open(MHONARC, "|mhonarc -single -outdir $DESTDIR > $htmlfile");
print MHONARC <STDIN>;
close(MHONARC);

## Reset STDIN to terminal for text based WWW clients (Lynx goes wacko
## if this isn't done)
close(STDIN);
open(STDIN, "/dev/tty") || &error("Unable to open terminal: $!\n");

## Call web client
print STDOUT "$webcmd\n";
if ($ret = system($webcmd)) {
    &error("Error status from $WWWClient: ", $ret/256, "\n");
}

&cleanup();

##---------------------------------------------------------------------------
sub cleanup {
    unlink $htmlfile;
    exit 0;
}
##---------------------------------------------------------------------------
sub error {
    warn @_;
    &cleanup();
}
