#!/usr/local/bin/perl

# rip out wav, convert it to mp3, resend
# meant for:
# echo "| /usr/local/script/resend-wav2mp3.pl email@example.com" > .qmail-user

use strict;
use Net::SMTP;
use MIME::Base64 qw/encode_base64/;

my $dest = $ARGV[0] || die "specify destination as argument\n";

my $tmp = "/tmp/$$.$^T" . int(rand(1000000000));
my $orig = "$tmp/rw2m.orig";
mkdir($tmp, 0700) || die "mkdir $tmp: $!\n"; # we want to know if it exists, too

my %header;
open(my $o, '>', $orig) || die "cannot write to $orig: $!\n";
while( defined(my $line=<STDIN>) ){
    if( $line =~ /^(subject|date|from)\s*:\s?(.+)/i ){
        $header{lc($1)} = $2;
    }
    print $o $line;
}
close $o;

open(my $read, $orig) || die "cannot read $orig: $!\n";
open(my $ripmime, "| ripmime --no-nameless -d $tmp -i -") || die "cannot fork ripmime: $!\n";
while(<$read>){
    print $ripmime $_;
}
close $ripmime;
close $read;


my $content = <<__EOC__
To: $dest
Subject: $header{subject}
Date: $header{date}
From: $header{from}
Content-Type: multipart/mixed;
 boundary="------------00070907090jkister030200"

This is a multi-part message in MIME format.
--------------00070907090jkister030200
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit

You've got voicemail from $header{from}

__EOC__
;


my $smtp = Net::SMTP->new('localhost');
$smtp->mail($dest); # cant do $from here- SPF, DK, etc.
$smtp->to($dest);
$smtp->data();   
$smtp->datasend($content);

opendir(DIR, $tmp) || die "cannot opendir $tmp: $!\n";
for my $file (grep {/\.wav$/i} readdir DIR){
    if( $file =~ /\.\./ ){
        warn "directory manipulation in $file\n";
        next;
    }

    my ($filebase) = $file =~ /^(.+)\.wav$/i;
    unless($filebase) {
        warn "can only accept WAV files (found $file)\n";
        next;
    }

    warn "working on $file\n";

    system( "nice -n 19 /usr/bin/sox $tmp/$file -s $tmp/$filebase.sox.wav" ) == 0 || die "sox error: " . sysconv($?) . "\n";
    system( "nice -n 19 /usr/local/bin/lame -b 96 -t -F -m m --bitwidth 16 --quiet $tmp/$filebase.sox.wav $tmp/$filebase.mp3" ) == 0 || warn "lame error: " . sysconv($?) . "\n";

    die "mp3 conversion failed\n" unless( -s "$tmp/$filebase.mp3" );

    my $aheader = <<__EOA__
--------------00070907090jkister030200
Content-Type: audio/mpeg;
 name="$filebase.mp3"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
 filename="$filebase.mp3"

__EOA__
    ;

    $smtp->datasend($aheader);
    my $buf;
    open(my $mp3, "$tmp/$filebase.mp3") || die "cannot read $tmp/$filebase.mp3: $!\n";
    while( read($mp3, $buf, 60*57) ){
        $smtp->datasend( encode_base64($buf) );
    }
    close $mp3;
}

$smtp->datasend("--------------00070907090jkister030200--");
$smtp->dataend();
$smtp->quit;

#system( "rm -rf $tmp/" ) == 0 || die "couldnt remove $tmp:" . sysconv($?) . "\n";


sub sysconv {
    my $value = shift;

    if ($value == -1) {
        print "failed to execute: $!\n";
    }elsif($value & 127) {
        printf "child died with signal %d, %s coredump\n",
               ($value & 127), ($value & 128) ? 'with' : 'without';
    }else{
        printf "child exited with value %d\n", $value >> 8;
    }
}
