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
-
- PATTERN=$1
-
- if [[ -z "${PATTERN}" ]]; then
- echo "No pattern." >&2
- exit 1
- fi
-
+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:.*"
+ ;;
+
+ "--fromto")
+ RE_PREFIX="^(From|To):.*"
+ ;;
+
+ *)
+ 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 [[ -z "${PATTERN}" ]]; then
+ usage
+ exit 1
+fi
- HEADER_ONLY="-H"
+[[ ${FILE_PATTERN} ]] || FILE_PATTERN="${MAIL_ARCHIVE_DIR}/*/*/*"
+[[ ${SIZE_FILE_PATTERN} ]] || SIZE_FILE_PATTERN="${MAIL_ARCHIVE_DIR}/"
- if [[ $1 == "--body" ]]; then
- HEADER_ONLY=""
- shift
- fi
+SIZE=$(\dus ${SIZE_FILE_PATTERN} | cut -f 1 -d" ")
- if [[ $1 ]]; then
- FILE_PATTERN="${MAIL_ARCHIVE_DIR}/${1}/*/*"
- SIZE=$(\dus ${MAIL_ARCHIVE_DIR}/${1} | cut -f 1 -d" ")
- else
- FILE_PATTERN="${MAIL_ARCHIVE_DIR}/*/*/*"
- SIZE=$(\dus ${MAIL_ARCHIVE_DIR}/ | cut -f 1 -d" ")
- fi
+echo "Looking for ${RE_PREFIX}${PATTERN}"
+echo " in ${FILE_PATTERN}"
+echo " writing result in ${RESULT_MBOX}"
- echo "Looking for ${PATTERN}"
- echo " in ${FILE_PATTERN}"
- echo " writing result in ${RESULT_MBOX}"
+if [[ ${KEEP_SPAM} ]]; then
cat ${FILE_PATTERN} \
| pv -s ${SIZE} -p -t -e - \
- | mboxgrep ${HEADER_ONLY} -i "${PATTERN}" \
- | mboxgrep ${HEADER_ONLY} -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."