HOW TO: Word Filter - V.3.1 - Filter and replace banned words before delivery

This section contains scripts that hMailServer has contributed with. hMailServer 5 is needed to use these.
Post Reply
User avatar
jimimaseye
Moderator
Moderator
Posts: 8116
Joined: 2011-09-08 17:48

HOW TO: Word Filter - V.3.1 - Filter and replace banned words before delivery

Post by jimimaseye » 2016-02-24 13:38

v3.1

(Original based on Kids SafeGuard script here: https://www.hmailserver.com/forum/viewt ... =14&t=4317 but with extra resilience and effective operation eliminating several flaws.)

This script will search all emails prior to delivery for words that you deem to be blacklisted and replaced accordingly. The words are maintained in a CSV file (to which you point the script to) It fully appreciates and ignores any leading and trailing characters, along with placement within the live (being the first word, last word or anything in between), allowing for full find/replace of your search word. It will search and replace your words in the banned list irrespective of any preceding or superseding punctuation (eliminating the flaw highlighted in the original script).

Note of warning:
The script works on INCOMING messages which is distinguished by having a non-authenticated connection from the sending server (to HMS) for email delivery (as if an external email server was passing an email INBOUND for delivery to a local account). However, if you use a local email client without authentication to send out emails, this email will also be subject to sanitising before being sent out for delivery.

USE

1, Create a CSV file called "filter.csv", and enter the full path/location in the script under the "Location=" variable.

"Filter.csv" file format should be:

BADWORD,REPLACEWITH,STRICT(1,0)

For Example:

Code: Select all

Bugger, B*g**r,1
Bloody, Blooming,1
shit,poo,0
BADWORD
This is the foul word you want to disallow/mask

REPLACEWITH
This is the clean word/pattern you want the foul word to be shown as

STRICT
STRICT is either ON (1) or OFF (0) on a per word basis, it works as follows:

eg, If you have "SHIT" in your csv and it changes to "****" and STRICT is off ("0") then "BULLSHIT" would be also be caught and changed to "BULL****". If you have STRICT on ("1") then the script would be restricted to "SHIT" and BULLSHIT would not be caught or changed.


2, Copy the following script in to your EVENTHANDLERS.VBS script (following normal hmailserver 'script enabling procedure').

Code: Select all

Sub OnAcceptMessage(oClient, oMessage)
   If (oClient.Username = "") Then
      Dim Location
      Location = "C:\Program Files (x86)\hMailServer\Events\Filter.csv"

      Dim objFSO
      Dim objTextFile
      Dim strWords,i
      Dim restrict

      Set objFSO = CreateObject("Scripting.FileSystemObject")
      Set objTextFile = objFSO.OpenTextFile(Location, 1)

      Dim objRegEx
      Set objRegEx = CreateObject("VBScript.RegExp")

      objRegEx.Global = True   
      objRegEx.IgnoreCase = True

      Do While objTextFile.AtEndOfStream <> True
         strWords = split(objTextFile.Readline, ",")
         restrict = strWords(2)
         if restrict = 1 then
            objRegEx.Pattern = "(\s|_|\W|[0-9])" & strWords(0) & "(\s|_|\W|[0-9])"
            oMessage.Body = objRegEx.Replace(oMessage.Body, "$1" & strWords(1) & "$2" )
            oMessage.HTMLBody = objRegEx.Replace(oMessage.HTMLBody, "$1" & strWords(1) & "$2" )
         else
            oMessage.Body = Replace(oMessage.Body,strWords(0),strWords(1),1,-1,1)
            oMessage.HTMLBody = Replace(oMessage.HTMLBody,strWords(0),strWords(1),1,-1,1)
         end if
      Loop
      oMessage.Save()
   End If
End Sub

Comments welcome.
HMS 5.6.6 B2383 on Win Server 2008 R2 Foundation, + 5.6.7-B2415 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

User avatar
jimimaseye
Moderator
Moderator
Posts: 8116
Joined: 2011-09-08 17:48

HOW TO: Word Filter - V.3.1 - Filter and replace banned words before delivery

Post by jimimaseye » 2016-02-25 00:14

v3.1 Modified to to run only on inbound email/non-authenticated client only (to stop sanitising OUTBOUND email to external users)
HMS 5.6.6 B2383 on Win Server 2008 R2 Foundation, + 5.6.7-B2415 on test.
SpamassassinForWindows 3.4.0 spamd service
AV: Clamwin + Clamd service + sanesecurity defs : https://www.hmailserver.com/forum/viewtopic.php?f=21&t=26829

Post Reply