2
# -*- coding: utf-8 -*-
4
# (c) Copyright 2003-2015 HP Development Company, L.P.
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.
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.
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
24
__title__ = 'CUPS Fax Backend (hpfax:)'
25
__doc__ = "CUPS backend for PC send fax. Generally this backend is run by CUPS, not directly by a user. To send a fax as a user, run hp-sendfax or print to the device's CUPS fax queue."
36
if sys.version_info[0] == 3:
39
import ConfigParser as configparser
41
CUPS_BACKEND_OK = 0 # Job completed successfully
42
CUPS_BACKEND_FAILED = 1 # Job failed, use error-policy
43
CUPS_BACKEND_AUTH_REQUIRED = 2 # Job failed, authentication required
44
CUPS_BACKEND_HOLD = 3 # Job failed, hold job
45
CUPS_BACKEND_STOP = 4 # Job failed, stop queue
46
CUPS_BACKEND_CANCEL = 5 # Job failed, cancel job
52
config_file = '/etc/hp/hplip.conf'
57
syslog.syslog("hpfax[%d]: error: %s\n" % (pid, msg))
58
log.stderr("ERROR: %s\n" % msg)
61
if os.path.exists(config_file):
62
config = configparser.ConfigParser()
63
config.read(config_file)
66
home_dir = config.get('dirs', 'home')
68
bug("Error setting home directory: home= under [dirs] not found.")
71
bug("Error setting home directory: /etc/hp/hplip.conf not found")
74
if not home_dir or not os.path.exists(home_dir):
75
bug("Error setting home directory: Home directory %s not found." % home_dir)
78
sys.path.insert(0, home_dir)
84
from base.codes import *
85
from base import device
86
from base import utils
88
except ImportError as e:
89
bug("Error importing HPLIP modules: %s\n" % (pid, e))
93
syslog.syslog("SIGPIPE!")
96
USAGE = [(__doc__, "", "para", True),
97
("Usage: hpfax [job_id] [username] [title] [copies] [options]", "", "summary", True),
99
utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
103
def usage(typ='text'):
105
utils.log_title(__title__, __version__)
107
utils.format_text(USAGE, typ, title=__title__, crumb='hpfax:')
108
sys.exit(CUPS_BACKEND_OK)
110
# Send dbus event to hpssd on dbus system bus
111
def send_message(device_uri, printer_name, event_code, username, job_id, title, pipe_name=''):
112
args = [device_uri, printer_name, event_code, username, job_id, title, pipe_name]
113
msg = lowlevel.SignalMessage('/', 'com.hplip.StatusService', 'Event')
114
msg.append(signature='ssisiss', *args)
116
SystemBus().send_message(msg)
120
opts, args = getopt.getopt(sys.argv[1:], 'l:hg', ['level=', 'help', 'help-rest', 'help-man'])
122
except getopt.GetoptError:
127
if o in ('-l', '--logging'):
128
log_level = a.lower().strip()
129
log.set_level(log_level)
132
log.set_level('debug')
134
elif o in ('-h', '--help'):
137
elif o == '--help-rest':
140
elif o == '--help-man':
145
cups11 = utils.to_bool(sys_conf.get('configure', 'cups11', '0'))
148
probed_devices = device.probeDevices(['usb', 'par'], filter={'fax-type': (operator.gt, 0)})
150
sys.exit(CUPS_BACKEND_FAILED)
153
for uri in probed_devices:
155
back_end, is_hp, bus, model, serial, dev_file, host, zc, port = \
156
device.parseDeviceURI(uri)
160
mq = device.queryModelByModel(model)
162
if mq.get('fax-type', FAX_TYPE_NONE) in (FAX_TYPE_MARVELL,):
165
print('direct %s "HP Fax 3" "%s USB %s HP Fax HPLIP" "MFG:HP;MDL:Fax 3;DES:HP Fax 3;"' % \
166
(uri.replace("hp:", "hpfax:"), model.replace('_', ' '), serial))
169
print('direct %s "HP Fax 3" "%s LPT HP Fax HPLIP" "MFG:HP;MDL:Fax 3;DES:HP Fax 3;"' % \
170
(uri.replace("hp:", "hpfax:"), model.replace('_', ' ')))
172
elif mq.get('fax-type', FAX_TYPE_NONE) in (FAX_TYPE_SOAP,) or mq.get('fax-type', FAX_TYPE_NONE) in (FAX_TYPE_LEDMSOAP,):
175
print('direct %s "HP Fax 2" "%s USB %s HP Fax HPLIP" "MFG:HP;MDL:Fax 2;DES:HP Fax 2;"' % \
176
(uri.replace("hp:", "hpfax:"), model.replace('_', ' '), serial))
179
print('direct %s "HP Fax 2" "%s LPT HP Fax HPLIP" "MFG:HP;MDL:Fax 2;DES:HP Fax 2;"' % \
180
(uri.replace("hp:", "hpfax:"), model.replace('_', ' ')))
181
elif mq.get('fax-type', FAX_TYPE_NONE) in (FAX_TYPE_LEDM,):
184
print('direct %s "HP Fax 4" "%s USB %s HP Fax HPLIP" "MFG:HP;MDL:Fax 4;DES:HP Fax 4;"' % \
185
(uri.replace("hp:", "hpfax:"), model.replace('_', ' '), serial))
188
print('direct %s "HP Fax 4" "%s LPT HP Fax HPLIP" "MFG:HP;MDL:Fax 4;DES:HP Fax 4;"' % \
189
(uri.replace("hp:", "hpfax:"), model.replace('_', ' ')))
194
print('direct %s "HP Fax" "%s USB %s HP Fax HPLIP" "MFG:HP;MDL:Fax;DES:HP Fax;"' % \
195
(uri.replace("hp:", "hpfax:"), model.replace('_', ' '), serial))
198
print('direct %s "HP Fax" "%s LPT HP Fax HPLIP" "MFG:HP;MDL:Fax;DES:HP Fax;"' % \
199
(uri.replace("hp:", "hpfax:"), model.replace('_', ' ')))
203
if good_devices == 0:
205
print('direct hpfax:/no_device_found "HP Fax" "no_device_found" ""')
207
print('direct hpfax "Unknown" "HP Fax (HPLIP)" ""')
209
sys.exit(CUPS_BACKEND_OK)
215
from dbus import SystemBus, lowlevel
217
bug("HPLIP pc send fax requires dbus and python-dbus")
218
sys.exit(CUPS_BACKEND_FAILED)
221
# Ignore: .../dbus/connection.py:242: DeprecationWarning: object.__init__() takes no parameters
222
# (occurring on Python 2.6/dBus 0.83/Ubuntu 9.04)
223
warnings.simplefilter("ignore", DeprecationWarning)
225
# CUPS provided environment
227
device_uri = os.environ['DEVICE_URI']
228
printer_name = os.environ['PRINTER']
230
bug("Improper environment: Must be run by CUPS.")
231
sys.exit(CUPS_BACKEND_FAILED)
236
job_id, username, title, copies, options = args[0:5]
239
bug("Invalid command line: invalid arguments.")
240
sys.exit(CUPS_BACKEND_FAILED)
242
send_message(device_uri, printer_name, EVENT_START_FAX_PRINT_JOB, username, job_id, title)
245
input_fd = open(args[5], 'r')
249
if os.path.exists("/home/%s/.hplip"%username):
250
tmp_dir = "/home/%s/.hplip"%username
254
pipe_name = os.path.join(tmp_dir, "hp_fax-pipe-%d" % job_id)
256
# Create the named pipe. Make sure it exists before sending
265
# Send dbus event to hpssd
266
send_message(device_uri, printer_name, EVENT_FAX_RENDER_COMPLETE, username, job_id, title, pipe_name)
269
pipe = os.open(pipe_name, os.O_WRONLY)
273
data = os.read(input_fd, PIPE_BUF)
279
#syslog.syslog("Writing %d to pipe..." % len(data))
280
bytes_read += len(data)
283
bug("No data on input file descriptor.")
284
sys.exit(CUPS_BACKEND_FAILED)
290
send_message(device_uri, printer_name, EVENT_END_FAX_PRINT_JOB, username, job_id, title)
292
sys.exit(CUPS_BACKEND_OK)