]> git.draconx.ca Git - scripts.git/blob - maildir-clean.zsh
Add script to convert "JWK"-format RSA keys to normal.
[scripts.git] / maildir-clean.zsh
1 #!/usr/bin/env zsh
2 #
3 # Copyright (C) 2009,2011 Nick Bowler
4 #
5 # Delete email older than a given date from a maildir.  Assumes that the
6 # Date headers in the mails are valid and correct, and is therefore
7 # somewhat less useful for cleaning up spam.
8 #
9 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
10 # This is free software: you are free to do what the fuck you want to.
11 # There is NO WARRANTY, to the extent permitted by law.
12
13 cutoff='last week'
14 maildir=
15
16 while getopts 'd:' opt $@; do
17         case $opt in
18         d) cutoff=$OPTARG ;;
19         \?) exit 1 ;;
20         esac
21 done
22
23 shift $((OPTIND-1))
24
25 # Determine maildir
26 if [[ -n $1 ]]; then
27         maildir=$1
28 elif [[ -n $MAIL ]]; then
29         maildir=$MAIL
30 fi
31
32 # Validate arguments
33 date=`date --rfc-3339=date --date=$cutoff` || exit 1
34
35 if [[ -z $maildir ]]; then
36         printf '%s: you must specify a maildir.\n' $0
37         exit 1
38 fi
39
40 if ! [[ -d $maildir/cur && -d $maildir/new && -d $maildir/tmp ]]; then
41         printf '%s: %s is not a valid maildir.\n' $0 $maildir
42         exit 1
43 fi
44
45 total=`ls $maildir/cur | wc -l`.
46 count=0
47
48 for i in $maildir/cur/*; do
49         datehdr=`formail -x Date < $i`
50         maildate=`date --rfc-3339=date --date=$datehdr`
51
52         count=$((count+1))
53
54         if [[ $maildate < $date ]]; then
55                 printf '(%.0f%%) %s: deleting %s\n' $((100*count/total)) $0 $i
56                 rm -f $i
57         else
58                 printf '(%.0f%%) %s: keeping %s\n' $((100*count/total)) $0 $i
59         fi
60 done