#!/usr/bin/eperl
###
#
# Copyright (c) Ensim Corporation 2000, 2001   All Rights Reserved.
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or any other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of the software is hereby
# transferred.
#
# The information in this software is subject to change without notice
# and  should  not be  construed  as  a commitment by Ensim Corporation.
# Ensim assumes no responsibility for the use or  reliability  of its
# software on equipment which is not supplied by Ensim.
#
# ----------------------------------------------------------------------
# Usage: add_responder <alias>
#        data
#        ...
#        <EOF>
# ----------------------------------------------------------------------

my($sendmail_path);
$sendmail_path = ($ENV{'OCW_SVCPATH'} || "/usr/lib/opcenter")."/sendmail";
push @INC, $sendmail_path;
push @INC, ($ENV{'OCW_SVCPATH'} || "/usr/lib/opcenter")."/cmdline_common";

use strict;
use Getopt::Std;
use IO::File;
require sendmail;
require CmdLineCoder;

use lib ($ENV{OCW_SVCPATH} or "/usr/lib/opcenter") . "/sendmail";
use lib ($ENV{OCW_SVCPATH} or "/usr/lib/opcenter") . "/cmdline_common";

use ERRORS;
use Carper;

&CmdLineCoder::decode_args();

my( $sendmailcf );    # The 'sendmail.cf' config file in a perl data structure
my( @aliases );       # List of all aliases in perl data structure
my( $alias );         # The name of the alias we are updating (from args)
my( $fullalias );     # The name of the alias, including domain name
my( @text );          # The text sent by the responder
my( $domain );

my( $usage ) = "Usage: add_responder <alias>\n";
$fullalias = shift;
die $usage if @ARGV;

# Validate the $alias
$cerr += $E_NOTFULLEMAIL and die "Alias does not contain full email address\n" unless $fullalias =~ /^(.+)\@(.+)$/ ;
($alias,$domain) = ($1,$2);
print "- $alias --- $domain -\n";
$cerr += $E_INVALIDALIAS and die "Invalid alias name" unless $alias eq CmdLineCoder::purify_name($alias);
$cerr += $E_INVALIDDOMAININALIAS and die "Invalid domain in alias" unless $domain eq CmdLineCoder::purify_name($domain);


@text = <STDIN>;

# Write the file
my( $filename, $fh ) = &mktemp( '/usr/share/opcenter/responders' );
die $filename unless $fh;

print $fh @text;
$fh->close();

my( $ret );
$ret = system("$sendmail_path/update_alias -r $alias \"|responder.sh%20$alias%20$filename\"");
unlink($filename) if $ret;

exit $ret;

sub mktemp {
    my( $path ) = @_;
    my( $fh, $filename, $iterations );
    $iterations = 30;
    while ( $iterations-- > 0 ) {
        $filename = "$path/responder." . int(rand(999999)); 
        $fh = IO::File->new( $filename, O_RDWR|O_CREAT|O_EXCL);
        return ($filename,$fh) if $fh;
    }
    $cerr += $E_UNABLECREATERESPONDER ;
    return ('Unable to create responder file',0);
}
