home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / cups / backend / hpfax < prev    next >
Encoding:
Text File  |  2007-04-04  |  7.8 KB  |  254 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2007 Hewlett-Packard 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: Don Welch
  21. #
  22.  
  23. __version__ = '2.6'
  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."
  26.  
  27. import sys
  28. import getopt
  29. import ConfigParser
  30. import os.path, os
  31. import socket
  32. import syslog
  33. import time
  34.  
  35. CUPS_BACKEND_OK = 0 # Job completed successfully
  36. CUPS_BACKEND_FAILED = 1 # Job failed, use error-policy
  37. CUPS_BACKEND_AUTH_REQUIRED = 2 # Job failed, authentication required
  38. CUPS_BACKEND_HOLD = 3 # Job failed, hold job
  39. CUPS_BACKEND_STOP = 4 #  Job failed, stop queue
  40. CUPS_BACKEND_CANCEL = 5 # Job failed, cancel job
  41.  
  42. pid = os.getpid()
  43. config_file = '/etc/hp/hplip.conf'
  44. home_dir = ''
  45.  
  46. if os.path.exists(config_file):
  47.     config = ConfigParser.ConfigParser()
  48.     config.read(config_file)
  49.  
  50.     try:
  51.         home_dir = config.get('dirs', 'home')
  52.     except:
  53.         syslog.syslog(syslog.LOG_CRIT, "hpfax[%d]: error: Error setting home directory: home= under [dirs] not found." % pid)
  54.         sys.exit(1)
  55. else:
  56.     syslog.syslog(syslog.LOG_CRIT, "hpfax[%d]: error: Error setting home directory: /etc/hp/hplip.conf not found." % pid)
  57.     sys.exit(1)
  58.  
  59. if not home_dir or not os.path.exists(home_dir):
  60.     syslog.syslog(syslog.LOG_CRIT, "hpfax[%d]: error: Error setting home directory: Home directory %s not found." % (pid, home_dir))
  61.     sys.exit(1)
  62.  
  63. sys.path.insert( 0, home_dir )
  64.  
  65. try:
  66.     from base.g import *
  67.     from base.codes import *
  68.     from base import device, utils, msg
  69.     from base.service import sendEvent
  70.     from prnt import cups
  71. except ImportError:
  72.     syslog.syslog(syslog.LOG_CRIT, "hpfax[%d]: error: Error importing HPLIP modules." % pid)
  73.     sys.exit(1)
  74.  
  75. log.set_module("hpfax")
  76.  
  77. USAGE = [(__doc__, "", "para", True),
  78.          ("Usage: hpfax [OPTIONS] [job_id] [username] [title] [copies] [options]", "", "summary", True),
  79.          utils.USAGE_OPTIONS,
  80.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  81.          utils.USAGE_HELP,
  82.         ]
  83.  
  84. def usage(typ='text'):
  85.     if typ == 'text':
  86.         utils.log_title(__title__, __version__)
  87.  
  88.     utils.format_text(USAGE, typ, title=__title__, crumb='hpfax:')
  89.     sys.exit(CUPS_BACKEND_OK)        
  90.  
  91.  
  92. try:
  93.     opts, args = getopt.getopt(sys.argv[1:], 'l:hg', ['level=', 'help', 'help-rest', 'help-man'])
  94.  
  95. except getopt.GetoptError:
  96.     usage()
  97.  
  98. for o, a in opts:
  99.  
  100.     if o in ('-l', '--logging'):
  101.         log_level = a.lower().strip()
  102.         log.set_level(log_level)
  103.  
  104.     elif o == '-g':
  105.         log.set_level('debug')
  106.  
  107.     elif o in ('-h', '--help'):
  108.         usage()
  109.  
  110.     elif o == '--help-rest':
  111.         usage('rest')
  112.  
  113.     elif o == '--help-man':
  114.         usage('man')
  115.  
  116.  
  117. if len( args ) == 0:
  118.     cups11 = utils.to_bool(sys_cfg.configure.cups11, False)
  119.     
  120.     try:
  121.         probed_devices = device.probeDevices(None, 'usb,par', filter='fax')
  122.     except Error:
  123.         log.stderr("hpfax[%d]: error: Unable to contact HPLIP I/O (hpssd)." % pid)
  124.         sys.exit(CUPS_BACKEND_FAILED)
  125.  
  126.     good_devices = 0
  127.     for uri in probed_devices:
  128.         try:
  129.             back_end, is_hp, bus, model, serial, dev_file, host, port = \
  130.                 device.parseDeviceURI(uri)
  131.         except Error:
  132.             continue
  133.  
  134.         print 'direct %s "HP Fax" "%s HP Fax HPLIP" "MFG:HP;MDL:Fax;DES:HP Fax;"' % \
  135.             (uri.replace("hp:", "hpfax:"), model.replace("_", " "))
  136.             
  137.         good_devices += 1
  138.  
  139.     if good_devices == 0:
  140.         if cups11:
  141.             print 'direct hpfax:/no_device_found "HP Fax" "no_device_found" ""'
  142.         else:
  143.             print 'direct hpfax "Unknown" "HP Fax (HPLIP)" ""' 
  144.  
  145.     sys.exit(CUPS_BACKEND_OK)
  146.  
  147. else:
  148.     # CUPS provided environment
  149.     try:
  150.         device_uri = os.environ['DEVICE_URI']
  151.         printer_name = os.environ['PRINTER']
  152.     except KeyError:
  153.         log.stderr("hpfax[%d]: error: Improper environment: Must be run by CUPS." % pid)
  154.         sys.exit(CUPS_BACKEND_FAILED)
  155.  
  156.     log.debug(args)
  157.  
  158.     try:
  159.         job_id, username, title, copies, options = args[0:5]
  160.     except IndexError:
  161.         log.stderr("hpfax[%d]: error: Invalid command line: Invalid arguments." % pid)
  162.         sys.exit(CUPS_BACKEND_FAILED)
  163.  
  164.     try:
  165.         input_fd = file(args[5], 'r')
  166.     except IndexError:
  167.         input_fd = 0
  168.  
  169.     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  170.     try:
  171.         sock.connect((prop.hpssd_host, prop.hpssd_port))
  172.     except socket.error:
  173.         log.stderr("hpfax[%d]: error: Unable to contact HPLIP I/O (hpssd)." % pid)
  174.         sys.exit(CUPS_BACKEND_FAILED)
  175.  
  176.     fax_data = os.read(input_fd, prop.max_message_len)
  177.  
  178.     if not len(fax_data):
  179.         log.stderr("hpfax[%d]: error: No data!" % pid)
  180.  
  181.         sendEvent(sock, EVENT_ERROR_NO_DATA_AVAILABLE, 'error',
  182.                   job_id, username, device_uri)
  183.  
  184.         sock.close()
  185.         sys.exit(CUPS_BACKEND_FAILED)
  186.  
  187.  
  188.     sendEvent(sock, EVENT_START_FAX_PRINT_JOB, 'event',
  189.               job_id, username, device_uri)
  190.  
  191.     while True:
  192.         try:
  193.             fields, data, result_code = \
  194.                 msg.xmitMessage(sock, "HPFaxBegin", 
  195.                                      None,
  196.                                      {"username": username,
  197.                                       "job-id": job_id,
  198.                                       "device-uri": device_uri,
  199.                                       "printer": printer_name,
  200.                                       "title": title,
  201.                                      })
  202.  
  203.         except Error:
  204.             log.stderr("hpfax[%d]: error: Unable to send event to HPLIP I/O (hpssd)." % pid)
  205.             sys.exit(CUPS_BACKEND_FAILED) 
  206.  
  207.         if result_code == ERROR_GUI_NOT_AVAILABLE:
  208.             # New behavior in 1.6.6a (10sec retry)
  209.             log.stderr("hpfax[%d]: error: You must run hp-sendfax first. Run hp-sendfax now to continue. Fax will resume within 10 seconds." % pid)
  210.  
  211.             sendEvent(sock, EVENT_ERROR_FAX_MUST_RUN_SENDFAX_FIRST, 'event',
  212.                       job_id, username, device_uri)
  213.  
  214.         else: # ERROR_SUCCESS
  215.             break
  216.  
  217.         time.sleep(10)
  218.  
  219.  
  220.     bytes_read = 0
  221.     while True:
  222.         if not len(fax_data):
  223.             fields, data, result_code = \
  224.                 msg.xmitMessage(sock, "HPFaxEnd", 
  225.                                      None,
  226.                                      {"username": username,
  227.                                       "job-id": job_id,
  228.                                       "printer": printer_name,
  229.                                       "title": title,
  230.                                       "options": options,
  231.                                       "device-uri": device_uri,
  232.                                       "job-size": bytes_read,
  233.                                      })
  234.  
  235.             break
  236.  
  237.  
  238.         bytes_read += len(fax_data) 
  239.  
  240.         fields, data, result_code = \
  241.             msg.xmitMessage(sock, "HPFaxData", 
  242.                                  fax_data,
  243.                                  {"username": username,
  244.                                   "job-id": job_id,
  245.                                  })
  246.  
  247.         fax_data = os.read(input_fd, prop.max_message_len)
  248.  
  249.     os.close(input_fd)
  250.     sock.close()
  251.     sys.exit(CUPS_BACKEND_OK)
  252.  
  253.  
  254.