/* * call-seq: * print_job.cancel -> true or false * * Cancel the current job. Returns true if successful, false otherwise. */ static VALUE cups_cancel(VALUE self) { VALUE printer, job_id; printer = rb_iv_get(self, "@printer"); job_id = rb_iv_get(self, "@job_id"); if (NIL_P(job_id)) { return Qfalse; // If @job_id is nil } else { // Otherwise attempt to cancel int job = NUM2INT(job_id); char *target = RSTRING_PTR(printer); // Target printer string int cancellation; cancellation = cupsCancelJob(target, job); return Qtrue; } }
void cgiPrintCommand(http_t *http, /* I - Connection to server */ const char *dest, /* I - Destination printer */ const char *command, /* I - Command to send */ const char *title) /* I - Page/job title */ { int job_id; /* Command file job */ char uri[HTTP_MAX_URI], /* Job URI */ resource[1024], /* Printer resource path */ refresh[1024], /* Refresh URL */ command_file[1024]; /* Command "file" */ http_status_t status; /* Document status */ cups_option_t hold_option; /* job-hold-until option */ const char *user; /* User name */ ipp_t *request, /* Get-Job-Attributes request */ *response; /* Get-Job-Attributes response */ ipp_attribute_t *attr; /* Current job attribute */ static const char * const job_attrs[] =/* Job attributes we want */ { "job-state", "job-printer-state-message" }; /* * Create the CUPS command file... */ snprintf(command_file, sizeof(command_file), "#CUPS-COMMAND\n%s\n", command); /* * Show status... */ if (cgiSupportsMultipart()) { cgiStartMultipart(); cgiStartHTML(title); cgiCopyTemplateLang("command.tmpl"); cgiEndHTML(); fflush(stdout); } /* * Send the command file job... */ hold_option.name = "job-hold-until"; hold_option.value = "no-hold"; if ((user = getenv("REMOTE_USER")) != NULL) cupsSetUser(user); else cupsSetUser("anonymous"); if ((job_id = cupsCreateJob(http, dest, title, 1, &hold_option)) < 1) { cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver"))); cgiSetVariable("ERROR", cupsLastErrorString()); cgiStartHTML(title); cgiCopyTemplateLang("error.tmpl"); cgiEndHTML(); if (cgiSupportsMultipart()) cgiEndMultipart(); return; } status = cupsStartDocument(http, dest, job_id, NULL, CUPS_FORMAT_COMMAND, 1); if (status == HTTP_CONTINUE) status = cupsWriteRequestData(http, command_file, strlen(command_file)); if (status == HTTP_CONTINUE) cupsFinishDocument(http, dest); if (cupsLastError() >= IPP_REDIRECTION_OTHER_SITE) { cgiSetVariable("MESSAGE", cgiText(_("Unable to send command to printer driver"))); cgiSetVariable("ERROR", cupsLastErrorString()); cgiStartHTML(title); cgiCopyTemplateLang("error.tmpl"); cgiEndHTML(); if (cgiSupportsMultipart()) cgiEndMultipart(); cupsCancelJob(dest, job_id); return; } /* * Wait for the job to complete... */ if (cgiSupportsMultipart()) { for (;;) { /* * Get the current job state... */ snprintf(uri, sizeof(uri), "ipp://localhost/jobs/%d", job_id); request = ippNewRequest(IPP_GET_JOB_ATTRIBUTES); 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); ippAddStrings(request, IPP_TAG_OPERATION, IPP_TAG_KEYWORD, "requested-attributes", 2, NULL, job_attrs); if ((response = cupsDoRequest(http, request, "/")) != NULL) cgiSetIPPVars(response, NULL, NULL, NULL, 0); attr = ippFindAttribute(response, "job-state", IPP_TAG_ENUM); if (!attr || attr->values[0].integer >= IPP_JOB_STOPPED || attr->values[0].integer == IPP_JOB_HELD) { ippDelete(response); break; } /* * Job not complete, so update the status... */ ippDelete(response); cgiStartHTML(title); cgiCopyTemplateLang("command.tmpl"); cgiEndHTML(); fflush(stdout); sleep(5); } } /* * Send the final page that reloads the printer's page... */ snprintf(resource, sizeof(resource), "/printers/%s", dest); cgiFormEncode(uri, resource, sizeof(uri)); snprintf(refresh, sizeof(refresh), "5;URL=%s", uri); cgiSetVariable("refresh_page", refresh); cgiStartHTML(title); cgiCopyTemplateLang("command.tmpl"); cgiEndHTML(); if (cgiSupportsMultipart()) cgiEndMultipart(); }
int /* O - Exit status */ main(int argc, /* I - Number of command-line arguments */ char *argv[]) /* I - Command-line arguments */ { http_t *http; /* HTTP connection to server */ int i; /* Looping var */ int job_id; /* Job ID */ const char *dest; /* Destination printer */ char *instance; /* Pointer to instance name */ char uri[1024]; /* Printer or job URI */ ipp_t *request; /* IPP request */ ipp_t *response; /* IPP response */ ipp_op_t op; /* Operation */ int num_dests; /* Number of destinations */ cups_dest_t *dests, /* Destinations */ *defdest; /* Default destination */ http_encryption_t encryption; /* Encryption? */ _cupsSetLocale(argv); /* * Setup to cancel individual print jobs... */ op = IPP_CANCEL_JOB; job_id = 0; dest = NULL; response = NULL; http = NULL; encryption = cupsEncryption(); /* * Open a connection to the server... */ if ((http = httpConnectEncrypt(cupsServer(), ippPort(), encryption)) == NULL) { _cupsLangPuts(stderr, _("lprm: Unable to contact server!\n")); return (1); } num_dests = cupsGetDests2(http, &dests); defdest = cupsGetDest(NULL, NULL, num_dests, dests); dest = defdest ? defdest->name : NULL; /* * Process command-line arguments... */ for (i = 1; i < argc; i ++) if (argv[i][0] == '-' && argv[i][1] != '\0') switch (argv[i][1]) { case 'E' : /* Encrypt */ #ifdef HAVE_SSL encryption = HTTP_ENCRYPT_REQUIRED; httpEncryption(http, encryption); cupsSetEncryption(encryption); #else _cupsLangPrintf(stderr, _("%s: Sorry, no encryption support compiled in!\n"), argv[0]); #endif /* HAVE_SSL */ break; case 'P' : /* Cancel jobs on a printer */ if (argv[i][2]) dest = argv[i] + 2; else { i ++; dest = argv[i]; } if ((instance = strchr(dest, '/')) != NULL) *instance = '\0'; if (cupsGetDest(dest, NULL, num_dests, dests) == NULL) { _cupsLangPrintf(stderr, _("%s: Error - unknown destination \"%s\"!\n"), argv[0], dest); cupsFreeDests(num_dests, dests); httpClose(http); return(1); } break; case 'U' : /* Username */ if (argv[i][2] != '\0') cupsSetUser(argv[i] + 2); else { i ++; if (i >= argc) { _cupsLangPrintf(stderr, _("%s: Error - expected username after " "\'-U\' option!\n"), argv[0]); return (1); } cupsSetUser(argv[i]); } break; case 'h' : /* Connect to host */ if (argv[i][2] != '\0') cupsSetServer(argv[i] + 2); else { i ++; if (i >= argc) { _cupsLangPrintf(stderr, _("%s: Error - expected hostname after " "\'-h\' option!\n"), argv[0]); return (1); } else cupsSetServer(argv[i]); } httpClose(http); cupsFreeDests(num_dests, dests); if ((http = httpConnectEncrypt(cupsServer(), ippPort(), encryption)) == NULL) { _cupsLangPuts(stderr, _("lprm: Unable to contact server!\n")); return (1); } num_dests = cupsGetDests2(http, &dests); defdest = cupsGetDest(NULL, NULL, num_dests, dests); dest = defdest ? defdest->name : NULL; break; default : _cupsLangPrintf(stderr, _("%s: Error - unknown option \'%c\'!\n"), argv[0], argv[i][1]); cupsFreeDests(num_dests, dests); httpClose(http); return (1); } else { /* * Cancel a job or printer... */ if (isdigit(argv[i][0] & 255) && cupsGetDest(argv[i], NULL, num_dests, dests) == NULL) { dest = NULL; op = IPP_CANCEL_JOB; job_id = atoi(argv[i]); } else if (!strcmp(argv[i], "-")) { /* * Cancel all jobs */ op = IPP_PURGE_JOBS; } else { dest = argv[i]; job_id = 0; } /* * Build an IPP request, which requires the following * attributes: * * attributes-charset * attributes-natural-language * printer-uri + job-id *or* job-uri * [requesting-user-name] */ request = ippNewRequest(op); if (dest) { httpAssembleURIf(HTTP_URI_CODING_ALL, uri, sizeof(uri), "ipp", NULL, "localhost", 0, "/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()); /* * Do the request and get back a response... */ if (op == IPP_PURGE_JOBS) response = cupsDoRequest(http, request, "/admin/"); else response = cupsDoRequest(http, request, "/jobs/"); ippDelete(response); if (cupsLastError() > IPP_OK_CONFLICT) { _cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString()); cupsFreeDests(num_dests, dests); httpClose(http); return (1); } } /* * If nothing has been canceled yet, cancel the current job on the specified * (or default) printer... */ if (response == NULL) if (!cupsCancelJob(dest, 0)) { _cupsLangPrintf(stderr, "%s: %s\n", argv[0], cupsLastErrorString()); cupsFreeDests(num_dests, dests); httpClose(http); return (1); } cupsFreeDests(num_dests, dests); httpClose(http); return (0); }