#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <config.h>
#include <cups/cups.h>
#include <cups/string.h>
#include <cups/language.h>
int
main(int argc,
char *argv[])
{
http_t *http;
int i;
int job_id;
char *dest,
*host,
*job;
char name[255];
char uri[1024];
ipp_t *request;
ipp_t *response;
ipp_op_t op;
cups_lang_t *language;
http_encryption_t encryption;
op = IPP_CANCEL_JOB;
job_id = 0;
dest = NULL;
http = NULL;
encryption = cupsEncryption();
for (i = 1; i < argc; i ++)
if (argv[i][0] == '-' && argv[i][1])
switch (argv[i][1])
{
case 'E' :
#ifdef HAVE_LIBSSL
encryption = HTTP_ENCRYPT_REQUIRED;
if (http)
httpEncryption(http, encryption);
#else
fprintf(stderr, "%s: Sorry, no encryption support compiled in!\n",
argv[0]);
#endif
break;
case 'a' :
op = IPP_PURGE_JOBS;
break;
case 'h' :
if (http != NULL)
httpClose(http);
if (argv[i][2] != '\0')
http = httpConnectEncrypt(argv[i] + 2, ippPort(), encryption);
else
{
i ++;
if (i >= argc)
{
fputs("cancel: Error - expected hostname after \'-h\' option!\n", stderr);
return (1);
}
else
http = httpConnectEncrypt(argv[i], ippPort(), encryption);
}
if (http == NULL)
{
perror("cancel: Unable to connect to server");
return (1);
}
break;
case 'u' :
if (argv[i][2] != '\0')
cupsSetUser(argv[i] + 2);
else
{
i ++;
if (i >= argc)
{
fputs("cancel: Error - expected username after \'-u\' option!\n", stderr);
return (1);
}
else
cupsSetUser(argv[i]);
}
break;
default :
fprintf(stderr, "cancel: Unknown option \'%c\'!\n", argv[i][1]);
return (1);
}
else
{
if (isdigit(argv[i][0]))
{
dest = NULL;
op = IPP_CANCEL_JOB;
job_id = atoi(argv[i]);
}
else if (argv[i][0] == '-')
{
dest = "";
job_id = 0;
}
else
{
strlcpy(name, argv[i], sizeof(name));
dest = name;
job_id = 0;
if ((job = strrchr(name, '-')) != NULL)
if (isdigit(job[1]))
{
*job++ = '\0';
job_id = atoi(job);
}
if (job_id)
op = IPP_CANCEL_JOB;
if ((host = strchr(name, '@')) != NULL)
{
if (http != NULL)
httpClose(http);
*host++ = '\0';
if ((http = httpConnectEncrypt(host, ippPort(), encryption)) == NULL)
{
perror("cancel: Unable to connect to server");
return (1);
}
}
}
if (http == NULL)
if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
encryption)) == NULL)
{
fputs("cancel: Unable to contact server!\n", stderr);
return (1);
}
request = ippNew();
request->request.op.operation_id = op;
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);
if (dest)
{
snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", dest);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
ippAddInteger(request, IPP_TAG_OPERATION, IPP_TAG_INTEGER, "job-id",
job_id);
}
else
{
sprintf(uri, "ipp://localhost/jobs/%d", job_id);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI, "job-uri", NULL,
uri);
}
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, cupsUser());
if (op == IPP_PURGE_JOBS)
response = cupsDoRequest(http, request, "/admin/");
else
response = cupsDoRequest(http, request, "/jobs/");
if (response == NULL ||
response->request.status.status_code > IPP_OK_CONFLICT)
{
fprintf(stderr, "cancel: %s failed: %s\n",
op == IPP_PURGE_JOBS ? "purge-jobs" : "cancel-job",
response ? ippErrorString(response->request.status.status_code) :
ippErrorString(cupsLastError()));
if (response)
ippDelete(response);
return (1);
}
ippDelete(response);
}
return (0);
}