]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/accept.c
Import CUPS 1.4svn-r7226.
[thirdparty/cups.git] / systemv / accept.c
1 /*
2 * "$Id: accept.c 6649 2007-07-11 21:46:42Z mike $"
3 *
4 * "accept", "disable", "enable", and "reject" commands for the Common
5 * UNIX Printing System (CUPS).
6 *
7 * Copyright 2007-2008 by Apple Inc.
8 * Copyright 1997-2006 by Easy Software Products.
9 *
10 * These coded instructions, statements, and computer programs are the
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/".
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
37 int /* O - Exit status */
38 main(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 */
49 int cancel; /* Cancel jobs? */
50
51
52 _cupsSetLocale(argv);
53
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
63 cancel = 0;
64
65 if (!strcmp(command, "accept"))
66 op = CUPS_ACCEPT_JOBS;
67 else if (!strcmp(command, "reject"))
68 op = CUPS_REJECT_JOBS;
69 else if (!strcmp(command, "cupsdisable") || !strcmp(command, "disable"))
70 op = IPP_PAUSE_PRINTER;
71 else if (!strcmp(command, "cupsenable") || !strcmp(command, "enable"))
72 op = IPP_RESUME_PRINTER;
73 else
74 {
75 _cupsLangPrintf(stderr, _("%s: Don't know what to do!\n"),
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
98 _cupsLangPrintf(stderr,
99 _("%s: Sorry, no encryption support compiled in!\n"),
100 command);
101 #endif /* HAVE_SSL */
102 break;
103
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
123 case 'c' : /* Cancel jobs */
124 cancel = 1;
125 break;
126
127 case 'h' : /* Connect to host */
128 if (http)
129 {
130 httpClose(http);
131 http = NULL;
132 }
133
134 if (argv[i][2] != '\0')
135 cupsSetServer(argv[i] + 2);
136 else
137 {
138 i ++;
139 if (i >= argc)
140 {
141 _cupsLangPrintf(stderr,
142 _("%s: Error - expected hostname after "
143 "\'-h\' option!\n"),
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 {
160 _cupsLangPrintf(stderr,
161 _("%s: Error - expected reason text after "
162 "\'-r\' option!\n"),
163 command);
164 return (1);
165 }
166
167 reason = argv[i];
168 }
169 break;
170
171 default :
172 _cupsLangPrintf(stderr, _("%s: Error - unknown option \'%c\'!\n"),
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 {
187 _cupsLangPrintf(stderr,
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
203 request = ippNewRequest(op);
204
205 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
206 "localhost", 0, "/printers/%s", argv[i]);
207 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
208 "printer-uri", NULL, uri);
209
210 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
211 "requesting-user-name", NULL, cupsUser());
212
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 {
225 _cupsLangPrintf(stderr,
226 _("%s: Operation failed: %s\n"),
227 command, ippErrorString(cupsLastError()));
228 return (1);
229 }
230
231 ippDelete(response);
232 }
233 else
234 {
235 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
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
254 request = ippNewRequest(IPP_PURGE_JOBS);
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 {
263 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
264 return (1);
265 }
266
267 ippDelete(response);
268 }
269 else
270 {
271 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
272 return (1);
273 }
274 }
275 }
276
277 if (http != NULL)
278 httpClose(http);
279
280 return (0);
281 }
282
283
284 /*
285 * End of "$Id: accept.c 6649 2007-07-11 21:46:42Z mike $".
286 */