#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <cups/string.h>
#include <cups/cups.h>
#include <cups/i18n.h>
#include <cups/debug.h>
static int move_job(http_t *http, const char *src, int jobid,
const char *dest);
int
main(int argc,
char *argv[])
{
int i;
http_t *http;
const char *job;
int jobid;
int num_dests;
cups_dest_t *dests;
const char *src,
*dest;
_cupsSetLocale(argv);
dest = NULL;
dests = NULL;
http = NULL;
job = NULL;
jobid = 0;
num_dests = 0;
src = 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
_cupsLangPrintf(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)
{
_cupsLangPuts(stderr,
_("Error: need hostname after \'-h\' option!\n"));
return (1);
}
cupsSetServer(argv[i]);
}
break;
default :
_cupsLangPrintf(stderr, _("lpmove: Unknown option \'%c\'!\n"),
argv[i][1]);
return (1);
}
else if (!jobid && !src)
{
if (num_dests == 0)
num_dests = cupsGetDests(&dests);
if ((job = strrchr(argv[i], '-')) != NULL &&
cupsGetDest(argv[i], NULL, num_dests, dests) == NULL)
jobid = atoi(job + 1);
else if (isdigit(argv[i][0] & 255) &&
!cupsGetDest(argv[i], NULL, num_dests, dests))
jobid = atoi(argv[i]);
else
src = argv[i];
}
else if (dest == NULL)
dest = argv[i];
else
{
_cupsLangPrintf(stderr, _("lpmove: Unknown argument \'%s\'!\n"),
argv[i]);
return (1);
}
if ((!jobid && !src) || !dest)
{
_cupsLangPuts(stdout, _("Usage: lpmove job/src dest\n"));
return (1);
}
if (!http)
{
http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
if (http == NULL)
{
_cupsLangPrintf(stderr,
_("lpmove: Unable to connect to server: %s\n"),
strerror(errno));
return (1);
}
}
return (move_job(http, src, jobid, dest));
}
static int
move_job(http_t *http,
const char *src,
int jobid,
const char *dest)
{
ipp_t *request;
char job_uri[HTTP_MAX_URI],
printer_uri[HTTP_MAX_URI];
if (!http)
return (1);
request = ippNewRequest(CUPS_MOVE_JOB);
if (jobid)
{
snprintf(job_uri, sizeof(job_uri), "ipp://localhost/jobs/%d", jobid);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
job_uri);
}
else
{
httpAssembleURIf(HTTP_URI_CODING_ALL, job_uri, sizeof(job_uri), "ipp", NULL,
"localhost", 0, "/printers/%s", src);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "printer-uri", NULL,
job_uri);
}
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME, "requesting-user-name",
NULL, cupsUser());
httpAssembleURIf(HTTP_URI_CODING_ALL, printer_uri, sizeof(printer_uri),
"ipp", NULL, "localhost", 0, "/printers/%s", dest);
ippAddString(request, IPP_TAG_JOB, IPP_TAG_URI, "job-printer-uri",
NULL, printer_uri);
ippDelete(cupsDoRequest(http, request, "/jobs"));
if (cupsLastError() > IPP_OK_CONFLICT)
{
_cupsLangPrintf(stderr, "lpmove: %s\n", cupsLastErrorString());
return (1);
}
else
return (0);
}