add debugging and dynamic date ranges to improve speed of finding and tagging mails

This commit is contained in:
2017-07-28 17:12:21 +02:00
parent 82762b4ea3
commit 880c654c43
2 changed files with 38 additions and 4 deletions

View File

@@ -5,6 +5,8 @@
CURDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
DEBUG=false # use 'true' to echo verbose debug messages
CSV="$CURDIR"/tag-list.csv # Table of tags/folders
MAILDIR="$HOME/Mails/FSFE" # Maildir
TRASHTAG="del" # Tag of trashed mails
@@ -17,10 +19,13 @@ LOG="$CURDIR"/move-tag.log # Location of logfile
USETAGS=".+" # List of tags which shall be moved
THREADTAGS=".+" # List of tags for which single, but connected messages in INBOX shall be tagged
BL_USETAGS=""
BL_THREADTAGS="$TRASHTAG|$SPAMTAG|$ARCHTAG|draft"
BL_USETAGS="" # Tags which are *not* considered to be moved
BL_THREADTAGS="$TRASHTAG|$SPAMTAG|$ARCHTAG|draft" # Tags for which their whole thread will not be tagged if they are still in INBOX
touch "$LOG" # Create logfile or update time stamp
LASTRUN="$CURDIR"/move-tag-lastrun.log # file where the date of the last successful run of this script is saved
var_conn=$1 # supposed to be "on" or "off"
function pdate {
DATE=$(date +%y-%m-%d_%H:%M:%S)
@@ -33,6 +38,21 @@ function logrun {
eval "$@" 2>> "$LOG"
}
function debugmsg {
if ${DEBUG}; then
echo "$(pdate) $@"
fi
}
# Set date until when files are searched for, depending on current connection status
if [ "${var_conn}" == "off" ]; then
DATELIMIT=$(date -d "$(cat ${LASTRUN}) - 1 days" +%Y-%m-%d)
logrun echo "Datelimit for search set to $DATELIMIT"
else
DATELIMIT=$(date -d "$(cat ${LASTRUN}) - 1 weeks" +%Y-%m-%d)
logrun echo "Datelimit for search set to $DATELIMIT"
fi
while read line; do
TAG=$(echo $line | cut -d";" -f1)
FOL=$(echo $line | cut -d";" -f2)
@@ -42,16 +62,19 @@ while read line; do
# TRASH tag "del"
if [ "$TAG" = "$TRASHTAG" ]; then
debugmsg "search trash tagged mails"
HITFILES=$(notmuch search --output=files --exclude=false tag:$TRASHTAG and not path:$TRASHFOL/**) # Files which are tagged with "del" but not in Trash bin
FOL="$TRASHFOL" # Set destination folder to Trash
# SPAM tag "spam"
elif [ "$TAG" = "$SPAMTAG" ]; then
debugmsg "search spam tagged mails"
HITFILES=$(notmuch search --output=files --exclude=false tag:$SPAMTAG and not path:$SPAMFOL/**) # Files which are tagged with "spam" but not in Spam folder
FOL="$SPAMFOL" # Set destination folder to INBOX.Junk
# ARCHIVE tag "archive"
elif [ "$TAG" = "$ARCHTAG" ]; then
debugmsg "search archive tagged mails"
HITFILES=$(notmuch search --output=files --exclude=false tag:$ARCHTAG and not path:$ARCHFOL/**) # Files which are tagged with "archive" but not in ARCHIVE folder
# Folder will be derived from the current position later
@@ -62,9 +85,11 @@ while read line; do
# If one file is tagged (except del, spam, archive), also tag the other mails of its thread respectively, IF they are located in INBOX [TODO: is this the best approach?]. Only then they can be moved by this script
# 1. Get thread ids of all entries with the searched tag
THREADS=$(notmuch search --exclude=false tag:$TAG and date:$(date -d "-6 weeks" +%Y-%m-%d)..$(date -d "+1 year" +%Y-%m-%d) | cut -d" " -f1 | cut -d":" -f2)
debugmsg "search for all threads tagged with $TAG"
THREADS=$(notmuch search --exclude=false tag:$TAG and date:$DATELIMIT..$(date -d "+1 year" +%Y-%m-%d) | cut -d" " -f1 | cut -d":" -f2)
# 2. Get message ids of all messages in these threads
debugmsg "search for all IDs of these threads"
MID=
for t in $THREADS; do
MID="$MID "$(notmuch search --output=messages --exclude=false thread:$t)
@@ -72,19 +97,23 @@ while read line; do
done
# 3. Tag all message ids of messages which are still in INBOX
debugmsg "tag all these IDs if they are in INBOX:" $(echo "$MID" | wc -w) "files"
for m in $MID; do
if $(notmuch search --exclude=false --output=files "$m" | grep -q "$MAILDIR/INBOX/"); then
debugmsg "tag $m with $TAG"
logrun notmuch tag +"$TAG" -- "$m"
fi
done
# Search for these lone messages in INBOX
debugmsg "search for these messages which are still in INBOX"
HITFILES=$(notmuch search --output=files --exclude=false path:INBOX/** and tag:$TAG | grep "$MAILDIR/INBOX/")
fi
fi
# Move files to their destination folders
debugmsg "move the files of tag $TAG"
for m in $HITFILES; do
# Destination folder is derived from the current location
@@ -99,5 +128,10 @@ while read line; do
logrun mv "$m" "$DESTFOL"
done
fi
debugmsg "Tag $TAG finished"
debugmsg ""
done < "$CSV"
echo $(date +%Y-%m-%d) > ${LASTRUN}