#!/bin/sh # the next line restarts using tclsh \ exec tclsh "$0" ${1+"$@"} # # explains the initial line noise. ######## # # This is a simple but handy utility for forwarding a conventional # Unix mail spool. # # 'Twould be straightforward enough to rewrite this in Python, # sh, Python, Rexx, ... Tcl worked out for me. Perl has, # I believe, a module that knows how to disassemble mail # spools. # ######## # Cut the spool into segments which begin with "^From ..."; each of # these is a message, and can be fed directly into sendmail. # Note that modern BSDs keep spools in /var/spool/mail/$old. # I won't bother for now to make this robust. # If I can't assume sendmail, what *is* safe? proc usage {} { puts "Usage: 'forward.tcl OLD_ACCOUNT NEW_ADDRESS'." puts "Examples: 'forward.tcl someone someone@newdomain.com'." puts " 'tclsh forward.tcl person newname'." puts " 'forward.tcl person person73@somewhere.org'." exit 1 } if {2 != $argc} { usage } foreach {old new} $argv {} set fp [open /usr/spool/mail/$old] set message {} while {![eof $fp]} { gets $fp line if [regexp {^From } $line] { if {{} != $message} { puts [exec sendmail $new << $message] } set message {} } append message "$line\n" } # Thanks to Stefan Bertels for noticing the necessity of the # following clause. Without it, the last message is # subject to loss. if {{} != $message} { puts [exec sendmail $new << $message] } close $fp