<%doc>
############################################################################
###    (C)opyright 2003 - 2008 RIPE NCC
###    This file is part of DNSMon
###
###    DNSMon 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 3 of the License, or
###    (at your option) any later version.
###
###    DNSMon 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 DNSMon.  If not, see <http://www.gnu.org/licenses/>.
############################################################################

Email an error of any errors/exception to us so we can debug
what went wrong.

Taint has troubles with the PATH so I used a bit of local magic
to clobber the PATH down to /usr/lib so that it can find sendmail.

</%doc>
<%once>
	use Data::Dumper;
	use Mail::Mailer qw(sendmail);
</%once>
<%args>
	$error => undef
	  $indent => ''
</%args>
<%init>
	local $ENV{PATH} = '/usr/lib:/usr/sbin'; # keeps taint happy. sendmail is /usr/lib

	# open email
	my ($msg, $msg_close);
	if ($CONFIG{error_email_to}) {
		my @email_to = ref $CONFIG{error_email_to}
		  ? @{$CONFIG{error_email_to}} : $CONFIG{error_email_to};
		my $mailer = Mail::Mailer->new('sendmail');
		$mailer->open(
			{
				To      => @email_to,
				Subject => 'DNSMon Error',
			}
		  );
		$msg = sub { print $mailer shift };
		$msg_close = sub { $mailer->close };
	}
	else {
		$msg = sub { warn shift };
		$msg_close = sub { };
	}

	# to email body
	$msg->($indent.'Time of request:      '. scalar localtime ($r->request_time)."\n\n");
	if ($TICKET) {
		$msg->($indent."Id:                   ".$TICKET->id."\n");
		$msg->($indent."Logname:              ".$TICKET->logname."\n\n");
	}
	else {
		$msg->($indent."TICKET:               no \$TICKET object found\n\n");
	}
	$msg->($indent.("-"x70)."\n");
	$msg->($indent."Error ref:            ". ref($error). "\n");
	my $errstr = "$error";
	$errstr =~ s/^/$indent/gm; # indent
	$msg->($indent."Error:\n\n$errstr\n");
	$msg->("\n".$indent.("-"x70)."\n");

	{
		local $Data::Dumper::Sortkeys = 1;
		my $env_dump = Data::Dumper::Dumper(\%ENV);

		#$env_dump =~ s/^/$indent/gm;
		$msg->($indent."Environment \%ENV:  $env_dump\n\n");
	};

	$msg_close->();
</%init>
