#!/usr/bin/env bash # searchmail --- Wrapper around mairix to open results in mutt # # Copyright (C) 2017 Sean Whitton # # searchmail is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # searchmail is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with searchmail. If not, see . us="$(basename $0)" USAGE="$us: usage: $us [-n] search-patterns" mairix_opts="" mutt_opts="" # check args if [ "$#" = "0" ]; then echo >&2 "$USAGE" exit 1 fi # process the one arg this script doesn't pass on to mairix if [ "$1" = "-n" ]; then shift # we assume the user has "sort = threads" in their ~/.muttrc, and # override that when not including whole threads mutt_opts="$mutt_opts -e \"set sort=date\"" else mairix_opts="$mairix_opts --threads" fi # set up temporary maildir mfolder=$(mktemp --tmpdir -d ${us}XXXX) mkdir $mfolder/cur $mfolder/new $mfolder/tmp chmod 700 $mfolder # pass the mfolder to mairix and mutt mairix_opts="$mairix_opts --mfolder $mfolder" mutt_opts="$mutt_opts -Rf $mfolder" # run mairix and check if there were any search results. We use the # special "$@" to ensure that each argument is individually quoted in # case they contain spaces, but note that mairix search terms should # not contain spaces. Instead of `f:"Sean Whitton"` you should pass # `f:Sean f:Whitton` output=$(mairix $mairix_opts "$@") if ! [ "$output" = "Matched 0 messages" ]; then # set terminal title to search query xtitle "mairix results: $@" # there were some results; open them in mutt eval "mutt $mutt_opts" fi # clean up rm -rf $mfolder