Skip to content

Instantly share code, notes, and snippets.

@keith
Created July 25, 2015 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keith/1d0b8e493c83c6ab5f69 to your computer and use it in GitHub Desktop.
Save keith/1d0b8e493c83c6ab5f69 to your computer and use it in GitHub Desktop.
Archive or delete maildir messages
#!/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
@keith
Copy link
Author

keith commented Jul 25, 2015

Usage:

maildir [FILE]

Where FILE is in an inbox.

Example:

maildir 1234_1.2345.hostname,U=12345,FMD5=abc123:2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment