]> git.ipfire.org Git - thirdparty/cups.git/blame - systemv/accept.c
Merge changes from CUPS 1.4svn-r7932.
[thirdparty/cups.git] / systemv / accept.c
CommitLineData
ef416fc2 1/*
75bd9771 2 * "$Id: accept.c 7221 2008-01-16 22:20:08Z mike $"
ef416fc2 3 *
4 * "accept", "disable", "enable", and "reject" commands for the Common
5 * UNIX Printing System (CUPS).
6 *
91c84a35 7 * Copyright 2007-2008 by Apple Inc.
ef416fc2 8 * Copyright 1997-2006 by Easy Software Products.
9 *
10 * These coded instructions, statements, and computer programs are the
bc44d920 11 * property of Apple Inc. and are protected by Federal copyright
12 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
13 * which should have been included with this file. If this file is
14 * file is missing or damaged, see the license at "http://www.cups.org/".
ef416fc2 15 *
16 * Contents:
17 *
18 * main() - Parse options and accept/reject jobs or disable/enable printers.
19 */
20
21/*
22 * Include necessary headers...
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <errno.h>
28#include <cups/string.h>
29#include <cups/cups.h>
30#include <cups/i18n.h>
31
32
33/*
34 * 'main()' - Parse options and accept/reject jobs or disable/enable printers.
35 */
36
37int /* O - Exit status */
38main(int argc, /* I - Number of command-line arguments */
39 char *argv[]) /* I - Command-line arguments */
40{
41 http_t *http; /* HTTP connection to server */
42 int i; /* Looping var */
43 char *command, /* Command to do */
44 uri[1024], /* Printer URI */
45 *reason; /* Reason for reject/disable */
46 ipp_t *request; /* IPP request */
47 ipp_t *response; /* IPP response */
48 ipp_op_t op; /* Operation */
ef416fc2 49 int cancel; /* Cancel jobs? */
50
51
07725fee 52 _cupsSetLocale(argv);
d09495fa 53
ef416fc2 54 /*
55 * See what operation we're supposed to do...
56 */
57
58 if ((command = strrchr(argv[0], '/')) != NULL)
59 command ++;
60 else
61 command = argv[0];
62
fa73b229 63 cancel = 0;
ef416fc2 64
49d87452 65 if (!strcmp(command, "cupsaccept") || !strcmp(command, "accept"))
ef416fc2 66 op = CUPS_ACCEPT_JOBS;
49d87452 67 else if (!strcmp(command, "cupsreject") || !strcmp(command, "reject"))
ef416fc2 68 op = CUPS_REJECT_JOBS;
bd7854cb 69 else if (!strcmp(command, "cupsdisable") || !strcmp(command, "disable"))
ef416fc2 70 op = IPP_PAUSE_PRINTER;
bd7854cb 71 else if (!strcmp(command, "cupsenable") || !strcmp(command, "enable"))
ef416fc2 72 op = IPP_RESUME_PRINTER;
73 else
74 {
fa73b229 75 _cupsLangPrintf(stderr, _("%s: Don't know what to do!\n"),
ef416fc2 76 command);
77 return (1);
78 }
79
80 http = NULL;
81 reason = NULL;
82
83 /*
84 * Process command-line arguments...
85 */
86
87 for (i = 1; i < argc; i ++)
88 if (argv[i][0] == '-')
89 switch (argv[i][1])
90 {
91 case 'E' : /* Encrypt */
92#ifdef HAVE_SSL
93 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
94
95 if (http)
96 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
97#else
fa73b229 98 _cupsLangPrintf(stderr,
ef416fc2 99 _("%s: Sorry, no encryption support compiled in!\n"),
100 command);
101#endif /* HAVE_SSL */
102 break;
103
fa73b229 104 case 'U' : /* Username */
105 if (argv[i][2] != '\0')
106 cupsSetUser(argv[i] + 2);
107 else
108 {
109 i ++;
110 if (i >= argc)
111 {
112 _cupsLangPrintf(stderr,
113 _("%s: Error - expected username after "
114 "\'-U\' option!\n"),
115 command);
116 return (1);
117 }
118
119 cupsSetUser(argv[i]);
120 }
121 break;
122
ef416fc2 123 case 'c' : /* Cancel jobs */
124 cancel = 1;
125 break;
126
127 case 'h' : /* Connect to host */
91c84a35
MS
128 if (http)
129 {
ef416fc2 130 httpClose(http);
91c84a35
MS
131 http = NULL;
132 }
ef416fc2 133
134 if (argv[i][2] != '\0')
135 cupsSetServer(argv[i] + 2);
136 else
137 {
138 i ++;
139 if (i >= argc)
140 {
fa73b229 141 _cupsLangPrintf(stderr,
142 _("%s: Error - expected hostname after "
143 "\'-h\' option!\n"),
ef416fc2 144 command);
145 return (1);
146 }
147
148 cupsSetServer(argv[i]);
149 }
150 break;
151
152 case 'r' : /* Reason for cancellation */
153 if (argv[i][2] != '\0')
154 reason = argv[i] + 2;
155 else
156 {
157 i ++;
158 if (i >= argc)
159 {
fa73b229 160 _cupsLangPrintf(stderr,
161 _("%s: Error - expected reason text after "
162 "\'-r\' option!\n"),
ef416fc2 163 command);
164 return (1);
165 }
166
167 reason = argv[i];
168 }
169 break;
170
171 default :
fa73b229 172 _cupsLangPrintf(stderr, _("%s: Error - unknown option \'%c\'!\n"),
ef416fc2 173 command, argv[i][1]);
174 return (1);
175 }
176 else
177 {
178 /*
179 * Accept/disable/enable/reject a destination...
180 */
181
182 if (http == NULL)
183 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
184
185 if (http == NULL)
186 {
fa73b229 187 _cupsLangPrintf(stderr,
ef416fc2 188 _("%s: Unable to connect to server: %s\n"),
189 command, strerror(errno));
190 return (1);
191 }
192
193 /*
194 * Build an IPP request, which requires the following
195 * attributes:
196 *
197 * attributes-charset
198 * attributes-natural-language
199 * printer-uri
200 * printer-state-message [optional]
201 */
202
fa73b229 203 request = ippNewRequest(op);
ef416fc2 204
a4d04587 205 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
206 "localhost", 0, "/printers/%s", argv[i]);
ef416fc2 207 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
208 "printer-uri", NULL, uri);
209
fa73b229 210 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
211 "requesting-user-name", NULL, cupsUser());
212
ef416fc2 213 if (reason != NULL)
214 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT,
215 "printer-state-message", NULL, reason);
216
217 /*
218 * Do the request and get back a response...
219 */
220
221 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL)
222 {
223 if (response->request.status.status_code > IPP_OK_CONFLICT)
224 {
fa73b229 225 _cupsLangPrintf(stderr,
ef416fc2 226 _("%s: Operation failed: %s\n"),
227 command, ippErrorString(cupsLastError()));
228 return (1);
229 }
230
231 ippDelete(response);
232 }
233 else
234 {
fa73b229 235 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
ef416fc2 236 return (1);
237 }
238
239 /*
240 * Cancel all jobs if requested...
241 */
242
243 if (cancel)
244 {
245 /*
246 * Build an IPP_PURGE_JOBS request, which requires the following
247 * attributes:
248 *
249 * attributes-charset
250 * attributes-natural-language
251 * printer-uri
252 */
253
fa73b229 254 request = ippNewRequest(IPP_PURGE_JOBS);
ef416fc2 255
256 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
257 "printer-uri", NULL, uri);
258
259 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL)
260 {
261 if (response->request.status.status_code > IPP_OK_CONFLICT)
262 {
fa73b229 263 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
ef416fc2 264 return (1);
265 }
266
267 ippDelete(response);
268 }
269 else
270 {
fa73b229 271 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
ef416fc2 272 return (1);
273 }
274 }
275 }
276
277 if (http != NULL)
278 httpClose(http);
279
280 return (0);
281}
282
283
284/*
75bd9771 285 * End of "$Id: accept.c 7221 2008-01-16 22:20:08Z mike $".
ef416fc2 286 */