#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <cups/cups.h>
#include <cups/language.h>
#include <cups/debug.h>
#include <cups/string.h>
static void move_job(http_t *, int, const char *);
int
main(int argc,
char *argv[])
{
int i;
http_t *http;
const char *job;
int num_dests;
cups_dest_t *dests;
const char *dest;
http = NULL;
job = NULL;
dest = NULL;
num_dests = 0;
dests = NULL;
for (i = 1; i < argc; i ++)
if (argv[i][0] == '-')
switch (argv[i][1])
{
case 'E' :
#ifdef HAVE_SSL
cupsSetEncryption(HTTP_ENCRYPT_REQUIRED);
if (http)
httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
#else
fprintf(stderr, "%s: Sorry, no encryption support compiled in!\n",
argv[0]);
#endif
break;
case 'h' :
if (http)
{
httpClose(http);
http = NULL;
}
if (argv[i][2] != '\0')
cupsSetServer(argv[i] + 2);
else
{
i ++;
if (i >= argc)
{
fputs("Error: need hostname after \'-h\' option!\n", stderr);
return (1);
}
cupsSetServer(argv[i]);
}
break;
default :
fprintf(stderr, "lpmove: Unknown option \'%c\'!\n", argv[i][1]);
return (1);
}
else if (job == NULL)
{
if (num_dests == 0)
num_dests = cupsGetDests(&dests);
if ((job = strrchr(argv[i], '-')) != NULL &&
cupsGetDest(argv[i], NULL, num_dests, dests) == NULL)
job ++;
else
job = argv[i];
}
else if (dest == NULL)
dest = argv[i];
else
{
fprintf(stderr, "lpmove: Unknown argument \'%s\'!\n", argv[i]);
return (1);
}
if (job == NULL || dest == NULL)
{
puts("Usage: lpmove job dest");
return (1);
}
if (!http)
{
http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
if (http == NULL)
{
perror("lpmove: Unable to connect to server");
return (1);
}
}
move_job(http, atoi(job), dest);
return (0);
}
static void
move_job(http_t *http,
int jobid,
const char *dest)
{
ipp_t *request,
*response;
cups_lang_t *language;
char job_uri[HTTP_MAX_URI],
printer_uri[HTTP_MAX_URI];
if (http == NULL)
return;
request = ippNew();
request->request.op.operation_id = CUPS_MOVE_JOB;
request->request.op.request_id = 1;
language = cupsLangDefault();
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_CHARSET,
"attributes-charset", NULL, cupsLangEncoding(language));
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_LANGUAGE,
"attributes-natural-language", NULL, language->language);
snprintf(job_uri, sizeof(job_uri), "ipp://localhost/jobs/%d", jobid);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL, job_uri);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, cupsUser());
snprintf(printer_uri, sizeof(printer_uri), "ipp://localhost/printers/%s", dest);
ippAddString(request, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri",
NULL, printer_uri);
if ((response = cupsDoRequest(http, request, "/jobs")) != NULL)
{
if (response->request.status.status_code > IPP_OK_CONFLICT)
{
fprintf(stderr, "lpmove: move-job failed: %s\n",
ippErrorString(response->request.status.status_code));
ippDelete(response);
return;
}
ippDelete(response);
}
else
fprintf(stderr, "lpmove: move-job failed: %s\n",
ippErrorString(cupsLastError()));
}