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