#!/usr/um/bin/perl # MH script to automatically refile messages based on header contents # by Dave Disser (disser@engin.umich.edu) # # depends on file named below, with format similar to following: # # header:regexp:folder # from:bob:friends/bob # subject:software.*install:packages/install $refiles="$ENV{'HOME'}/.refiles"; $ret=0; while($0=~m@/@) { $0 = $'; } while ($ARGV[0]=~/^-/) { ($ARGV[0]=~/^-n/) && (shift,$noact++,next); ($ARGV[0]=~/^-q/) && (shift,$query++,next); ($ARGV[0]=~/^-h/) && &help; print "Unknown option $ARGV[0]. Use -h for help.\n"; exit 1; } $args=join(" ",@ARGV); $args="cur" unless $args; @files=`mhpath $args`; $path=`mhpath`; grep(chop,@files); open(FILE,$refiles) || die $!; while() { chop; next unless /:/; push(@refs,$_); } close(FILE) || die $!; for $file (@files) { open(FILE,$file) || die $!; undef %header; $refiled=0; # Read the header while() { chop; last if /^$/; if (/^\s+/) { $value .= $'; } elsif (/:\s+/) { if ($header) { push(@hlist,$header); $header=~y/A-Z/a-z/; $header{$header}=$value; } $header=$`; $value=$'; } } if ($header) { push(@hlist,$header); $header=~y/A-Z/a-z/; $header{$header}=$value; } close(FILE) || die $!; # Munge it. for (@refs) { split(/:/); if ($header{$_[0]}=~/$_[1]/i) { $file=~m@/([0-9]+)$@; $msg=$1; print "$_[0]=\"$header{$_[0]}\" --> +$_[2]\n"; if ($query) { print "Refile (yes/No/show) ? "; chop($in=); if ($in =~ /^s/) { system("show $msg"); redo; } last if ($in !~ /^y/); } $refiles{$_[2]} .= "$msg "; $refiled++; last; } } (print("No sort for $file found.\n"),$ret++) unless $refiled; } while (($f,$m)=each %refiles) { print "refile $m +$f\n"; system("refile $m +$f") unless $noact; } exit $ret; sub help { print <