#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;
int num_dests;
cups_dest_t *dests;
char *dest,
*job,
*user;
int purge;
char uri[1024];
ipp_t *request;
ipp_t *response;
ipp_op_t op;
cups_lang_t *language;
op = IPP_CANCEL_JOB;
purge = 0;
job_id = 0;
dest = NULL;
user = NULL;
http = NULL;
num_dests = 0;
dests = NULL;
for (i = 1; i < argc; i ++)
if (argv[i][0] == '-' && argv[i][1])
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 'a' :
purge = 1;
op = IPP_PURGE_JOBS;
break;
case 'h' :
if (http != NULL)
httpClose(http);
if (argv[i][2] != '\0')
cupsSetServer(argv[i] + 2);
else
{
i ++;
if (i >= argc)
{
fputs("cancel: Error - expected hostname after \'-h\' option!\n", stderr);
return (1);
}
else
cupsSetServer(argv[i]);
}
break;
case 'u' :
op = IPP_PURGE_JOBS;
if (argv[i][2] != '\0')
user = argv[i] + 2;
else
{
i ++;
if (i >= argc)
{
fputs("cancel: Error - expected username after \'-u\' option!\n", stderr);
return (1);
}
else
user = argv[i];
}
break;
default :
fprintf(stderr, "cancel: Unknown option \'%c\'!\n", argv[i][1]);
return (1);
}
else
{
if (num_dests == 0)
num_dests = cupsGetDests(&dests);
if (strcmp(argv[i], "-") == 0)
{
dest = "";
job_id = 0;
}
else if (cupsGetDest(argv[i], NULL, num_dests, dests) != NULL)
{
dest = argv[i];
job_id = 0;
}
else if ((job = strrchr(argv[i], '-')) != NULL && isdigit(job[1]))
{
dest = NULL;
op = IPP_CANCEL_JOB;
job_id = atoi(job + 1);
}
else if (isdigit(argv[i][0]))
{
dest = NULL;
op = IPP_CANCEL_JOB;
job_id = atoi(argv[i]);
}
else
{
fprintf(stderr, "cancel: Unknown destination \"%s\"!\n", argv[i]);
return (1);
}
if (job_id && (i + 1) < argc &&
cupsGetDest(argv[i + 1], NULL, num_dests, dests) != NULL)
i ++;
if (http == NULL)
if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
cupsEncryption())) == 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);
}
if (user)
{
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, user);
ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
}
else
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, cupsUser());
if (op == IPP_PURGE_JOBS)
ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", purge);
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);
}
if (num_dests == 0 && op == IPP_PURGE_JOBS)
{
if (http == NULL)
if ((http = httpConnectEncrypt(cupsServer(), ippPort(),
cupsEncryption())) == 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);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, "ipp://localhost/printers/");
if (user)
{
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, user);
ippAddBoolean(request, IPP_TAG_OPERATION, "my-jobs", 1);
}
else
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_NAME,
"requesting-user-name", NULL, cupsUser());
ippAddBoolean(request, IPP_TAG_OPERATION, "purge-jobs", purge);
response = cupsDoRequest(http, request, "/admin/");
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);
}