#!/usr/bin/perl # # mail notify when new fax is received; send fax as attached pdf file # # --- USED MODULES use strict; use MIME::Base64 qw(encode_base64); use Time::localtime; # --- GLOBAL VARS - please set values according to your config my $sff2pdf = '/usr/bin/sff2pdf'; my $sendmail = '/usr/sbin/sendmail'; # --- VAR DEFINITIONS my $new_fax = $ARGV[0]; my %faxdata = (); # --- HELPER SUBS sub get_fax_data ($) { my $filename = shift; $filename =~ s/^f//g; my %fax = (); $fax{'resolution'} = substr ($filename, 0, 1); if ($fax{'resolution'} eq "n") { $fax{'resolution'} = "normal"; } else { $fax{'resolution'} = "fein"; } $fax{'pages'} = substr ($filename, 1, 3); $fax{'pages'} =~ s/^[0]*//g; $fax{'time'} = hex (substr ($filename, 4, 8)); my $tm = localtime ($fax{'time'}); $fax{'time'} = sprintf ("%02d.%02d.%04d, %02d:%02d Uhr", $tm->mday, $tm->mon + 1, $tm->year + 1900, $tm->hour, $tm->min, $tm->sec); $fax{'callid'} = substr ($filename, 12, length ($filename) - 12); $fax{'callid'} =~ s/_/ /g; return (%fax); } # --- MAIN --- # -- get fax data %faxdata = get_fax_data ($new_fax); # -- convert fax (SFF) file to pdf open (PIPE, "$sff2pdf /var/spool/fax/$new_fax $faxdata{'resolution'} |"); close (PIPE); # -- pipe into sendmail and base64-encode pdf attachment open (PIPE, "| $sendmail -t"); print PIPE "From: Fax-Daemon \n"; print PIPE "To: Leonhard Fellermayr \n"; print PIPE "Subject: [fax] Neues Fax von " . $faxdata{'callid'} . " (" . $faxdata{'pages'} . " Seite/n)\n"; print PIPE "Mime-Version: 1.0\n"; print PIPE "Content-type: multipart/mixed; boundary=$new_fax\n"; print PIPE "\n"; print PIPE "--$new_fax\n"; print PIPE "Content-type: text/plain; charset=us-ascii\n"; print PIPE "\n"; print PIPE "Soeben wurde ein neues Fax mit folgenden Daten auf dem Server empfangen:\n"; print PIPE "\n\n"; print PIPE "\tAbsender-Kennung ..... " . $faxdata{'callid'} . "\n"; print PIPE "\tAnzahl Seiten ........ " . $faxdata{'pages'} . "\n"; print PIPE "\tEmpfangszeit ......... " . $faxdata{'time'} . "\n"; print PIPE "\tAufloesung ........... " . $faxdata{'resolution'} . "\n"; print PIPE "\n\n"; print PIPE "Das Fax wurde unter /var/spool/fax/" . $new_fax . " abgelegt.\n"; print PIPE "\n"; # -- attach pdf file print PIPE "--$new_fax\n"; print PIPE "Content-type: application/pdf\n"; print PIPE "Content-Transfer-Encoding: base64\n"; print PIPE "Content-Disposition: attachment; filename=\"$new_fax.pdf\"\n"; print PIPE "\n"; open (PAGE, "/tmp/$new_fax.pdf"); my $buf; while (read(PAGE, $buf, 60*57)) { print PIPE encode_base64 ($buf); } close (PAGE); print PIPE "\n"; # -- finish MIME boundary print PIPE "--$new_fax\n"; print PIPE "\n"; # -- finish mail close (PIPE); # -- delete pdf file from temporary directory unlink ("/tmp/$new_fax.pdf");