summaryrefslogtreecommitdiff
path: root/archive/bin/searchmail
blob: f04287487df429fcbe38057fc3031e3c3c3c0563 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/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 <http://www.gnu.org/licenses/>.

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