Program execution Flow:
1. Start accepting mail.
2. Check if the sender is in the blacklist, and if it refuses to accept it, continue
3. Check whether the sender is in the whitelist, and if it is receiving mail, continue
4. Keyword filtering of the message, if the message contains filtered keyword information, then refused to receive
The message, otherwise, receive the message.
Usage:sh mailfilter.sh [-R Reject-addr] [-a add-addr][-k keywords]
###########################################################################
#/bin/bash;
#当有邮件到达时, copy a user analysis process first
If_mail ()
{
User= ' WhoAmI ';
If Test-s/var/spool/mail/$User;
Then
' cp/var/spool/mail/$User/var/spool/mail/tmp ';
return 1;
Else
# echo ' no mail to deal with ';
return 0;
Fi
}
#定位制定email行号
Locate ()
{
If [-F "/var/spool/mail/tmp"];
Then
get_string= ' Grep-n $1/var/spool/mail/tmp|head-1 ';
#${varible%%string*} to intercept the string after the last string from right to left
get_n=${get_string%%:*};
#Get_Sting = ' sed ' $n, touch@localhost.localdomain '/var/spool/mail/tmp|head-1 ';
#echo $Get _sting;
Else
get_n=0;
Fi
return $Get _n;
}
Deal_black_list ()
{
#此邮件判断在黑名单中
mail_addr=$1;
Locate $Mail _addr;
#$? Displays the exit status of the last command
if test $? -eq 1;
Then
echo "$Mail _addr has been blacklisted, rejected this email!!!";
#清空文件
' Echo >/var/spool/mail/$User ';
# Delete blank Lines
' Sed-i '/^$/d '/var/spool/mail/$User ';
' Rm/var/spool/mail/tmp ';
return 1;
Else
return 0;
Fi
}
Deal_white_list ()
{
email_addr=$1;
Locate $Email _addr;
#$? Displays the exit status of the last command
if test $? -eq 1;
Then
return 1;
Else
return 0;
Fi
}
Filter_keywords ()
{
If [-F "/var/spool/mail/tmp"] && [0-lt ' grep-c $1/var/spool/mail/tmp '];
Then
echo "contains filter keywords $, reject this email!!!";
#清空文件
' Echo >/var/spool/mail/$User ';
# Delete blank Lines
' Sed-i '/^$/d '/var/spool/mail/$User ';
' Rm/var/spool/mail/tmp ';
return 1;
Else
echo "Mail Acceptance";
return 0;
Fi
}
While Getopts:r:a:k:h opt
Do
Case $opt in
R
Black_addr= $OPTARG;
;;
A
White_addr= $OPTARG;
;;
K
Keywords= $OPTARG;
;;
H
echo "Usage:sh mail.sh [-R Reject-addr] [-a add-addr]";
Exit 0;
;;
*)
echo "-$opt parameter input Error! ";
Exit 0;
;;
Esac
Done
echo ' Mail filtering system enabled ';
Old_hash= ' md5sum/var/spool/mail/$USER |cut-d '-f1 ';
New_hash= ' md5sum/var/spool/mail/$USER |cut-d '-f1 ';
While:
Do
Sleep 3;
n=0;
If_mail;
if test $? -eq 0;
Then
#邮件为空
Continue
Else
New_hash= ' md5sum/var/spool/mail/$USER |cut-d '-f1 ';
Fi
#判断是否有新文件
If ["$Old _hash" = "$New _hash"];
Then
Continue
Else
Echo ' new mail arrives and the system is about to be processed! ‘;
Old_hash= ' md5sum/var/spool/mail/$USER |cut-d '-f1 ';
Fi
#黑名单过滤
Deal_black_list $Black _addr;
if test $? -eq 1;
Then
Continue
Fi
#白名单过滤
Deal_white_list $White _addr;
if test $? -eq 1;
Then
echo "$White _addr Mail has been received"
Continue
Fi
#关键词过滤
Filter_keywords $Keywords;
Done
###########################################################################
This is one of my homework, the first time to write shell scripts, there is no shortage of flaws. Look at it!
Shell Script Simulation Milter implementation of black and white list and keyword filtering