Created
July 25, 2015 16:47
-
-
Save keith/1d0b8e493c83c6ab5f69 to your computer and use it in GitHub Desktop.
Archive or delete maildir messages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
mail=~/.mail | |
maildirs=($mail/*) | |
inbox=INBOX | |
folders=(new cur) | |
archive="archive/cur" | |
trash="trash/cur" | |
usage() { | |
echo "usage: $(basename "$0") [archive|delete] [FILENAME]" | |
exit 1 | |
} | |
if [ $# -ne 2 ]; then | |
usage | |
fi | |
cmd=$1 | |
file=$2 | |
read_file_name() { | |
file=$1 | |
flags=$(echo "$file" | cut -d , -f 4) | |
if [[ $flags == *"S"* ]]; then | |
echo "$file" | |
else | |
echo "${file}S" | |
fi | |
} | |
find_file() { | |
for inbox in "${maildirs[@]}"/$inbox | |
do | |
for folder in "${folders[@]}" | |
do | |
file="$inbox/$folder/$1" | |
if [[ -f $file ]]; then | |
echo "$file" | |
return | |
fi | |
done | |
done | |
echo "'$1' not found" | |
exit 1 | |
} | |
root() { | |
dirname "$(dirname "$(dirname "$1")")" | |
} | |
move_message() { | |
file="$1" | |
folder="$2" | |
path=$(find_file "$file") | |
root=$(root "$path") | |
new_name=$(read_file_name "$file") | |
new_path="$root/$folder" | |
if [[ ! -d "$new_path" ]]; then | |
echo "'$new_path' does not exist" | |
exit 1 | |
fi | |
mv -v "$path" "$new_path/$new_name" | |
} | |
archive() { | |
echo "archiving $1" | |
move_message "$1" "$archive" | |
} | |
delete() { | |
echo "Deleting $1" | |
move_message "$1" "$trash" | |
} | |
$cmd "$file" || usage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Where
FILE
is in an inbox.Example: