Skip to content

jensbrak/SpamDetector

Repository files navigation

SpamDetector

Server Status
GitHub Actions .NET Linux
GitHub Actions .NET Win
NuGet .github/workflows/nuget.yml

Description

A Piranha CMS module providing spam detection for page and post comments.

About

Project page: zon3.se/spamdetector
Source code: github.com/jensbrak/SpamDetector
NuGet package: nuget.org/packages/Zon3.SpamDetector/

Piranha CMS provides a feature to allow visitors to comment posts and pages. Out of the box, there is no mechanism to prevent spamming the site with unsolicited comments. This module adds automatic spam detection functionality for submitted comments. It does so by using the free to use Akismet service for spam detection. Once configured, the module will intercept all comments before they are published. If considered spam, the comment is marked as not approved, effectively stopping it from being published. If not considered spam, the comment is marked as approved and is published.

Note

While providing working antispam functionality, the purpose of this module was primarly to explore and learn about Piranha. The documentation and samples are helpful when writing a module with one exception: Manager support. I wanted to see what it took to make a module with full Manager support, including persistance, localization (while keeping the look and feel of the internal Piranha modules). In other words: You might have use for this module even if antispam/comments are not of interest - if you just want to understand how Manager support can be added to a custom Piranha module. There's no claim that this is the right way to do it, however.

Dependencies

  • .NET 8
  • Zon3.SpamDetector.Localization
  • Microsoft.Extensions.Http version 8
  • Microsoft.Extensions.Localization version 8
  • Piranha version 11
  • Piranha.Manager version 11
  • Also: An Akismet API key

Demo

Please go ahead and try it out by posting a comment with a greeting on my blog. It runs PiranhaCMS with this module active. If your comment is directly visible it's been classified by akismet as non-spam and approved by the SpamDetector module as a valid comment. If not, stop spamming! (Or report a bug to me ;) )

My PiranhaCMS demo site: zon3.se

Installation

See Instructions below and/or the example Startup.cs (for Piranha prior version 10.0.3) or Program.cs (for Piranha version 10.0.3 and up). The files are from the piranha.razor template with relevant code added.

Code adjustments in your Piranha project

  1. Get and add the SpamDetector module to your Piranha project, either by source or package:
    1. Using source code: Downloading the source code and add a project reference to Zon3.SpamDetector.csproj + Zon3.SpamDetector.localization.csproj OR
    2. Using NuGet package manager: Add the SpamDetector and SpamDetector.Localization packages as a NuGet dependencies
  2. Add a reference to Zon3.SpamDetector in your Piranha project startup file (Program.cs for Piranha version 10.0.3 and up, Startup.cs otherwise)
  3. In startup file add code to register SpamDetector service and middleware and attach it to the proper hook:
    1. Register SpamDetector service
    2. Register SpamDetector middleware
    3. Register a Comment validation hook and call SpamDetector.ReviewAsync(Comment c) to get validation result.
    4. Make sure the hook use the validation to set the comment status (IsApproved)

Settings

Once installed, the module is accessed and configured via Piranha Manager, under Settings in the menu. Values that are mandatory are marked with (Required). Without these properly set, the module won't work.

Module settings

These settings control the module:

  • (Required) Module enabled : use this to turn the module on or off.
  • (Required) Test mode enabled: use this while setting up and testing the module. This will advice Akismet that comments sent are for testing purposes only. Don't forget to turn off once the module is setup and testing is done.
  • (Required) API URL: the personal, site-specific API key to use to make calls to Akismet (see akismet.com/development/api).

Site settings

These settings are sent to Akismet along with the comments to help review the content submitted:

  • (Required) URL: The full URL of the frontpage of the blog/site using the SpamDetector module.
  • Language: The language(s) used by the site (ie the expected languages of the comments submitted). A comma separated list of ISO 639-1 formatted for all languages used by the site.
  • Encoding: The character encoding for the form values of submitted comments ('Your name', 'your email address', 'An (optional) URL to your website' and 'Your awesome comment').
  • User role: The user role of the user who submitted the comment (if any).

Localization

The module itself supports localization, it works similar to how Piranha is localized. Note: support for localization is not 100%, see source for more info.

Issues and feedback

Found an issue with the module? Feedback or ideas about it?
Visit the GitHub page of the module and submit an issue.

Issues: github.com/jensbrak/SpamDetector/issues

Roadmap / Shortcomings

Things I'd like to do with the module to make it more complete/useful/interesting:

  1. Support reporting spam/ham in Akismet. I have ideas but there are some challenges to do it tidy yet useful (see note below).
  2. Make localization supported 100%. I over complicated some aspects of it and did not take it all the way.
  3. Add support for a second anti spam service. Would be interesting and also force me improve existing code.
  4. Known issue: my demo site shows quite a number of comments approved that shouldn't be approved. Not sure if it's my use of Akismet that is lacking or if it's the ever evolving spam landscape that causes it. So yes, I am aware but have not looked into it. Suggestions are welcome!

Note: the comment feature that Piranha provides is rather crude, by design. Managing comments, for instance, is limited to approving or deleting them (one by one!). Adopting SpamDetector module to support spam/ham reporting should be possible using existing comment functionality and quite limited code changes (something like: report ham if this module has set a comment to not approved if the user then explicitly approves it. Similarly for comments that has been been approved incorrectly - report it as missed spam if user explicitly disapproves it. Neat to use existing interface, but I am also a bit hesitant to include this "implicit behaviour" in the existing approve/disapprove functionality) On the other hand, it's very tempting to extend the manager interface to be more useful when managing comments while at it. Separate buttons to do the spam/ham reporting, bulk edits etc etc. That would quickly go beyond the scope and ambitions of the SpamDetector module. It would be interesting to do, but I feel it should be a separate project. Ideas?

Further reading

Selected links relevant to this module: