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