#!/usr/bin/perl -wT # Read STDIN into MIME Parser # Calc MD5 for all .ps, .pdf, and .doc files # Save non-duplicate .ps, .pdf, and .doc files # Convert them to .tif # Merge the .tif files # Generate the .call file # $file =~ s/[^\w\/.-]//g; my $context = "toll-access"; $verbose = 1; use MIME::Parser; use Mail::Address; use File::Temp qw(tempdir); $ENV{PATH} = "/bin:/usr/bin"; delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; # Set the Real UID and GID to be the same as the effective UID and GID verbose("Setting the Real UID and GID to be the same as the effective UID and GID"); $< = $>; $( = $); my $spool_dir = "/var/spool/asterisk/email2fax"; if(!stat("$spool_dir")) { mkdir("$spool_dir"); } $temp_dir = tempdir(DIR=>"$spool_dir"); mkdir("$temp_dir"); chdir("$temp_dir"); verbose("Changing to $temp_dir"); open($message, ">message.copy"); while($line = ) { $line =~ s/%/\\%/g; print($message "$line"); } close($message); $parser = new MIME::Parser; $parser->output_dir("$temp_dir"); open($message, "<$temp_dir/message.copy"); $entity = $parser->read($message); close($message); $to = $entity->get("To:"); chomp($to); verbose("To: $to"); $from = $entity->get("From:"); chomp($from); verbose("From: $from"); opendir($temp_dir_handle, "$temp_dir"); @all_files = readdir($temp_dir_handle); @all_files = grep(/(\.ps$|\.pdf$|\.doc$|\.txt$)/i, @all_files); foreach $file (@all_files) { $filename = quotemeta("$file"); $new_filename = lc("$file"); $new_filename =~ tr/& \*/_/s; if ($new_filename =~ /^([-\@\w.]+)$/) { $new_filename = $1; # $data now untainted } else { verbose("Bad data in $new_filename"); } $new_filename = quotemeta("$file"); if("$filename" ne "$new_filename") { verbose("Renaming \"$file\" to \"$new_filename\""); system("/bin/mv $filename $new_filename"); $file = $new_filename; }; } foreach $file (@all_files) { ($filename, $extension) = $file =~ m/(^.*)(\..*$)/; if($extension eq ".ps") { next; } if($extension eq ".pdf") { $command = "/usr/bin/acroread -toPostScript -pairs $filename.pdf $filename.ps"; verbose("Converting Acrobat to Postscript: $command"); system($command); } if($extension eq ".txt") { $command = "/usr/bin/fmt $filename" . "$extension"; verbose("Formatting Text: $command"); open($output_file_handle, "> $filename.fmt"); open($command_file_handle, "$command |"); while($line = <$command_file_handle>) { printf($output_file_handle $line); } close($command_file_handle); close($output_file_handle); $command = "/usr/bin/enscript -B --media=Letter --font=Times-Roman12 --word-wrap -o $filename.ps $filename.fmt"; verbose("Converting Text to Postscript: $command"); system("$command"); } if($extension eq ".doc") { $command = "/usr/bin/wvPS $filename.doc $filename.ps"; verbose("Converting MS-Word to Postscript: $command"); system($command); } } foreach $file (@all_files) { ($filename, undef) = $file =~ m/(^.*)(\..*$)/; $command = "/usr/bin/gs -q -sDEVICE=tiffg3 -sPAPERSIZE=letter -r204x196 -sOutputFile=$filename.tif -dNOPAUSE -dBATCH -- $filename.ps"; verbose("Converting Postscript to TIFFG3: $command"); system("$command "); } $command = "/usr/bin/tiffcp"; foreach $file (@all_files) { ($filename, undef) = $file =~ m/(^.*)(\..*$)/; $command .= " $filename.tif"; } $command .= " $temp_dir.tif"; verbose("Merging TIFFG3 files: $command"); system("$command "); @addresses = Mail::Address->parse($to); foreach $address (@addresses) { $phone_number = $address->user; $phone_number =~ s/^fax\+//i; $phone_number =~ s/\-//g; if((length($phone_number) == 7) || (length($phone_number) == 10)) { $phone_number = "9" . $phone_number; } if(!defined($phone_number)) { verbose("Bad Address: " . $address->format); next; } verbose("Address: " . $address->format . " parses to phone number: $phone_number"); open($call_file_handle, ">", $temp_dir . ".call") or die "Cant open $temp_dir.call"; printf($call_file_handle "Channel: Local/%s@%s\n", $phone_number, $context); printf($call_file_handle "WaitTime: 30\n"); printf($call_file_handle "RetryTime: 120\n"); printf($call_file_handle "MaxRetries: 6\n"); printf($call_file_handle "Application: TxFax\n"); printf($call_file_handle "Data: %s.tif|caller\n", $temp_dir); printf($call_file_handle "Callerid: \"E-Mail to Fax\" \<9857648749\>\n"); printf($call_file_handle "SetVar: NOTIFY=$from\n"); printf($call_file_handle "SetVar: SPOOL=$temp_dir.call\n"); close($call_file_handle); system("/bin/cp $temp_dir.call /var/spool/asterisk/outgoing"); } exit(0); sub verbose { if (defined($verbose)) { system("logger -t email2fax \'$command\'"); printf("--------------------------------------------------------------\n"); printf("@_\n"); printf("--------------------------------------------------------------\n"); return(0); } }