md = USERHOME+"/Mail/"


def tell():
    "announce just arrived mail if I am logged in"
    "of course, xtell is installed on the computer"
    a=os.popen("xtell "+USERNAME+" >/dev/null 2>&1", "w")
    a.write("Mail from %s\n" % (ADDR_FROM[0] or ADDR_FROM[1]))
    a.write("Subject: %s\n" % SUBJECT)
    a.close()



if "bablo@view.net.au" in ADDRLIST_TO+ADDRLIST_CC:
    Set(MailBox(md+"bablo"))
    Stop()


if InHeader("X-Mailing-list", "debian"):
    listaddr = mailmsg.getaddr("X-Mailing-list")[1]
    sublist = string.split(listaddr, '@')[0]
    Set(
        Forward("krivanek@dmpc.dbp.fmph.uniba.sk"),
        MailBox(md+sublist)
        )
    Stop()


if InHeader("X-list", "freeciv-dev"):
    Set(MailBox(md+"freeciv-dev"))
    Stop()

if InHeader("X-list", "freeciv"):
    Set(MailBox(md+"freeciv"))
    Stop()

if InHeader("Sender", "owner-atlas"):
    Set(MailBox(md+"atlas"))
    Stop()

if ADDR_FROM[1] == "kjf@center.fmph.uniba.sk":
    Set(MailBox(md+"kjf"))
    tell()
    Stop()


# simple spam test

if not Contains(SUBJECT, "it was no spam"):

    spamvalue = 0
    if not (Contains(TO, USERNAME) or Contains(CC, USERNAME)):
        spamvalue = spamvalue+10

    if mailmsg.getheader('To') == None:
        spamvalue = spamvalue+5
    
    for spamword in ["FREE", "BUSINESS", "WIN", "RICH"]:
        s = 2*Contains(BODY, spamword, case = 0) # two points for each word
        if s>10: s=10                            # maximum of 10 points
        spamvalue = spamvalue+s
        s = 2*Contains(BODY, spamword, case = 1) # plus additional 2 point (total 4)
        if s>20: s=20                            # if the word is in CAPITAL LETTERS
        spamvalue = spamvalue+s

    if Contains(BODY, "1-800"):
        spamvalue = spamvalue+10

    if Contains(SUBJECT, "ADV?:", rex=1):
        spamvalue = spamvalue+15


    if spamvalue >= 15:
        # note that we fake our username, so that spammer will
        # not get confirmation that this address is really valid
        Reply(sender=USERNAME+".nospam",
              subject="Possible spam (Re: %s)" % SUBJECT,
              text=
"""
Hi
I am sorry, but you have just hit my anti-spam filter.
Your mail was classified as spam, with spam value %i, while 15 is
a limit for non-spam mails.
This means your mail was moved to a very infrequently read mailbox.
If it is really was no spam, send me a mail again (do not reply 
to this mail, use the original address), and place words "it was no spam"
somewhere in the Subject: line.
Thank you and sorry for the inconvenience.

""" % spamvalue
              )

        Set(MailBox(md+"spam"))
        Stop()

tell()

