[78eb866c] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
[6b7441a] | 3 | # This script was largely written by Jessica Hamrick (jhamrick), with |
---|
| 4 | # help from Kyle Brogle (broglek) |
---|
[78eb866c] | 5 | |
---|
| 6 | use strict; |
---|
| 7 | use warnings; |
---|
| 8 | |
---|
| 9 | use Net::CUPS; |
---|
| 10 | use Net::CUPS::Destination; |
---|
[7cdd65d] | 11 | use Getopt::Long; |
---|
[78eb866c] | 12 | |
---|
[fc8707b] | 13 | # usage |
---|
| 14 | my $usage = <<USAGE; |
---|
| 15 | Usage: gbq [options] [-q QUEUE] |
---|
[78eb866c] | 16 | |
---|
[fc8707b] | 17 | -q, --queue Specify a queue other than the default |
---|
| 18 | -h, --help Print this message |
---|
| 19 | USAGE |
---|
| 20 | |
---|
| 21 | # initialize the variables for the options |
---|
[7cdd65d] | 22 | my $q = ""; |
---|
[fc8707b] | 23 | my $help = 0; |
---|
| 24 | |
---|
| 25 | # parse the options |
---|
| 26 | GetOptions ('q|queue=s' => \$q, |
---|
| 27 | 'h|help' => \$help); |
---|
| 28 | |
---|
| 29 | # if the -h flag was passed, then print the usage and exit |
---|
| 30 | if ($help) { |
---|
| 31 | print $usage; |
---|
| 32 | exit 0; |
---|
| 33 | } |
---|
[78eb866c] | 34 | |
---|
[6b7441a] | 35 | # if the -q option is not specified, then assume we're using the |
---|
| 36 | # default queue |
---|
[78eb866c] | 37 | if (!$q) { |
---|
[7cdd65d] | 38 | $q = "DEFAULT"; |
---|
[78eb866c] | 39 | } |
---|
| 40 | |
---|
[6b7441a] | 41 | # set configuration path, and complain if it doesn't exist |
---|
[78eb866c] | 42 | my $configpath = "$ENV{'HOME'}/.gutenbach/$q"; |
---|
[85a1ac1] | 43 | if (! -e $configpath) { |
---|
[0d02eca] | 44 | print "Queue '$q' does not exist! Did you forget to add it with 'gutenbach-client-config'?\n"; |
---|
[85a1ac1] | 45 | exit 1; |
---|
| 46 | } |
---|
| 47 | |
---|
[6b7441a] | 48 | # initialize the host and queue variables: host holds the address for |
---|
| 49 | # the machine on which the remote queue runs, and queue holds the name |
---|
| 50 | # of the printer |
---|
[78eb866c] | 51 | my ($host, $queue); |
---|
| 52 | |
---|
[6b7441a] | 53 | # load the configuration file (this will set $host and $queue) |
---|
[78eb866c] | 54 | if (-r $configpath) { |
---|
| 55 | local $/; |
---|
| 56 | my $fh; |
---|
| 57 | open $fh, $configpath; |
---|
| 58 | eval <$fh>; |
---|
| 59 | } |
---|
| 60 | |
---|
[6b7441a] | 61 | # initialize a new CUPS session |
---|
[78eb866c] | 62 | my $cups = Net::CUPS->new(); |
---|
[6b7441a] | 63 | # set the server to the one specified in the config file |
---|
[78eb866c] | 64 | $cups->setServer("$host"); |
---|
[6b7441a] | 65 | # set the printer name to the one specified in the config file |
---|
[78eb866c] | 66 | my $printer = $cups->getDestination("$queue"); |
---|
| 67 | |
---|
[6b7441a] | 68 | # if $printer is not defined, then throw an error |
---|
[9fdf4a1] | 69 | unless( $printer){ |
---|
| 70 | print "Cannot access queue $q...do you have network connectivity and permission to view the queue?\n"; |
---|
| 71 | exit 1; |
---|
| 72 | } |
---|
[6b7441a] | 73 | |
---|
| 74 | # print pretty headings and stuff |
---|
[78eb866c] | 75 | print "Queue listing for queue '$queue' on '$host'\n\n"; |
---|
[9dee329] | 76 | printf ("%-8s%-15s%s\n","Job","Owner","Title"); |
---|
| 77 | print "-"x70 . "\n"; |
---|
[6b7441a] | 78 | |
---|
| 79 | # get the list of jobs from the printer |
---|
[78eb866c] | 80 | my @jobs = $printer->getJobs(0, 0); |
---|
[6b7441a] | 81 | |
---|
| 82 | # initialize the job reference and job id variables |
---|
[78eb866c] | 83 | my ($job_ref, $jobid); |
---|
| 84 | |
---|
[6b7441a] | 85 | # for each job in the list of jobs: |
---|
[78eb866c] | 86 | foreach $jobid(@jobs) |
---|
| 87 | { |
---|
[6b7441a] | 88 | # get the reference to the job (so we can get various related |
---|
| 89 | # variables) |
---|
| 90 | $job_ref = $printer->getJob($jobid); |
---|
[78eb866c] | 91 | |
---|
[6b7441a] | 92 | # get the id of the job |
---|
| 93 | my $id = $job_ref->{'id'}; |
---|
| 94 | # get the user who printed the job |
---|
| 95 | my $user = $job_ref->{'user'}; |
---|
| 96 | # get the title of the job |
---|
| 97 | my $title = $job_ref->{'title'}; |
---|
| 98 | |
---|
| 99 | # print the job information to the screen |
---|
| 100 | printf ("%-8s%-15s%s\n","$id",substr("$user",0,15),substr("$title",0,47)); |
---|
[78eb866c] | 101 | } |
---|