#!/usr/bin/perl -w use strict; my $matchdir = '/home/rader/Mail/matches'; my $debug = 0; my $matchwholewords = 0; my $index = 0; my $summary = 0; my $args = ''; my $margs = ''; my $fuzz = ''; my $folder = ''; #------------------------------ while (@ARGV) { my $arg = $ARGV[0]; if ( $debug ) { print "ARG $arg\n"; } shift @ARGV; if ( $arg eq '-h' ) { &usage; exit; } if ( $arg eq '-v' ) { $debug = 1; next; } if ( $arg eq '-x' ) { $index = 1; next; } if ( $arg eq '-a' ) { $args .= " -a"; next; } if ( $arg eq '-u' ) { $summary = 1; next; } if ( $arg eq '-w' ) { $matchwholewords = 1; next; } if ( $arg =~ /^-(\d+)/ ) { $fuzz = $1; next; } if ( $arg =~ /^\+(.*)/ ) { $folder = $1; next; } if ( $arg =~ /^-(.)$/ ) { my $f = $1; if ( $f !~ /^[tcfsbndx]{1}$/ ) { &usage; exit; } if ( ! $ARGV[0] ) { &usage; exit; } if ( $debug ) { print "ARG $ARGV[0]\n"; } if ( $margs eq '' ) { $margs = "$f:"; } else { $margs = "$f$margs"; } if ( $debug ) { print "MARGS $margs\n"; } if ( $matchwholewords ) { $args .= " $margs$ARGV[0]$fuzz"; } else { $args .= " $margs$ARGV[0]=$fuzz"; } shift @ARGV; next; } $arg =~ s/^-/~/; if ( $matchwholewords ) { $args .= " $margs$arg"; } else { $args .= " $margs$arg="; } } if ( $debug ) { print "ARGS$args\n"; } if ( $args eq '' && ! $index ) { &usage; exit; } #------------------------------ if ( $index ) { &mairix_index; exit; } #------------------------------ if ( $debug ) { print "EXEC mairix$args\n"; } my $mout = `mairix$args 2>&1`; if ( $mout !~ /^Matched/ ) { print $mout; exit; } #------------------------------ if ( $folder ) { &prune_search_results; } #------------------------------ if ( $debug ) { print "EXEC sortm +matches -datefield date\n"; } my $out = `sortm +matches -datefield date 2>&1`; if ( ! $summary ) { print $out; } if ( $debug ) { print "EXEC folder +matches -pack\n"; } $out = `folder +matches -pack 2>&1`; if ( ! $summary ) { print $out; } if ( $debug ) { print "EXEC rm -f $matchdir/.mh_sequences\n"; } `rm -f $matchdir/.mh_sequences 2>&1`; if ( $debug ) { print "EXEC rm -f $matchdir/.xmhcache\n"; } `rm -f $matchdir/.xmhcache 2>&1`; #------------------------------ if ( $summary ) { if ( $mout =~ /Matched 1 messages/ ) { print "Matched 1 message\n"; } else { print $mout; } exit; } if ( $debug ) { print "EXEC scan -form filter.scan.grep-mail +matches\n"; } print `scan -form filter.scan.grep-mail +matches 2>&1`; exit; #--------------------------------------------------------------------------- sub mairix_index { my $l = 0; my $w = 0; my $n = 0; my $p = 0; if ( $debug ) { print "EXEC mairix -v -p\n"; } open(IN,"mairix -v -p 2>&1 |"); while() { if ( $_ =~ /Loaded (\d+) existing/ ) { $l = $1; } if ( $_ =~ /Wrote (\d+) messages/ ) { $w = $1; } if ( $_ =~ /No new messages found/ ) { $n = 1; } if ( $_ =~ /(\d+) newly dead messages/ ) { $p = $1; } if ( $_ =~ /^Database .* appears to be locked/ ) { print "Database is locked\n"; exit; } } close(IN); if ( $p == 1 ) { print "Removed 1 old message\n"; } if ( $p > 1 ) { print "Removed $p old messages\n"; } if ( $n == 0 ) { $n = $w - $l + $p; if ( $n == 1 ) { print "Added 1 new message\n"; } else { print "Added $n new messages\n"; } } else { print "No new messages found\n"; } if ( $w == 1 ) { print "Indexed 1 message\n"; } elsif ( $w > 0 ) { print "Indexed $w messages\n"; } else { print "Indexed $l messages\n"; } } #--------------------------------------------------------------------------- sub prune_search_results { if ( $debug ) { print "EXEC prune...\n"; } my $i = 0; opendir(DIR,"$matchdir"); for my $f (readdir(DIR)) { if ( $f !~ /^\d+$/ ) { next; } my $dst = readlink("$matchdir/$f"); if ($dst =~ /\/$folder\/\d+$/ ) { $i++; } else { if ( $debug ) { print("REMOVE $matchdir/$f -> $dst\n"); } print `rm -f $matchdir/$f`; } } closedir(DIR); $mout = "Matched $i messages\n"; } #--------------------------------------------------------------------------- sub usage { print <