#!/usr/bin/perl
use Mail::Internet;
use strict;

# Globals
my $offline = 0;
my $DEBUG = 0;
my $HOME = "/home/video";
my $RSCRIPT = "$HOME/bin/getvid.sh";

my $mail = Mail::Internet->new( [ <STDIN> ] );
my $FROM = get_sender($mail);
my $ARGUMENT = shift;

# A psuedo-command that lets us remove the PVR for maintenance
if ($offline)
	 { $ARGUMENT = "offline"; }

# Construct the reply
my $REPLY = $mail->reply();
my $REPLY_MSG = process_mail($mail, $ARGUMENT);
$REPLY->body($REPLY_MSG);
$REPLY->send();

exit;

# Rip out return email. This is given to the bash script
sub get_sender()
{
my $mail = $_[0];
my $headers = $mail->head->header_hashref;
my $from = $headers->{From}[0];

# Remove <name@domain> brockets from email address
if ($from=~/<(.+)>/) { $from = $1;}

return $from;
}


sub process_mail()
{
my $mail = $_[0];
my $cmd = $_[1];
my $freq = "1";	# default freq!
my $duration = "1";
my $password = "";
my $time = "now";
my $reply;
my $arg;

for(my $i=0;$i<10;$i++)
{
my $line = $mail->body()->[$i];

# freq technically refers to a channel (1=bbc1, 2=bbc2 etc), which are
# determined by the getvid.sh shell script
if ($line=~/ch[^:\s]*[:\s]*(.+)/i) { $freq = $1; }

# The shortest time we allow a record for is one minute
if ($line=~/length[^:\s]*[:\s]*([0-9]*)/i) { $duration = $1*60; }
if ($line=~/length[^:\s]*[:\s]*([0-9]*)\s*hours/i) { $duration = $1*60*60; }

# Out temporary password
if ($line=~/pass[^:\s]*[:\s]*(.+)/i) { $password = $1; }

# This 'at' time must be in the correct format for the program 'at'
# We also support 'now' as a parameter, which is picked up below to run
# the script immediately
if ($line=~/at[^:\s]*[:\s]*(.+)/i) { $time = $1; }
}


if (need_password($cmd) && !is_password_valid($password))
{
	# Reply with an error
	return "You need to enter a password for this feature...\n";
}
else
{
	# Get rid of temporary passwords!
	if (need_password($cmd)){ unlink "$HOME/.pass.d/$password"; }

	# Process the command
	# (case insentivity isn't really needed here, but stops head
	#  scraching later on)
	if ($cmd=~/help/i)	{ $reply = do_help(); }
	elsif ($cmd=~/record/i) { $reply = do_record($freq, $duration, $time); }
	elsif ($cmd=~/offline/i){ $reply = do_offline(); }
	else 			{ $reply = do_error(); }
}

return $reply;
}

sub need_password()
{
	if ($_[0]=~/record/i)	{ return 1; }

	return 0;
}

sub is_password_valid()
{
	if (-f "$HOME/.pass.d/$_[0]") { return 1; }

	return 0;
}


sub do_record()
{
my ($freq, $length, $when) = @_;
my ($msg, $cmd);

if ($length eq "" || $freq eq "") 
	{ return "I need a channel and duration...\n"; }

$cmd = "$RSCRIPT $freq $length $FROM";

if ($when != "now") 
	{ $cmd = "echo $cmd | at $when"; }

$msg = "Recording...$when\n";
$msg .= "Command: $cmd\n";
$msg .= `$cmd`;

return $msg;
}

sub do_help()
{
# Using a heredoc makes the email easy to copy. We could
# also have copied one from a separate file.
return <<'HERE'

Subject: record
Body: ch:[station number, 1=BBC1, 2=BBC2, etc]
      at:[time in hours:minutes]
      length:[time in minutes]
      pass:[the password]

HERE
;

}

sub do_offline()
{
# For no reason, here's Apu - in another Simpsons reference :)
return "The video is current offline for maintenance\nThank you, please come again...";
}

sub do_error()
{
# Should never get here, because procmail doesn't deliver
# any messages with faulty subject lines
return "Unknown command.\n\nPlease consult your sysadmin...";
}

