Added options for From: To: and From|To:.
authorFrancois Fleuret <francois@fleuret.org>
Thu, 8 Apr 2010 16:15:04 +0000 (18:15 +0200)
committerFrancois Fleuret <francois@fleuret.org>
Thu, 8 Apr 2010 16:15:04 +0000 (18:15 +0200)
gma.sh

diff --git a/gma.sh b/gma.sh
index 6d25f0f..640c7f3 100755 (executable)
--- a/gma.sh
+++ b/gma.sh
 
 set -e
 
+function usage () {
+    echo "gma.sh [--body] [--from|--to|--fromto] <pattern> [<year>]"
+}
+
 RESULT_MBOX="/tmp/gma"
 
 if [[ -z "${MAIL_ARCHIVE_DIR}" ]]; then
@@ -26,44 +30,81 @@ 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."