blacklist.py :  » Blog » Frog » FrogComplete-1.8 » webapps » frog » frog » antispam » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Blog » Frog 
Frog » FrogComplete 1.8 » webapps » frog » frog » antispam » blacklist.py
#
#   Blacklist anti-spam.
#   The blacklist is assumed to be the MovableType blacklist.
#   It used to do auto-updating, but the maintainer has removed the
#   master blacklist website. So only a static file is cached.
#

import urllib2
import os, sre

import logging
log=logging.getLogger("Snakelets.logger")


CACHEFILE = "blacklist.txt"


class MTBlackList:
    def __init__(self, storageDir):
        self.cachefile=os.path.join(storageDir, CACHEFILE)
        log.debug("blacklist cachefile="+self.cachefile)
        self.blacklist=[]
        self.updated_file=None
        self.last_mtime=0
        self.__parselist()

    def __str__(self):
        if self.blacklist:
            return "Blacklist, timestamp=%s, %d entries" % (self.updated_file, len(self.blacklist))
        else:
            return "Blacklist NOT FOUND OR EMPTY, NO CHECKING DONE!"

    # update the spamchecker (in this case: the blacklist file)
    def update(self):
        log.info("updating blacklist from file")
        self.__parselist()

    # check the text for spam, return None if okay, errormessage if spam.
    def check(self, text):
        if text:
            for rx in self.blacklist:
                if rx.search(text):
                    return "blacklisted url found"
        return None

    def __parselist(self):
        if not os.path.isfile(self.cachefile):
            msg="*** ANTISPAM BLACKLIST NOT FOUND; ANTISPAM DISABLED ***"
            log.warn(msg)
            print msg
            return
        mtime=os.path.getmtime(self.cachefile)
        if mtime==self.last_mtime:
            log.info("not a new version, don't re-read")
            return
        else:
            self.last_mtime=mtime
            
        data=open(self.cachefile,"r").read()
        match=sre.search(r"^#\s+Last update:\s+(.+)\s*$",data,sre.IGNORECASE | sre.MULTILINE)
        if match:
            list_updated=match.group(1).strip()
            self.updated_file=list_updated
        else:
            self.updated_file=None
        # strip the comments
        self.blacklist=[]
        for line in data.split('\n'):
            line=line.strip()
            if line and not line.startswith('#'):
                match=sre.match(r"(.+)\s+#.+$", line)

                if match:
                    line=match.group(1).strip()
                self.blacklist.append(line)
        self.blacklist = [sre.compile(rx, sre.IGNORECASE) for rx in self.blacklist]
        if self.blacklist:
            log.debug("using blacklist @ %s (%d entries)" % (self.updated_file,len(self.blacklist) ) )
        else:
            msg="*** ANTISPAM BLACKLIST CONTAINS NO ENTRIES; ANTISPAM DISABLED ***"
            log.warn(msg)
            print msg


if __name__=="__main__":
    # test code
    m=MTBlackList("/tmp")
    print "#entries=",len(m.blacklist)
    print "updated",m.updated_file

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.