Skip to content

Commit ce1ea08

Browse files
committed
use global spam blacklist at zwiki.org
1 parent 6a3c899 commit ce1ea08

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

Defaults.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,5 @@ def registerPageMetaData(t):
107107
'path',
108108
]
109109

110+
ZWIKI_SPAMPATTERNS_URL = 'http://zwiki.org/spampatterns.txt'
111+
ZWIKI_SPAMPATTERNS_TIMEOUT = 1 # s

Editing.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
from email.Message import Message
1212
from copy import deepcopy
1313
import os.path
14+
import socket
15+
import urllib2
1416

1517
import ZODB # need this for pychecker
1618
from AccessControl import getSecurityManager, ClassSecurityInfo, Unauthorized
@@ -31,7 +33,8 @@
3133
import OFS.Image
3234

3335
from plugins.pagetypes import PAGETYPES
34-
from Defaults import DISABLE_JAVASCRIPT, LARGE_FILE_SIZE, LEAVE_PLACEHOLDER
36+
from Defaults import DISABLE_JAVASCRIPT, LARGE_FILE_SIZE, LEAVE_PLACEHOLDER, \
37+
ZWIKI_SPAMPATTERNS_URL, ZWIKI_SPAMPATTERNS_TIMEOUT
3538
import Permissions
3639
from Regexps import javascriptexpr, htmlheaderexpr, htmlfooterexpr
3740
from Utils import get_transaction, BLATHER, INFO, parseHeadersBody, isunicode, \
@@ -766,16 +769,43 @@ def checkForSpam(self, t=''):
766769
username = self.usernameFrom(REQUEST,ip_address=0)
767770
path = self.getPath()
768771
def forbid(reason):
769-
INFO('%s blocked edit from %s (%s), %s' % (path, ip, username, reason))
770-
BLATHER('blocked content:\n%s' % t)
772+
BLATHER('%s blocked edit from %s (%s), %s:\n%s' % (path, ip, username, reason, t))
771773
raise Forbidden, "There was a problem, please contact the site admin."
772774

773775
# content matches a banned pattern ?
774-
pats = getattr(self.folder(),'spampatterns',[])
775-
for pat in pats:
776+
pats = self
777+
for pat in self.getSpamPatterns():
776778
pat = strip(pat)
777779
if pat and re.search(pat,t): forbid("spam pattern found")
778780

781+
def getSpamPatterns(self):
782+
"""Fetch spam patterns from the global zwiki spam blacklist, or
783+
a local property.
784+
"""
785+
if safe_hasattr(self.folder(), 'spampatterns'):
786+
return list(getattr(self.folder(),'spampatterns',[]))
787+
else:
788+
BLATHER('checking zwiki.org spam blacklist')
789+
req = urllib2.Request(
790+
ZWIKI_SPAMPATTERNS_URL,
791+
None,
792+
{'User-Agent':'Zwiki %s' % self.zwiki_version()}
793+
)
794+
# have to set timeout this way for python 2.4. XXX safe ?
795+
saved = socket.getdefaulttimeout()
796+
socket.setdefaulttimeout(ZWIKI_SPAMPATTERNS_TIMEOUT)
797+
try:
798+
response = urllib2.urlopen(req)
799+
#hdrs = response.info()
800+
t = response.read()
801+
except urllib2.URLError, e:
802+
BLATHER('failed to read blacklist, skipping (%s)' % e)
803+
t = ''
804+
finally:
805+
socket.setdefaulttimeout(saved)
806+
pats = t.split('\n')
807+
return pats
808+
779809
def cleanupText(self, t):
780810
"""Clean up incoming text and convert to unicode for internal use."""
781811
def stripcr(t): return re.sub('\r\n','\n',t)

0 commit comments

Comments
 (0)