#!/usr/bin/perl

#=============================================================================
#
# Id	  : @(#) $Id: log_mail.pl,v 1.4 2003/03/02 17:35:51 aeby Exp $
# Package : swatch
# File	  : log_mail
# Author  : Tom Aeby 
# Date	  : 26/02/96
#
# Usage	: $prog
#
# Function :
#
#	send log messages through mail
#
# $Log: log_mail.pl,v $
# Revision 1.4  2003/03/02 17:35:51  aeby
# made "common.pm" a true module with package BigSister::common, all global
# variables are now in package BigSister::common
#
# Revision 1.3  2002/01/17 19:37:42  aeby
# fixed security relevant bug: when running "$mailprog": use exec() instead
# of open() since open will start a subshell!
#
# Revision 1.8  2002/01/04 19:06:14  aeby
# moved log_mail and notify to the deprecated directory
#
# Revision 1.7  2001/08/16 15:52:25  aeby
# convert single line messages to multiline (|> -> \n) (Bug #451567)
#
# Revision 1.6  2001/06/30 17:55:33  aeby
# use "use lib" in place of -I
#
# Revision 1.5  2001/02/23 14:52:49  aeby
# PATH is centrally managed via common.pm and etc/resources & adm/resources
# files
#
# Revision 1.4  2000/06/20 21:33:34  jgooch
#
#
# Set QMAILUSER and QMAILNAME to some defaults.  If using qmail, it sets the
# outgoing fields in mail, if not, it's harmless anyway.
#
# Revision 1.3  1999/06/12 15:16:43  aeby
# tested on WinNT and fixed flaky implementations
#
# Revision 1.2  1999/05/22 09:44:02  aeby
# replaced require common by the new use ... proginit() construct
#
# Revision 1.1.1.1  1999/05/06 19:23:32  aeby
# release 0.31 imported into CVS
#
# Revision 1.3  1997/03/04 12:54:28  tom
# (outch) rather use a string for message code
#
# Revision 1.2  1997/03/04 12:48:00  tom
# added numerical message code, time stamp
#
# Revision 1.1  1996/02/26  12:43:13  tom
# Initial revision
#
#=============================================================================
$BigSister::common::Usage = "[-D debug] [-m message] [-s severity] [-h host|-u user]";

@BigSister::common::options = ( "H", "s=s", "S=i", "o=s", "m=s", "t=s", "c=s", "h=s", "M=s" );
use lib "."; use lib "uxmon"; use lib "platforms"; 		#inslib
use BigSister::common;
proginit();

use strict;

my $mailprog = "/usr/ucb/mail";
unless( -f $mailprog ) {
    $mailprog = "/usr/bin/mail";
}
unless( -f $mailprog ) {
    $mailprog = "mail";
}

my( $host, @users );

my $default_user = "sys";
$host = $BigSister::common::hostname;
chomp $host;

$ENV{'QMAILNAME'}="Big Sister";
$ENV{'QMAILUSER'}="bs";

my $dl = $BigSister::common::dl;
my $extended_subject = $BigSister::common::opt_H;
my $subject = $BigSister::common::opt_s;
my $severity = $BigSister::common::opt_S;
my $offender = $BigSister::common::opt_o;
my $msgcode = $BigSister::common::opt_c;
my $type = $BigSister::common::opt_t."_";
if( open( IN, "</l/adm/log_mail_key" ) ) {
    $type .= <IN>;
    close IN;
}

my $message = $BigSister::common::opt_m ? $BigSister::common::opt_m:$subject;
($host = $BigSister::common::opt_h) if( $BigSister::common::opt_h );
my $Mailhost = $BigSister::common::opt_M;

( @users = @ARGV ) || ( @users = ($default_user) );

if( $extended_subject ) {
    $subject = $host.": ".$subject;
}

my $text = "Type: $type\n";

$message =~ s/\|\>/\n         /g;
$text .="Message: $message\n";
$text .="Host: $host\n";
$text .="Code: $msgcode\n" if( $msgcode );
$text .="Offender: $offender\n" if( $offender );
$text .="Severity: $severity\n" if( $severity );
$text .="Time: ".time."\n";

$text .= "\n\n--\n";
$text .= 
"WARNING: log_mail and notify are deprecated and will become
unsupported in a future Big Sister release. Please use
pager=sendmail (or pager=smtpmail on Windows systems) instead
of pager=notify or pager=log_mail in bb_event_generator.cfg!";

print "text is: $text\n" if( $BigSister::common::dl );

if( $Mailhost ) {
    require Net::SMTP;
    my $user;

    my $smtp = Net::SMTP->new($Mailhost); # connect to an SMTP server
    $smtp->mail( "bigsister" );     # use the sender's address here
    foreach $user (@users) {
	$smtp->to($user);        # recipient's address
    }
    $smtp->data();                      # Start the mail

    # Send the header.
    #
    $smtp->datasend("To: ".join(", ",@users)."\n");
    $smtp->datasend("From: Big Sister <bigsister>\n");
    $smtp->datasend("Subject: $subject\n");
    $smtp->datasend("\n");

    # Send the body.
    #
    $smtp->datasend($text);

    $smtp->dataend();                   # Finish sending the mail
    $smtp->quit;                       
}
else {
    $subject = untaint( $subject );
    my $pid = open( MAIL, "|-" );
    if( ! defined $pid ) {
	print STDERR "cannot invoke mail prog\n";
	exit 1;
    }

    unless( $pid ) {
	exec( $mailprog, "-s", "$subject", @users );
	exit 1;
    }

    print MAIL $text;
    close MAIL;
    if( $? ) {
	    print STDERR "mail program returned error $?\n";
	    exit 1;
    }

}



sub usage {
    exit 1;
}


sub untaint {
    my( $val ) = @_;
    $val =~ /^(.*)$/;
    return $1;
}


