#! /bin/bash

cd ~/public_html/spamcounter

exec >~/public_html/spam_statistics.html

cat <<EOF
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html" />
<meta http-equiv="Refresh" content="3600" />
<meta http-equiv="Pragma" content="no-cache" />
<meta name="generator" content="spam_statistics_gen.sh" />
<title>Spam statistics for utx@penguin.cz</title>
<style type="text/css">
/*<![CDATA[*/
 body {color: black; background-color: white }
 a {color: blue; text-decoration: none }
 a:link { color: blue }
 a:visited { color: teal; text-decoration: none }
 a:hover { color: teal; text-decoration: underline }
 a:focus { color: red }
 .center { text-align: center }
 img { border: 0px}
 table { border-style: solid; border-width: thin }
 th { text-align: center; border-style: solid; border-width: thin ; padding: .1em }
 td { text-align: right; border-style: solid; border-width: thin ; padding: .1em }
/*]]>*/
</style>
</head>
<body>

<h1>Spam statistics for utx@penguin.cz</h1>

<p>I use my permanent e-mail address for about 5 years. It's used
daily for GNU/Linux development and personal purposes. Now it is
heavily attacked by spamers and virus owners.</p>

<p>Here is the statistics of deleted junk mails:</p>

EOF

echo "<h2>Hourly statistics (last 24 hours)</h2>"
echo "<table><tbody>"
echo "<tr><th></th><th colspan=\"2\">spams</th><th colspan=\"2\">viruses</th><th colspan=\"2\">bounces</th><th colspan=\"2\">passed</th></tr>"
echo "<tr><th>Time (UTC)</th><th>#</th><th>size</th><th>#</th><th>size</th><th>#</th><th>size</th><th>#</th><th>size</th></tr>"
tail -n 24 ~/spam.log |
(
    while read unixtime date time spam spaml attach attachl bounce bouncel manual manuall
    do
      echo "<tr><td>$date $time</td><td>$spam</td><td>$(((512+$spaml)/1024))kiB</td><td>$attach</td><td>$(((512+$attachl)/1024))kiB</td><td>$bounce</td><td>$(((512+$bouncel)/1024))kiB</td><td>$manual</td><td>$(((512+$manuall)/1024))kiB</td></tr>"
    done
)
echo "</tbody></table>"

echo "<h2>Daily statistics (last week)</h2>"
echo "<table><tbody>"
echo "<tr><th></th><th colspan=\"2\">spams</th><th colspan=\"2\">viruses</th><th colspan=\"2\">bounces</th><th colspan=\"2\">passed</th></tr>"
echo "<tr><th>Time (UTC)</th><th>#</th><th>size</th><th>#</th><th>size</th><th>#</th><th>size</th><th>#</th><th>size</th></tr>"
tail -n 168 ~/spam.log |
(
    spams=0
    spamlen=0
    attachs=0
    attachlen=0
    bounces=0
    bounceslen=0
    manuals=0
    manuallen=0
    h=0
    while read unixtime date time spam spaml attach attachl bounce bouncel manual manuall
    do
      let spams+=spam
      let spamlen+=spaml
      let attachs+=attach
      let attachlen+=attachl
      let bounces+=bounce
      let bounceslen+=bouncel
      let manuals+=manual
      let manuallen+=manuall
      let h++
      if test $h = 24
      then
	  echo "<tr><td>$date $time</td><td>$spams</td><td>$(( (524288+$spamlen)/1048576))MiB</td><td>$attachs</td><td>$(( (524288+$attachlen)/1048576))MiB</td><td>$bounces</td><td>$(( (524288+$bounceslen)/1048576))MiB</td><td>$manuals</td><td>$(( (524288+$manuallen)/1048576))MiB</td></tr>"
	  spams=0
	  spamlen=0
	  attachs=0
	  attachlen=0
	  bounces=0
	  bounceslen=0
	  manuals=0
	  manuallen=0
	  h=0
      fi
    done
)
echo "</tbody></table>"

cat <<EOF

<p>Note: Viruses are catched by attachment file name suffix rules, spams
are catched by SpamAssassin, false bounces are catched by manual set of
rules, mails with spam level &#8805;9 are blocked before delivery and
are not included in this statistics.</p>

<p>Generated: <i>$(date "+%Y-%m-%d %H:%M:%S %Z")</i></p>

<p style="color: red">Summary: Once in the past, e-mail was the
communication media. Now it is playground of spamers and virus owners.
Even in late Saturday night, I need to wait only three minutes for next
spam. E-mail is dead.</p>

<p>Source code: <a href="spamcounter/spam_statistics.sh">spam_statistics.sh</a> (started hourly), <a href="spamcounter/spam_statistics_gen.sh">spam_statistics_gen.sh</a>.</p>

<hr />
<div class="center">
<p class="center">Notes and questions send to: <a href="mailto:utx@penguin.cz">Stanislav Brabec</a></p>
<p class="center"><a href=".">Main page of Penguin's UTX
WWW</a></p>

<p class="center"><a href="http://www.penguin.cz/"><img src="penguin.gif" alt="Penguin server" height="31" width="88" /></a>
<a href="http://validator.w3.org/check/referer"><img
          src="http://www.w3.org/Icons/valid-xhtml10"
          alt="Valid XHTML 1.0!" height="31" width="88" /></a>
<a href="http://jigsaw.w3.org/css-validator/check/referer"><img style="width:88px;height:31px"
       src="http://jigsaw.w3.org/css-validator/images/vcss" 
       alt="Valid CSS!" /></a></p>
</div>
</body>
</html>
EOF

