]> git.ipfire.org Git - thirdparty/cups.git/blame - systemv/accept.c
Load cups into easysw/current.
[thirdparty/cups.git] / systemv / accept.c
CommitLineData
ef416fc2 1/*
bc44d920 2 * "$Id: accept.c 6649 2007-07-11 21:46:42Z mike $"
ef416fc2 3 *
4 * "accept", "disable", "enable", and "reject" commands for the Common
5 * UNIX Printing System (CUPS).
6 *
bc44d920 7 * Copyright 2007 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
65 if (!strcmp(command, "accept"))
66 op = CUPS_ACCEPT_JOBS;
67 else if (!strcmp(command, "reject"))
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 */
128 if (http != NULL)
129 httpClose(http);
130
131 if (argv[i][2] != '\0')
132 cupsSetServer(argv[i] + 2);
133 else
134 {
135 i ++;
136 if (i >= argc)
137 {
fa73b229 138 _cupsLangPrintf(stderr,
139 _("%s: Error - expected hostname after "
140 "\'-h\' option!\n"),
ef416fc2 141 command);
142 return (1);
143 }
144
145 cupsSetServer(argv[i]);
146 }
147 break;
148
149 case 'r' : /* Reason for cancellation */
150 if (argv[i][2] != '\0')
151 reason = argv[i] + 2;
152 else
153 {
154 i ++;
155 if (i >= argc)
156 {
fa73b229 157 _cupsLangPrintf(stderr,
158 _("%s: Error - expected reason text after "
159 "\'-r\' option!\n"),
ef416fc2 160 command);
161 return (1);
162 }
163
164 reason = argv[i];
165 }
166 break;
167
168 default :
fa73b229 169 _cupsLangPrintf(stderr, _("%s: Error - unknown option \'%c\'!\n"),
ef416fc2 170 command, argv[i][1]);
171 return (1);
172 }
173 else
174 {
175 /*
176 * Accept/disable/enable/reject a destination...
177 */
178
179 if (http == NULL)
180 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
181
182 if (http == NULL)
183 {
fa73b229 184 _cupsLangPrintf(stderr,
ef416fc2 185 _("%s: Unable to connect to server: %s\n"),
186 command, strerror(errno));
187 return (1);
188 }
189
190 /*
191 * Build an IPP request, which requires the following
192 * attributes:
193 *
194 * attributes-charset
195 * attributes-natural-language
196 * printer-uri
197 * printer-state-message [optional]
198 */
199
fa73b229 200 request = ippNewRequest(op);
ef416fc2 201
a4d04587 202 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
203 "localhost", 0, "/printers/%s", argv[i]);
ef416fc2 204 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
205 "printer-uri", NULL, uri);
206
fa73b229 207 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
208 "requesting-user-name", NULL, cupsUser());
209
ef416fc2 210 if (reason != NULL)
211 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT,
212 "printer-state-message", NULL, reason);
213
214 /*
215 * Do the request and get back a response...
216 */
217
218 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL)
219 {
220 if (response->request.status.status_code > IPP_OK_CONFLICT)
221 {
fa73b229 222 _cupsLangPrintf(stderr,
ef416fc2 223 _("%s: Operation failed: %s\n"),
224 command, ippErrorString(cupsLastError()));
225 return (1);
226 }
227
228 ippDelete(response);
229 }
230 else
231 {
fa73b229 232 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
ef416fc2 233 return (1);
234 }
235
236 /*
237 * Cancel all jobs if requested...
238 */
239
240 if (cancel)
241 {
242 /*
243 * Build an IPP_PURGE_JOBS request, which requires the following
244 * attributes:
245 *
246 * attributes-charset
247 * attributes-natural-language
248 * printer-uri
249 */
250
fa73b229 251 request = ippNewRequest(IPP_PURGE_JOBS);
ef416fc2 252
253 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
254 "printer-uri", NULL, uri);
255
256 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL)
257 {
258 if (response->request.status.status_code > IPP_OK_CONFLICT)
259 {
fa73b229 260 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
ef416fc2 261 return (1);
262 }
263
264 ippDelete(response);
265 }
266 else
267 {
fa73b229 268 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
ef416fc2 269 return (1);
270 }
271 }
272 }
273
274 if (http != NULL)
275 httpClose(http);
276
277 return (0);
278}
279
280
281/*
bc44d920 282 * End of "$Id: accept.c 6649 2007-07-11 21:46:42Z mike $".
ef416fc2 283 */