On Fri, Apr 29, 2011 at 02:08:33PM -0700, Omen Wild wrote:
Quoting Ed Blackman <e...@edgewood.to> on Fri, Apr 29 17:03:

In my case, mairixquery is a Perl script that prompts me for the mairix
search string, gives me yes or no prompts for whether to search threads
or augment previous results, and saves the most recent 100 searches so
that I can make edits if the query returns too little or way too much.

That sounds really interesting.  Any chance you would be willing to
share it?

Sure.  I've attached it.

It was originally the shell script that Christian Ebert posted a couple of years ago. He reposted an enhanced version in this thread. I got frustrated with having to retype the same queries with slight variations, so converted it to Perl to take advantage of the built-in history feature in Term::ReadLine, and have enhanced it from there.

Features:
- saves last 100 (configurable) queries in $HOME/.mairixquery (configurable)
- automatically replaces "X:me" in typed queries with "X:myem...@example.com" (you'll want to edit $me in the script to use your own adress, of course!). I quite frequently want to find emails that I sent or that were addresssed or copied to me, and this cuts down on typing my own address quite a bit. - saves the answers to "include threads?" and "add to existing results?" in the history, and uses them to set the default for the questions. If I didn't want to use threads the last time I used this query, I most likely don't want to this time. - expands "%name%" so that it matches all email addresses reurned from a lbdbq search for "name". This is the one idiosyncratic piece that's hardcoded. Ithought about making lbdbq configurable, but the code around it is very dependant on lbdbq's output structure. If you don't use "%foo%", it will never call lbdbq, though.

Patches and suggestions welcome. If there are people interested in hacking on it, I'd be happy to start a little github project.

--
Ed Blackman
#!/usr/bin/perl -w

# customizable variables

my $me = 'e...@edgewood.to';
my $histFile = "$ENV{HOME}/.mairixquery";
my $histSaveLines = 100;

# code begins

use strict;
use Term::ReadLine;

$ENV{PATH} = "$ENV{HOME}/bin:$ENV{PATH}";

my ($Agent) = $0 =~ m@.*/([^/]*)@;

print "Enter mairix query:\n";

my $term = new Term::ReadLine $Agent;

if($histFile) {
  $term->read_history($histFile);
  # don't automatically add to history, so lines can be changed before being 
saved
  $term->MinLine(undef);
}

my $query = $term->readline('query>') or die "No query\n";
my @args;

# mairix doesn't like multiple commas, so collapse runs of commas to one
$query =~ s/,,+/,/g;

# if there are words in percents ("%bill%"), expand it out using lbdb
$query = expandNames($query);

my ($threads) = $query =~ /--(no)?threads /;
$threads = 'yes' if(!defined($threads));

if(yorn("Retrieve thread(s)?", $threads)) {
  unshift @args, "--threads";
  $threads = 'yes';
}
else {
  $threads = 'no';
}

$query =~ s/--(no)?threads //;

my ($augment) = $query =~ /--(no)?augment /;
$augment = 'no' if(!defined($augment));

if(yorn("Append message(s) to mfolder?", $augment)) {
  unshift @args, "--augment";
  $augment = 'yes';
}
else {
  $augment = 'no';
}

$query =~ s/--(no)?augment //;

push @args, map {
  # make it easy to find my address without typing it out
  s/^(\w+):me$/$1:$me/;

  # limit message-id strings to 31 chars (mairix limit)
  if(/^(m:.{0,31}).*/) {
    $_ = "$1";
  }

  $_;
} split(' ', $query);

system(('mairix', @args));

# mairix doesn't understand --no arguments, so they need to be added to saved 
args after execution
unshift @args, '--nothreads' if($threads eq 'no');
unshift @args, '--noaugment' if($augment eq 'no');

if($histSaveLines && $histSaveLines > 0) {
  $term->add_history(join(' ' ,  @args));
  $term->write_history($histFile);
  $term->history_truncate_file($histFile, $histSaveLines);
}

sub yorn {
  my $prompt = shift;
  my $default = shift;

  $| = 1;
  $prompt .= " (" . ( lc($default) eq 'yes' ? "[yes]/no" : "yes/[no]") . ") ";

  my $ans = $term->readline($prompt);

  $ans = 'yes' if($ans eq '' && $default eq 'yes');

  return $ans =~ /^y.*/i;
}

sub expandNames {
  my $query = shift;

  while ($query =~ /[\W\b]%(\w+)%(?:[\W\b]|$)/) {
    my $name = $1;
    my @expanded;

    foreach my $match (split(/\n/, qx(lbdbq '$name' | tail -n +2))) {
      my ($address, $name, $where) = split(/\t/, $match);

      push @expanded, $address;
      push @expanded, join(',', split(/ /, $name));
    }

    my $expanded = join('/', @expanded);

    $expanded = $name if(!$expanded);   # fall back to replacing '%foo%' with 
'foo' if no matches

    $query =~ s/%$name%/$expanded/;
  }

  return $query;
}

Attachment: signature.txt
Description: Digital signature

Reply via email to