#!/bin/bash
-##################################################################
-# START_IP_HEADER #
-# #
-# Written by Francois Fleuret #
-# Contact <francois.fleuret@idiap.ch> for comments & bug reports #
-# #
-# END_IP_HEADER #
-##################################################################
+#########################################################################
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the version 3 of the GNU General Public License #
+# as published by the Free Software Foundation. #
+# #
+# This program is distributed in the hope that it will be useful, but #
+# WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU #
+# General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <http://www.gnu.org/licenses/>. #
+# #
+# Written by and Copyright (C) Francois Fleuret #
+# Contact <francois.fleuret@idiap.ch> for comments & bug reports #
+#########################################################################
set -e
+function usage () {
+ echo "gma.sh [--body] [--from|--to|--fromto] <pattern> [<year>]"
+}
+
RESULT_MBOX="/tmp/gma"
if [[ -z "${MAIL_ARCHIVE_DIR}" ]]; then
exit 1
fi
-if [[ $1 ]]; then
+HEADER_ONLY="-H"
+
+while [[ $1 ]]; do
+
+ case $1 in
+
+ "--help"|"-h")
+ usage
+ exit 0
+ ;;
+
+ "--body")
+ HEADER_ONLY=""
+ ;;
+
+ "--keepspam")
+ KEEP_SPAM=1
+ ;;
+
+ "--from")
+ RE_PREFIX="^From:.*"
+ ;;
+
+ "--to")
+ RE_PREFIX="^To:.*"
+ ;;
- PATTERN=$1
+ "--fromto")
+ RE_PREFIX="^(From|To):.*"
+ ;;
- if [[ -z "${PATTERN}" ]]; then
- echo "No pattern." >&2
- exit 1
- fi
+ *)
+ if [[ ${PATTERN} ]]; then
+ if [[ ${FILE_PATTERN} ]]; then
+ usage
+ exit 1
+ else
+ FILE_PATTERN="${MAIL_ARCHIVE_DIR}/${1}/*/*"
+ SIZE_FILE_PATTERN="${MAIL_ARCHIVE_DIR}/${1}"
+ fi
+ else
+ PATTERN=$1
+ fi
+ ;;
+ esac
+ shift
+done
- if [[ $2 ]]; then
- FILE_PATTERN="${MAIL_ARCHIVE_DIR}/${2}/*/*gz"
- SIZE=$(\dus ${MAIL_ARCHIVE_DIR}/${2} | cut -f 1 -d" ")
- else
- FILE_PATTERN="${MAIL_ARCHIVE_DIR}/*/*/*gz"
- SIZE=$(\dus ${MAIL_ARCHIVE_DIR}/ | cut -f 1 -d" ")
- fi
+if [[ -z "${PATTERN}" ]]; then
+ usage
+ exit 1
+fi
+
+[[ ${FILE_PATTERN} ]] || FILE_PATTERN="${MAIL_ARCHIVE_DIR}/*/*/*"
+[[ ${SIZE_FILE_PATTERN} ]] || SIZE_FILE_PATTERN="${MAIL_ARCHIVE_DIR}/"
+
+SIZE=$(\dus ${SIZE_FILE_PATTERN} | cut -f 1 -d" ")
- echo "Looking for ${PATTERN}"
- echo " in ${FILE_PATTERN}"
- echo " writing result in ${RESULT_MBOX}"
+echo "Looking for ${RE_PREFIX}${PATTERN}"
+echo " in ${FILE_PATTERN}"
+echo " writing result in ${RESULT_MBOX}"
+
+if [[ ${KEEP_SPAM} ]]; then
cat ${FILE_PATTERN} \
| pv -s ${SIZE} -p -t -e - \
- | zcat \
- | mboxgrep -i "$1" \
- | mboxgrep -v '^Subject:.*SPAM' > ${RESULT_MBOX}
- echo "Found "$(grep ^"From " ${RESULT_MBOX} | wc -l)" messages."
+ | mboxgrep ${HEADER_ONLY} -i "${RE_PREFIX}${PATTERN}" > ${RESULT_MBOX}
else
- rm -i ${RESULT_MBOX}
+ cat ${FILE_PATTERN} \
+ | pv -s ${SIZE} -p -t -e - \
+ | mboxgrep ${HEADER_ONLY} -i "${RE_PREFIX}${PATTERN}" \
+ | mboxgrep ${HEADER_ONLY} -v '^Subject:.*SPAM' > ${RESULT_MBOX}
fi
+
+echo "Found "$(grep ^"From " ${RESULT_MBOX} | wc -l)" messages."