]> git.ipfire.org Git - thirdparty/cups.git/blame - systemv/cupsaccept.c
Merge changes from CUPS 1.5svn-r8849.
[thirdparty/cups.git] / systemv / cupsaccept.c
CommitLineData
ef416fc2 1/*
1f6f3dbc 2 * "$Id$"
ef416fc2 3 *
61cf44e2
MS
4 * "cupsaccept", "cupsdisable", "cupsenable", and "cupsreject" commands for
5 * the Common UNIX Printing System (CUPS).
ef416fc2 6 *
91c84a35 7 * Copyright 2007-2008 by Apple Inc.
ef416fc2 8 * Copyright 1997-2006 by Easy Software Products.
9 *
10 * These coded instructions, statements, and computer programs are the
bc44d920 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/".
ef416fc2 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
37int /* O - Exit status */
38main(int argc, /* I - Number of command-line arguments */
39 char *argv[]) /* I - Command-line arguments */
40{
ef416fc2 41 int i; /* Looping var */
42 char *command, /* Command to do */
43 uri[1024], /* Printer URI */
44 *reason; /* Reason for reject/disable */
45 ipp_t *request; /* IPP request */
ef416fc2 46 ipp_op_t op; /* Operation */
ef416fc2 47 int cancel; /* Cancel jobs? */
48
49
07725fee 50 _cupsSetLocale(argv);
d09495fa 51
ef416fc2 52 /*
53 * See what operation we're supposed to do...
54 */
55
56 if ((command = strrchr(argv[0], '/')) != NULL)
57 command ++;
58 else
59 command = argv[0];
60
fa73b229 61 cancel = 0;
ef416fc2 62
49d87452 63 if (!strcmp(command, "cupsaccept") || !strcmp(command, "accept"))
ef416fc2 64 op = CUPS_ACCEPT_JOBS;
49d87452 65 else if (!strcmp(command, "cupsreject") || !strcmp(command, "reject"))
ef416fc2 66 op = CUPS_REJECT_JOBS;
bd7854cb 67 else if (!strcmp(command, "cupsdisable") || !strcmp(command, "disable"))
ef416fc2 68 op = IPP_PAUSE_PRINTER;
bd7854cb 69 else if (!strcmp(command, "cupsenable") || !strcmp(command, "enable"))
ef416fc2 70 op = IPP_RESUME_PRINTER;
71 else
72 {
4d301e69 73 _cupsLangPrintf(stderr, _("%s: Don't know what to do\n"),
ef416fc2 74 command);
75 return (1);
76 }
77
ef416fc2 78 reason = NULL;
79
80 /*
81 * Process command-line arguments...
82 */
83
84 for (i = 1; i < argc; i ++)
85 if (argv[i][0] == '-')
61cf44e2 86 {
ef416fc2 87 switch (argv[i][1])
88 {
89 case 'E' : /* Encrypt */
90#ifdef HAVE_SSL
91 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
ef416fc2 92#else
fa73b229 93 _cupsLangPrintf(stderr,
4d301e69 94 _("%s: Sorry, no encryption support compiled in\n"),
ef416fc2 95 command);
96#endif /* HAVE_SSL */
97 break;
98
fa73b229 99 case 'U' : /* Username */
100 if (argv[i][2] != '\0')
101 cupsSetUser(argv[i] + 2);
102 else
103 {
104 i ++;
105 if (i >= argc)
106 {
107 _cupsLangPrintf(stderr,
108 _("%s: Error - expected username after "
4d301e69 109 "\'-U\' option\n"),
fa73b229 110 command);
111 return (1);
112 }
113
114 cupsSetUser(argv[i]);
115 }
116 break;
117
ef416fc2 118 case 'c' : /* Cancel jobs */
119 cancel = 1;
120 break;
121
122 case 'h' : /* Connect to host */
ef416fc2 123 if (argv[i][2] != '\0')
124 cupsSetServer(argv[i] + 2);
125 else
126 {
127 i ++;
128 if (i >= argc)
129 {
fa73b229 130 _cupsLangPrintf(stderr,
131 _("%s: Error - expected hostname after "
4d301e69 132 "\'-h\' option\n"),
ef416fc2 133 command);
134 return (1);
135 }
136
137 cupsSetServer(argv[i]);
138 }
139 break;
140
141 case 'r' : /* Reason for cancellation */
142 if (argv[i][2] != '\0')
143 reason = argv[i] + 2;
144 else
145 {
146 i ++;
147 if (i >= argc)
148 {
fa73b229 149 _cupsLangPrintf(stderr,
150 _("%s: Error - expected reason text after "
4d301e69 151 "\'-r\' option\n"),
ef416fc2 152 command);
153 return (1);
154 }
155
156 reason = argv[i];
157 }
158 break;
159
61cf44e2
MS
160 case '-' :
161 if (!strcmp(argv[i], "--hold"))
162 op = IPP_HOLD_NEW_JOBS;
163 else if (!strcmp(argv[i], "--release"))
164 op = IPP_RELEASE_HELD_NEW_JOBS;
165 else
166 {
4d301e69 167 _cupsLangPrintf(stderr, _("%s: Error - unknown option \'%s\'\n"),
61cf44e2
MS
168 command, argv[i]);
169 return (1);
170 }
171 break;
172
ef416fc2 173 default :
4d301e69 174 _cupsLangPrintf(stderr, _("%s: Error - unknown option \'%c\'\n"),
ef416fc2 175 command, argv[i][1]);
176 return (1);
177 }
61cf44e2 178 }
ef416fc2 179 else
180 {
181 /*
182 * Accept/disable/enable/reject a destination...
183 */
184
fa73b229 185 request = ippNewRequest(op);
ef416fc2 186
a4d04587 187 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
188 "localhost", 0, "/printers/%s", argv[i]);
ef416fc2 189 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
190 "printer-uri", NULL, uri);
191
fa73b229 192 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
193 "requesting-user-name", NULL, cupsUser());
194
ef416fc2 195 if (reason != NULL)
196 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT,
197 "printer-state-message", NULL, reason);
198
199 /*
200 * Do the request and get back a response...
201 */
202
61cf44e2
MS
203 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/admin/"));
204
205 if (cupsLastError() > IPP_OK_CONFLICT)
ef416fc2 206 {
61cf44e2
MS
207 _cupsLangPrintf(stderr,
208 _("%s: Operation failed: %s\n"),
209 command, ippErrorString(cupsLastError()));
ef416fc2 210 return (1);
211 }
212
213 /*
214 * Cancel all jobs if requested...
215 */
216
217 if (cancel)
218 {
219 /*
220 * Build an IPP_PURGE_JOBS request, which requires the following
221 * attributes:
222 *
223 * attributes-charset
224 * attributes-natural-language
225 * printer-uri
226 */
227
fa73b229 228 request = ippNewRequest(IPP_PURGE_JOBS);
ef416fc2 229
230 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
231 "printer-uri", NULL, uri);
232
61cf44e2 233 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/admin/"));
ef416fc2 234
61cf44e2 235 if (cupsLastError() > IPP_OK_CONFLICT)
ef416fc2 236 {
61cf44e2 237 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
ef416fc2 238 return (1);
239 }
240 }
241 }
242
ef416fc2 243 return (0);
244}
245
246
247/*
1f6f3dbc 248 * End of "$Id$".
ef416fc2 249 */