#include <stdio.h>
#include <stdlib.h>
#include <cups/cups.h>
#include <cups/language.h>
#include <cups/string.h>
int
main(int argc,
char *argv[])
{
http_t *http;
int i;
int job_id;
const char *dest;
char *instance;
char uri[1024];
ipp_t *request;
ipp_t *response;
ipp_op_t op;
cups_lang_t *language;
int num_dests;
cups_dest_t *dests;
http_encryption_t encryption;
op = IPP_CANCEL_JOB;
job_id = 0;
dest = NULL;
response = NULL;
http = NULL;
encryption = cupsEncryption();
num_dests = cupsGetDests(&dests);
for (i = 0; i < num_dests; i ++)
if (dests[i].is_default)
dest = dests[i].name;
if ((http = httpConnectEncrypt(cupsServer(), ippPort(), encryption)) == NULL)
{
fputs("lprm: Unable to contact server!\n", stderr);
cupsFreeDests(num_dests, dests);
return (1);
}
for (i = 1; i < argc; i ++)
if (argv[i][0] == '-' && argv[i][1] != '\0')
switch (argv[i][1])
{
case 'E' :
#ifdef HAVE_LIBSSL
encryption = HTTP_ENCRYPT_REQUIRED;
httpEncryption(http, encryption);
#else
fprintf(stderr, "%s: Sorry, no encryption support compiled in!\n",
argv[0]);
#endif
break;
case 'P' :
if (argv[i][2])
dest = argv[i] + 2;
else
{
i ++;
dest = argv[i];
}
if ((instance = strchr(dest, '/')) != NULL)
*instance = '\0';
break;
default :
fprintf(stderr, "lprm: Unknown option \'%c\'!\n", argv[i][1]);
cupsFreeDests(num_dests, dests);
httpClose(http);
return (1);
}
else
{
if (isdigit(argv[i][0]))
{
dest = NULL;
op = IPP_CANCEL_JOB;
job_id = atoi(argv[i]);
}
else if (strcmp(argv[i], "-") == 0)
{
op = IPP_PURGE_JOBS;
}
else
{
dest = argv[i];
job_id = 0;
}
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)
{
switch (response->request.status.status_code)
{
case IPP_NOT_FOUND :
fputs("lprm: Job or printer not found!\n", stderr);
break;
case IPP_NOT_AUTHORIZED :
fputs("lprm: Not authorized to lprm job(s)!\n", stderr);
break;
case IPP_FORBIDDEN :
fprintf(stderr, "lprm: You don't own job ID %d!\n", job_id);
break;
default :
if (response->request.status.status_code > IPP_OK_CONFLICT)
fputs("lprm: Unable to lprm job(s)!\n", stderr);
break;
}
if (response->request.status.status_code > IPP_OK_CONFLICT)
{
ippDelete(response);
cupsFreeDests(num_dests, dests);
httpClose(http);
return (1);
}
ippDelete(response);
}
else
{
fputs("lprm: Unable to cancel job(s)!\n", stderr);
cupsFreeDests(num_dests, dests);
httpClose(http);
return (1);
}
}
if (response == NULL)
if (!cupsCancelJob(dest, 0))
{
fputs("lprm: Unable to cancel job(s)!\n", stderr);
cupsFreeDests(num_dests, dests);
httpClose(http);
return (1);
}
cupsFreeDests(num_dests, dests);
httpClose(http);
return (0);
}