*** 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 function usage () {
23     echo "gma.sh [--body] [--from|--to|--fromto] <pattern> [<year>]"
24 }
25
26 RESULT_MBOX="/tmp/gma"
27
28 if [[ -z "${MAIL_ARCHIVE_DIR}" ]]; then
29     echo "Undefined \$MAIL_ARCHIVE_DIR" >&2
30     exit 1
31 fi
32
33 HEADER_ONLY="-H"
34
35 while [[ $1 ]]; do
36
37     case $1 in
38
39         "--help"|"-h")
40             usage
41             exit 0
42             ;;
43
44         "--body")
45             HEADER_ONLY=""
46             ;;
47
48         "--keepspam")
49             KEEP_SPAM=1
50             ;;
51
52         "--from")
53             RE_PREFIX="^From:.*"
54             ;;
55
56         "--to")
57             RE_PREFIX="^To:.*"
58             ;;
59
60         "--fromto")
61             RE_PREFIX="^(From|To):.*"
62             ;;
63
64         *)
65             if [[ ${PATTERN} ]]; then
66                 if [[ ${FILE_PATTERN} ]]; then
67                     usage
68                     exit 1
69                 else
70                     FILE_PATTERN="${MAIL_ARCHIVE_DIR}/${1}/*/*"
71                     SIZE_FILE_PATTERN="${MAIL_ARCHIVE_DIR}/${1}"
72                 fi
73             else
74                 PATTERN=$1
75             fi
76             ;;
77     esac
78     shift
79 done
80
81 if [[ -z "${PATTERN}" ]]; then
82     usage
83     exit 1
84 fi
85
86 [[ ${FILE_PATTERN} ]] || FILE_PATTERN="${MAIL_ARCHIVE_DIR}/*/*/*"
87 [[ ${SIZE_FILE_PATTERN} ]] || SIZE_FILE_PATTERN="${MAIL_ARCHIVE_DIR}/"
88
89 SIZE=$(\dus ${SIZE_FILE_PATTERN} | cut -f 1 -d" ")
90
91 echo "Looking for ${RE_PREFIX}${PATTERN}"
92 echo "        in ${FILE_PATTERN}"
93 echo "        writing result in ${RESULT_MBOX}"
94
95 if [[ ${KEEP_SPAM} ]]; then
96
97     cat ${FILE_PATTERN} \
98         | pv -s ${SIZE} -p -t -e - \
99         | mboxgrep ${HEADER_ONLY} -i "${RE_PREFIX}${PATTERN}" > ${RESULT_MBOX}
100
101 else
102
103     cat ${FILE_PATTERN} \
104         | pv -s ${SIZE} -p -t -e - \
105         | mboxgrep ${HEADER_ONLY} -i "${RE_PREFIX}${PATTERN}" \
106         | mboxgrep ${HEADER_ONLY} -v '^Subject:.*SPAM' > ${RESULT_MBOX}
107
108 fi
109
110 echo "Found "$(grep ^"From " ${RESULT_MBOX} | wc -l)" messages."