*** empty log message ***
[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     if [[ $2 ]]; then
39         FILE_PATTERN="${MAIL_ARCHIVE_DIR}/${2}/*/*gz"
40         SIZE=$(\dus ${MAIL_ARCHIVE_DIR}/${2} | cut -f 1 -d" ")
41     else
42         FILE_PATTERN="${MAIL_ARCHIVE_DIR}/*/*/*gz"
43         SIZE=$(\dus ${MAIL_ARCHIVE_DIR}/ | cut -f 1 -d" ")
44     fi
45
46     echo "Looking for ${PATTERN}"
47     echo "        in ${FILE_PATTERN}"
48     echo "        writing result in ${RESULT_MBOX}"
49
50     cat ${FILE_PATTERN} \
51         | pv -s ${SIZE} -p -t -e - \
52         | zcat \
53         | mboxgrep -i "$1" \
54         | mboxgrep -v '^Subject:.*SPAM' > ${RESULT_MBOX}
55     echo "Found "$(grep ^"From " ${RESULT_MBOX} | wc -l)" messages."
56
57 else
58
59     rm -i ${RESULT_MBOX}
60
61 fi