]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
503b54c9 | 2 | * "cancel" command for CUPS. |
ef416fc2 | 3 | * |
503b54c9 MS |
4 | * Copyright 2007-2013 by Apple Inc. |
5 | * Copyright 1997-2006 by Easy Software Products. | |
ef416fc2 | 6 | * |
503b54c9 MS |
7 | * These coded instructions, statements, and computer programs are the |
8 | * property of Apple Inc. and are protected by Federal copyright | |
9 | * law. Distribution and use rights are outlined in the file "LICENSE.txt" | |
10 | * which should have been included with this file. If this file is | |
11 | * file is missing or damaged, see the license at "http://www.cups.org/". | |
ef416fc2 | 12 | */ |
13 | ||
14 | /* | |
15 | * Include necessary headers... | |
16 | */ | |
17 | ||
71e16022 | 18 | #include <cups/cups-private.h> |
ef416fc2 | 19 | |
20 | ||
21 | /* | |
22 | * 'main()' - Parse options and cancel jobs. | |
23 | */ | |
24 | ||
25 | int /* O - Exit status */ | |
26 | main(int argc, /* I - Number of command-line arguments */ | |
27 | char *argv[]) /* I - Command-line arguments */ | |
28 | { | |
29 | http_t *http; /* HTTP connection to server */ | |
30 | int i; /* Looping var */ | |
31 | int job_id; /* Job ID */ | |
32 | int num_dests; /* Number of destinations */ | |
33 | cups_dest_t *dests; /* Destinations */ | |
34 | char *dest, /* Destination printer */ | |
35 | *job, /* Job ID pointer */ | |
36 | *user; /* Cancel jobs for a user */ | |
37 | int purge; /* Purge or cancel jobs? */ | |
38 | char uri[1024]; /* Printer or job URI */ | |
39 | ipp_t *request; /* IPP request */ | |
40 | ipp_t *response; /* IPP response */ | |
41 | ipp_op_t op; /* Operation */ | |
ef416fc2 | 42 | |
43 | ||
07725fee | 44 | _cupsSetLocale(argv); |
d09495fa | 45 | |
ef416fc2 | 46 | /* |
47 | * Setup to cancel individual print jobs... | |
48 | */ | |
49 | ||
fa73b229 | 50 | op = IPP_CANCEL_JOB; |
51 | purge = 0; | |
fa73b229 | 52 | dest = NULL; |
53 | user = NULL; | |
54 | http = NULL; | |
55 | num_dests = 0; | |
56 | dests = NULL; | |
ef416fc2 | 57 | |
58 | ||
59 | /* | |
60 | * Process command-line arguments... | |
61 | */ | |
62 | ||
63 | for (i = 1; i < argc; i ++) | |
64 | if (argv[i][0] == '-' && argv[i][1]) | |
db8b865d | 65 | { |
ef416fc2 | 66 | switch (argv[i][1]) |
67 | { | |
68 | case 'E' : /* Encrypt */ | |
69 | #ifdef HAVE_SSL | |
70 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); | |
71 | ||
72 | if (http) | |
73 | httpEncryption(http, HTTP_ENCRYPT_REQUIRED); | |
74 | #else | |
fa73b229 | 75 | _cupsLangPrintf(stderr, |
0837b7e8 | 76 | _("%s: Sorry, no encryption support."), argv[0]); |
ef416fc2 | 77 | #endif /* HAVE_SSL */ |
78 | break; | |
79 | ||
fa73b229 | 80 | case 'U' : /* Username */ |
81 | if (argv[i][2] != '\0') | |
82 | cupsSetUser(argv[i] + 2); | |
83 | else | |
84 | { | |
85 | i ++; | |
86 | if (i >= argc) | |
87 | { | |
88 | _cupsLangPrintf(stderr, | |
89 | _("%s: Error - expected username after " | |
0837b7e8 | 90 | "\"-U\" option."), argv[0]); |
fa73b229 | 91 | return (1); |
92 | } | |
93 | ||
94 | cupsSetUser(argv[i]); | |
95 | } | |
96 | break; | |
88f9aafc | 97 | |
ef416fc2 | 98 | case 'a' : /* Cancel all jobs */ |
db8b865d | 99 | op = purge ? IPP_PURGE_JOBS : IPP_CANCEL_JOBS; |
ef416fc2 | 100 | break; |
101 | ||
102 | case 'h' : /* Connect to host */ | |
103 | if (http != NULL) | |
8ca02f3c | 104 | { |
ef416fc2 | 105 | httpClose(http); |
8ca02f3c | 106 | http = NULL; |
107 | } | |
ef416fc2 | 108 | |
109 | if (argv[i][2] != '\0') | |
110 | cupsSetServer(argv[i] + 2); | |
111 | else | |
112 | { | |
113 | i ++; | |
114 | ||
115 | if (i >= argc) | |
116 | { | |
fa73b229 | 117 | _cupsLangPrintf(stderr, |
118 | _("%s: Error - expected hostname after " | |
0837b7e8 | 119 | "\"-h\" option."), argv[0]); |
ef416fc2 | 120 | return (1); |
121 | } | |
122 | else | |
123 | cupsSetServer(argv[i]); | |
124 | } | |
125 | break; | |
126 | ||
127 | case 'u' : /* Username */ | |
db8b865d | 128 | op = IPP_CANCEL_MY_JOBS; |
ef416fc2 | 129 | |
130 | if (argv[i][2] != '\0') | |
131 | user = argv[i] + 2; | |
132 | else | |
133 | { | |
134 | i ++; | |
135 | ||
136 | if (i >= argc) | |
137 | { | |
fa73b229 | 138 | _cupsLangPrintf(stderr, |
139 | _("%s: Error - expected username after " | |
0837b7e8 | 140 | "\"-u\" option."), argv[0]); |
ef416fc2 | 141 | return (1); |
142 | } | |
143 | else | |
144 | user = argv[i]; | |
145 | } | |
146 | break; | |
147 | ||
db8b865d MS |
148 | case 'x' : /* Purge job(s) */ |
149 | purge = 1; | |
150 | ||
151 | if (op == IPP_CANCEL_JOBS) | |
152 | op = IPP_PURGE_JOBS; | |
153 | break; | |
154 | ||
ef416fc2 | 155 | default : |
fa73b229 | 156 | _cupsLangPrintf(stderr, |
0837b7e8 | 157 | _("%s: Error - unknown option \"%c\"."), |
fa73b229 | 158 | argv[0], argv[i][1]); |
ef416fc2 | 159 | return (1); |
160 | } | |
db8b865d | 161 | } |
ef416fc2 | 162 | else |
163 | { | |
164 | /* | |
165 | * Cancel a job or printer... | |
166 | */ | |
167 | ||
168 | if (num_dests == 0) | |
169 | num_dests = cupsGetDests(&dests); | |
170 | ||
bd7854cb | 171 | if (!strcmp(argv[i], "-")) |
ef416fc2 | 172 | { |
173 | /* | |
174 | * Delete the current job... | |
175 | */ | |
176 | ||
177 | dest = ""; | |
178 | job_id = 0; | |
179 | } | |
180 | else if (cupsGetDest(argv[i], NULL, num_dests, dests) != NULL) | |
181 | { | |
182 | /* | |
183 | * Delete the current job on the named destination... | |
184 | */ | |
185 | ||
186 | dest = argv[i]; | |
187 | job_id = 0; | |
188 | } | |
189 | else if ((job = strrchr(argv[i], '-')) != NULL && isdigit(job[1] & 255)) | |
190 | { | |
191 | /* | |
192 | * Delete the specified job ID. | |
193 | */ | |
194 | ||
195 | dest = NULL; | |
196 | op = IPP_CANCEL_JOB; | |
197 | job_id = atoi(job + 1); | |
198 | } | |
199 | else if (isdigit(argv[i][0] & 255)) | |
200 | { | |
201 | /* | |
202 | * Delete the specified job ID. | |
203 | */ | |
204 | ||
205 | dest = NULL; | |
206 | op = IPP_CANCEL_JOB; | |
207 | job_id = atoi(argv[i]); | |
208 | } | |
209 | else | |
210 | { | |
211 | /* | |
212 | * Bad printer name! | |
213 | */ | |
214 | ||
fa73b229 | 215 | _cupsLangPrintf(stderr, |
0837b7e8 | 216 | _("%s: Error - unknown destination \"%s\"."), |
fa73b229 | 217 | argv[0], argv[i]); |
ef416fc2 | 218 | return (1); |
219 | } | |
220 | ||
221 | /* | |
222 | * For Solaris LP compatibility, ignore a destination name after | |
223 | * cancelling a specific job ID... | |
224 | */ | |
225 | ||
226 | if (job_id && (i + 1) < argc && | |
227 | cupsGetDest(argv[i + 1], NULL, num_dests, dests) != NULL) | |
228 | i ++; | |
229 | ||
230 | /* | |
231 | * Open a connection to the server... | |
232 | */ | |
233 | ||
234 | if (http == NULL) | |
235 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
236 | cupsEncryption())) == NULL) | |
237 | { | |
fa73b229 | 238 | _cupsLangPrintf(stderr, |
84315f46 | 239 | _("%s: Unable to connect to server."), argv[0]); |
ef416fc2 | 240 | return (1); |
241 | } | |
242 | ||
243 | /* | |
244 | * Build an IPP request, which requires the following | |
245 | * attributes: | |
246 | * | |
247 | * attributes-charset | |
248 | * attributes-natural-language | |
249 | * printer-uri + job-id *or* job-uri | |
250 | * [requesting-user-name] | |
251 | */ | |
252 | ||
fa73b229 | 253 | request = ippNewRequest(op); |
ef416fc2 | 254 | |
255 | if (dest) | |
256 | { | |
a4d04587 | 257 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, |
258 | "localhost", 0, "/printers/%s", dest); | |
ef416fc2 | 259 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, |
260 | "printer-uri", NULL, uri); | |
261 | ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id", | |
262 | job_id); | |
263 | } | |
264 | else | |
265 | { | |
266 | sprintf(uri, "ipp://localhost/jobs/%d", job_id); | |
267 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, | |
268 | uri); | |
269 | } | |
270 | ||
271 | if (user) | |
272 | { | |
273 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
274 | "requesting-user-name", NULL, user); | |
275 | ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); | |
db8b865d MS |
276 | |
277 | if (op == IPP_CANCEL_JOBS) | |
278 | op = IPP_CANCEL_MY_JOBS; | |
ef416fc2 | 279 | } |
280 | else | |
281 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
282 | "requesting-user-name", NULL, cupsUser()); | |
283 | ||
db8b865d | 284 | if (purge) |
68c4690a | 285 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", (char)purge); |
ef416fc2 | 286 | |
287 | /* | |
288 | * Do the request and get back a response... | |
289 | */ | |
290 | ||
db8b865d | 291 | if (op == IPP_CANCEL_JOBS && (!user || _cups_strcasecmp(user, cupsUser()))) |
ef416fc2 | 292 | response = cupsDoRequest(http, request, "/admin/"); |
293 | else | |
294 | response = cupsDoRequest(http, request, "/jobs/"); | |
295 | ||
296 | if (response == NULL || | |
297 | response->request.status.status_code > IPP_OK_CONFLICT) | |
298 | { | |
0837b7e8 | 299 | _cupsLangPrintf(stderr, _("%s: %s failed: %s"), argv[0], |
ef416fc2 | 300 | op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job", |
bd7854cb | 301 | cupsLastErrorString()); |
ef416fc2 | 302 | |
303 | if (response) | |
304 | ippDelete(response); | |
305 | ||
306 | return (1); | |
307 | } | |
308 | ||
309 | ippDelete(response); | |
310 | } | |
311 | ||
242b936a | 312 | if (num_dests == 0 && op != IPP_CANCEL_JOB) |
ef416fc2 | 313 | { |
314 | /* | |
315 | * Open a connection to the server... | |
316 | */ | |
317 | ||
318 | if (http == NULL) | |
319 | if ((http = httpConnectEncrypt(cupsServer(), ippPort(), | |
320 | cupsEncryption())) == NULL) | |
321 | { | |
0837b7e8 | 322 | _cupsLangPrintf(stderr, _("%s: Unable to contact server."), argv[0]); |
ef416fc2 | 323 | return (1); |
324 | } | |
325 | ||
326 | /* | |
327 | * Build an IPP request, which requires the following | |
328 | * attributes: | |
329 | * | |
330 | * attributes-charset | |
331 | * attributes-natural-language | |
332 | * printer-uri + job-id *or* job-uri | |
333 | * [requesting-user-name] | |
334 | */ | |
335 | ||
fa73b229 | 336 | request = ippNewRequest(op); |
ef416fc2 | 337 | |
338 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
339 | "printer-uri", NULL, "ipp://localhost/printers/"); | |
340 | ||
341 | if (user) | |
342 | { | |
343 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
344 | "requesting-user-name", NULL, user); | |
345 | ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1); | |
346 | } | |
347 | else | |
348 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
349 | "requesting-user-name", NULL, cupsUser()); | |
350 | ||
68c4690a | 351 | ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", (char)purge); |
ef416fc2 | 352 | |
353 | /* | |
354 | * Do the request and get back a response... | |
355 | */ | |
356 | ||
357 | response = cupsDoRequest(http, request, "/admin/"); | |
358 | ||
359 | if (response == NULL || | |
360 | response->request.status.status_code > IPP_OK_CONFLICT) | |
361 | { | |
0837b7e8 | 362 | _cupsLangPrintf(stderr, _("%s: %s failed: %s"), argv[0], |
ef416fc2 | 363 | op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job", |
fa73b229 | 364 | cupsLastErrorString()); |
ef416fc2 | 365 | |
366 | if (response) | |
367 | ippDelete(response); | |
368 | ||
369 | return (1); | |
370 | } | |
371 | ||
372 | ippDelete(response); | |
373 | } | |
374 | ||
375 | return (0); | |
376 | } |