#!/usr/local/bin/perl -wT # # NAME faxstat.pl - CGI interface to interrogating HylaFAX server # by Phil Abercrombie , September 1997 # # SYNOPSIS # # http://www/cgi-bin/faxstat.pl # http://www/cgi-bin/faxstat.pl?FAXSERVER=localhostname.net # http://www/cgi-bin/faxstat.pl?j=2000 # http://www/cgi-bin/faxstat.pl?ps=docq%2Fdoc17777.ps # http://www/cgi-bin/faxstat.pl?fax=recvq%2Ffax00009.fax # # http://www/cgi-bin/faxstat.pl?d=2000 # # DESCRIPTION # # C implements a web interface to querying a HylaFAX server, # in a manner similar to L. # # With no HTTP parameters, an HTML page is generated listing faxes in the # sendq (queued, not yet sent), the doneq (queued and sent), and the # recvq (faxes received). # # With a C parameter, an HTML page is generated containing all the # job info for job nnnn. # # With a C parameter, the script attempts to access the document named xxx # and serve it as content type application/postscript # # With a C parameter, the script attempts to access the document named xxx # and serve is as content type image/fax # # With a C parameter, the script attempts to delete the pending (or # completed) job nnnn. # # Other HTTP parameters # # FAXSERVER # # FAXSERVER may be used with any of the above to nominate the host which # the script should attempt to query. In practice it might be easier to # edit the code. # # debug # # The value of the C parameter will be passed to the C # method of the C object used internally. Note that debugging # is to STDERR so will probably appear in your server log # # BUGS # # There is no way to contact the HylaFAX server as a particular # (authenticated) user, other than by relying on the www server to do # authentication and to require no passwords on the hylafax server. # That sucks. # # Doesn't read the system hyla.conf file for FAXSERVER # # 19-Sep-1997 # Added POD use CGI; use Config; use Net::FTP; my $cgi = new CGI; $FAXSERVER = $cgi->param("FAXSERVER")||"localhostname.com"; $HYLAFAX = 4559; eval{&main}; if ($@) { my $a=$@; print CGI->header,CGI->start_html("Error"), "

", join ("
", split "\n", "$a"), CGI->end_html; die $a } sub main { my $hfax = new Net::FTP $FAXSERVER, Port=>$HYLAFAX or die "Can't connect to host $FAXSERVER"; $hfax->debug($cgi->param("debug")); $hfax->login(CGI->remote_user) or die "Can't login to host $FAXSERVER"; $hfax->binary; if ($cgi->param("ps")) { &psinfo($hfax, $cgi->param("ps")) } elsif ($cgi->param("tiff")) { &tiffinfo($hfax, $cgi->param("tiff")) } elsif ($in{comm}) { &comminfo($hfax, "log/c$in{comm}") } elsif ($cgi->param("d")) { &killjob($hfax, $cgi->param("d")) } elsif ($cgi->param("j")) { &jobinfo($hfax, $cgi->param("j")) } else { &faxstat($hfax) } } sub list ($@) { my $hfax = shift; my $cx = $hfax->list(@_) || die $hfax->message; my @results; # 19-Sep-1997 # handle continuation lines correctly while(<$cx>) { if($results[$#results]=~ /\\\n$/) { $results[$#results].=$_; } else { push(@results,$_); } } $cx->close; @results; } sub list_jobs($@) { my $hfax = shift; map do { my %d; @d{"a".."z","A".."Z"}=split /$;/; \%d }, list $hfax, @_; } sub faxstat { my $hfax = shift; my $jobfmt = join "$;", map "%$_", "a".."z", "A".."Z"; print CGI->header; print CGI->start_html("Fax System Status"); print "

Fax System Status

\n"; my @status = map [split(/:/)], list $hfax, "status"; $hfax->quot("JOBFMT","\"$jobfmt\"") or die $hfax->message; my @send = list_jobs $hfax, "sendq"; $hfax->quot("JOBFMT","\"$jobfmt\""); die $hfax->message unless $hfax->ok; my @done = list_jobs $hfax, "doneq"; $hfax->quot("RCVFMT","\"$jobfmt\""); die $hfax->message unless $hfax->ok; my @recv = list_jobs $hfax, "recvq"; print "

Status

\n"; for(@status) { for($_->[1]) { if (/job (\d+)/) { $cgi->param(j=>$1); my $surl = $cgi->self_url; s!job (\d+)!job $1!g; $cgi->delete("j"); } } print "", map( "", @$_), "\n"; } print "
$_
\n"; &show_outq( "Jobs in Send Queue", @send ); &show_outq( "Jobs in Done Queue", @done ); &show_inq( "Jobs in Receive Queue", @recv ); print CGI->end_html; } sub show_outq { my $title = shift; my @jobs = @_; if (@jobs) { print qq{

$title

\n\n}; print "",map "", "Job", "Sender", "To","Dials","Pages","Status"; print "\n"; for(@jobs) { my $k; for $k (keys %$_){$_->{$k} ||= " "}; $_->{"e"} =~ s/^\+644/+64 4 /; $_->{"s"} =~ s/ / /g; $cgi->param(j=>$_->{j}); my $self = $cgi->self_url; print < JOB ; } print "
$_
$_->{j} $_->{M} $_->{e} $_->{D} $_->{P} $_->{"s"}
\n"; } } sub show_inq { my $title = shift; my @jobs = @_; my $self = $cgi->self_url; if (@jobs) { print qq{

$title

\n\n}; print "",map "", "Filename", "Sender/TSI", "Pages","Received@"; print "\n"; for(@jobs) { $cgi->param(tiff=>"recvq/$_->{f}"); my $myself = $cgi->self_url; print < JOB ; } print "
$_
$_->{f} $_->{"s"} $_->{p}$_->{z} $_->{t}
\n"; } } sub jobinfo { my $hfax = shift; my $job = shift; my $cx; my $self = CGI->script_name; print CGI->header; print CGI->start_html("Fax Job Info"); print "

Job Info for job $job

\n"; $cx = $hfax->retr( "sendq/q$job" ) || $hfax->retr( "doneq/q$job" ) || die $hfax->message; my @job; while(<$cx>) { if(@job && $job[$#job] =~ s/\\$/
/) { $job[$#job] .= $_; } else { push(@job,$_); } } die "No job info available" unless @job; $cx->close or die "Data connection error"; $cgi->param(d=>$job); my $myself=$cgi->self_url; print qq{Delete Me\n}; $cgi->delete('d'); print "\n"; for (@job) { chomp; $cgi->delete("ps","tiff","comm"); my ($key,$val)=split(/:/,$_,2); $val = localtime($val) if $key eq 'tts' || $key =~ /time$/; if ($key =~ /postscript|tiff|fax/) { my $param = ($& eq "postscript")?"ps":"tiff"; my ($ref) = ($val =~ m!(docq/\S+)!); $cgi->param($param,$ref); my $myself = $cgi->self_url; $val =~ s!docq/\S+!$&!; } elsif($key eq commid) { $cgi->param(comm=>$val); my $self = $cgi->self_url; $val = qq{$val}; } print "\n"; } print "
$key$val
\n"; print CGI->end_html; } sub psinfo { my $hfax = shift; my $psfile = shift; $cx = $hfax->retr( $psfile ) || die $hfax->message; my @job=<$cx>; $cx->close or die "Data connection error"; print CGI->header( "application/postscript" ); print @job; } sub tiffinfo { my $hfax = shift; my $tiff = shift; $cx = $hfax->retr( $tiff ) || die $hfax->message; my @job=<$cx>; $cx->close or die "Data connection error"; print CGI->header( "image/tiff" ); print @job; } sub comminfo { my $hfax = shift; my $comm = shift; $cx = $hfax->retr( $comm ) || die $hfax->message; my @job=<$cx>; $cx->close or die "Data connection error"; print CGI->header( "text/plain" ); print @job; } sub killjob { my $hfax = shift; my $job = shift; $hfax->quot( "JSUSP", $job ); my ($mess,$ok)=($hfax->message, $hfax->ok); $hfax->quot( "JDELE", $job ); die "$mess\n".$hfax->message unless $hfax->ok; print CGI->redirect( $cgi->self_url ); }