#!/bin/sh
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    ( http://www.fsf.org/licenses/gpl.txt )
#     

#
# Release: 2002-08-09
# last update: 2018-02-28
#
# New versions of the script are available at
#    http://www.fuschlberger.net/programs/mutt/
#
# Feedback is welcome!
#
# This script writes a list of mailaddresses to the file ~/.procmailrc. 
# Mails from these addresses are written to the file $TARGET.
#
#
# I wrote this script to avoid the checking of mails from known
# sender-addresses by spamassassin which meant 5 seconds 100% CPU-usage on my
# old P133
#
# .procmailrc1 + .procmailrc2 + .procmailrc3 = .procmailrc
#
# default-behavior is to copy the original ~/.procmailrc to  ~/.procmailrc3 and
# to touch (=create) ~/.procmailrc1 so the generated procmail-rule will be
# written at the beginning of the orignial ~/.procmailrc
#
# .procmailrc[13] is the orginal split-up .procmailrc, .procmailrc2 is the file
# generated by the script, which is written between *1 and *3 into the new
# .procmailrc.  So split up your .procmailrc into two parts at the point where
# you want the script to insert the procmail-rules to avoid further filtering
# (in my case spamassassin).
#

#
# write backup, just in case...
#

if [ ! -f ~/.procmailrc.bakxxx ]; then
	echo -n "Writing backup..."
	cp ~/.procmailrc ~/.procmailrc.bakxxx
    if [ "$?" != "0" ]; then
      echo "failed!"
      exit 1
    fi
    echo "done"
fi

#
# copy ~/.procmailrc to ~/.procmailrc3 so per default the rule generated from
# the mailaddresses will be written at the beginning of the 'old' ~/.procmailrc
#

if [ ! -f ~/.procmailrc3 ] && [ ! -f ~/.procmailrc1 ]; then
	echo -n "Copying ~/.procmailrc to ~/.procmailrc3..."
    cp ~/.procmailrc ~/.procmailrc3 && echo done.
    if [ "$?" != "0" ]; then
      echo "failed!"
      exit 1
    fi
    echo -n "Touching ~/.procmailrc1..."
    touch ~/.procmailrc1 && echo done.
    if [ "$?" != "0" ]; then
      echo "failed!"
      exit 1
    fi
fi

#
# file from which to get email-addresses:
# 1. read filename from ~/.muttrc
# 2. read filename from files which are sourced from ~/.muttrc
# 3. read filename from /etc/Muttrc, 
#    if neither exists 
# 4. default to ~/.muttrc
#
# 1.
#

ALIASFILE=$(grep ^[[:space:]]*set[[:space:]]*alias_file $HOME/.muttrc | sed -e 's/^.*=//g' -e 's/"//g' -e 's!^~!'$HOME'!g') # ":char needed for code2html

#
# 2.
#

if [ "$ALIASFILE" = "" ]; then
  SOURCEFILES=$(grep ^[[:space:]]*source $HOME/.muttrc | sed -e 's/^.[[:alpha:]]*[[:space:]]*//g' -e 's/"//g' -e 's!~/!'$HOME'/!g')

  for i in $SOURCEFILES; do
    ALIASFILEPART=$(grep ^[[:space:]]*set[[:space:]]*alias_file $i | sed -e 's/^.*=//g' -e 's/"//g' -e 's!^~!'$HOME'!g')
    ALIASFILE="$ALIASFILE $ALIASFILEPART"
  done;
fi

#
# eliminate spaces
#

ALIASFILE=$(echo $ALIASFILE)

#
# 3.
#

if [ "$ALIASFILE" = "" ]; then
  ALIASFILE=$(grep ^[[:space:]]*set[[:space:]]*alias_file /etc/Muttrc | sed -e 's/^.*=//g' -e 's/"//g' -e 's!^~!'$HOME'!g') # ":char needed for code2html 
fi

#
# 4.
#

if [ "$ALIASFILE" = "" ]; then
  ALIASFILE="$HOME/.muttrc"
fi

#
# check if an alias exists in $ALIASFILE
#

if [ "$(grep "^[[:space:]]*alias" $ALIASFILE)" = "" ]; then
  echo "Error: No alias found in $ALIASFILE"
  exit 1
fi

#
# the file this script generates:
#

DIRECT2FILE=$HOME/.procmailrc2

#
# the default target is the mailspool-file
#

TARGET=\$DEFAULT

#
# read lines from $ALIASFILE and isolate mailaddresses
#1: Strip everything behind the pound-sign '#'
#2: Kill everything before the alias definition and replace it with
#   the '>'-character. Now every unused text is between > and < which
#   makes filtering lot more easier
#3: Remove everything between > and < and print out a space instead
#4: Remove leading spaces, trailing spaces + last '>'-character and
#   everything after it
#5: replace all spaces with newlines
#6: replace all duplicates and sort alphabetically
#

MAILADDRESSES=$(sed -e 's/\([^#]*\)#.*/\1/g' \
    -e 's/^[[:space:]]*alias[[:space:]]*[^[:space:]]*[[:space:]]/>/g' \
    -e 's/>[^<]*</ /g' \
    -e 's/\(^[[:space:]]*\|>.*\)//g' \
    -e 's/ /\
/g' <$ALIASFILE | sort -u)

cat > $DIRECT2FILE << EOF
### Start of procmail-whitelist for instant delivery ###

:0:
EOF
for i in $MAILADDRESSES; do
	echo "* 1^0 ^From: .*$i" >> $DIRECT2FILE
done;

cat >> $DIRECT2FILE << EOF
$TARGET

### End of procmail-whitelist for instant delivery ###

EOF

#
# write new .procmailrc from the two parts of the old .procmailrc + in the
# middle the file generated by this script:
#

cat ~/.procmailrc1 "$DIRECT2FILE" ~/.procmailrc3 > ~/.procmailrc


syntax highlighted by Code2HTML, v. 0.9.1