Skip to content

Instantly share code, notes, and snippets.

@golderweb
Created June 23, 2014 18:28
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save golderweb/e02d405699588af09383 to your computer and use it in GitHub Desktop.
Automatic generation of e-mail black- and whitelists for maildrop from mails in specific (IMAP-)folders by "From"-header
#!/bin/bash
#
# Automatic generation of e-mail black- and whitelists for maildrop from mails in specific (IMAP-)folders by "From"-header
#
# For more information see <http://golderweb.de/2014/06/blacklist-fuer-maildrop-halbautomatisch-erstellen/> (German)
#
# Copyright 2014, GOLDERWEB – Jonathan Golder <jonathan@golderweb.de>
#
# 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 3
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>
#
function auto_list(){
# Make sure OUTPUT and INPUT are directories
if [[ ! -d $OUTPUT || ! -d $INPUT ]]
then
echo 'ERROR: OUTPUT and INPUT must be directories!'
exit 1
fi
# Warn if other list then black or white
if [[ $1 != 'black' && $1 != 'white' ]]
then
echo "WARNING: $1 normaly need to be 'black' or 'white'"
fi
# Create empty list if there is none before to prevent errors
if [[ ! -f $OUTPUT/${1}list-auto ]]
then
touch $OUTPUT/${1}list-auto
fi
# Make a copy of current list
cp $OUTPUT/${1}list-auto $OUTPUT/.${1}list-auto.bak
# Grep all "From"-headers width mailadresses from mails in $INPUT and write to $OUTPUT/.raw.tmp
grep -rEo -m 1 "^From: .*\@.*$" $INPUT/* >$OUTPUT/.raw.tmp
# Grep email adresses and write to $OUTPUT/.done.tmp
grep -oE '[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+' $OUTPUT/.raw.tmp >$OUTPUT/.done.tmp
# Concatenate old and new list, sort alphabetically ascending and unique entries
cat $OUTPUT/.${1}list-auto.bak $OUTPUT/.done.tmp | sort | uniq -i >$OUTPUT/${1}list-auto
# Remove temporary files
rm $OUTPUT/.raw.tmp
rm $OUTPUT/.done.tmp
rm $OUTPUT/.${1}list-auto.bak
}
# Configure OUTPUT for both black and whitelist
OUTPUT=$HOME/etc/maildrop
#Configure INPUT for blacklist and create it
INPUT=$HOME/users/golderweb/.Spam.Blacklist
auto_list 'black'
#Configure INPUT for whitelist and create it
INPUT=$HOME/users/golderweb/.Spam.Whitelist
auto_list 'white'
@timwattenberg
Copy link

How are the lists integrated into maildrop?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment