]> 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/*
f7faf1f5 2 * "$Id: accept.c 5104 2006-02-15 03:21:04Z 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
61 /*
62 * See what operation we're supposed to do...
63 */
64
65 if ((command = strrchr(argv[0], '/')) != NULL)
66 command ++;
67 else
68 command = argv[0];
69
fa73b229 70 cancel = 0;
ef416fc2 71
72 if (!strcmp(command, "accept"))
73 op = CUPS_ACCEPT_JOBS;
74 else if (!strcmp(command, "reject"))
75 op = CUPS_REJECT_JOBS;
bd7854cb 76 else if (!strcmp(command, "cupsdisable") || !strcmp(command, "disable"))
ef416fc2 77 op = IPP_PAUSE_PRINTER;
bd7854cb 78 else if (!strcmp(command, "cupsenable") || !strcmp(command, "enable"))
ef416fc2 79 op = IPP_RESUME_PRINTER;
80 else
81 {
fa73b229 82 _cupsLangPrintf(stderr, _("%s: Don't know what to do!\n"),
ef416fc2 83 command);
84 return (1);
85 }
86
87 http = NULL;
88 reason = NULL;
89
90 /*
91 * Process command-line arguments...
92 */
93
94 for (i = 1; i < argc; i ++)
95 if (argv[i][0] == '-')
96 switch (argv[i][1])
97 {
98 case 'E' : /* Encrypt */
99#ifdef HAVE_SSL
100 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
101
102 if (http)
103 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
104#else
fa73b229 105 _cupsLangPrintf(stderr,
ef416fc2 106 _("%s: Sorry, no encryption support compiled in!\n"),
107 command);
108#endif /* HAVE_SSL */
109 break;
110
fa73b229 111 case 'U' : /* Username */
112 if (argv[i][2] != '\0')
113 cupsSetUser(argv[i] + 2);
114 else
115 {
116 i ++;
117 if (i >= argc)
118 {
119 _cupsLangPrintf(stderr,
120 _("%s: Error - expected username after "
121 "\'-U\' option!\n"),
122 command);
123 return (1);
124 }
125
126 cupsSetUser(argv[i]);
127 }
128 break;
129
ef416fc2 130 case 'c' : /* Cancel jobs */
131 cancel = 1;
132 break;
133
134 case 'h' : /* Connect to host */
135 if (http != NULL)
136 httpClose(http);
137
138 if (argv[i][2] != '\0')
139 cupsSetServer(argv[i] + 2);
140 else
141 {
142 i ++;
143 if (i >= argc)
144 {
fa73b229 145 _cupsLangPrintf(stderr,
146 _("%s: Error - expected hostname after "
147 "\'-h\' option!\n"),
ef416fc2 148 command);
149 return (1);
150 }
151
152 cupsSetServer(argv[i]);
153 }
154 break;
155
156 case 'r' : /* Reason for cancellation */
157 if (argv[i][2] != '\0')
158 reason = argv[i] + 2;
159 else
160 {
161 i ++;
162 if (i >= argc)
163 {
fa73b229 164 _cupsLangPrintf(stderr,
165 _("%s: Error - expected reason text after "
166 "\'-r\' option!\n"),
ef416fc2 167 command);
168 return (1);
169 }
170
171 reason = argv[i];
172 }
173 break;
174
175 default :
fa73b229 176 _cupsLangPrintf(stderr, _("%s: Error - unknown option \'%c\'!\n"),
ef416fc2 177 command, argv[i][1]);
178 return (1);
179 }
180 else
181 {
182 /*
183 * Accept/disable/enable/reject a destination...
184 */
185
186 if (http == NULL)
187 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
188
189 if (http == NULL)
190 {
fa73b229 191 _cupsLangPrintf(stderr,
ef416fc2 192 _("%s: Unable to connect to server: %s\n"),
193 command, strerror(errno));
194 return (1);
195 }
196
197 /*
198 * Build an IPP request, which requires the following
199 * attributes:
200 *
201 * attributes-charset
202 * attributes-natural-language
203 * printer-uri
204 * printer-state-message [optional]
205 */
206
fa73b229 207 request = ippNewRequest(op);
ef416fc2 208
a4d04587 209 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
210 "localhost", 0, "/printers/%s", argv[i]);
ef416fc2 211 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
212 "printer-uri", NULL, uri);
213
fa73b229 214 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
215 "requesting-user-name", NULL, cupsUser());
216
ef416fc2 217 if (reason != NULL)
218 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT,
219 "printer-state-message", NULL, reason);
220
221 /*
222 * Do the request and get back a response...
223 */
224
225 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL)
226 {
227 if (response->request.status.status_code > IPP_OK_CONFLICT)
228 {
fa73b229 229 _cupsLangPrintf(stderr,
ef416fc2 230 _("%s: Operation failed: %s\n"),
231 command, ippErrorString(cupsLastError()));
232 return (1);
233 }
234
235 ippDelete(response);
236 }
237 else
238 {
fa73b229 239 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
ef416fc2 240 return (1);
241 }
242
243 /*
244 * Cancel all jobs if requested...
245 */
246
247 if (cancel)
248 {
249 /*
250 * Build an IPP_PURGE_JOBS request, which requires the following
251 * attributes:
252 *
253 * attributes-charset
254 * attributes-natural-language
255 * printer-uri
256 */
257
fa73b229 258 request = ippNewRequest(IPP_PURGE_JOBS);
ef416fc2 259
260 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
261 "printer-uri", NULL, uri);
262
263 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL)
264 {
265 if (response->request.status.status_code > IPP_OK_CONFLICT)
266 {
fa73b229 267 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
ef416fc2 268 return (1);
269 }
270
271 ippDelete(response);
272 }
273 else
274 {
fa73b229 275 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
ef416fc2 276 return (1);
277 }
278 }
279 }
280
281 if (http != NULL)
282 httpClose(http);
283
284 return (0);
285}
286
287
288/*
f7faf1f5 289 * End of "$Id: accept.c 5104 2006-02-15 03:21:04Z mike $".
ef416fc2 290 */