| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# (c) Copyright 2003-2015 HP Development Company, L.P. |
| 5 |
# |
| 6 |
# This program is free software; you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation; either version 2 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program; if not, write to the Free Software |
| 18 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
# |
| 20 |
# Author: Gaurav Sood, Sanjay Kumar |
| 21 |
# |
| 22 |
|
| 23 |
__version__ = '1.1' |
| 24 |
__title__ = 'PostScript Finishing Filter (hpps)' |
| 25 |
__doc__ = "CUPS filter that implements job storage (secure printing), Job Accounting and Born On Date (BOD) features on specific capable printers." |
| 26 |
|
| 27 |
# StdLib |
| 28 |
import sys |
| 29 |
import getopt |
| 30 |
import os.path |
| 31 |
import os |
| 32 |
import syslog |
| 33 |
import time |
| 34 |
import tempfile |
| 35 |
import subprocess |
| 36 |
import time |
| 37 |
PY3 = sys.version_info[0] == 3 |
| 38 |
if PY3: |
| 39 |
import configparser |
| 40 |
else: |
| 41 |
import ConfigParser as configparser |
| 42 |
|
| 43 |
CUPS_FILTER_OK = 0 |
| 44 |
CUPS_FILTER_FAILED = 1 |
| 45 |
|
| 46 |
input_fd = 0 |
| 47 |
output_fd = 1 |
| 48 |
|
| 49 |
job_id = 0 |
| 50 |
pid = os.getpid() |
| 51 |
config_file = '/etc/hp/hplip.conf' |
| 52 |
home_dir = '' |
| 53 |
|
| 54 |
|
| 55 |
def bug(m): |
| •56 |
try: |
| 57 |
log.stderr("ERROR: %s" % m) |
| 58 |
except NameError: |
| 59 |
sys.stderr.write("ERROR: %s\n" % m) |
| 60 |
|
| 61 |
def msg(m): |
| •62 |
try: |
| 63 |
log.stderr("INFO: %s" % msg) |
| 64 |
except NameError: |
| 65 |
sys.stderr.write("INFO: %s\n" % msg) |
| 66 |
|
| 67 |
if os.path.exists(config_file): |
| 68 |
config = configparser.ConfigParser() |
| 69 |
config.read(config_file) |
| 70 |
|
| 71 |
try: |
| 72 |
home_dir = config.get('dirs', 'home') |
| 73 |
except: |
| 74 |
bug("Error setting home directory: home= under [dirs] not found.") |
| 75 |
sys.exit(CUPS_FILTER_FAILED) |
| 76 |
else: |
| 77 |
bug("Error setting home directory: /etc/hp/hplip.conf not found") |
| 78 |
sys.exit(CUPS_FILTER_FAILED) |
| 79 |
|
| 80 |
if not home_dir or not os.path.exists(home_dir): |
| 81 |
bug("Error setting home directory: Home directory %s not found." % home_dir) |
| 82 |
sys.exit(CUPS_FILTER_FAILED) |
| 83 |
|
| 84 |
sys.path.insert(0, home_dir) |
| 85 |
os.chdir(home_dir) |
| 86 |
|
| 87 |
|
| 88 |
# HPLIP |
| 89 |
try: |
| 90 |
from base.g import * |
| 91 |
from base.codes import * |
| 92 |
from base import device |
| 93 |
from base import utils |
| 94 |
from prnt import cups |
| 95 |
from base.sixext import to_bytes_utf8, to_string_utf8 |
| 96 |
except ImportError as e: |
| 97 |
bug("Error importing HPLIP modules:%s %s\n" % (pid, e)) |
| 98 |
sys.exit(CUPS_FILTER_FAILED) |
| 99 |
|
| 100 |
|
| 101 |
def usage(typ='text'): |
| 102 |
if typ == 'text': |
| 103 |
utils.log_title(__title__, __version__) |
| 104 |
|
| 105 |
utils.format_text(USAGE, typ, title=__title__, crumb='pscfilter') |
| 106 |
sys.exit(CUPS_FILTER_OK) |
| 107 |
|
| 108 |
try: |
| 109 |
opts, args = getopt.getopt(sys.argv[1:], 'l:hg', ['level=', 'help', 'help-rest', 'help-man']) |
| 110 |
|
| 111 |
except getopt.GetoptError: |
| 112 |
usage() |
| 113 |
|
| 114 |
for o, a in opts: |
| 115 |
|
| 116 |
if o in ('-l', '--logging'): |
| 117 |
log_level = a.lower().strip() |
| 118 |
log.set_level(log_level) |
| 119 |
|
| 120 |
elif o == '-g': |
| 121 |
log.set_level('debug') |
| 122 |
|
| 123 |
elif o in ('-h', '--help'): |
| 124 |
usage() |
| 125 |
|
| 126 |
elif o == '--help-rest': |
| 127 |
usage('rest') |
| 128 |
|
| 129 |
elif o == '--help-man': |
| 130 |
usage('man') |
| 131 |
|
| 132 |
|
| 133 |
# CUPS provided environment |
| 134 |
try: |
| 135 |
ppd_file = os.environ['PPD'] |
| 136 |
except KeyError: |
| 137 |
bug("Improper environment: Must be run by CUPS.") |
| 138 |
sys.exit(CUPS_FILTER_FAILED) |
| 139 |
|
| 140 |
try: |
| 141 |
job_id, username, title, copies, options = args[0:5] |
| 142 |
job_id = int(job_id) |
| 143 |
except IndexError: |
| 144 |
bug("Invalid command line: invalid arguments.") |
| 145 |
sys.exit(CUPS_FILTER_FAILED) |
| 146 |
|
| 147 |
START_JOB = b"\x1b%-12345X@PJL JOBNAME=" |
| 148 |
UEL = b"@PJL ENTER LANGUAGE=POSTSCRIPT\x0a" |
| 149 |
END_JOB = b"\x1b%-12345X@PJL EOJ\x0a\x1b%-12345X" |
| 150 |
|
| 151 |
try: |
| 152 |
ppd_str = to_string_utf8(open(ppd_file, 'rb').read()) |
| 153 |
except IOError: |
| 154 |
bug("Unabel to read PPD File") |
| 155 |
sys.exit(CUPS_FILTER_FAILED) |
| 156 |
|
| 157 |
opts = {} |
| 158 |
|
| 159 |
for opt in options.split(): |
| 160 |
try: |
| 161 |
o, v = opt.split('=') |
| 162 |
except ValueError: |
| 163 |
continue |
| 164 |
else: |
| 165 |
opts[o] = v |
| 166 |
|
| 167 |
key_list = [x for x in ['HPAccountingInfo', 'HPBOD', 'HPPJLEconoMode', 'HPPJLEconoMode2', 'HPPJLOutputMode', 'HPPJLDryTime', |
| 168 |
'HPPJLSaturation', 'HPPJLInkBleed', 'HPPJLColorAsGray', 'HPPJLTrueBlack','HPPJLPrintQuality'] if x in ppd_str] |
| 169 |
|
| 170 |
#output_fd = os.open("/tmp/PSC.out", os.O_WRONLY | os.O_CREAT) |
| 171 |
|
| 172 |
def header(): |
| 173 |
os.write(output_fd, START_JOB) |
| 174 |
os.write(output_fd, to_bytes_utf8("hplip_%s_%s\x0a" % (username, job_id))) |
| 175 |
os.write(output_fd, to_bytes_utf8('@PJL SET USERNAME="%s"\x0a' % username)) |
| 176 |
os.write(output_fd, to_bytes_utf8('@PJL SET JOBNAME="%s"\x0a' % title)) |
| 177 |
|
| 178 |
# --------------------Secure Printing--------------------- |
| 179 |
# Embeds private job PJL's into the print stream |
| 180 |
# ---------------------------------------------------------- |
| 181 |
|
| 182 |
def secure_print(): |
| 183 |
os.write(output_fd, b"@PJL SET HOLD=ON\x0a") |
| 184 |
os.write(output_fd, b"@PJL SET HOLDTYPE=PRIVATE\x0a") |
| 185 |
secpin = "" |
| 186 |
if "HPDigit" in ppd_str: |
| 187 |
try: |
| 188 |
tmp = opts['HPDigit'] |
| 189 |
if "Custom." in tmp: |
| 190 |
tmp = tmp[7:] |
| 191 |
if tmp.isdigit() and len(tmp) == 4: |
| 192 |
secpin += tmp |
| 193 |
else: |
| 194 |
secpin = "0000" |
| 195 |
except KeyError as e: |
| 196 |
msg("Setting Pin to default") |
| 197 |
else: |
| 198 |
szKeyInitials = ['HPFIDigit', 'HPSEDigit', 'HPTHDigit', 'HPFTDigit'] |
| 199 |
for x in szKeyInitials: |
| 200 |
try: |
| 201 |
secpin += opts[x] |
| 202 |
except KeyError: |
| 203 |
secpin += '0' |
| 204 |
os.write(output_fd, to_bytes_utf8('@PJL SET HOLDKEY="%s"\x0a' % secpin)) |
| 205 |
|
| 206 |
|
| 207 |
#os.write(output_fd, to_bytes_utf8('@PJL SET COPIES="%s"\x0a' % copies)) |
| 208 |
|
| 209 |
#----------------Job Accouting---------------------------- |
| 210 |
# Embeds the job accouting (1-8) PJL's into print stream |
| 211 |
# ----------------------------------------------------- |
| 212 |
|
| 213 |
class HPPS_Filter(object): |
| 214 |
def HPAccountingInfo(self): |
| 215 |
p = subprocess.Popen(['hostname', '-s'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 216 |
system_name , err = p.communicate() |
| 217 |
if system_name == '': |
| 218 |
system_name = 'unknown_system_name' |
| 219 |
p = subprocess.Popen(['hostname', '-d'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 220 |
domain_name, err = p.communicate() |
| 221 |
if domain_name == '': |
| 222 |
domain_name = 'unknown_domain_name' |
| 223 |
|
| 224 |
os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct1=%s\"\x0a' % username)) |
| 225 |
os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct2=%s\"\x0a' % system_name.rstrip(b'\n'))) |
| 226 |
os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct3=%s\"\x0a' % domain_name.rstrip(b'\n'))) |
| 227 |
os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct4=%s\"\x0a' % time.strftime("%Y%m%d%I%M%S", time.localtime()))) |
| 228 |
os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct5=%s\"\x0a' % opts['job-uuid'])) |
| 229 |
os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct6=%s\"\x0a' % "HP Linux Printing")) |
| 230 |
os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct7=%s\"\x0a' % "HP Linux Printing")) |
| 231 |
os.write(output_fd, to_bytes_utf8('@PJL SET JOBATTR=\"JobAcct8=%s\"\x0a' % username)) |
| 232 |
|
| 233 |
# -------------------------Born On Date ---------------------------- |
| 234 |
# Embeds the born on date PJL's in to print stream. Uses GMT Time Stamp |
| 235 |
# Based upon the "BornOnDate_PJL_DMINFO_PJL_TIMESTAMP.doc |
| 236 |
# ----------------------------------------------------------------- |
| 237 |
def HPBOD(self): |
| 238 |
BOD_fixed = "0400040101020D10100115" # HEX value of the time stamp OID |
| 239 |
BOD_time = time.strftime("%Y%m%d%I%M%S", time.gmtime()) # Timestamp GMT |
| 240 |
BOD_Var = '' |
| 241 |
for x in BOD_time: |
| 242 |
BOD_Var = BOD_Var + hex(ord(x))[2:] |
| 243 |
os.write(output_fd, to_bytes_utf8('@PJL DMINFO ASCIIHEX=\"%s%s\"\x0A' % (BOD_fixed, BOD_Var))) |
| 244 |
|
| 245 |
# -------------------------Other Features ---------------------------- |
| 246 |
def HPPJLEconoMode(self): |
| 247 |
if 'HPPJLEconoMode' in options and 'noHPPJLEconoMode' not in options: |
| 248 |
os.write(output_fd, b"@PJL SET ECONOMODE=ON\x0a") |
| 249 |
else: |
| 250 |
os.write(output_fd, b"@PJL SET ECONOMODE=OFF\x0a") |
| 251 |
|
| 252 |
os.write(output_fd, b"@PJL SET RESOLUTION=600\x0a") |
| 253 |
os.write(output_fd, b"@PJL SET BITSPERPIXEL=2\x0a") |
| 254 |
|
| 255 |
|
| 256 |
def HPPJLEconoMode2(self): |
| 257 |
pq = opts.get('HPPJLEconoMode2', "") |
| 258 |
if pq == 'yes' or pq == 'on': |
| 259 |
os.write(output_fd, b"@PJL SET ECONOMODE=ON\x0a") |
| 260 |
elif pq == 'no' or pq == 'off': |
| 261 |
os.write(output_fd, b"@PJL SET ECONOMODE=OFF\x0a") |
| 262 |
else: |
| 263 |
pass |
| 264 |
os.write(output_fd, b"@PJL SET RESOLUTION=600\x0a") |
| 265 |
os.write(output_fd, b"@PJL SET BITSPERPIXEL=1\x0a") |
| 266 |
|
| 267 |
def HPPJLPrintQuality(self): |
| 268 |
pq = opts.get('HPPJLPrintQuality',"") |
| 269 |
if pq == 'FastRes1200': |
| 270 |
os.write(output_fd, b"@PJL SET RESOLUTION=600\x0a@PJL SET BITSPERPIXEL=2\x0a") |
| 271 |
elif pq == '600dpi': |
| 272 |
os.write(output_fd, b"@PJL SET RESOLUTION=600\x0a@PJL SET BITSPERPIXEL=1\x0a") |
| 273 |
elif pq == 'ProRes1200': |
| 274 |
os.write(output_fd, b"@PJL SET RESOLUTION=1200\x0a@PJL SET BITSPERPIXEL=1\x0a") |
| 275 |
else: |
| 276 |
os.write(output_fd, b"@PJL SET RESOLUTION=600\x0a@PJL SET BITSPERPIXEL=2\x0a") |
| 277 |
|
| 278 |
|
| 279 |
def HPPJLOutputMode(self): |
| 280 |
pq = opts.get('HPPJLOutputMode', "") |
| 281 |
if pq == 'GeneralOffice': |
| 282 |
os.write(output_fd, b'@PJL SET PRINTQUALITY=DRAFT\x0a') |
| 283 |
elif pq == 'Professional': |
| 284 |
os.write(output_fd, b'@PJL SET PRINTQUALITY=NORMAL\x0a') |
| 285 |
elif pq == 'Presentation': |
| 286 |
os.write(output_fd, b'@PJL SET PRINTQUALITY=BEST\x0a') |
| 287 |
elif pq == 'MaximumDPI': |
| 288 |
os.write(output_fd, b'@PJL SET PRINTQUALITY=MAX\x0a') |
| 289 |
else: |
| 290 |
os.write(output_fd, b'@PJL SET PRINTQUALITY=NORMAL\x0a') |
| 291 |
|
| 292 |
def HPPJLDryTime(self): |
| 293 |
pq = opts.get('HPPJLDryTime', "") |
| 294 |
if pq == '0' or pq == 'Default': |
| 295 |
os.write(output_fd, b'@PJL SET DRYTIME=0\x0a') |
| 296 |
elif pq == 'Medium': |
| 297 |
os.write(output_fd, b'@PJL SET DRYTIME=4\x0a') |
| 298 |
elif pq == 'Long': |
| 299 |
os.write(output_fd, b'@PJL SET DRYTIME=7\x0a') |
| 300 |
else: |
| 301 |
os.write(output_fd, b'@PJL SET DRYTIME=0\x0a') |
| 302 |
|
| 303 |
def HPPJLSaturation(self): |
| 304 |
pq = opts.get('HPPJLSaturation', "") |
| 305 |
if pq == '-2': |
| 306 |
os.write(output_fd, b'@PJL SET SATURATION=0\x0a') |
| 307 |
elif pq == '-1': |
| 308 |
os.write(output_fd, b'@PJL SET SATURATION=2\x0a') |
| 309 |
elif pq == '0' or pq == 'default': |
| 310 |
os.write(output_fd, b'@PJL SET SATURATION=4\x0a') |
| 311 |
elif pq == '+1': |
| 312 |
os.write(output_fd, b'@PJL SET SATURATION=6\x0a') |
| 313 |
elif pq == '+2': |
| 314 |
os.write(output_fd, b'@PJL SET SATURATION=8\x0a') |
| 315 |
else: |
| 316 |
os.write(output_fd, b'@PJL SET SATURATION=4\x0a') |
| 317 |
|
| 318 |
def HPPJLInkBleed(self): |
| 319 |
pq = opts.get('HPPJLInkBleed', "") |
| 320 |
if pq == '-2' or pq == 'Default': |
| 321 |
os.write(output_fd, b'@PJL SET INKBLEED=0\x0a') |
| 322 |
elif pq == '-1': |
| 323 |
os.write(output_fd, b'@PJL SET INKBLEED=2\x0a') |
| 324 |
elif pq == '0' or pq == 'Less': |
| 325 |
os.write(output_fd, b'@PJL SET INKBLEED=4\x0a') |
| 326 |
elif pq == '+1': |
| 327 |
os.write(output_fd, b'@PJL SET INKBLEED=6\x0a') |
| 328 |
elif pq == 'Least': |
| 329 |
os.write(output_fd, b'@PJL SET INKBLEED=7\x0a') |
| 330 |
elif pq == '+2': |
| 331 |
os.write(output_fd, b'@PJL SET INKBLEED=8\x0a') |
| 332 |
else: |
| 333 |
os.write(output_fd, b'@PJL SET INKBLEED=4\x0a') |
| 334 |
|
| 335 |
def HPPJLColorAsGray(self): |
| 336 |
pq = opts.get('HPPJLColorAsGray', "") |
| 337 |
if pq == 'off' or pq == 'False': |
| 338 |
os.write(output_fd, b'@PJL SET GRAYSCALE=OFF\x0a') |
| 339 |
elif pq == 'HighQuality': |
| 340 |
os.write(output_fd, b'@PJL SET GRAYSCALE=COMPOSITE\x0a') |
| 341 |
os.write(output_fd, b'@PJL SET PLANESINUSE=1\x0a') |
| 342 |
elif pq == 'BlackInkOnly': |
| 343 |
os.write(output_fd, b'@PJL SET GRAYSCALE=BLACKONLY\x0a') |
| 344 |
os.write(output_fd, b'@PJL SET PLANESINUSE=1\x0a') |
| 345 |
elif pq == 'yes' or pq == 'on' : |
| 346 |
os.write(output_fd, b'@PJL SET RESOLUTION=600\x0a') |
| 347 |
os.write(output_fd, b'@PJL SET BITSPERPIXEL=8\x0a') |
| 348 |
os.write(output_fd, b'@PJL SET GRAYSCALE=COMPOSITE\x0a') |
| 349 |
elif pq == 'no': |
| 350 |
os.write(output_fd, b'@PJL SET RESOLUTION=600\x0a') |
| 351 |
os.write(output_fd, b'@PJL SET BITSPERPIXEL=8\x0a') |
| 352 |
elif pq == 'Plain' or pq == 'True': # for device 586 and device 586 flow |
| 353 |
os.write(output_fd, b'@PJL SET PLANESINUSE=1\x0a') |
| 354 |
# elif pq == 'check': # for device 556 no value is passed |
| 355 |
# os.write(output_fd, b'') |
| 356 |
else: |
| 357 |
os.write(output_fd, b'@PJL SET GRAYSCALE=OFF\x0a') |
| 358 |
|
| 359 |
def HPPJLTrueBlack(self): |
| 360 |
pq = opts.get('HPPJLTrueBlack', "") |
| 361 |
if pq == 'HighQuality': |
| 362 |
os.write(output_fd, b'@PJL SET GRAYSCALE=COMPOSITE\x0a') |
| 363 |
elif pq == 'BlackInkOnly': |
| 364 |
os.write(output_fd, b'@PJL SET GRAYSCALE=BLACKONLY\x0a') |
| 365 |
else: |
| 366 |
os.write(output_fd, b'@PJL SET GRAYSCALE=BLACKONLY\x0a') |
| 367 |
|
| 368 |
try: |
| 369 |
header() |
| 370 |
if 'HPPinPrnt' in options and 'noHPPinPrnt' not in options: |
| 371 |
secure_print() |
| 372 |
hpps_filter = HPPS_Filter() |
| 373 |
for x in key_list: |
| 374 |
(getattr(hpps_filter, x))() |
| 375 |
|
| 376 |
os.write(output_fd, UEL) |
| 377 |
while True: |
| 378 |
data = os.read(0, 4096) |
| 379 |
if not data: |
| 380 |
break |
| 381 |
os.write(output_fd, data) |
| 382 |
os.write(output_fd, END_JOB) |
| 383 |
|
| 384 |
except: |
| 385 |
bug('HPPS filter failure') |
| 386 |
sys.exit(CUPS_FILTER_FAILED) |
| 387 |
sys.exit(CUPS_FILTER_OK) |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|