206 lines
5.9 KiB
Python
Executable File
206 lines
5.9 KiB
Python
Executable File
#!@PYTHON@
|
|
# -*- mode: python -*-
|
|
#
|
|
# capisuitefax - capisuite tool for enqueuing faxes
|
|
# ---------------------------------------------------
|
|
# copyright : (C) 2002 by Gernot Hillier
|
|
# email : gernot@hillier.de
|
|
# version : $Revision: 1.7 $
|
|
#
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
# (at your option) any later version.
|
|
|
|
import getopt, os, sys, pwd, string, commands
|
|
# capisuite stuff
|
|
import capisuite.fax
|
|
import capisuite.config
|
|
|
|
def usage(error=""):
|
|
print """capisuitefax - capisuite tool for enqueueing faxes
|
|
|
|
usage:
|
|
capisuitefax [<send options>] -d <number> file1 [file2...] or
|
|
capisuitefax [-q] -a <id>
|
|
capisuitefax -l
|
|
capisuitefax -h
|
|
|
|
possible send options are:
|
|
|
|
-d <dial>, --dialstring=<dial> send fax to this number (mandatory)
|
|
-n, --noprefix ignore configured dial prefix for this call
|
|
(for internal calls)
|
|
-u <user>, --user=<user> send fax as <user> (only when called as root!)
|
|
-A <addr>, --addressee=<addr> addressee (for informational purposes)
|
|
-S <subj>, --subject=<subj> some subject (for informational purposes)
|
|
|
|
other options:
|
|
|
|
-q, --quiet be quiet, don't output informational messages
|
|
-a <id>, --abort=<id> abort fax job with id (id is a number)
|
|
-h, --help print this usage information
|
|
-l, --list print jobs in the send queue
|
|
|
|
The given files must be in Adobe PostScript or PDF format"""
|
|
if error:
|
|
print
|
|
print "ERROR:",error
|
|
sys.exit(1)
|
|
|
|
|
|
def convert2Fax(filenames, faxname):
|
|
# todo: clean this up, maybe rewrite it
|
|
"""
|
|
convert file 'filename' to a SFF fax file 'faxfile'.
|
|
"""
|
|
def checkFile(fname, fname_):
|
|
if not os.access(fname, os.R_OK):
|
|
print >> sys.stderr, "can't open", fname
|
|
return 0
|
|
status, filetype = commands.getstatusoutput(
|
|
"file -b -i %s 2>/dev/null" % fname_)
|
|
if status:
|
|
usage("Error when executing command 'file'")
|
|
return 0
|
|
if filetype.find("application/postscript") < 0 \
|
|
and filetype.find("application/pdf") < 0:
|
|
print >> sys.stderr, arg, "is not a PostScript/PDF file"
|
|
return 0
|
|
return 1
|
|
|
|
# todo: catch errors!
|
|
files = []
|
|
for fname in filenames:
|
|
fname_ = commands.mkarg(fname)
|
|
if not checkFile(fname, fname_):
|
|
return 0
|
|
files.append(fname_)
|
|
command = "gs -dNOPAUSE -dQUIET -dBATCH -sPAPERSIZE=a4 -sDEVICE=cfax " \
|
|
"-sOutputFile=%s %s" % (faxname, ' '.join(files))
|
|
ret = os.system(command) >> 8
|
|
if ret:
|
|
print >> sys.stderr, "error during SFF-conversion ot file", arg
|
|
print >> sys.stderr, "Ghostscript not installed?"
|
|
sys.exit()
|
|
|
|
|
|
def showQueue(config, user):
|
|
try:
|
|
jobs = capisuite.fax.getQueueDetails(config, user)
|
|
except capisuite.config.NoOptionError:
|
|
print "ERROR: option fax_user_dir not set in fax configuration"
|
|
return 10
|
|
if not jobs:
|
|
print "--- queue empty ---"
|
|
else:
|
|
format = "%s\t%s %2s\t%-24s %s"
|
|
print format % ('ID', 'Numb./Addr.', 'Try', 'Next try', 'Subject')
|
|
for entry in jobs:
|
|
print format % entry
|
|
return 0
|
|
|
|
|
|
def abortJob(config, user, job):
|
|
# todo: test whether 'job' is an integer
|
|
try:
|
|
capisuite.fax.abortUserJob(config, user, jobnum=job)
|
|
except capisuite.fax.InvalidJob:
|
|
print "job to abort not valid"
|
|
return 1
|
|
except capisuite.fax.JobLockedError:
|
|
print "Job is currently in transmission. Can't abort."
|
|
return 1
|
|
return 0
|
|
|
|
try:
|
|
optlist,args = getopt.getopt(sys.argv[1:], "d:a:u:lhqnA:S:",
|
|
['dialstring=','noprefix','help',"abort=","list","quiet","user=",
|
|
'addressee=','subject='])
|
|
|
|
except getopt.GetoptError, e:
|
|
usage(e.msg)
|
|
sys.exit(1)
|
|
|
|
# read options
|
|
dialstring = addressee = subject = abort = user = ""
|
|
quiet = listqueue = 0
|
|
useprefix = 1
|
|
|
|
for option, param in optlist:
|
|
# commands
|
|
if option in ('-h','--help'): usage()
|
|
elif option in ('-l','--list'): listqueue = 1
|
|
elif option in ('-a','--abort'): abort = param
|
|
# send options
|
|
elif option in ('-d','--dialstring'): dialstring = param
|
|
elif option in ('-A','--addressee'): addressee = param
|
|
elif option in ('-S','--subject'): subject = param
|
|
elif option in ('-n','--noprefix'): useprefix = 0
|
|
elif option in ('-q','--quiet'): quiet = 1
|
|
elif option in ('-u','--user'):
|
|
if not os.getuid():
|
|
user = param
|
|
else:
|
|
usage("--user may only used as root!")
|
|
|
|
if not abort and not listqueue and not dialstring:
|
|
usage("No usable command given.")
|
|
|
|
config = capisuite.config.readGlobalConfig()
|
|
if not user:
|
|
user = pwd.getpwuid(os.getuid()).pw_name
|
|
|
|
if listqueue:
|
|
sys.exit(showQueue(config, user))
|
|
elif abort:
|
|
try:
|
|
jobnum = int(abort)
|
|
except ValueError:
|
|
print "Error: job id has to be a number, but is %r" % abort
|
|
sys.exit(1)
|
|
sys.exit(abortJob(config, user, jobnum))
|
|
else:
|
|
# queue fax for sending
|
|
|
|
# filter out common separators from dialstring, check it
|
|
dialstring = dialstring.translate(string.maketrans("",""),"-/ ()")
|
|
for i in dialstring:
|
|
if not i in '+0123456789*#':
|
|
usage("Invalid dialstring given, character %r is not allowed." % i)
|
|
|
|
if dialstring and len(args)==0:
|
|
usage("No fax files given")
|
|
|
|
# test if this user is allowed to send faxes
|
|
if not config.has_section(user):
|
|
print "Sorry, you're no valid user for CapiSuite"
|
|
sys.exit(1)
|
|
|
|
if not config.has_option(user, "outgoing_MSN") and \
|
|
not config.has_option(user, "fax_numbers"):
|
|
print "Sorry, you're not allowed to use fax services"
|
|
sys.exit(1)
|
|
|
|
if useprefix:
|
|
dialstring = config.getUser(user, "dial_prefix", '') + dialstring
|
|
|
|
# convert and enqueue files
|
|
jobDesc = {
|
|
"dialstring": dialstring,
|
|
#"user": user,
|
|
"addressee": addressee,
|
|
"subject": subject,
|
|
# todo: allow these to be specified for this jobs
|
|
#'stationID': ...
|
|
#'headline': ...
|
|
}
|
|
try:
|
|
jobnum = capisuite.fax.enqueueJob(config, user, args,
|
|
convert2Fax, **jobDesc)
|
|
except IOError, e:
|
|
print e
|
|
print "can't write to queue dir"
|
|
sys.exit(1)
|
|
print "Successful enqueued as job", jobnum, "for", dialstring
|