Files that make up the Print-To-Email Filter

Here, you will find the details on the various /var/spool/lpd files that we wrote in order to implement our 'print-to-email' lpd support. They were:

  • /var/spool/lpd/jesprt.filter
  • /usr/local/bin/collapse
  • /var/spool/lpd/tools/jesprt.mailto.awk
  • /var/spool/lpd/tools/jesprt.subject.awk
  • /var/spool/lpd/tools/jesprt.notice.txt

    /var/spool/lpd/jesprt.filter

    The /var/spool/lpd/jesprt.filter script is invoked through the printcap Input Filter (:if:) setting for the jesprt printer. The filter captures it's stdin, which is the print data from the host and processes it. No output data is generated.

    #!/bin/bash
    # print filter to redirect output to Email
    # This script will be invoked by lpd (from a printcap entry)
    # with the parameters
    #   [-c] -w# -l# -i# -n logonid -h host acct-file
    #
    
    # first, drop the -c if it was supplied
    if [ "$1" = "-c" ]
    then
      shift
    fi
    
    optlogonid=$5
    opthost=$7
    
    # assign a name to the temporary spool file
    tmpfile=/tmp/jesprt.filter.$$
    
    # collapse the VPS/WinNT overprint data into printlines, extract the start flash page
    # (NB: flash pages are 56 lines long)
    #
    /usr/local/bin/collapse | /usr/bin/tee $tmpfile 2>/dev/null | /bin/head -56 >$tmpfile.flash
    
    #------------------------------------------------------------------------------------
    # Strip the flash pages off of tmpfile  !
    #----------------------------------------
    /bin/rm -f $tmpfile.bc $tmpfile.rpt
    
    # Strip off leading flash page
    /usr/bin/tail +57 $tmpfile >$tmpfile.bc
    
    # Strip of trailing flash page
    RptFlash=`/usr/bin/wc -l <$tmpfile.bc`
    RptOnly=`/usr/bin/expr $RptFlash - 56`
    /bin/head -$RptOnly $tmpfile.bc >$tmpfile.rpt
    
    # Replace old spool file with stripped file
    /bin/rm -f $tmpfile $tmpfile.bc
    /bin/mv $tmpfile.rpt $tmpfile
    #------------------------------------------------------------------------------------
    /usr/bin/cat /var/spool/lpd/tools/jesprt.notice.text >>$tmpfile
    
    # gather information about print job, to be used in email later
    jobid=`/usr/bin/awk '/JOBID:/ { print $3 }' $tmpfile.flash`
    jobname=`/usr/bin/awk '/JOBNAME:/ { print $3 }' $tmpfile.flash`
    system=`/usr/bin/awk '/SYSTEM:/ { print $3 }' $tmpfile.flash`
    email=`/usr/bin/awk -f /var/spool/lpd/tools/jesprt.mailto.awk $tmpfile.flash`
    subject=`/usr/bin/awk -f /var/spool/lpd/tools/jesprt.subject.awk $tmpfile.flash`
    
    if [ "$email" = "#UNKNOWN#" ]
    then
      # Log an error, and dont respool the print
      /usr/bin/logger -t jesprt.filter "Print job ${jobname-UNKNOWN} from $optlogonid/$opthost discarded"
    else
      # Spool the output to email and log the event
      /usr/bin/fastmail -s "$subject($jobname / $jobid from $system)" $tmpfile $email
      /usr/bin/logger -t jesprt.filter "Print job $jobname ($jobid) from $system to email for $email"
    fi
    
    # clean up the files, then log the activity to the syslog
    /bin/rm $tmpfile $tmpfile.flash
    


    collapse.c - Source for /usr/local/bin/collapse

    collapse.c is the source for the /usr/local/bin/collapse program. It reads stdin, and reprocesses the weird overprint data generated by the combination of JES2 and VPS.

    /*
    ** collapse.c	collapse VPS overprint lines into a single line
    **
    **		This program is invoked by the lpd filter that
    **		handles VPS print from MVS. It seems that the VPS
    **		print stream consists of multiple overprint segments
    **		making up single printed lines. Additionally, the
    **		overprint segments begin at different spots on the
    **		final print line, necessitating the preprocessing
    **		of each line prior to printing
    **
    ** Author:	Lew Pitcher
    */
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    	char buffer[1024];
    	int posn,
    	    lineno, start,
    	    datum;
    
    	lineno = posn = 0;
    	memset(buffer, 0, sizeof buffer);
    
    	while ((datum = getchar()) != EOF)
    	{
    		switch (datum)
    		{
    			case '\f':
    			case '\n':
    				puts(buffer);
    				memset(buffer, 0, sizeof buffer);
    				start = lineno = posn = 0;
    				break;
    
    			case '\r':
    				posn = 0;
    				++lineno;
    				start = (lineno+1)/2;
    				break;
    
    			case ' ':
    				if (start+posn < (sizeof buffer) - 2)
    				{
    					if (buffer[start+posn] == 0)
    						buffer[start+posn] = ' ';
    					++posn;
    				}
    				break;
    
    			case 0:
    				datum = ' ';
    			default:
    				if (start+posn < (sizeof buffer) - 2)
    				{
    					buffer[start+(posn++)] = datum;
    				}
    				break;
    		}
    	}
    
    	if (posn > 0)	puts(buffer);
    
    	return EXIT_SUCCESS;
    }
    


    /var/spool/lpd/tools/jesprt.mailto.awk

    The /var/spool/lpd/tools/jesprt.mailto.awk AWK script locates and extracts an email address from the flashpage. It begins by assuming that there is no email address, and prepares a default sentinal address of "#UNKNOWN#"

    If the USERID: text is found as the second word on any line, and no other email address has yet been found, the third word from that line is extracted and used as the target email address. This covers the case where the email address has been let default to the MVS SAF userid.

    If the ADDRESS: text is found as the second word on any line, and the text EMAIL: is found as the third word on the same line, and the current email address is either the default (state = 1) or has been extracted from the USERID: line (state = 2), then the fourth word from that line is extracted and used as the target email address. This covers the case where the JES2 OUTPUT ADDRESS parameter has been used to specify the email address, with the email address being specified in the first of the four address lines.

    However, If the EMAIL: text is found as the second word on any line, and the current email address is either the default (state = 1) or has been extracted from the USERID: line (state = 2), then the fourth word from that line is extracted and used as the target email address. This covers the case where the JES2 OUTPUT ADDRESS parameter has been used to specify the email address, with the email address being specified in the second, third, or fourth address line.

    Finally, when EOF is encountered, the final email address is printed.

    BEGIN		{
    			state = 1
    			email = "#UNKNOWN#"
    		}
    
    $2 ~ /USERID:/	{
    			if (state < 2)
    			{
    				state = 2
    				email = $3 "@localhost.localnet"
    			}
    		}
    
    $2 ~ /ADDRESS:/ && $3 ~ /EMAIL:/	{
    			if (state < 3)
    			{
    				state = 3
    				email = $4 
    			}
    		}
    
    $2 ~ /EMAIL:/	{
    			if (state < 3)
    			{
    				state = 3
    				email = $3
    			}
    		}
    
    END		{
    			print email
    		}
    




    /var/spool/lpd/tools/jesprt.subject.awk

    The /var/spool/lpd/tools/jesprt.subject.awk AWK script locates and extracts an report title, so that the filter may use it as the email subject line.

    Initially, the report title is empty, but once the TITLE: line of the flash page is encountered, all the text from the JES2 OUTPUT TITLE parameter is extracted and concatinated together to make a single line.

    When EOF is encountered on the input data, this single line is written to stdout so that the filter script can retrieve it and use it as the email subject line.

    BEGIN	{
    	title = ""
    }
    
    $2 ~ /TITLE:/	{
    	title = ""
    	for (i = 3; i < NF; i++)
    		title = title $i " "
    }
    
    END	{
    	print title
    }
    


    /var/spool/lpd/tools/jesprt.notice.text

    What can one say about this? We append a notice to the report, so that the recipients can report problems.

    
    
      +====+----------------------------------------------------------------------.
     /      \  This print job was emailed by an experimental host Print-To-Email   !
    ( NOTICE ) facility. For information or assistance, please call Lew Pitcher    !
     \      /                                                                      !
      +====+----------------------------------------------------------------------'
    
    


    Copyright (©) Lew Pitcher, March 2002