6d25f0f19a900eadb943f22afe2fcb8771698314
[scripts.git] / gma.sh
1 #!/bin/bash
2
3 #########################################################################
4 # This program is free software: you can redistribute it and/or modify  #
5 # it under the terms of the version 3 of the GNU General Public License #
6 # as published by the Free Software Foundation.                         #
7 #                                                                       #
8 # This program is distributed in the hope that it will be useful, but   #
9 # WITHOUT ANY WARRANTY; without even the implied warranty of            #
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      #
11 # General Public License for more details.                              #
12 #                                                                       #
13 # You should have received a copy of the GNU General Public License     #
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.  #
15 #                                                                       #
16 # Written by and Copyright (C) Francois Fleuret                         #
17 # Contact <francois.fleuret@idiap.ch> for comments & bug reports        #
18 #########################################################################
19
20 set -e
21
22 RESULT_MBOX="/tmp/gma"
23
24 if [[ -z "${MAIL_ARCHIVE_DIR}" ]]; then
25     echo "Undefined \$MAIL_ARCHIVE_DIR" >&2
26     exit 1
27 fi
28
29 if [[ $1 ]]; then
30
31     PATTERN=$1
32
33     if [[ -z "${PATTERN}" ]]; then
34         echo  "No pattern." >&2
35         exit 1
36     fi
37
38     shift
39
40     HEADER_ONLY="-H"
41
42     if [[ $1 == "--body" ]]; then
43         HEADER_ONLY=""
44         shift
45     fi
46
47     if [[ $1 ]]; then
48         FILE_PATTERN="${MAIL_ARCHIVE_DIR}/${1}/*/*"
49         SIZE=$(\dus ${MAIL_ARCHIVE_DIR}/${1} | cut -f 1 -d" ")
50     else
51         FILE_PATTERN="${MAIL_ARCHIVE_DIR}/*/*/*"
52         SIZE=$(\dus ${MAIL_ARCHIVE_DIR}/ | cut -f 1 -d" ")
53     fi
54
55     echo "Looking for ${PATTERN}"
56     echo "        in ${FILE_PATTERN}"
57     echo "        writing result in ${RESULT_MBOX}"
58
59     cat ${FILE_PATTERN} \
60         | pv -s ${SIZE} -p -t -e - \
61         | mboxgrep ${HEADER_ONLY} -i "${PATTERN}" \
62         | mboxgrep ${HEADER_ONLY} -v '^Subject:.*SPAM' > ${RESULT_MBOX}
63     echo "Found "$(grep ^"From " ${RESULT_MBOX} | wc -l)" messages."
64
65 else
66
67     rm -i ${RESULT_MBOX}
68
69 fi