#!/usr/bin/perl
open(VOICEMAIL,"|/usr/sbin/sendmail -t");
open(LAMEDEC,"|/usr/bin/dos2unix|/usr/bin/base64 -di|/usr/bin/lame --quiet --preset voice - /var/spool/asterisk/tmp/vmout.$$.mp3");
open(VM,">/var/spool/asterisk/tmp/vmout.debug.txt");
my $inaudio = 0;
loop: while(<>){
if(/^\.$/){
last loop;
}
if(/^Content-Type: audio\/x-wav/i){
$inaudio = 1;
}
if($inaudio){
while(s/^(Content-.*)wav(.*)$/$1mp3$2/gi){}
if(/^\n$/){
iloop: while(<>){
print LAMEDEC $_;
if(/^\n$/){
last iloop;
}
}
close(LAMEDEC);
print VOICEMAIL "\n";
print VM "\n";
open(B64,"/usr/bin/base64 /var/spool/asterisk/tmp/vmout.$$.mp3|");
while(<B64>){
print VOICEMAIL $_;
print VM $_;
}
close(B64);
print VOICEMAIL "\n";
print VM "\n";
$inaudio = 0;
}
}
print VOICEMAIL $_;
print VM $_;
}
print VOICEMAIL "\.";
print VM "\.";
close(VOICEMAIL);
close(VM);
#CLEAN UP THE TEMP FILES CREATED
#This has to be done in a separate cron type job
#because unlinking at the end of this script is too fast,
#the message has not even gotten piped to send mail yet