Update.
[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] [--nospam] [--subject|--from|--to|--fromto|--sender] <pattern> [<year>]"
24 }
25
26 RESULT_MBOX="/tmp/gma"
27 KEEP_SPAM=1
28
29 if [[ -z "${MAIL_ARCHIVE_DIR}" ]]; then
30     echo "Undefined \$MAIL_ARCHIVE_DIR" >&2
31     exit 1
32 fi
33
34 if [[ $(which ionice) ]]; then
35     IONICE="ionice -c3"
36 else
37     IONICE=""
38 fi
39
40 HEADER_ONLY="-H"
41
42 while [[ $1 ]]; do
43
44     case $1 in
45
46         "--help"|"-h")
47             usage
48             exit 0
49             ;;
50
51         "--body")
52             HEADER_ONLY=""
53             ;;
54
55         "--intense")
56             IONICE=""
57             ;;
58
59         "--nospam")
60             KEEP_SPAM=0
61             ;;
62
63         "--from")
64             RE_PREFIX="^From:.*"
65             ;;
66
67         "--to")
68             RE_PREFIX="^To:.*"
69             ;;
70
71         "--fromto")
72             RE_PREFIX="^(From|To):.*"
73             ;;
74
75         "--sender")
76             RE_PREFIX="^Sender:.*"
77             ;;
78
79         "--subject")
80             RE_PREFIX="^Subject:.*"
81             ;;
82
83         *)
84             if [[ ${PATTERN} ]]; then
85                 # If we already have the pattern to match in the
86                 # message, get the new argument as a pattern for the
87                 # filename
88                 if [[ ${FILE_PATTERN} ]]; then
89                     usage
90                     exit 1
91                 else
92                     FILE_PATTERN="${MAIL_ARCHIVE_DIR}/${1}/*/*"
93                     SIZE_FILE_PATTERN="${MAIL_ARCHIVE_DIR}/${1}"
94                 fi
95             else
96                 PATTERN=$1
97             fi
98             ;;
99     esac
100     shift
101 done
102
103 if [[ -z "${PATTERN}" ]]; then
104     usage
105     exit 1
106 fi
107
108 [[ ${FILE_PATTERN} ]] || FILE_PATTERN="${MAIL_ARCHIVE_DIR}/*/*/*"
109 [[ ${SIZE_FILE_PATTERN} ]] || SIZE_FILE_PATTERN="${MAIL_ARCHIVE_DIR}"
110
111 SIZE=$(\dus ${SIZE_FILE_PATTERN} | cut -f 1 -d" ")
112
113 echo "Looking for ${RE_PREFIX}${PATTERN}"
114 echo "        in ${FILE_PATTERN}"
115 echo "        writing result in ${RESULT_MBOX}"
116
117 if [[ ${KEEP_SPAM} == "1" ]]; then
118
119     ${IONICE} cat ${FILE_PATTERN} \
120         | pv -s ${SIZE} -p -t -e - \
121         | mboxgrep ${HEADER_ONLY} -i "${RE_PREFIX}${PATTERN}" > ${RESULT_MBOX}
122
123 else
124
125     ${IONICE} cat ${FILE_PATTERN} \
126         | pv -s ${SIZE} -p -t -e - \
127         | mboxgrep ${HEADER_ONLY} -i "${RE_PREFIX}${PATTERN}" \
128         | mboxgrep ${HEADER_ONLY} -v '^Subject:.*SPAM' > ${RESULT_MBOX}
129
130 fi
131
132 echo "Found "$(grep ^"From " ${RESULT_MBOX} | wc -l)" messages."