]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cupsaccept.c
Merge changes from CUPS 1.5svn-r8849.
[thirdparty/cups.git] / systemv / cupsaccept.c
1 /*
2 * "$Id$"
3 *
4 * "cupsaccept", "cupsdisable", "cupsenable", and "cupsreject" commands for
5 * the Common 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 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 */
46 ipp_op_t op; /* Operation */
47 int cancel; /* Cancel jobs? */
48
49
50 _cupsSetLocale(argv);
51
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
61 cancel = 0;
62
63 if (!strcmp(command, "cupsaccept") || !strcmp(command, "accept"))
64 op = CUPS_ACCEPT_JOBS;
65 else if (!strcmp(command, "cupsreject") || !strcmp(command, "reject"))
66 op = CUPS_REJECT_JOBS;
67 else if (!strcmp(command, "cupsdisable") || !strcmp(command, "disable"))
68 op = IPP_PAUSE_PRINTER;
69 else if (!strcmp(command, "cupsenable") || !strcmp(command, "enable"))
70 op = IPP_RESUME_PRINTER;
71 else
72 {
73 _cupsLangPrintf(stderr, _("%s: Don't know what to do\n"),
74 command);
75 return (1);
76 }
77
78 reason = NULL;
79
80 /*
81 * Process command-line arguments...
82 */
83
84 for (i = 1; i < argc; i ++)
85 if (argv[i][0] == '-')
86 {
87 switch (argv[i][1])
88 {
89 case 'E' : /* Encrypt */
90 #ifdef HAVE_SSL
91 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
92 #else
93 _cupsLangPrintf(stderr,
94 _("%s: Sorry, no encryption support compiled in\n"),
95 command);
96 #endif /* HAVE_SSL */
97 break;
98
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 "
109 "\'-U\' option\n"),
110 command);
111 return (1);
112 }
113
114 cupsSetUser(argv[i]);
115 }
116 break;
117
118 case 'c' : /* Cancel jobs */
119 cancel = 1;
120 break;
121
122 case 'h' : /* Connect to host */
123 if (argv[i][2] != '\0')
124 cupsSetServer(argv[i] + 2);
125 else
126 {
127 i ++;
128 if (i >= argc)
129 {
130 _cupsLangPrintf(stderr,
131 _("%s: Error - expected hostname after "
132 "\'-h\' option\n"),
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 {
149 _cupsLangPrintf(stderr,
150 _("%s: Error - expected reason text after "
151 "\'-r\' option\n"),
152 command);
153 return (1);
154 }
155
156 reason = argv[i];
157 }
158 break;
159
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 {
167 _cupsLangPrintf(stderr, _("%s: Error - unknown option \'%s\'\n"),
168 command, argv[i]);
169 return (1);
170 }
171 break;
172
173 default :
174 _cupsLangPrintf(stderr, _("%s: Error - unknown option \'%c\'\n"),
175 command, argv[i][1]);
176 return (1);
177 }
178 }
179 else
180 {
181 /*
182 * Accept/disable/enable/reject a destination...
183 */
184
185 request = ippNewRequest(op);
186
187 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
188 "localhost", 0, "/printers/%s", argv[i]);
189 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
190 "printer-uri", NULL, uri);
191
192 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
193 "requesting-user-name", NULL, cupsUser());
194
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
203 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/admin/"));
204
205 if (cupsLastError() > IPP_OK_CONFLICT)
206 {
207 _cupsLangPrintf(stderr,
208 _("%s: Operation failed: %s\n"),
209 command, ippErrorString(cupsLastError()));
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
228 request = ippNewRequest(IPP_PURGE_JOBS);
229
230 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
231 "printer-uri", NULL, uri);
232
233 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/admin/"));
234
235 if (cupsLastError() > IPP_OK_CONFLICT)
236 {
237 _cupsLangPrintf(stderr, "%s: %s\n", command, cupsLastErrorString());
238 return (1);
239 }
240 }
241 }
242
243 return (0);
244 }
245
246
247 /*
248 * End of "$Id$".
249 */