|
|
...making Linux just a little more fun! Bash Shell and Beyond AppliedBy William Park
Deleting Spam on a POP3 ServerThis article will illustrate the use of my extended 'case' and 'read' Bash shell builtins (See my other articles in issues 108, 109 and 110) to delete Spam on my ISP's POP3 mail server before it gets downloaded into my local mail system. The example scripts use these extended functions, so they require that you have my shell extensions installed. On average, I get 1 MB of spam per hour on my Yahoo account. The most troublesome of these, both in size and number, are Microsoft Swen and Netsky worms. Fortunately, they are easy to identify, and can be deleted right on the POP3 server.
Telnet to POP3 serverIn order to understand the shell script, you should first log in to your POP3 server using Telnet, because a shell script only automates what you type on the command line. So, let's do that:
telnet pop.your.isp 110
user username
pass password
will connect to remote POP3 server (port 110), and log in using
your 'username' and 'password'.
stat
top 1 10
Here, stat returns the number of messages and total size, and
top 1 10 prints the header of the 1st email plus the top 10 lines
of the body. For our purpose, we are only interested in the header,
specifically the 'boundary' parameter; so, top 1 0 is what
we need for our script. Note that a single '.' (dot) on a line by itself signals the
end of output.
dele 1
quit
dele 1 marks the 1st message to be deleted, and
quit ends the POP3 session upon which the server removes
all messages marked for deletion.
Shell script
UsageYou can source the 3 functions and run
check pop.your.isp username password
from the command line or in a script. However, if you use Fetchmail to
download emails (like I do), then you already have servers,
usernames, and passwords in ~/.fetchmailrc. You can
extract these data using fetchmail --configdump directly:
(
fetchmail --configdump
cat << EOF
for server in fetchmailrc['servers']:
if server['protocol'] == 'POP3':
for user in server['users']:
print server['pollname'], user['remote'], user['password']
EOF
) | python | while read server user pass; do
# use (...) to prevent 'exit' terminating entire script
check "$server" "$user" "$pass"
done
The entire script is available from popcheck.bash, and should be run just before Fetchmail,
popcheck.bash && fetchmail
usually from crontab.
SummaryAlthough the script deals with Microsoft Swen/Netsky worms, you can add your own patterns. For example,
'boundary="=+[0-9]+=+"' ))
echo TAG.spam ;;
'(Subject|From): =\?[A-Za-z0-9_-]+\?' ))
echo non.English ;;
'charset="(ks_c_5601-1987|euc-kr|big5|gb2312|iso-2022-jp|shift-jis)"'
))
echo APIC.charset ;;
'<(5[89]|6[01]|20[23]|21[0189]|22[012])(\.[0-9]{1,3}){3}>'
))
echo APIC.IP ;;
'Content-Type: text/html' ))
echo HTML.header ;;
|
I learned Unix using the original Bourne shell. And, after my
journey through language wilderness, I have come full-circle
back to shell. Recently, I've been patching features into Bash,
giving other scripting languages a run for their money.
Slackware has been my primary distribution since the beginning,
because I can type. In my toolbox, I have Vim, Bash, Mutt, Tin,
TeX/LaTeX, Python, Awk, Sed. Even my shell command line is in
Vi-mode.