2017-04-13 12:28:53 +02:00
#!/bin/bash
# Script moves tagged mails to their respective folders if they are still lying around in INBOX
2018-02-05 17:47:43 +01:00
# It also moves mails tagged with "del" to the Trash folder, "archive" to Archive, and "spam" to Spam folder
2017-04-13 12:28:53 +02:00
CURDIR = $( cd " $( dirname " ${ BASH_SOURCE [0] } " ) " && pwd )
2018-07-19 10:44:58 +02:00
DEBUG = false # use 'true' to echo verbose debug messages
2017-07-28 17:12:21 +02:00
2017-04-13 12:28:53 +02:00
CSV = " $CURDIR " /tag-list.csv # Table of tags/folders
2018-07-19 11:42:43 +02:00
MAILDIR = ` notmuch config get database.path` # Maildir
2017-04-13 12:28:53 +02:00
TRASHTAG = "del" # Tag of trashed mails
TRASHFOL = "Trash" # Trash bin folder
SPAMTAG = "spam" # Tag of spam mails
SPAMFOL = "INBOX.Junk" # Spam folder
ARCHTAG = "archive" # Archive tag
ARCHFOL = "ARCHIVE" # Archive folder
LOG = " $CURDIR " /move-tag.log # Location of logfile
2017-05-30 16:59:02 +02:00
USETAGS = ".+" # List of tags which shall be moved
THREADTAGS = ".+" # List of tags for which single, but connected messages in INBOX shall be tagged
2017-04-13 12:28:53 +02:00
2017-07-28 17:12:21 +02:00
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
2017-04-13 12:28:53 +02:00
touch " $LOG " # Create logfile or update time stamp
2017-07-28 17:12:21 +02:00
var_conn = $1 # supposed to be "on" or "off"
2017-04-13 12:28:53 +02:00
function pdate {
DATE = $( date +%y-%m-%d_%H:%M:%S)
echo " [ $DATE ] "
}
function logrun {
# Write command itself to log, and pipe errors to log
echo " $( pdate) $@ " >> " $LOG "
2017-05-30 21:26:59 +02:00
eval " $@ " 2>> " $LOG "
2017-04-13 12:28:53 +02:00
}
2017-07-28 17:12:21 +02:00
function debugmsg {
if ${ DEBUG } ; then
echo " $( pdate) $@ "
fi
}
# Set date until when files are searched for, depending on current connection status
2018-07-05 17:57:01 +02:00
DATELIMIT = "30d"
echo " Datelimit for search set to $DATELIMIT "
2017-07-28 17:12:21 +02:00
2017-04-13 12:28:53 +02:00
while read line; do
2018-07-19 10:43:10 +02:00
if $( echo " $line " | grep -qE " ^\s*#|^\s* $" ) ; then continue ; fi
2017-04-13 12:28:53 +02:00
TAG = $( echo $line | cut -d";" -f1)
FOL = $( echo $line | cut -d";" -f2)
2017-07-12 15:30:01 +02:00
if ! $( echo $BL_USETAGS | grep -qE " ${ TAG } ( $|\|) " ) && $( echo $TAG | grep -qE " $USETAGS " ) ; then
2017-05-30 16:59:02 +02:00
BL_USETAGS = " $TAG , $BL_USETAGS " # Add current tag to list of tags which do not have to be searched for another time
2017-04-13 12:28:53 +02:00
# TRASH tag "del"
if [ " $TAG " = " $TRASHTAG " ] ; then
2017-07-28 17:12:21 +02:00
debugmsg "search trash tagged mails"
2017-04-13 12:28:53 +02:00
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
2017-07-28 17:12:21 +02:00
debugmsg "search spam tagged mails"
2018-12-04 10:45:23 +01:00
HITFILES = $( notmuch search --output= files --exclude= false " tag: $SPAMTAG and (not path: $SPAMFOL /**) and (not path: $ARCHFOL /**) " ) # Files which are tagged with "spam" but not in Spam folder
2017-04-13 12:28:53 +02:00
FOL = " $SPAMFOL " # Set destination folder to INBOX.Junk
# ARCHIVE tag "archive"
elif [ " $TAG " = " $ARCHTAG " ] ; then
2017-07-28 17:12:21 +02:00
debugmsg "search archive tagged mails"
2017-04-13 12:28:53 +02:00
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
# DEFAULT
else
# For some tags, also tag its threads
2017-07-12 15:30:01 +02:00
if ! $( echo $BL_THREADTAGS | grep -qE " ${ TAG } ( $|\|) " ) && $( echo $TAG | grep -qE " $THREADTAGS " ) ; then
2017-04-13 12:28:53 +02:00
# 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
2017-07-28 17:12:21 +02:00
debugmsg " search for all threads tagged with $TAG "
2018-07-04 09:26:14 +02:00
THREADS = $( notmuch search --exclude= false --output= threads tag:$TAG and date:${ DATELIMIT } ..)
if [ [ -z $THREADS ] ] ; then debugmsg " Tag $TAG finished. No thread within ${ DATELIMIT } found. " ; debugmsg "" ; continue ; fi
2017-04-13 12:28:53 +02:00
# 2. Get message ids of all messages in these threads
2018-07-04 09:26:14 +02:00
debugmsg "search for all IDs of these " $( echo " $THREADS " | wc -w) " threads"
MID = $( notmuch search --output= messages --exclude= false $THREADS )
# MID consists of many something like "id:5XXX0CD1.305516@fsfe.org"
# 3. Mark all files of messages if they are still in INBOX
debugmsg "mark all files of " $( echo " $MID " | wc -w) " IDs if they are in INBOX"
HITFILES = $( notmuch search --output= files --exclude= false " $MID " | grep " $MAILDIR /INBOX/ " )
2017-04-13 12:28:53 +02:00
fi
2017-05-30 16:59:02 +02:00
2017-04-13 12:28:53 +02:00
fi
# Move files to their destination folders
2018-07-04 09:26:14 +02:00
debugmsg "move " $( echo " $HITFILES " | wc -w) " files of tag $TAG from INBOX "
2017-04-13 12:28:53 +02:00
for m in $HITFILES ; do
# Destination folder is derived from the current location
if [ " $TAG " = " $ARCHTAG " ] ; then
SUBFOL = $( echo $m | sed -E " s|( $MAILDIR )/(.+?)/cur/.*|\2|g " )
FOL = " $ARCHFOL / $SUBFOL "
fi
DESTFOL = " $MAILDIR / $FOL /cur "
if [ ! -e " $DESTFOL " ] ; then
logrun mkdir -p " $DESTFOL "
fi
logrun mv " $m " " $DESTFOL "
done
fi
2017-07-28 17:12:21 +02:00
debugmsg " Tag $TAG finished "
debugmsg ""
2017-04-13 12:28:53 +02:00
done < " $CSV "