#!/bin/sh # mbox-compress - reduces the file size of mbox mail folder by # removing some headers. # # formail(1), part of procmail(1), is used to keep only certain # headers, removing all other headers. # # I have hard coded the directoties here to avoid typing errors. # This can be easily adjusted for flexible use. find(1) is used to # find the files (mbox format is assumed) in given directoties. # Directories in which to find mbox files. dirs="${HOME}/mail/In*" dirs="$dirs ${HOME}/mail/Ignore" dirs="$dirs ${HOME}/mail/Live-elements" case $# in 0 ) ;; * ) echo "No input is accepted; will use files in '$dirs'." 1>&2 set - ;; esac botched_copy() { echo "Error occured $@" 1>&2 echo "$mbox remains intact, botched copy is ${mbox}.new" 1>&2 } make_list() { flag=$1 shift var= case $# in 0 ) ;; * ) for v in $@ ; do var="$var $flag $v" ; done ;; esac printf "$var" } # List of headers to keep. Keep=$( make_list -X \ Return-Path: Sender: \ Delivered-To: X-Envelope-To: \ \ Date: \ Subject: \ From: Reply-To: To: CC: Bcc: \ Message-ID: In-Reply-To: References: \ \ Newsgroups: Followup-to: \ Mail-Followup-to: \ \ X-Bogosity: \ \ Mailing-List: List-ID: \ List-Subscribe: List-Unsubscribe: \ \ X-FreeBSD-CVS-Branch: \ \ Status: X-Status: \ Lines: \ MIME-Version: \ Content-Length: Content-Type: \ Content-Disposition: \ Content-Transfer-Encoding: \ \ Fax: X-Fax: Phone: X-Phone: URL: X-URL: \ \ X-Corp: X-Corp-Fax: X-Corp-Phone: X-Corp-URL: \ X-Work-Start: X-Work-location: \ \ X-pair-Authenticated: \ ) # List of headers to remove specifically. Remove=$( make_list -I X-Face: ) # Find the files in $dirs, and operate on them. for mbox in $( find $dirs -type f -not -name 'xx.*' -print ) do # Skip "special" files. case $mbox in *[Cc][Aa][Cc][Hh][Ee]* | *.db | *.tar | *.bz2 | *.gz | *.tbz2 | *.tgz ) continue ;; esac [ ! -s "$mbox" -o ! -f "$mbox" -o ! -w "$mbox" ] \ && { echo "Skipped $mbox ...." 1>&2 ; continue; } #echo "Processing $mbox ..." [ -e "${mbox}.new" ] && rm -f "${mbox}.new" # Do the real work, finally. if { formail -z -c -U Delivered-To: \ -k -X 'From ' \ ${Keep} ${Remove} \ -s < "$mbox" >> "${mbox}.new" } then # touch(1) restores the access & modfication times so that MUA # like mutt(1) don't list the updates files to contain new # messages. touch -r "$mbox" "${mbox}.new" \ && mv -f "${mbox}.new" "$mbox" \ || botched_copy "while moving ${mbox}.new to $mbox" else botched_copy "while processing" fi done echo '... done'