]> git.ipfire.org Git - thirdparty/HylaFAX.git/commitdiff
Add templated version of notify, using common-functions
authorAidan Van Dyk <aidan@ifax.com>
Sat, 2 Sep 2006 12:13:03 +0000 (12:13 +0000)
committerAidan Van Dyk <aidan@ifax.com>
Sat, 2 Sep 2006 12:13:03 +0000 (12:13 +0000)
util/common-functions.sh.in
util/notify.sh.in [new file with mode: 0644]

index c315886ea103f651a95c90af60774805b50f3c9e..08369582f6e8df63fbd9d892c156b47d69a97e08 100644 (file)
@@ -69,17 +69,15 @@ parseQfile()
     # inside such strings and must be put outside of them. We thus replace
     # (') with ('\'') which terminates the current string, adds a single
     # quote and starts a new string.
-    #
-    # New lines in eval could cause problems so we escape them. As with
-    # single quotes above, we must first close the current string, add
-    # the escaped new line (double quoted) and start a new string.
+#
+    # We no longer escape newlines, because we don't eval
     #
     # print out variable name and value so we can eval it in the shell
-    eval `($AWK -F: ' 
+    #
+    eval "`($AWK -F: ' 
     function p(varname,val)
     {
         gsub(/\\047/, "\047\\\\\047\047", val);
-        gsub(/\n/, "\047\042\\\\n\042\047", val);
         printf "%s=\\047%s\\047\n",varname,val
         printf "export %s\n",varname
     }
@@ -144,23 +142,22 @@ parseQfile()
     /^page:/    { p("pins_"++npins, $4); }
     /^data:/    { p("files_"++nfiles, $4); }
     /^poll/        { p("poll", " -p"); }
-    END { p("nfiles", nfiles); p("npins", npins) } ' $QFILE )`
+    END { p("nfiles", nfiles); p("npins", npins) } ' $QFILE )`"
 
 }
 
 parseFaxInfo()
 {
     # We use the same basic stuff as parseQfile(), but just to straight assignment
-    eval `$INFO -S '' -s '%s=!=!=!=!=!=!=!=!=' $1 | $AWK -F'=!=!=!=!=!=!=!=!=' '
+    eval "`$INFO -S '' -s '%s=!=!=!=!=!=!=!=!=' $1 | $AWK -F'=!=!=!=!=!=!=!=!=' '
     function p(varname,val)
     {
         gsub(/\\047/, "\047\\\\\047\047", val);
-        gsub(/\n/, "\047\042\\\\n\042\047", val);
         printf "%s=\\047%s\\047\n",varname,val
         printf "export %s\n",varname
     }
     { p(toupper($1),$2); }
-    '`
+    '`"
 }
 
 #
@@ -269,3 +266,131 @@ CreateMailMessage ()
        echo "--$MIMEBOUNDARY--"
     fi
 }
+
+
+## A Simple function to trace stuff
+## to STDERR, not STDOUT
+TraceLog ()
+{
+    echo $* 1>&2
+}
+
+## ConvertFile <file> <oldtype> <newtype>
+## Convert $1 from $2 to type $3
+##
+## This will (possibly) create a tempoary file
+## in the Private $TMPDIR area for the new file
+## If the original file is in the proper form, no
+## new file will be created
+##
+## It prints the file name of the converted
+## file to stdout
+ConvertFile ()
+{
+    case "$3" in
+       PostScript|postscript|PS|ps)
+           TraceLog "Converting $1 to Postscript"
+           case $2 in
+               PostScript|Postscript|PS|ps)
+                   TraceLog "No conversion"
+                   echo $1
+                   ;;
+               PDF|pdf)
+                   TraceLog "pdf2ps necessary"
+                   name="`basename $1 .pdf`"
+                   $PDF2PS $1 $TMPDIR/$name.ps
+                   echo "$TMPDIR/$name.ps"
+                   ;;
+               TIFF|tiff|TIF|tif)
+                   TraceLog "tiff2ps necessary"
+                   name="`basename $1 .tif`"
+                   $TIFF2PS -a $1 > $TMPDIR/$name.ps
+                   echo "$TMPDIR/$name.ps"
+                   ;;
+           esac
+           ;;
+       PDF|pdf)
+           TraceLog "Converting $1 to PDF"
+           case $2 in
+               PostScript|Postscript|PS|ps)
+                   TraceLog "Using ps2pdf"
+                   name="`basename $1 .ps`"
+                   $PS2PDF $1 $TMPDIR/$name.pdf
+                   echo "$TMPDIR/$name.pdf"
+                   ;;
+               PDF|pdf)
+                   TraceLog "Nothing necessary"
+                   ;;
+               TIFF|tiff|TIF|tif)
+                   TraceLog "Using tiff2pdf"
+                   name="`basename $1 .tif`" $TIFF2PDF  -o $TMPDIR/$name.pdf $1 echo "$TMPDIR/$name.pdf"
+           esac
+           ;;
+       TIFF|TIF|tif|tiff)
+           TraceLog "Converting $1 to TIFF"
+           case $2 in 
+               PostScript|Postscript|PS|ps)
+                   TraceLog "Using ps2fax"
+                   name="`basename $1 .ps`"
+                   $PS2FAX -r $resolution $1 $TMPDIR/$name.tif
+                   echo "$TMPDIR/$name.tif"
+                   ;;
+               PDF|pdf)
+                   TraceLog "Using pdf2fax"
+                   name="`basename $1 .pdf`"
+                   $PDF2FAX -r $resolution $1 $TMPDIR/$name.tif
+                   echo "$TMPDIR/$name.tif"
+                   ;;
+               TIFF|tiff|TIF|tif)
+                   TraceLog "Nothing neccessary"
+                   ;;
+           esac
+           ;;
+       *)
+           TraceLog "Ignoring $1 to \"$3\""
+           ;;
+    esac
+
+}
+
+LocalSequence ()
+{
+    if [ $1 -gt $2 ]; then
+           return
+    fi
+    COUNT=$1
+    while [ $COUNT -le $2 ]
+    do
+       echo $COUNT
+       COUNT=`expr $COUNT + 1`
+    done
+}
+
+SetupPrivateTmp()
+{
+    TMPDIR="/tmp/tmp-$$"
+
+    mkdir -m 0700 "$TMPDIR"
+    if [ $? != 0 ]
+    then
+       echo "Couldn't setup private temp area - exiting!" 1>&2
+       exit 1
+    fi
+}
+
+CleanupPrivateTmp ()
+{
+    if [ -d "$TMPDIR" ]
+    then
+       case "$TMPDIR" in
+           "/tmp/tmp-$$")
+               rm -Rf "$TMPDIR"
+               ;;
+           *)
+               echo "Cleanup of temporary dir fail: Bad dir $TMPDIR" 1>&2
+               ;;
+       esac
+    fi
+}
+
+
diff --git a/util/notify.sh.in b/util/notify.sh.in
new file mode 100644 (file)
index 0000000..2f37114
--- /dev/null
@@ -0,0 +1,215 @@
+#!/bin/bash
+#
+# notify qfile why jobtime [nextTry]
+#
+# Return mail to the submitter of a job when notification is needed.
+#
+# This is a rewrite of the notify.sh.
+# This version uses the "template" capabilityes to send the e-mails.
+# FaxQueuer only calls notify if the job requested notification,
+# So if we are called, we *will* send an email (barring any errors)
+#
+# etc/FaxNotify controls *what* we e-mail
+# - TEMPLATE=subdir
+# - RETURNFILETYPE=raw|PDF|PS|TIFF
+#
+# It send the e-mail to $mailaddr, which is the mailaddr of the job.
+# The e-mail template used is selectec from:
+#   - etc/templates/$TEMPLATE/notify-$WHY-page.txt (only for pager jobs)
+#   - etc/templates/$TEMPLATE/notify-$WHY.txt
+# If it is a pager job, and notify-$WHY-page.txt does *not* exist, it
+# will use the notify-$WHY.txt template.
+#
+# It honours the RETURNFILETYPE varialbe that can be set by FaxNotify.
+# If it is set, it will return *all* the submitted documents as attachments
+# with the e-mail, converted to the RETURNFILETYPE.
+
+if [ $# != 3 ] && [ $# != 4 ]; then
+    echo "Usage: $0 qfile why jobtime [nextTry]"
+    exit 1
+fi
+
+test -f etc/setup.cache || {
+    SPOOL=`pwd`
+    cat<<EOF
+
+FATAL ERROR: $SPOOL/etc/setup.cache is missing!
+
+The file $SPOOL/etc/setup.cache is not present.  This
+probably means the machine has not been setup using the faxsetup(@MANNUM1_8@)
+command.  Read the documentation on setting up HylaFAX before you
+startup a server system.
+
+EOF
+    exit 1
+}
+
+# need to parse out the command line here.  some may be needed
+# in the FaxNotify.
+QFILE=$1
+WHY=$2
+JTIME=$3
+NEXT=${4:-'??:??'}
+
+# These settings may not be present in setup.cache if user upgraded and
+# didn't re-run faxsetup; we set them before calling setup.cache for
+# backward compatibility.
+ENCODING=base64
+MIMENCODE=mimencode
+TIFF2PDF=bin/tiff2pdf
+TTYCMD=tty
+CHARSET=us-ascii
+
+. etc/setup.cache
+. bin/common-functions
+
+INFO=$SBIN/faxinfo
+TIFFINFO=tiffinfo
+FAX2PS=$TIFFBIN/fax2ps
+TIFF2PS=tiff2ps
+PS2PDF=ps2pdf
+PDF2PS=pdf2ps
+PS2FAX=bin/ps2fax
+PDF2FAX=bin/pdf2fax
+TOADDR=FaxMaster
+FROMADDR=fax
+NOTIFY_FAXMASTER=never
+RETURNFILETYPE=
+MIMEBOUNDARY="NextPart$$" RETURNTECHINFO=yes 
+# Redirect errors to a tty, if possible, rather than
+# dev-nulling them or allowing them to creep into
+# the mail.
+#
+if $TTYCMD >/dev/null 2>&1; then
+    ERRORSTO=`$TTYCMD`
+else
+    ERRORSTO=/dev/null
+fi
+
+##
+## BuildAttachArgs <returnfiletype>
+##
+## Build the list of arguments to be passed on to the 
+## Functions which do the actual mailing/attaching
+##
+## It will conver to the requested <returnfiletype>
+##
+## The result is formatted like:
+## "file1" "type1" "name1" "description1" \
+##  ["file2" "type2" "name2" "description2"  [...] ]
+BuildAttachArgs ()
+{
+    case $1 in
+       PostScript|Postscript|PS|ps)
+           a_type="application/postscript"
+           a_desc="FAX Document (PostScript)"
+           ;;
+       PDF|pdf)
+           a_type="application/PDF"
+           a_desc="FAX Document (PDF)"
+           ;;
+       TIFF|tiff|TIF|tif)
+           a_type="image/tiff"
+           a_desc="FAX Document (TIFF)"
+           ;;
+    esac
+
+    for i in `LocalSequence 1 $nfiles`
+    do
+       eval filename="$"files_"$i"
+       TraceLog "FILE $i: $filename"
+       if [ -f "$filename" ]; then
+           eval filetype="$"filetype_"$i"
+           if [ "$1" = "raw" ]; then
+               a_file=$filename;
+               a_name=`basename $filename`;
+               a_desc="FAX Document"
+               case "$filetype" in
+                   PostScript)
+                       TraceLog "Attaching $file in Postscript format"
+                       a_type="application/postscript"
+                       ;;
+                   PDF)
+                       TraceLog "Attaching $file in PDF format"
+                       a_type="application/PDF"
+                       ;;
+                   TIFF)
+                       TraceLog "Attaching $file in PDF format"
+                       a_type="application/PDF"
+                       ;;
+                   *)
+                   TraceLog "Attaching $file in unknown[$filetype] format"
+                       a_type="application/unknown"
+                       ;;
+               esac
+           else
+               a_file=`ConvertFile "$filename" "$filetype" "$1"`
+               a_name=`basename $a_file`
+           fi
+
+       fi
+       if [ -f "$a_file" ]
+       then
+           ARGS="$ARGS"" \"$a_file\" \"$a_type\" \"$a_name\" \"$a_desc\""
+       fi
+    done
+    printf '%s' "$ARGS"
+}
+
+##########
+##  MAIN
+##########
+
+SetupPrivateTmp
+
+# we parse the q file fisrt in case any of the varialbe setting 
+# operations may want to know about the details of the fax
+parseQfile  
+
+# We need a few special things
+## - SESSION_LOG
+if [ -f "log/c$commid" ]; then
+       SESSION_LOG="`cat log/c$commid | grep -v '-- data'`"
+fi
+
+if [ "$doneop" = "default" ] ; then
+    doneop="remove"
+fi
+if [ "$jobtype" = "pager" ] ; then
+    number=$pagernum
+fi
+DESTINATION="$receiver"
+if [ -n "$DESTINATION" ]; then
+    DESTINATION="$DESTINATION ($number)"
+else
+    DESTINATION="$number"
+fi
+
+SENDTO=$mailaddr
+export SENDTO FROMADDR TOADDR SESSION_LOG DESTINATION
+
+#
+# Apply customizations.  All customizable variables should
+# be set to their non-customized defaults prior to this.
+#
+if [ -f etc/FaxNotify ]; then
+    # source notify preferecnes
+    . etc/FaxNotify
+fi
+
+
+
+if [ -n "$RETURNFILETYPE" ]; then
+    ATTACH_ARGS=`BuildAttachArgs $RETURNFILETYPE`
+fi
+
+template="etc/templates/$TEMPLATE/notify-$WHY.txt"
+if [ "$jobtype" != "facsimile" ] && [ -f etc/templates/$TEMPLATE/notify-$WHY-page.txt ]; then
+       template="etc/templates/$TEMPLATE/notify-$WHY-page.txt"
+fi
+
+
+eval CreateMailMessage "$template" $ATTACH_ARGS \
+       2>$ERRORSTO | $SENDMAIL -f$FROMADDR -oi $mailaddr
+
+CleanupPrivateTmp