]> 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/*
2 * "$Id: accept.c 4906 2006-01-10 20:53:28Z mike $"
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 */
58 cups_lang_t *language; /* Language */
59 int cancel; /* Cancel jobs? */
60
61
62 /*
63 * See what operation we're supposed to do...
64 */
65
66 if ((command = strrchr(argv[0], '/')) != NULL)
67 command ++;
68 else
69 command = argv[0];
70
71 cancel = 0;
72 language = cupsLangDefault();
73
74 if (!strcmp(command, "accept"))
75 op = CUPS_ACCEPT_JOBS;
76 else if (!strcmp(command, "reject"))
77 op = CUPS_REJECT_JOBS;
78 else if (!strcmp(command, "disable"))
79 op = IPP_PAUSE_PRINTER;
80 else if (!strcmp(command, "enable"))
81 op = IPP_RESUME_PRINTER;
82 else
83 {
84 _cupsLangPrintf(stderr, language, _("%s: Don't know what to do!\n"),
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
107 _cupsLangPrintf(stderr, language,
108 _("%s: Sorry, no encryption support compiled in!\n"),
109 command);
110#endif /* HAVE_SSL */
111 break;
112
113 case 'c' : /* Cancel jobs */
114 cancel = 1;
115 break;
116
117 case 'h' : /* Connect to host */
118 if (http != NULL)
119 httpClose(http);
120
121 if (argv[i][2] != '\0')
122 cupsSetServer(argv[i] + 2);
123 else
124 {
125 i ++;
126 if (i >= argc)
127 {
128 _cupsLangPrintf(stderr, language,
129 _("%s: Expected server name after -h!\n"),
130 command);
131 return (1);
132 }
133
134 cupsSetServer(argv[i]);
135 }
136 break;
137
138 case 'r' : /* Reason for cancellation */
139 if (argv[i][2] != '\0')
140 reason = argv[i] + 2;
141 else
142 {
143 i ++;
144 if (i >= argc)
145 {
146 _cupsLangPrintf(stderr, language,
147 _("%s: Expected reason text after -r!\n"),
148 command);
149 return (1);
150 }
151
152 reason = argv[i];
153 }
154 break;
155
156 default :
157 _cupsLangPrintf(stderr, language, _("%s: Unknown option \'%c\'!\n"),
158 command, argv[i][1]);
159 return (1);
160 }
161 else
162 {
163 /*
164 * Accept/disable/enable/reject a destination...
165 */
166
167 if (http == NULL)
168 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
169
170 if (http == NULL)
171 {
172 _cupsLangPrintf(stderr, language,
173 _("%s: Unable to connect to server: %s\n"),
174 command, strerror(errno));
175 return (1);
176 }
177
178 /*
179 * Build an IPP request, which requires the following
180 * attributes:
181 *
182 * attributes-charset
183 * attributes-natural-language
184 * printer-uri
185 * printer-state-message [optional]
186 */
187
188 request = ippNew();
189
190 request->request.op.operation_id = op;
191 request->request.op.request_id = 1;
192
193 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
194 "attributes-charset", NULL, cupsLangEncoding(language));
195
196 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
197 "attributes-natural-language", NULL, language->language);
198
199 httpAssembleURIf(uri, sizeof(uri), "ipp", NULL, "localhost", 0,
200 "/printers/%s", argv[i]);
201 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
202 "printer-uri", NULL, uri);
203
204 if (reason != NULL)
205 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT,
206 "printer-state-message", NULL, reason);
207
208 /*
209 * Do the request and get back a response...
210 */
211
212 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL)
213 {
214 if (response->request.status.status_code > IPP_OK_CONFLICT)
215 {
216 _cupsLangPrintf(stderr, language,
217 _("%s: Operation failed: %s\n"),
218 command, ippErrorString(cupsLastError()));
219 return (1);
220 }
221
222 ippDelete(response);
223 }
224 else
225 {
226 _cupsLangPrintf(stderr, language,
227 _("%s: Operation failed: %s\n"),
228 command, ippErrorString(cupsLastError()));
229 return (1);
230 }
231
232 /*
233 * Cancel all jobs if requested...
234 */
235
236 if (cancel)
237 {
238 /*
239 * Build an IPP_PURGE_JOBS request, which requires the following
240 * attributes:
241 *
242 * attributes-charset
243 * attributes-natural-language
244 * printer-uri
245 */
246
247 request = ippNew();
248
249 request->request.op.operation_id = IPP_PURGE_JOBS;
250 request->request.op.request_id = 1;
251
252 language = cupsLangDefault();
253
254 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
255 "attributes-charset", NULL, cupsLangEncoding(language));
256
257 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
258 "attributes-natural-language", NULL, language->language);
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 {
267 _cupsLangPrintf(stderr, language,
268 _("%s: Operation failed: %s\n"),
269 command, ippErrorString(cupsLastError()));
270 return (1);
271 }
272
273 ippDelete(response);
274 }
275 else
276 {
277 _cupsLangPrintf(stderr, language,
278 _("%s: Operation failed: %s\n"),
279 command, ippErrorString(cupsLastError()));
280 return (1);
281 }
282 }
283 }
284
285 if (http != NULL)
286 httpClose(http);
287
288 return (0);
289}
290
291
292/*
293 * End of "$Id: accept.c 4906 2006-01-10 20:53:28Z mike $".
294 */