changeset 3159:915a431b663c

logging: security package refactored, moved frozenset to python_compatibility
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sat, 01 Mar 2008 22:24:51 +0100
parents 3ea8d2e4af32
children eeb27d88d39b
files MoinMoin/security/antispam.py MoinMoin/security/textcha.py MoinMoin/support/python_compatibility.py
diffstat 3 files changed, 25 insertions(+), 39 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/security/antispam.py	Sat Mar 01 21:39:29 2008 +0100
+++ b/MoinMoin/security/antispam.py	Sat Mar 01 22:24:51 2008 +0100
@@ -2,24 +2,16 @@
 """
     This implements a global (and a local) blacklist against wiki spammers.
 
-    @copyright: 2005-2007 MoinMoin:ThomasWaldmann
+    @copyright: 2005-2008 MoinMoin:ThomasWaldmann
     @license: GNU GPL, see COPYING for details
 """
 
-# give some log entries to stderr
-debug = 0
-
 import re, time, datetime
 
-# needed for py 2.3 compat:
-try:
-    frozenset
-except NameError:
-    from sets import ImmutableSet as frozenset
-
 from MoinMoin import log
 logging = log.getLogger(__name__)
 
+from MoinMoin.support.python_compatibility import frozenset
 from MoinMoin.security import Permissions
 from MoinMoin import caching, wikiutil
 
@@ -46,13 +38,6 @@
 
 # Functions ------------------------------------------------------------
 
-def dprint(s):
-    if debug:
-        if isinstance(s, unicode):
-            s = s.encode('utf-8')
-        logging.debug('antispam: %s' % s)
-
-
 def makelist(text):
     """ Split text into lines, strip them, skip # comments """
     lines = text.splitlines()
@@ -81,7 +66,7 @@
         failure = caching.CacheEntry(request, "antispam", "failure", scope='wiki')
         fail_time = failure.mtime() # only update if no failure in last hour
         if (mymtime < tooold) and (fail_time < tooold):
-            dprint("%d *BadContent too old, have to check for an update..." % tooold)
+            logging.info("%d *BadContent too old, have to check for an update..." % tooold)
             import xmlrpclib
             import socket
 
@@ -89,8 +74,8 @@
             old_timeout = socket.getdefaulttimeout()
             socket.setdefaulttimeout(timeout)
 
-            master = xmlrpclib.ServerProxy(request.cfg.antispam_master_url)
-
+            master_url = request.cfg.antispam_master_url
+            master = xmlrpclib.ServerProxy(master_url)
             try:
                 # Get BadContent info
                 master.putClientInfo('ANTISPAM-CHECK',
@@ -112,10 +97,10 @@
                     # for python <= 2.4.x
                     mydate = xmlrpclib.DateTime(tuple(time.gmtime(mymtime)))
 
-                dprint("master: %s mine: %s" % (masterdate, mydate))
+                logging.debug("master: %s mine: %s" % (masterdate, mydate))
                 if mydate < masterdate:
                     # Get new copy and save
-                    dprint("Fetching page from master...")
+                    logging.info("Fetching page from %s..." % master_url)
                     master.putClientInfo('ANTISPAM-FETCH', request.http_host + request.script_name)
                     response = master.getPage(pagename)
                     if isinstance(response, dict) and 'faultCode' in response:
@@ -128,21 +113,18 @@
                                        # is no updated master page
 
             except (socket.error, xmlrpclib.ProtocolError), err:
-                # Log the error
-                dprint('Timeout / socket / protocol error when accessing'
-                       ' moinmaster: %s' % str(err))
+                logging.error('Timeout / socket / protocol error when accessing %s: %s' % (master_url, str(err)))
                 # update cache to wait before the next try
                 failure.update("")
 
             except (xmlrpclib.Fault, ), err:
-                # Log the error
-                dprint('Fault on moinmaster: %s' % str(err))
+                logging.error('Fault on %s: %s' % (master_url, str(err)))
                 # update cache to wait before the next try
                 failure.update("")
 
             except Error, err:
                 # In case of Error, we log the error and use the local BadContent copy.
-                dprint(str(err))
+                logging.error(str(err))
 
             # set back socket timeout
             socket.setdefaulttimeout(old_timeout)
@@ -179,7 +161,10 @@
                         try:
                             mmblcache.append(re.compile(blacklist_re, re.I))
                         except re.error, err:
-                            dprint("Error in regex '%s': %s. Please check the pages %s." % (blacklist_re, str(err), ', '.join(BLACKLISTPAGES)))
+                            logging.error("Error in regex '%s': %s. Please check the pages %s." % (
+                                          blacklist_re,
+                                          str(err),
+                                          ', '.join(BLACKLISTPAGES)))
                     request.cfg.cache.antispam_blacklist = (latest_mtime, mmblcache)
 
                 from MoinMoin.Page import Page
@@ -197,16 +182,15 @@
                 for blacklist_re in request.cfg.cache.antispam_blacklist[1]:
                     match = blacklist_re.search(addedtext)
                     if match:
-                        # Log error and raise SaveError, PageEditor
-                        # should handle this.
+                        # Log error and raise SaveError, PageEditor should handle this.
                         _ = editor.request.getText
-                        msg = _('Sorry, can not save page because "%(content)s"'
-                                ' is not allowed in this wiki.') % {
-                            'content': match.group()
-                            }
-                        dprint(msg)
+                        msg = _('Sorry, can not save page because "%(content)s" is not allowed in this wiki.') % {
+                                'content': match.group()
+                              }
+                        logging.info(msg)
                         raise editor.SaveError(msg)
             request.clock.stop('antispam')
 
         # No problem to save if my base class agree
         return Permissions.save(self, editor, newtext, rev, **kw)
+
--- a/MoinMoin/security/textcha.py	Sat Mar 01 21:39:29 2008 +0100
+++ b/MoinMoin/security/textcha.py	Sat Mar 01 22:24:51 2008 +0100
@@ -54,10 +54,10 @@
         textchas = cfg.textchas
         if textchas:
             lang = user.language or request.lang
-            #logging.debug(u"TextCha: user.language == '%s'." % lang)
+            logging.debug(u"TextCha: user.language == '%s'." % lang)
             if lang not in textchas:
                 lang = cfg.language_default
-                #logging.debug(u"TextCha: fallback to language_default == '%s'." % lang)
+                logging.debug(u"TextCha: fallback to language_default == '%s'." % lang)
                 if lang not in textchas:
                     logging.error(u"TextCha: The textchas do not have content for language_default == '%s'! Falling back to English." % lang)
                     lang = 'en'
@@ -70,7 +70,7 @@
         if lang is None:
             return None
         else:
-            #logging.debug(u"TextCha: using lang = '%s'" % lang)
+            logging.debug(u"TextCha: using lang = '%s'" % lang)
             return textchas[lang]
 
     def _init_qa(self, question=None):
--- a/MoinMoin/support/python_compatibility.py	Sat Mar 01 21:39:29 2008 +0100
+++ b/MoinMoin/support/python_compatibility.py	Sat Mar 01 22:24:51 2008 +0100
@@ -53,8 +53,10 @@
 """
 try:
     set = set
+    frozenset = frozenset
 except NameError:
     from sets import Set as set
+    from sets import ImmutableSet as frozenset
 
 """
 This is a feature from python 2.5, needed for compatibility with python 2.3 and 2.4.