]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/cupsaccept.c
Merge pull request #4792 from OdyX/fix-spelling-error-in-ipp-var
[thirdparty/cups.git] / systemv / cupsaccept.c
1 /*
2 * "cupsaccept", "cupsdisable", "cupsenable", and "cupsreject" commands for
3 * CUPS.
4 *
5 * Copyright 2007-2010 by Apple Inc.
6 * Copyright 1997-2006 by Easy Software Products.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Apple Inc. and are protected by Federal copyright
10 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
11 * which should have been included with this file. If this file is
12 * file is missing or damaged, see the license at "http://www.cups.org/".
13 */
14
15 /*
16 * Include necessary headers...
17 */
18
19 #include <cups/cups-private.h>
20
21
22 /*
23 * 'main()' - Parse options and accept/reject jobs or disable/enable printers.
24 */
25
26 int /* O - Exit status */
27 main(int argc, /* I - Number of command-line arguments */
28 char *argv[]) /* I - Command-line arguments */
29 {
30 int i; /* Looping var */
31 char *command, /* Command to do */
32 uri[1024], /* Printer URI */
33 *reason; /* Reason for reject/disable */
34 ipp_t *request; /* IPP request */
35 ipp_op_t op; /* Operation */
36 int cancel; /* Cancel jobs? */
37
38
39 _cupsSetLocale(argv);
40
41 /*
42 * See what operation we're supposed to do...
43 */
44
45 if ((command = strrchr(argv[0], '/')) != NULL)
46 command ++;
47 else
48 command = argv[0];
49
50 cancel = 0;
51
52 if (!strcmp(command, "cupsaccept") || !strcmp(command, "accept"))
53 op = CUPS_ACCEPT_JOBS;
54 else if (!strcmp(command, "cupsreject") || !strcmp(command, "reject"))
55 op = CUPS_REJECT_JOBS;
56 else if (!strcmp(command, "cupsdisable") || !strcmp(command, "disable"))
57 op = IPP_PAUSE_PRINTER;
58 else if (!strcmp(command, "cupsenable") || !strcmp(command, "enable"))
59 op = IPP_RESUME_PRINTER;
60 else
61 {
62 _cupsLangPrintf(stderr, _("%s: Don't know what to do."), command);
63 return (1);
64 }
65
66 reason = NULL;
67
68 /*
69 * Process command-line arguments...
70 */
71
72 for (i = 1; i < argc; i ++)
73 if (argv[i][0] == '-')
74 {
75 switch (argv[i][1])
76 {
77 case 'E' : /* Encrypt */
78 #ifdef HAVE_SSL
79 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
80 #else
81 _cupsLangPrintf(stderr,
82 _("%s: Sorry, no encryption support."), command);
83 #endif /* HAVE_SSL */
84 break;
85
86 case 'U' : /* Username */
87 if (argv[i][2] != '\0')
88 cupsSetUser(argv[i] + 2);
89 else
90 {
91 i ++;
92 if (i >= argc)
93 {
94 _cupsLangPrintf(stderr,
95 _("%s: Error - expected username after "
96 "\"-U\" option."), command);
97 return (1);
98 }
99
100 cupsSetUser(argv[i]);
101 }
102 break;
103
104 case 'c' : /* Cancel jobs */
105 cancel = 1;
106 break;
107
108 case 'h' : /* Connect to host */
109 if (argv[i][2] != '\0')
110 cupsSetServer(argv[i] + 2);
111 else
112 {
113 i ++;
114 if (i >= argc)
115 {
116 _cupsLangPrintf(stderr,
117 _("%s: Error - expected hostname after "
118 "\"-h\" option."), command);
119 return (1);
120 }
121
122 cupsSetServer(argv[i]);
123 }
124 break;
125
126 case 'r' : /* Reason for cancellation */
127 if (argv[i][2] != '\0')
128 reason = argv[i] + 2;
129 else
130 {
131 i ++;
132 if (i >= argc)
133 {
134 _cupsLangPrintf(stderr,
135 _("%s: Error - expected reason text after "
136 "\"-r\" option."), command);
137 return (1);
138 }
139
140 reason = argv[i];
141 }
142 break;
143
144 case '-' :
145 if (!strcmp(argv[i], "--hold"))
146 op = IPP_HOLD_NEW_JOBS;
147 else if (!strcmp(argv[i], "--release"))
148 op = IPP_RELEASE_HELD_NEW_JOBS;
149 else
150 {
151 _cupsLangPrintf(stderr, _("%s: Error - unknown option \"%s\"."),
152 command, argv[i]);
153 return (1);
154 }
155 break;
156
157 default :
158 _cupsLangPrintf(stderr, _("%s: Error - unknown option \"%c\"."),
159 command, argv[i][1]);
160 return (1);
161 }
162 }
163 else
164 {
165 /*
166 * Accept/disable/enable/reject a destination...
167 */
168
169 request = ippNewRequest(op);
170
171 httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL,
172 "localhost", 0, "/printers/%s", argv[i]);
173 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
174 "printer-uri", NULL, uri);
175
176 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
177 "requesting-user-name", NULL, cupsUser());
178
179 if (reason != NULL)
180 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT,
181 "printer-state-message", NULL, reason);
182
183 /*
184 * Do the request and get back a response...
185 */
186
187 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/admin/"));
188
189 if (cupsLastError() > IPP_OK_CONFLICT)
190 {
191 _cupsLangPrintf(stderr,
192 _("%s: Operation failed: %s"),
193 command, ippErrorString(cupsLastError()));
194 return (1);
195 }
196
197 /*
198 * Cancel all jobs if requested...
199 */
200
201 if (cancel)
202 {
203 /*
204 * Build an IPP_PURGE_JOBS request, which requires the following
205 * attributes:
206 *
207 * attributes-charset
208 * attributes-natural-language
209 * printer-uri
210 */
211
212 request = ippNewRequest(IPP_PURGE_JOBS);
213
214 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
215 "printer-uri", NULL, uri);
216
217 ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/admin/"));
218
219 if (cupsLastError() > IPP_OK_CONFLICT)
220 {
221 _cupsLangPrintf(stderr, "%s: %s", command, cupsLastErrorString());
222 return (1);
223 }
224 }
225 }
226
227 return (0);
228 }