totoro/print.pl
2020-12-28 21:45:46 -08:00

82 lines
2.2 KiB
Perl
Executable File

#!/usr/bin/env perl
use strict;
use warnings;
my $jobs_dir = "/var/www/jobs";
use OpenBSD::Pledge;
use OpenBSD::Unveil;
pledge( qw(unveil rpath dns inet) ) || die "Unable to pledge: $!";
unveil( "/var/www/perl5", "r" ) || die "Unable to unveil: $!";
unveil( $jobs_dir, "r" ) || die "Unable to unveil: $!";
use local::lib '/var/www/perl5';
use Net::Printer;
use URI::Query;
print "content-type:text/html; charset=utf-8\n\n";
print "<html>\n";
print "<head>\n";
print "<title>totoro!</title>\n";
# print "<script>if( window.location.href.match(/.*\?.*/) ) window.location = window.location.href.split(\"?\")[0];</script>";
print "</head>\n";
print "<body>\n";
my $lp = new Net::Printer();
# generate hash from query string
my $q = URI::Query->new( $ENV{'QUERY_STRING'} );
my %hash = $q->hash;
#print Dumper( \%hash );
if($hash{'file'}) {
my $file = "$jobs_dir/$hash{'file'}";
if(-e $file) {
for (my $i=0; $i <= $hash{'qty'}; $i++) {
my $result = $lp->printfile("$file");
print "$result <br>\n";
# TODO: handle errors in finding file and printing
#$errstr = $printer->printerror();
}
}
}
# display print queue
# TODO: check if "lp@localhost: JetDirect lpd: no jobs queued on the port Auto"
print "<h2>print queue</h2>\n";
# TODO: format as table with buttons (cancel)
my @queue = $lp->queuestatus();
print join("<br />", @queue);
# display jobs available to print
opendir(my $dh, "$jobs_dir") || die "Can't opendir $jobs_dir: $!";
my @files = grep { /^[^\.]/ && -f "$jobs_dir/$_" } readdir($dh);
closedir $dh;
print "<h2>jobs</h2>\n";
foreach my $file (@files) {
my $job = $file;
$job =~ s/^(.*)\.[^\.]*$/$1/;
$job =~ s/-bk-[24]x$//;
print "<form action=\"\" method=\"GET\">\n";
print "<span style=\"max-width:50%; width:10em;\"><label for=\"$job\">$job</label></span>";
print "<input type=\"hidden\" name=\"job\" value=\"$job\">";
print "<input type=\"hidden\" name=\"file\" value=\"$file\">";
print "<input type=\"number\" name=\"qty\" style=\"width: 4em;\" placeholder=\"qty\">\n";
print "<input type=\"submit\" value=\"print\">";
print "</form>\n";
}
print "</body>\n";
print "</html>\n";
1;