]> git.ipfire.org Git - thirdparty/cups.git/blame - systemv/accept.c
Merge changes from 1.1.x into 1.2 devel.
[thirdparty/cups.git] / systemv / accept.c
CommitLineData
b310a06a 1/*
753453e4 2 * "$Id: accept.c,v 1.11.2.2 2001/12/26 16:52:56 mike Exp $"
b310a06a 3 *
4 * "accept", "disable", "enable", and "reject" commands for the Common
5 * UNIX Printing System (CUPS).
6 *
d2935a0f 7 * Copyright 1997-2001 by Easy Software Products.
b310a06a 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
8784b6a6 18 * 44141 Airport View Drive, Suite 204
b310a06a 19 * Hollywood, Maryland 20636-3111 USA
20 *
21 * Voice: (301) 373-9603
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 <ctype.h>
37
1c9e0181 38#include <config.h>
b310a06a 39#include <cups/cups.h>
40#include <cups/language.h>
41
42
43/*
44 * 'main()' - Parse options and accept/reject jobs or disable/enable printers.
45 */
46
47int /* O - Exit status */
48main(int argc, /* I - Number of command-line arguments */
49 char *argv[]) /* I - Command-line arguments */
50{
51 http_t *http; /* HTTP connection to server */
52 int i; /* Looping var */
53 char *command, /* Command to do */
54 hostname[HTTP_MAX_URI],
55 /* Name of host */
56 printer[HTTP_MAX_URI],
57 /* Name of printer or class */
58 uri[1024], /* Printer URI */
59 *reason; /* Reason for reject/disable */
753453e4 60 const char *server; /* Server name */
b310a06a 61 ipp_t *request; /* IPP request */
62 ipp_t *response; /* IPP response */
63 ipp_op_t op; /* Operation */
64 cups_lang_t *language; /* Language */
2a2fbe49 65 int cancel; /* Cancel jobs? */
1c9e0181 66 http_encryption_t encryption; /* Encryption? */
b310a06a 67
68
69 /*
70 * See what operation we're supposed to do...
71 */
72
73 if ((command = strrchr(argv[0], '/')) != NULL)
74 command ++;
75 else
76 command = argv[0];
77
2a2fbe49 78 cancel = 0;
79
b310a06a 80 if (strcmp(command, "accept") == 0)
81 op = CUPS_ACCEPT_JOBS;
82 else if (strcmp(command, "reject") == 0)
83 op = CUPS_REJECT_JOBS;
84 else if (strcmp(command, "disable") == 0)
85 op = IPP_PAUSE_PRINTER;
86 else if (strcmp(command, "enable") == 0)
87 op = IPP_RESUME_PRINTER;
88 else
89 {
90 fprintf(stderr, "%s: Don't know what to do!\n", command);
91 return (1);
92 }
93
753453e4 94 server = cupsServer();
1c9e0181 95 http = NULL;
96 reason = NULL;
97 encryption = cupsEncryption();
b310a06a 98
99 /*
100 * Process command-line arguments...
101 */
102
103 for (i = 1; i < argc; i ++)
104 if (argv[i][0] == '-')
105 switch (argv[i][1])
106 {
1c9e0181 107 case 'E' : /* Encrypt */
108#ifdef HAVE_LIBSSL
109 encryption = HTTP_ENCRYPT_REQUIRED;
110
111 if (http)
112 httpEncryption(http, encryption);
113#else
114 fprintf(stderr, "%s: Sorry, no encryption support compiled in!\n",
115 argv[0]);
116#endif /* HAVE_LIBSSL */
117 break;
118
2a2fbe49 119 case 'c' : /* Cancel jobs */
120 cancel = 1;
121 break;
122
808dd474 123 case 'h' : /* Connect to host */
124 if (http != NULL)
125 httpClose(http);
126
127 if (argv[i][2] != '\0')
753453e4 128 server = argv[i] + 2;
808dd474 129 else
130 {
131 i ++;
db8c0a8c 132 if (i >= argc)
133 {
134 fprintf(stderr, "%s: Expected server name after -h!\n",
135 command);
136 return (1);
137 }
138
753453e4 139 server = argv[i];
808dd474 140 }
141
753453e4 142 http = httpConnectEncrypt(server, ippPort(), encryption);
143
808dd474 144 if (http == NULL)
145 {
146 fputs(argv[0], stderr);
147 perror(": Unable to connect to server");
148 return (1);
149 }
150 break;
151
b310a06a 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 fprintf(stderr, "%s: Expected reason text after -r!\n", command);
161 return (1);
162 }
163
164 reason = argv[i];
165 }
166 break;
167
168 default :
169 fprintf(stderr, "%s: Unknown option \'%c\'!\n", command,
170 argv[i][1]);
171 return (1);
172 }
173 else
174 {
175 /*
176 * Accept/disable/enable/reject a destination...
177 */
178
970017a4 179 if (sscanf(argv[i], "%1023[^@]@%1023s", printer, hostname) == 1)
753453e4 180 {
181 strncpy(hostname, server, sizeof(hostname) - 1);
182 hostname[sizeof(hostname) - 1] = '\0';
183 }
b310a06a 184
185 if (http != NULL && strcasecmp(http->hostname, hostname) != 0)
186 {
187 httpClose(http);
188 http = NULL;
189 }
190
191 if (http == NULL)
b5cb0608 192 http = httpConnectEncrypt(hostname, ippPort(), encryption);
b310a06a 193
194 if (http == NULL)
195 {
96ecd6f0 196 fputs(argv[0], stderr);
197 perror(": Unable to connect to server");
b310a06a 198 return (1);
199 }
200
201 /*
202 * Build an IPP request, which requires the following
203 * attributes:
204 *
205 * attributes-charset
206 * attributes-natural-language
207 * printer-uri
208 * printer-state-message [optional]
209 */
210
211 request = ippNew();
212
213 request->request.op.operation_id = op;
214 request->request.op.request_id = 1;
215
216 language = cupsLangDefault();
217
218 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
219 "attributes-charset", NULL, cupsLangEncoding(language));
220
221 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
222 "attributes-natural-language", NULL, language->language);
223
970017a4 224 snprintf(uri, sizeof(uri), "ipp://%s:%d/printers/%s", hostname, ippPort(), printer);
b310a06a 225 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
226 "printer-uri", NULL, uri);
227
228 if (reason != NULL)
229 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT,
230 "printer-state-message", NULL, reason);
231
232 /*
233 * Do the request and get back a response...
234 */
235
236 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL)
96ecd6f0 237 {
238 if (response->request.status.status_code > IPP_OK_CONFLICT)
239 {
240 fprintf(stderr, "%s: Operation failed: %s\n", command,
241 ippErrorString(cupsLastError()));
242 return (1);
243 }
244
b310a06a 245 ippDelete(response);
96ecd6f0 246 }
b310a06a 247 else
248 {
96ecd6f0 249 fprintf(stderr, "%s: Operation failed: %s\n", command,
250 ippErrorString(cupsLastError()));
b310a06a 251 return (1);
252 }
2a2fbe49 253
254 /*
255 * Cancel all jobs if requested...
256 */
257
258 if (cancel)
259 {
260 /*
261 * Build an IPP_PURGE_JOBS request, which requires the following
262 * attributes:
263 *
264 * attributes-charset
265 * attributes-natural-language
266 * printer-uri
267 */
268
269 request = ippNew();
270
271 request->request.op.operation_id = IPP_PURGE_JOBS;
272 request->request.op.request_id = 1;
273
274 language = cupsLangDefault();
275
276 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
277 "attributes-charset", NULL, cupsLangEncoding(language));
278
279 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
280 "attributes-natural-language", NULL, language->language);
281
282 snprintf(uri, sizeof(uri), "ipp://%s:%d/printers/%s", hostname, ippPort(), printer);
283 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
284 "printer-uri", NULL, uri);
285
286 if ((response = cupsDoRequest(http, request, "/admin/")) != NULL)
96ecd6f0 287 {
288 if (response->request.status.status_code > IPP_OK_CONFLICT)
289 {
290 fprintf(stderr, "%s: Operation failed: %s\n", command,
291 ippErrorString(cupsLastError()));
292 return (1);
293 }
294
2a2fbe49 295 ippDelete(response);
96ecd6f0 296 }
297 else
298 {
299 fprintf(stderr, "%s: Operation failed: %s\n", command,
300 ippErrorString(cupsLastError()));
301 return (1);
302 }
2a2fbe49 303 }
b310a06a 304 }
305
306 if (http != NULL)
307 httpClose(http);
308
309 return (0);
310}
311
312
313/*
753453e4 314 * End of "$Id: accept.c,v 1.11.2.2 2001/12/26 16:52:56 mike Exp $".
b310a06a 315 */