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