UserPreferences

UselessCode/IsSpam


引数で与えたファイルがSPAMルールにひっかかればエラーコード1、そうでなければ0を返す。procmail用に作った。

isspam.py

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
#! /usr/bin/env python
import sys
import rfc822
import re

debug = 0
sys.path.append('/home/kankun/.isspam/')
import badmail

def isspam(file):

    mess = rfc822.Message(file)

    for header in badmail.headers:
        if mess.getheader(header.name):
            if debug: print header.name,':',mess.getheader(header.name)
            for p in header.patterns:
                m = re.compile(p)
                if m.search(""):
                    continue
                if m.search(mess.getheader(header.name)):
                     return 1
    return 0

if __name__ == '__main__':
    if len(sys.argv) >= 2:
        for file in sys.argv[1:]:
            if isspam(open(file)):
                print file
    else:
        if isspam(sys.stdin):
            sys.exit(1)
        else:
            sys.exit(0)

~/.isspam/badmail.py

  1 
  2 
  3 
  4 
  5 
  6 
  7 
  8 
  9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
#! /usr/bin/env python
import string

class Header:
    def __init__(self, name, patterns):
        self.name = name
        self.patterns = patterns
#     def __init__(self, name):
#         self.__init__(self, name, [])
    def append(pattern):
        self.patterns.append(pattern)


h_from = Header("from", string.split(
"""
upa_kankunk@yahoo\.co\.jp
822895vacationtime@excite\.com
@21cn\.com
@24horas\.com
@371\.net
@altavista\.se
@bigfoot\.com
@bluemail\.dk
@centifeet\.com
@eagle-mail\.com
@hawk-mail\.com
@hosyou21\.com
@inbox\.ru
@kilomile\.com
@kimo\.com\.tw
@kite-mail\.com
@meteryard\.com
@millinch\.com
@sparrow-mail\.com
@yahoo\.com\.au
bettingforprofit@ip3\.com
hansford@barrilnet\.com\.br
kankyokannri@swbell\.net
love-club@fne\.freeserve\.ne\.jp
a\.jarvis@math\.canterbury\.ac\.nz
jets@i-mail.com.au
Goldin2002a\d+@yahoo.com
(?i)@sixer\.com
@lime-jp.com
\d+@excite.fr
art1@smallworld.co.jp
@pavc.ne.jp
@5chome.com
""" ))

h_xmailer = Header(
    "X-Mailer",
    [
    "IM2000",
    "IM2001",
    "MailWorkZ"
    "RapidShot",
    "Oshirase-Mailer",
    "Achi-Kochi Mail",
    "Direct Email",
    "DM Mailer",
    "MultiSender",
    "dmqfrkpuqm",
    "melma.com",
    "CSMTPConnection",
    "Douhou@Mail",
    ])

h_subject = Header(
    "subject",
    [
    "!\*9-9p!\*",
    "GET MORE EXPOSURE ON THE NET W/MASS E-MAIL",
    "\d+[-A-Za-z !?$]{20,}\d\d\d\d+",
    "^[-A-Z !?$]{40,}(\d{5,})?$",
    "Boost Your Windows Reliability",
    "Better cell phone reception",
    ])

h_to = Header(
    "to",
    [
    "Undisclosed[ .-]Recipients@osa.att.ne.jp",
    ])

headers = [h_from, h_subject, h_xmailer, h_to]