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