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