#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;
char *command,
uri[1024],
*reason;
ipp_t *request;
ipp_t *response;
ipp_op_t op;
cups_lang_t *language;
int cancel;
if ((command = strrchr(argv[0], '/')) != NULL)
command ++;
else
command = argv[0];
cancel = 0;
if (strcmp(command, "accept") == 0)
op = CUPS_ACCEPT_JOBS;
else if (strcmp(command, "reject") == 0)
op = CUPS_REJECT_JOBS;
else if (strcmp(command, "disable") == 0)
op = IPP_PAUSE_PRINTER;
else if (strcmp(command, "enable") == 0)
op = IPP_RESUME_PRINTER;
else
{
fprintf(stderr, "%s: Don't know what to do!\n", command);
return (1);
}
http = NULL;
reason = 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",
command);
#endif
break;
case 'c' :
cancel = 1;
break;
case 'h' :
if (http != NULL)
httpClose(http);
if (argv[i][2] != '\0')
cupsSetServer(argv[i] + 2);
else
{
i ++;
if (i >= argc)
{
fprintf(stderr, "%s: Expected server name after -h!\n",
command);
return (1);
}
cupsSetServer(argv[i]);
}
break;
case 'r' :
if (argv[i][2] != '\0')
reason = argv[i] + 2;
else
{
i ++;
if (i >= argc)
{
fprintf(stderr, "%s: Expected reason text after -r!\n", command);
return (1);
}
reason = argv[i];
}
break;
default :
fprintf(stderr, "%s: Unknown option \'%c\'!\n", command,
argv[i][1]);
return (1);
}
else
{
if (http == NULL)
http = httpConnectEncrypt(cupsServer(), ippPort(), cupsEncryption());
if (http == NULL)
{
fputs(command, stderr);
perror(": Unable to connect to server");
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);
snprintf(uri, sizeof(uri), "ipp://localhost/printers/%s", argv[i]);
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_URI,
"printer-uri", NULL, uri);
if (reason != NULL)
ippAddString(request, IPP_TAG_OPERATION, IPP_TAG_TEXT,
"printer-state-message", NULL, reason);
if ((response = cupsDoRequest(http, request, "/admin/")) != NULL)
{
if (response->request.status.status_code > IPP_OK_CONFLICT)
{
fprintf(stderr, "%s: Operation failed: %s\n", command,
ippErrorString(cupsLastError()));
return (1);
}
ippDelete(response);
}
else
{
fprintf(stderr, "%s: Operation failed: %s\n", command,
ippErrorString(cupsLastError()));
return (1);
}
if (cancel)
{
request = ippNew();
request->request.op.operation_id = IPP_PURGE_JOBS;
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, uri);
if ((response = cupsDoRequest(http, request, "/admin/")) != NULL)
{
if (response->request.status.status_code > IPP_OK_CONFLICT)
{
fprintf(stderr, "%s: Operation failed: %s\n", command,
ippErrorString(cupsLastError()));
return (1);
}
ippDelete(response);
}
else
{
fprintf(stderr, "%s: Operation failed: %s\n", command,
ippErrorString(cupsLastError()));
return (1);
}
}
}
if (http != NULL)
httpClose(http);
return (0);
}