allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tvansteenbu...@apache.org
Subject [2/3] git commit: [#5384] Add Mollom spam filter
Date Wed, 09 Jan 2013 23:47:29 GMT
[#5384] Add Mollom spam filter


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/63fed81c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/63fed81c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/63fed81c

Branch: refs/heads/tv/5384
Commit: 63fed81ca6244579f312f7ec81c4d15ea57aca7e
Parents: 2ff98c2
Author: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Authored: Wed Jan 9 23:34:07 2013 +0000
Committer: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Committed: Wed Jan 9 23:34:07 2013 +0000

----------------------------------------------------------------------
 Allura/allura/lib/app_globals.py         |    4 +-
 Allura/allura/lib/spam/__init__.py       |   10 ++++-
 Allura/allura/lib/spam/akismet.py        |   48 ++++++++++++++++++++++
 Allura/allura/lib/spam/akismetservice.py |   41 -------------------
 Allura/allura/lib/spam/mollom.py         |   53 +++++++++++++++++++++++++
 Allura/setup.py                          |    3 +-
 6 files changed, 113 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/63fed81c/Allura/allura/lib/app_globals.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/app_globals.py b/Allura/allura/lib/app_globals.py
index b18215d..862a03c 100644
--- a/Allura/allura/lib/app_globals.py
+++ b/Allura/allura/lib/app_globals.py
@@ -169,10 +169,10 @@ class Globals(object):
 
     @LazyProperty
     def spam_checker(self):
-        """Return a SpamProvider implementation.
+        """Return a SpamFilter implementation.
         """
         from allura.lib import spam
-        return spam.SpamProvider.get(config, self.entry_points['spam'])
+        return spam.SpamFilter.get(config, self.entry_points['spam'])
 
     @LazyProperty
     def director(self):

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/63fed81c/Allura/allura/lib/spam/__init__.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/spam/__init__.py b/Allura/allura/lib/spam/__init__.py
index 4e1332f..11b8297 100644
--- a/Allura/allura/lib/spam/__init__.py
+++ b/Allura/allura/lib/spam/__init__.py
@@ -1,15 +1,21 @@
-class SpamProvider(object):
+import logging
+
+log = logging.getLogger(__name__)
+
+
+class SpamFilter(object):
     """Defines the spam checker interface and provides a default no-op impl."""
     def __init__(self, config):
         pass
 
     def check(self, text, artifact=None, user=None, content_type='comment', **kw):
         """Return True if ``text`` is spam, else False."""
+        log.info("No spam checking enabled")
         return False
 
     @classmethod
     def get(cls, config, entry_points):
-        """Return an instance of the SpamProvider impl specified in ``config``.
+        """Return an instance of the SpamFilter impl specified in ``config``.
         """
         method = config.get('spam.method')
         if not method:

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/63fed81c/Allura/allura/lib/spam/akismet.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/spam/akismet.py b/Allura/allura/lib/spam/akismet.py
new file mode 100644
index 0000000..fcb76bd
--- /dev/null
+++ b/Allura/allura/lib/spam/akismet.py
@@ -0,0 +1,48 @@
+import logging
+
+from pylons import request
+from pylons import tmpl_context as c
+
+from allura.lib import helpers as h
+from allura.lib.spam import SpamFilter
+
+import akismet
+
+
+log = logging.getLogger(__name__)
+
+
+class AkismetSpamFilter(SpamFilter):
+    """Spam checking implementation via Akismet service.
+
+    To enable Akismet spam filtering in your Allura instance, include
+    the following parameters in your .ini file::
+
+        spam.method = akismet
+        spam.key = <your Akismet key here>
+    """
+    def __init__(self, config):
+        self.service = akismet.Akismet(config.get('spam.key'), config.get('base_url'))
+        self.service.verify_key()
+
+    def check(self, text, artifact=None, user=None, content_type='comment', **kw):
+        log_msg = text
+        kw['comment_content'] = text
+        kw['comment_type'] = content_type
+        if artifact:
+            kw['permalink'] = artifact.url()
+            log_msg = artifact.url()
+        user = user or c.user
+        if user:
+            kw['comment_author'] = user.display_name or user.username
+            kw['comment_author_email'] = user.email_addresses[0] if user.email_addresses
else ''
+        user_ip = request.headers.get('X_FORWARDED_FOR', request.remote_addr)
+        kw['user_ip'] = user_ip.split(',')[0].strip()
+        kw['user_agent'] = request.headers.get('USER_AGENT')
+        kw['referrer'] = request.headers.get('REFERER')
+        # kw will be urlencoded, need to utf8-encode
+        for k, v in kw.items():
+            kw[k] = h.really_unicode(v).encode('utf8')
+        res = self.service.comment_check(text, data=kw, build_data=False)
+        log.info("spam=%s (akismet): %s" % (str(res), log_msg))
+        return res

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/63fed81c/Allura/allura/lib/spam/akismetservice.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/spam/akismetservice.py b/Allura/allura/lib/spam/akismetservice.py
deleted file mode 100644
index 2a9e08a..0000000
--- a/Allura/allura/lib/spam/akismetservice.py
+++ /dev/null
@@ -1,41 +0,0 @@
-import logging
-
-from pylons import request
-from pylons import tmpl_context as c
-
-from allura.lib import helpers as h
-
-import akismet
-
-from .. import SpamProvider
-
-
-log = logging.getLogger(__name__)
-
-
-class Akismet(SpamProvider):
-    def __init__(self, config):
-        self.service = akismet.Akismet(config.get('spam.key'), config.get('base_url'))
-        self.service.verify_key()
-
-    def check(self, text, artifact=None, user=None, content_type='comment', **kw):
-        log_msg = text
-        kw['comment_content'] = text
-        kw['comment_type'] = content_type
-        if artifact:
-            kw['permalink'] = artifact.url()
-            log_msg = artifact.url()
-        user = user or c.user
-        if user:
-            kw['comment_author'] = user.display_name or user.username
-            kw['comment_author_email'] = user.email_addresses[0] if user.email_addresses
else ''
-        user_ip = request.headers.get('X_FORWARDED_FOR', request.remote_addr)
-        kw['user_ip'] = user_ip.split(',')[0].strip()
-        kw['user_agent'] = request.headers.get('USER_AGENT')
-        kw['referrer'] = request.headers.get('REFERER')
-        # kw will be urlencoded, need to utf8-encode
-        for k, v in kw.items():
-            kw[k] = h.really_unicode(v).encode('utf8')
-        res = self.service.comment_check(text, data=kw, build_data=False)
-        log.info("spam=%s (akismet): %s" % (str(res), log_msg))
-        return res

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/63fed81c/Allura/allura/lib/spam/mollom.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/spam/mollom.py b/Allura/allura/lib/spam/mollom.py
new file mode 100644
index 0000000..f7f1d4f
--- /dev/null
+++ b/Allura/allura/lib/spam/mollom.py
@@ -0,0 +1,53 @@
+import logging
+
+from pylons import request
+from pylons import tmpl_context as c
+
+from allura.lib import helpers as h
+from allura.lib.spam import SpamFilter
+
+import Mollom
+
+log = logging.getLogger(__name__)
+
+
+class MollomSpamFilter(SpamFilter):
+    """Spam checking implementation via Mollom service.
+
+    To enable Mollom spam filtering in your Allura instance, include
+    the following parameters in your .ini file::
+
+        spam.method = mollom
+        spam.public_key = <your Mollom public key here>
+        spam.private_key = <your Mollom private key here>
+    """
+    def __init__(self, config):
+        self.service = Mollom.MollomAPI(
+            publicKey=config.get('spam.public_key'),
+            privateKey=config.get('spam.private_key'))
+        if not self.service.verifyKey():
+            raise Mollom.MollomFault('Your MOLLOM credentials are invalid.')
+
+    def check(self, text, artifact=None, user=None, content_type='comment', **kw):
+        """Basic content spam check via Mollom. For more options
+        see http://mollom.com/api#api-content
+        """
+        log_msg = text
+        kw['postBody'] = text
+        if artifact:
+            # Should be able to send url, but can't right now due to a bug in
+            # the PyMollom lib
+            # kw['url'] = artifact.url()
+            log_msg = artifact.url()
+        user = user or c.user
+        if user:
+            kw['authorName'] = user.display_name or user.username
+            kw['authorMail'] = user.email_addresses[0] if user.email_addresses else ''
+        user_ip = request.headers.get('X_FORWARDED_FOR', request.remote_addr)
+        kw['authorIP'] = user_ip.split(',')[0].strip()
+
+        cc = self.service.checkContent(**kw)
+        res = cc['spam'] == 2
+        log.info("spam=%s (mollom): %s" % (str(res), log_msg))
+        return res
+

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/63fed81c/Allura/setup.py
----------------------------------------------------------------------
diff --git a/Allura/setup.py b/Allura/setup.py
index dbc1e46..dcdeb3b 100644
--- a/Allura/setup.py
+++ b/Allura/setup.py
@@ -105,7 +105,8 @@ setup(
     allura = allura.lib.plugin:ThemeProvider
 
     [allura.spam]
-    akismet = allura.lib.spam.akismetservice:Akismet
+    akismet = allura.lib.spam.akismet:AkismetSpamFilter
+    mollom = allura.lib.spam.mollom:MollomSpamFilter
 
     [paste.paster_command]
     taskd = allura.command.taskd:TaskdCommand


Mime
View raw message