]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * "cupsaccept", "cupsdisable", "cupsenable", and "cupsreject" commands for | |
3 | * CUPS. | |
4 | * | |
5 | * Copyright 2007-2016 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 | * 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 | *opt, /* Option pointer */ | |
33 | uri[1024], /* Printer URI */ | |
34 | *reason; /* Reason for reject/disable */ | |
35 | ipp_t *request; /* IPP request */ | |
36 | ipp_op_t op; /* Operation */ | |
37 | int cancel; /* Cancel jobs? */ | |
38 | ||
39 | ||
40 | _cupsSetLocale(argv); | |
41 | ||
42 | /* | |
43 | * See what operation we're supposed to do... | |
44 | */ | |
45 | ||
46 | if ((command = strrchr(argv[0], '/')) != NULL) | |
47 | command ++; | |
48 | else | |
49 | command = argv[0]; | |
50 | ||
51 | cancel = 0; | |
52 | ||
53 | if (!strcmp(command, "cupsaccept") || !strcmp(command, "accept")) | |
54 | op = CUPS_ACCEPT_JOBS; | |
55 | else if (!strcmp(command, "cupsreject") || !strcmp(command, "reject")) | |
56 | op = CUPS_REJECT_JOBS; | |
57 | else if (!strcmp(command, "cupsdisable") || !strcmp(command, "disable")) | |
58 | op = IPP_PAUSE_PRINTER; | |
59 | else if (!strcmp(command, "cupsenable") || !strcmp(command, "enable")) | |
60 | op = IPP_RESUME_PRINTER; | |
61 | else | |
62 | { | |
63 | _cupsLangPrintf(stderr, _("%s: Don't know what to do."), command); | |
64 | return (1); | |
65 | } | |
66 | ||
67 | reason = NULL; | |
68 | ||
69 | /* | |
70 | * Process command-line arguments... | |
71 | */ | |
72 | ||
73 | for (i = 1; i < argc; i ++) | |
74 | { | |
75 | if (!strcmp(argv[i], "--hold")) | |
76 | op = IPP_HOLD_NEW_JOBS; | |
77 | else if (!strcmp(argv[i], "--release")) | |
78 | op = IPP_RELEASE_HELD_NEW_JOBS; | |
79 | else if (argv[i][0] == '-') | |
80 | { | |
81 | for (opt = argv[i] + 1; *opt; opt ++) | |
82 | { | |
83 | switch (*opt) | |
84 | { | |
85 | case 'E' : /* Encrypt */ | |
86 | #ifdef HAVE_SSL | |
87 | cupsSetEncryption(HTTP_ENCRYPT_REQUIRED); | |
88 | #else | |
89 | _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support."), command); | |
90 | #endif /* HAVE_SSL */ | |
91 | break; | |
92 | ||
93 | case 'U' : /* Username */ | |
94 | if (opt[1] != '\0') | |
95 | { | |
96 | cupsSetUser(opt + 1); | |
97 | opt += strlen(opt) - 1; | |
98 | } | |
99 | else | |
100 | { | |
101 | i ++; | |
102 | if (i >= argc) | |
103 | { | |
104 | _cupsLangPrintf(stderr, _("%s: Error - expected username after \"-U\" option."), command); | |
105 | return (1); | |
106 | } | |
107 | ||
108 | cupsSetUser(argv[i]); | |
109 | } | |
110 | break; | |
111 | ||
112 | case 'c' : /* Cancel jobs */ | |
113 | cancel = 1; | |
114 | break; | |
115 | ||
116 | case 'h' : /* Connect to host */ | |
117 | if (opt[1] != '\0') | |
118 | { | |
119 | cupsSetServer(opt + 1); | |
120 | opt += strlen(opt) - 1; | |
121 | } | |
122 | else | |
123 | { | |
124 | i ++; | |
125 | if (i >= argc) | |
126 | { | |
127 | _cupsLangPrintf(stderr, _("%s: Error - expected hostname after \"-h\" option."), command); | |
128 | return (1); | |
129 | } | |
130 | ||
131 | cupsSetServer(argv[i]); | |
132 | } | |
133 | break; | |
134 | ||
135 | case 'r' : /* Reason for cancellation */ | |
136 | if (opt[1] != '\0') | |
137 | { | |
138 | reason = opt + 1; | |
139 | opt += strlen(opt) - 1; | |
140 | } | |
141 | else | |
142 | { | |
143 | i ++; | |
144 | if (i >= argc) | |
145 | { | |
146 | _cupsLangPrintf(stderr, _("%s: Error - expected reason text after \"-r\" option."), command); | |
147 | return (1); | |
148 | } | |
149 | ||
150 | reason = argv[i]; | |
151 | } | |
152 | break; | |
153 | ||
154 | default : | |
155 | _cupsLangPrintf(stderr, _("%s: Error - unknown option \"%c\"."), command, *opt); | |
156 | return (1); | |
157 | } | |
158 | } | |
159 | } | |
160 | else | |
161 | { | |
162 | /* | |
163 | * Accept/disable/enable/reject a destination... | |
164 | */ | |
165 | ||
166 | request = ippNewRequest(op); | |
167 | ||
168 | httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, | |
169 | "localhost", 0, "/printers/%s", argv[i]); | |
170 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
171 | "printer-uri", NULL, uri); | |
172 | ||
173 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, | |
174 | "requesting-user-name", NULL, cupsUser()); | |
175 | ||
176 | if (reason != NULL) | |
177 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT, | |
178 | "printer-state-message", NULL, reason); | |
179 | ||
180 | /* | |
181 | * Do the request and get back a response... | |
182 | */ | |
183 | ||
184 | ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/admin/")); | |
185 | ||
186 | if (cupsLastError() > IPP_OK_CONFLICT) | |
187 | { | |
188 | _cupsLangPrintf(stderr, | |
189 | _("%s: Operation failed: %s"), | |
190 | command, ippErrorString(cupsLastError())); | |
191 | return (1); | |
192 | } | |
193 | ||
194 | /* | |
195 | * Cancel all jobs if requested... | |
196 | */ | |
197 | ||
198 | if (cancel) | |
199 | { | |
200 | /* | |
201 | * Build an IPP_PURGE_JOBS request, which requires the following | |
202 | * attributes: | |
203 | * | |
204 | * attributes-charset | |
205 | * attributes-natural-language | |
206 | * printer-uri | |
207 | */ | |
208 | ||
209 | request = ippNewRequest(IPP_PURGE_JOBS); | |
210 | ||
211 | ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, | |
212 | "printer-uri", NULL, uri); | |
213 | ||
214 | ippDelete(cupsDoRequest(CUPS_HTTP_DEFAULT, request, "/admin/")); | |
215 | ||
216 | if (cupsLastError() > IPP_OK_CONFLICT) | |
217 | { | |
218 | _cupsLangPrintf(stderr, "%s: %s", command, cupsLastErrorString()); | |
219 | return (1); | |
220 | } | |
221 | } | |
222 | } | |
223 | } | |
224 | ||
225 | return (0); | |
226 | } |