Updated 2012-09-12 10:43:30 by LkpPo

A print filter for e-mail that relies on a2ps and uses no temp files. The result of this application would be that an email message, read on stdin, would be formatted and then output (via the pipe to a2ps) to your system's printer.
 #!/bin/sh
 # \
 exec tclsh $0 ${1+"[email protected]"}
 
 # Fun little mail message formatting tool.
 # Relies on a2ps.
 
 set data [ read stdin ]
 
 set output <html>
 set sig 0
 set header 1
 set recipients 0
 
 set hpat {<b>\1</b><i>\2</i><br>}
 set spat {<b>\1</b><i><b>\2</b></i><br>}
 
 # the name of the print queue to use
 set printer hpmlknco1
 
 # if you want a graphic in the upper right corner
 # of page 1.
 set graphic /home/pehrens/public_html/mutt_letter.gif
 
 if { [ file readable $graphic ] } {
    lappend output "<img align=right src=$graphic>"
 }
 
 foreach line [ split $data "\n" ] {
    set done 0
 
    if { $header } {
       # handle header lines.
       if { [ regsub -nocase {^(Date:|Sent:|From:)(.+)$} $line $hpat line ] } {
          set recipients 0
          set done 1
       }
       # might be a long list of recipients...
       if { [ regsub -nocase {^(To:|Cc:)(.+)$}   $line $hpat line ] } {
          set recipients 1
          set done 1
       }
       # will be the last line of contained message headers
       if { [ regsub -nocase {^(Subject:)(.+)$}  $line $spat line ] } {
          set recipients 0
          set done 1
       }
       # will generally be the last line of the header
       if { [ regsub -nocase {^(X-mailer:)(.+)$} $line $hpat line ] } {
          set recipients 0
          set done 1
       }   
 
       # if we had a long recipients list in the "To:" or
       # "Cc:" field
       if { ! $done && $recipients && ! [ regexp : $line ] } {
          set line <i>$line</i><br>
          set done 1
       } else {
          set recipients 0
       }
 
       # horizontal rule and blank line after header
       if { ! $done && $header } {
          set header 0
          lappend output <hr><p><pre>
       }
    }  
 
    # we have reached a quoted line
    if { [ regexp {^(>.+)$} $line ] } {
       set done 1
    }
 
    # we have reached a line consisting solely of dashes.
    # this should signal the start of the signature
    if { [ regexp -- {^(-+)$} $line ] } {
       set line <p><p><pre><b>
       set sig 1
       set done 1
    }
 
    # we are in the body of the sig...
    if { $sig && ! [ string equal <p><p><pre><b> $line ] } {
       set done 1
    }
 
    if { ! $done } { set line <b>$line</b> }
    
    # collect htmlized lines
    lappend output $line
 }
 
 set output [ join $output "\n" ]
 
 set out [ open "|a2ps --printer=$printer --pretty-print=html" a+ ]
 
 puts $out $output
 
 catch { close $out }

Looks nice to me, but I don't know anything about TCL. I'd like to use this is a filter, i.e. writing to STDOUT instead of printing. Replacing --printer=$printer by --output=- doesn't seem to work. Can someone help me? Thanks, Jan