home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / cups / backend / hpfax < prev    next >
Encoding:
Text File  |  2006-08-30  |  6.7 KB  |  221 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2006 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__ = '1.1'
  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.  
  34. config_file = '/etc/hp/hplip.conf'
  35. home_dir = ''
  36.  
  37. if os.path.exists(config_file):
  38.     config = ConfigParser.ConfigParser()
  39.     config.read(config_file)
  40.  
  41.     try:
  42.         home_dir = config.get('dirs', 'home')
  43.     except:
  44.         syslog.syslog(syslog.LOG_CRIT, "hpfax: Error setting home directory: home= under [dirs] not found.")
  45.         sys.exit(1)
  46. else:
  47.     syslog.syslog(syslog.LOG_CRIT, "hpfax: Error setting home directory: /etc/hp/hplip.conf not found.")
  48.     sys.exit(1)
  49.  
  50. if not home_dir or not os.path.exists(home_dir):
  51.     syslog.syslog(syslog.LOG_CRIT, "hpfax: Error setting home directory: home directory %s not found." % home_dir)
  52.     sys.exit(1)
  53.  
  54. sys.path.insert( 0, home_dir )
  55.  
  56. try:
  57.     from base.g import *
  58.     from base.codes import *
  59.     from base import device, utils, msg
  60.     from base.service import sendEvent
  61. except ImportError:
  62.     syslog.syslog(syslog.LOG_CRIT, "Error importing HPLIP modules.")
  63.     sys.exit(1)
  64.  
  65.  
  66. USAGE = [(__doc__, "", "para", True),
  67.          ("Usage: hpfax [OPTIONS] [job_id] [username] [title] [copies] [options]", "", "summary", True),
  68.          utils.USAGE_OPTIONS,
  69.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2,
  70.          utils.USAGE_HELP,
  71.         ]
  72.  
  73. def usage(typ='text'):
  74.     if typ == 'text':
  75.         utils.log_title(__title__, __version__)
  76.         
  77.     utils.format_text(USAGE, typ, title=__title__, crumb='hpfax:')
  78.     sys.exit(0)        
  79.     
  80.  
  81. try:
  82.     opts, args = getopt.getopt(sys.argv[1:], 'l:h', ['level=', 'help', 'help-rest', 'help-man'])
  83.  
  84. except getopt.GetoptError:
  85.     usage()
  86.  
  87. for o, a in opts:
  88.  
  89.     if o in ('-l', '--logging'):
  90.         log_level = a.lower().strip()
  91.         log.set_level(log_level)
  92.  
  93.     elif o in ('-h', '--help'):
  94.         usage()
  95.         
  96.     elif o == '--help-rest':
  97.         usage('rest')
  98.     
  99.     elif o == '--help-man':
  100.         usage('man')
  101.         
  102.  
  103. log.set_module("hpfax")
  104.  
  105. if len( args ) == 0:
  106.     try:
  107.         devices = device.probeDevices(sock=None, bus='usb,par', timeout=5,
  108.                                       ttl=4, filter='fax', format='cups')
  109.     except Error:
  110.         log.stderr("ERROR: Unable to contact HPLIP I/O (hpssd).")
  111.         sys.exit(1)
  112.  
  113.     if len(devices):
  114.         for d in devices:
  115.             print d.replace('hp:/', 'hpfax:/')
  116.     else:
  117.         print 'direct hpfax:/no_device_found "Unknown" "hpfax no_device_found"'
  118.         
  119.     sys.exit(0)
  120.  
  121. else:
  122.     # CUPS provided environment
  123.     try:
  124.         device_uri = os.environ['DEVICE_URI']
  125.         printer_name = os.environ['PRINTER']
  126.     except KeyError:
  127.         log.stderr("ERROR: Improper environment: Must be run by CUPS.")
  128.         sys.exit(1)
  129.         
  130.     log.debug(args)
  131.     
  132.     try:
  133.         job_id, username, title, copies, options = args[0:5]
  134.     except IndexError:
  135.         log.stderr("ERROR: Invalid command line: Invalid arguments.")
  136.         sys.exit(1)
  137.         
  138.     try:
  139.         input_fd = file(args[5], 'r')
  140.     except IndexError:
  141.         input_fd = 0
  142.         
  143.     #log.error("INFO: URI=%s, Printer=%s, Job ID=%s, User=%s, Title=%s, Copies=%s, Options=%s" % \
  144.     #    (device_uri, printer_name, job_id, username, title, copies, options))
  145.  
  146.     pdb = pwd.getpwnam(username)
  147.     home_folder, uid, gid = pdb[5], pdb[2], pdb[3]
  148.     
  149.     #log.error("INFO: User home=%s, Uid=%d, Gid=%d" % (home_folder, uid, gid))
  150.     
  151.     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  152.     try:
  153.         sock.connect((prop.hpssd_host, prop.hpssd_port))
  154.     except socket.error:
  155.         log.error("ERROR: Unable to contact HPLIP I/O (hpssd).")
  156.         sys.exit(1)
  157.  
  158.     try:
  159.         fields, data, result_code = \
  160.             msg.xmitMessage(sock, "HPFaxBegin", 
  161.                                  None,
  162.                                  {"username": username,
  163.                                   "job-id": job_id,
  164.                                   "device-uri": device_uri,
  165.                                   "printer": printer_name,
  166.                                   "title": title,
  167.                                  })
  168.                            
  169.     except Error:
  170.         log.stderr("ERROR: Unable to send event to HPLIP I/O (hpssd).")
  171.         sys.exit(1) 
  172.    
  173.     if result_code == ERROR_GUI_NOT_AVAILABLE:
  174.         # New behavior in 0.9.11
  175.         log.stderr("ERROR: You must run hp-sendfax first. Run hp-sendfax now and then restart this queue to continue.")
  176.         
  177.         sendEvent(sock, EVENT_ERROR_FAX_MUST_RUN_SENDFAX_FIRST, 'event',
  178.                   job_id, username, device_uri)
  179.         
  180.         sys.exit(1)
  181.  
  182.     bytes_read = 0
  183.     while True:
  184.         data = os.read(input_fd, prop.max_message_len)
  185.         
  186.         if not data:
  187.             fields, data, result_code = \
  188.                 msg.xmitMessage(sock, "HPFaxEnd", 
  189.                                      None,
  190.                                      {"username": username,
  191.                                       "job-id": job_id,
  192.                                       "printer": printer_name,
  193.                                       "title": title,
  194.                                       "options": options,
  195.                                       "device-uri": device_uri,
  196.                                       "job-size": bytes_read,
  197.                                      })
  198.             
  199.             break
  200.                                    
  201.             
  202.         bytes_read += len(data) 
  203.         
  204.         fields, data, result_code = \
  205.             msg.xmitMessage(sock, "HPFaxData", 
  206.                                  data,
  207.                                  {"username": username,
  208.                                   "job-id": job_id,
  209.                                  })
  210.  
  211.     os.close(input_fd)
  212.     
  213.     if not bytes_read:
  214.         log.error("No data!")
  215.         sys.exit(1)
  216.     
  217.     sock.close()
  218.     sys.exit(0)
  219.     
  220.     
  221.