#!/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
#
# New versions of the script are available at
#    http://www.fuschlberger.net/programs/mutt/
#
# Feedback is welcome!
#
# This script reads mailadresses from $ALIASFILE (which has to be in the style
# of a Mutt-Alias-File) and converts them to a whitelist for spamassassin.
#
#
# these are the recognized alias-definitions:
# 
# alias somebody Name1 <address@domain>
# alias somebody Name1 Name2 <address@domain>
# alias groupaliasname <address1@domain1>, <address2@domain2>
#
# group aliases, which are referencing user aliases, are ignored, since
# expanding them would mean producing dupes, which are afterwards wiped out
# anyway
#

# 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, 
# 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') # ":char needed for code2html

  for i in $SOURCEFILES; do
    ALIASFILEPART=$(grep ^[[:space:]]*set[[:space:]]*alias_file $i | sed -e 's/^.*=//g' -e 's/"//g' -e 's!^~!'$HOME'!g') # ":char needed for code2html
    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 preferences-file for spamassassin
#

SAPREFFILE="$HOME/.spamassassin/user_prefs"

#
# file to which the original user_prefs is copied, which will be used as basis
# to generate new user_prefs
# be sure to alter only user_prefs.temp, since user_prefs will be overwritte at
# every run of the script
#

SAPREFTEMPLATE="$HOME/.spamassassin/user_prefs.temp"

#
# Extract mailaddresses from $ALIASFILE:
#
#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 newlines with spaces 
#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)

if [ ! -f "$SAPREFTEMPLATE" ]; then 
	echo
	echo Running for the first time
	echo
	echo Copying $SAPREFFILE to $SAPREFTEMPLATE
	echo
	cp "$SAPREFFILE" "$SAPREFTEMPLATE"
	echo Creating $SAPREFFILE from $SAPREFTEMPLATE and $ALIASFILE
fi
cat "$SAPREFTEMPLATE" > "$SAPREFFILE"
echo -n "whitelist_from " >> $SAPREFFILE
echo -n $MAILADDRESSES >> "$SAPREFFILE"



syntax highlighted by Code2HTML, v. 0.9.1