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] ID\n"; |
---|
14 | my $q = ""; |
---|
15 | GetOptions ('q|queue=s' => \$q); |
---|
16 | |
---|
17 | my @ids = @ARGV[0 .. $#ARGV]; |
---|
18 | |
---|
19 | # if the -q option is not specified, then assume we're using the |
---|
20 | # default queue |
---|
21 | if (!$q){ |
---|
22 | $q = "DEFAULT"; |
---|
23 | } |
---|
24 | |
---|
25 | # if there are no ids specified to remove, then print the usage |
---|
26 | if (!@ids) { |
---|
27 | print $usage; |
---|
28 | exit 1 |
---|
29 | } |
---|
30 | |
---|
31 | # set configuration path, and complain if it doesn't exist |
---|
32 | my $configpath = "$ENV{'HOME'}/.gutenbach/$q"; |
---|
33 | if (! -e $configpath) { |
---|
34 | print "Queue '$q' does not exist! Did you forget to add it with 'gutenbach-client-config'?\n"; |
---|
35 | exit 1; |
---|
36 | } |
---|
37 | |
---|
38 | # initialize the host and queue variables: host holds the address for |
---|
39 | # the machine on which the remote queue runs, and queue holds the name |
---|
40 | # of the printer |
---|
41 | my ($host, $queue); |
---|
42 | |
---|
43 | # load the configuration file (this will set $host and $queue) |
---|
44 | if (-r $configpath) { |
---|
45 | local $/; |
---|
46 | my $fh; |
---|
47 | open $fh, $configpath; |
---|
48 | eval <$fh>; |
---|
49 | } |
---|
50 | |
---|
51 | # initialize a new CUPS session |
---|
52 | my $cups = Net::CUPS->new(); |
---|
53 | # set the server to the one specified in the config file |
---|
54 | $cups->setServer("$host"); |
---|
55 | # set the printer name to the one specified in the config file |
---|
56 | my $printer = $cups->getDestination("$queue"); |
---|
57 | |
---|
58 | # if $printer is not defined, then throw an error |
---|
59 | unless( $printer){ |
---|
60 | print "Cannot access queue $q...do you have network connectivity and permission to view the queue?\n"; |
---|
61 | exit 1; |
---|
62 | } |
---|
63 | |
---|
64 | # get the list of jobs from the printer |
---|
65 | my @jobs = $printer->getJobs(0, 0); |
---|
66 | |
---|
67 | # for each id that we want to remove |
---|
68 | foreach my $id(@ids){ |
---|
69 | |
---|
70 | # if the id is 'all', then we remove all jobs |
---|
71 | if ($id eq "all") { |
---|
72 | foreach $id(@jobs) { |
---|
73 | cancel_job($id, $printer); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | # if the id is 'current', then we remove just the currently |
---|
78 | # playing job |
---|
79 | elsif ($id eq "current") { |
---|
80 | $id = $jobs[0]; |
---|
81 | cancel_job($id, $printer); |
---|
82 | } |
---|
83 | |
---|
84 | # if the id is 'last', then we remove just the last job |
---|
85 | elsif ($id eq "last") { |
---|
86 | $id = $jobs[-1]; |
---|
87 | cancel_job($id, $printer); |
---|
88 | } |
---|
89 | |
---|
90 | # otherwise, remove the job based on its actual (numeric) id |
---|
91 | else { |
---|
92 | foreach my $item(@jobs) { |
---|
93 | if($item =~ /$id/){ |
---|
94 | cancel_job($item, $printer); |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | # helper function to remove a job |
---|
101 | sub cancel_job { |
---|
102 | # get the id and printer from the arguments |
---|
103 | my ($id, $printer) = @_; |
---|
104 | # get the reference to the job |
---|
105 | my $job_ref = $printer->getJob($id); |
---|
106 | # find the job title (so we can print it out for the user) |
---|
107 | my $title = $job_ref->{'title'}; |
---|
108 | # cancel the job |
---|
109 | $printer->cancelJob($id); |
---|
110 | |
---|
111 | # print out that we canceled the job |
---|
112 | print "Canceled job '$title' (id $id)\n"; |
---|
113 | } |
---|