]> git.ipfire.org Git - thirdparty/cups.git/blob - systemv/lpmove.c
89ea5f3b7e6234e29756e50922bf6ff438a75bbb
[thirdparty/cups.git] / systemv / lpmove.c
1 /*
2 * "$Id: lpmove.c,v 1.5.2.6 2002/08/22 01:43:40 mike Exp $"
3 *
4 * "lpmove" command for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1997-2002 by Easy Software Products.
7 *
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636-3111 USA
19 *
20 * Voice: (301) 373-9603
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * Contents:
25 *
26 * main() - Parse options and move jobs.
27 * move_job() - Move a job.
28 */
29
30 /*
31 * Include necessary headers...
32 */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <ctype.h>
37 #include <cups/cups.h>
38 #include <cups/language.h>
39 #include <cups/debug.h>
40 #include <cups/string.h>
41
42
43 /*
44 * Local functions...
45 */
46
47 static void move_job(http_t *, int, const char *);
48
49
50 /*
51 * 'main()' - Parse options and show status information.
52 */
53
54 int
55 main(int argc, /* I - Number of command-line arguments */
56 char *argv[]) /* I - Command-line arguments */
57 {
58 int i; /* Looping var */
59 http_t *http; /* Connection to server */
60 const char *job; /* Job name */
61 int num_dests; /* Number of destinations */
62 cups_dest_t *dests; /* Destinations */
63 const char *dest; /* New destination */
64
65
66 http = NULL;
67 job = NULL;
68 dest = NULL;
69 num_dests = 0;
70 dests = NULL;
71
72 for (i = 1; i < argc; i ++)
73 if (argv[i][0] == '-')
74 switch (argv[i][1])
75 {
76 case 'E' : /* Encrypt */
77 #ifdef HAVE_LIBSSL
78 cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
79
80 if (http)
81 httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
82 #else
83 fprintf(stderr, "%s: Sorry, no encryption support compiled in!\n",
84 argv[0]);
85 #endif /* HAVE_LIBSSL */
86 break;
87
88 case 'h' : /* Connect to host */
89 if (http)
90 {
91 httpClose(http);
92 http = NULL;
93 }
94
95 if (argv[i][2] != '\0')
96 cupsSetServer(argv[i] + 2);
97 else
98 {
99 i ++;
100
101 if (i >= argc)
102 {
103 fputs("Error: need hostname after \'-h\' option!\n", stderr);
104 return (1);
105 }
106
107 cupsSetServer(argv[i]);
108 }
109 break;
110
111 default :
112 fprintf(stderr, "lpmove: Unknown option \'%c\'!\n", argv[i][1]);
113 return (1);
114 }
115 else if (job == NULL)
116 {
117 if (num_dests == 0)
118 num_dests = cupsGetDests(&dests);
119
120 if ((job = strrchr(argv[i], '-')) != NULL &&
121 cupsGetDest(argv[i], NULL, num_dests, dests) == NULL)
122 job ++;
123 else
124 job = argv[i];
125 }
126 else if (dest == NULL)
127 dest = argv[i];
128 else
129 {
130 fprintf(stderr, "lpmove: Unknown argument \'%s\'!\n", argv[i]);
131 return (1);
132 }
133
134 if (job == NULL || dest == NULL)
135 {
136 puts("Usage: lpmove job dest");
137 return (1);
138 }
139
140 if (!http)
141 {
142 http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
143
144 if (http == NULL)
145 {
146 perror("lpmove: Unable to connect to server");
147 return (1);
148 }
149 }
150
151 move_job(http, atoi(job), dest);
152
153 return (0);
154 }
155
156
157 /*
158 * 'move_job()' - Move a job.
159 */
160
161 static void
162 move_job(http_t *http, /* I - HTTP connection to server */
163 int jobid, /* I - Job ID */
164 const char *dest) /* I - Destination */
165 {
166 ipp_t *request, /* IPP Request */
167 *response; /* IPP Response */
168 cups_lang_t *language; /* Default language */
169 char job_uri[HTTP_MAX_URI],
170 /* job-uri */
171 printer_uri[HTTP_MAX_URI];
172 /* job-printer-uri */
173
174
175 if (http == NULL)
176 return;
177
178 /*
179 * Build a CUPS_MOVE_JOB request, which requires the following
180 * attributes:
181 *
182 * attributes-charset
183 * attributes-natural-language
184 * job-uri
185 * job-printer-uri
186 */
187
188 request = ippNew();
189
190 request->request.op.operation_id = CUPS_MOVE_JOB;
191 request->request.op.request_id = 1;
192
193 language = cupsLangDefault();
194
195 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
196 "attributes-charset", NULL, cupsLangEncoding(language));
197
198 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
199 "attributes-natural-language", NULL, language->language);
200
201 snprintf(job_uri, sizeof(job_uri), "ipp://localhost/jobs/%d", jobid);
202 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, job_uri);
203
204 ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
205 NULL, cupsUser());
206
207 snprintf(printer_uri, sizeof(printer_uri), "ipp://localhost/printers/%s", dest);
208 ippAddString(request, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri",
209 NULL, printer_uri);
210
211 /*
212 * Do the request and get back a response...
213 */
214
215 if ((response = cupsDoRequest(http, request, "/jobs")) != NULL)
216 {
217 if (response->request.status.status_code > IPP_OK_CONFLICT)
218 {
219 fprintf(stderr, "lpmove: move-job failed: %s\n",
220 ippErrorString(response->request.status.status_code));
221 ippDelete(response);
222 return;
223 }
224
225 ippDelete(response);
226 }
227 else
228 fprintf(stderr, "lpmove: move-job failed: %s\n",
229 ippErrorString(cupsLastError()));
230 }
231
232
233 /*
234 * End of "$Id: lpmove.c,v 1.5.2.6 2002/08/22 01:43:40 mike Exp $".
235 */