#!/bin/sh # bin mail-sort -ci "-vD" -- \ # mail-sort -- sort mail by the sender or the from header fields revision='$Revision: 1.10 $'; version(){ cat < This is free software; see the GNU General Public License version 2 or later for copying conditions. There is NO warranty. VRS false; } help() { cat <]*>/ {;s/.*<\([^@]*@[^ >]*\)>/\1/p;q;}' -e '/[^@ ]*@[^ ]*/ s/\([^@ ]*@[^ ]*\)/\1/p'| tr '[ [:upper:]]' '[_[:lower:]]' } log_mail() { [ "$*" ]&& echo "$pgm: $*" >&4; } sort_mail() { local dir="$1" folder out; ### Filter files into $dir/(sender|from) ### ### This single recipe/function sorts mail by $dir/(sender|from) ### filed according to the dir variable, and in this subdirectory, ### folders will be created based on the sender header field, if it exists, ### otherwise, folders will be created based on the from header field. while read -r file; do [ "$file" ]&& [ -f "$file" ]|| continue; # get sender from=`get_email_addr sender: <"$file"` [ "$from" ]|| from=`get_email_addr from: <"$file"` folder="$dir/$from" formail -l "$folder" <$file if [ -z "$debug" ]; then formail <$file | gzip >>"${folder}.gz"&& log_mail $(rm -f $verb "$file") fi done } warn() { echo $pgm:\ "$@">&2;} # default values INCLUDERC="$HOME/.procmailrc.include" MAILDIR=$HOME/Mail YEAR=`date +%Y`; SORT_LOG=log/sort.$YEAR SORT_DIR=From; SORT_FIND="-mtime +7"; UMASK=077; [ ! -f "$INCLUDERC" ]|| . "$INCLUDERC"|| exit; pgm=mail-sort one="DHVv" umask $UMASK; while getopts "d:f:S:$one" option;do case "$option" in H) help;; V) version $revision;; S) set $OPTARG;; D) debug=y;; d) SORT_DIR="$OPTARG";; f) SORT_FIND="$OPTARG";; v) verb=-v;; \?) false;; *) false;; esac|| exit; done shift $(($OPTIND-1)); [ "$MAILDIR" ]|| error "MAILDIR variable is null"|| exit; cd "$MAILDIR"|| exit; [ "$debug" ]&& SORT_LOG="${SORT_LOG}.debug" [ "$SORT_LOG" ]|| error "SORT_LOG variable is null"|| exit; [ -d "$SORT_DIR" ]|| mkdir -p $verb "$SORT_DIR"|| exit exec 4>>"$SORT_LOG"|| exit; while [ $# -gt 0 ]; do [ -d "$1/cur" ]&& find "$1/cur" $SORT_FIND | sort_mail "$SORT_DIR" >&4; shift done;