#!/usr/bin/env zsh # # Copyright (C) 2009,2011 Nick Bowler # # Delete email older than a given date from a maildir. Assumes that the # Date headers in the mails are valid and correct, and is therefore # somewhat less useful for cleaning up spam. # # License WTFPL2: Do What The Fuck You Want To Public License, version 2. # This is free software: you are free to do what the fuck you want to. # There is NO WARRANTY, to the extent permitted by law. cutoff='last week' maildir= while getopts 'd:' opt $@; do case $opt in d) cutoff=$OPTARG ;; \?) exit 1 ;; esac done shift $((OPTIND-1)) # Determine maildir if [[ -n $1 ]]; then maildir=$1 elif [[ -n $MAIL ]]; then maildir=$MAIL fi # Validate arguments date=`date --rfc-3339=date --date=$cutoff` || exit 1 if [[ -z $maildir ]]; then printf '%s: you must specify a maildir.\n' $0 exit 1 fi if ! [[ -d $maildir/cur && -d $maildir/new && -d $maildir/tmp ]]; then printf '%s: %s is not a valid maildir.\n' $0 $maildir exit 1 fi total=`ls $maildir/cur | wc -l`. count=0 for i in $maildir/cur/*; do datehdr=`formail -x Date < $i` maildate=`date --rfc-3339=date --date=$datehdr` count=$((count+1)) if [[ $maildate < $date ]]; then printf '(%.0f%%) %s: deleting %s\n' $((100*count/total)) $0 $i rm -f $i else printf '(%.0f%%) %s: keeping %s\n' $((100*count/total)) $0 $i fi done