Word Search

This section contains scripts that hMailServer has contributed with. hMailServer 5 is needed to use these.
Post Reply
bcpaul
Normal user
Normal user
Posts: 31
Joined: 2014-07-17 23:32

Word Search

Post by bcpaul » 2016-01-26 00:49

Based on
Word Filter - V.2.5 by Kids SafeGuard
Forum Link: viewtopic.php?f=14&t=4317

This script will search all incoming/outgoing emails for flagged words from a text file. The text file is located in the same folder as the script - C:\Program Files (x86)\hMailServer\Events\filter.csv
The script below is inserted into the events.vbs file: Find the Sub OnDeliverMessage(oMessage) in C:\Program Files (x86)\hMailServer\Events\EventHandlers.vbs and replace it with the code below
If the word is found, a copy is sent to another email account (just for this use) with the found word, from, to in the subject (all recipients). Maybe create an account called found@yourdomain.com. Just don't use it to send emails from.

NOTE: csv file format is as follows:
word or phrase to search,1
OR
word or phrase to search,0
However, the number is ignored--for now :)
The word searched for in the email MUST have a space on either side of the word, or it will not trigger a find.

This version works with hmailserver 5.6.4 on Windows using IIS

Code: Select all

Sub OnDeliverMessage(oMessage)
	
	Dim Location
	Location = "C:\Program Files (x86)\hMailServer\Events\filter.csv"

	Dim objFSO
	Dim objTextFile
	Dim strWords,i
	Dim restrict

	dim nMessage
	Set nMessage = CreateObject("hMailServer.Message")
			
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	Set objTextFile = objFSO.OpenTextFile(Location, 1)

	Do While objTextFile.AtEndOfStream <> True
	
		strWords = split(objTextFile.Readline, ",")
		restrict = strWords(1)
		strBadWord = " " & strWords(0) & " "
		'strBadWord = strWords(0)
		
		'EventLog.Write("TESTING for" & content)  ' LOG IN hmailserver/
		
		' *** IMPORTANT !!! make sure the FromAddress is not and cannot be used to SEND any emails or hmailserver will be stuck in infinite loop of sending emails
		if InStr(1,oMessage.HTMLBody,strBadWord, 1) > 0 and oMessage.FromAddress <> "found@yourdomain.com" then
			
			'-- Get hMailServer to output the *real* mailboxes it detected. --'
			Dim LoopCount
			Dim Recipients
			For LoopCount = 0 To oMessage.Recipients.Count-1
			Recipients = Recipients & " TO: " & oMessage.Recipients(LoopCount).Address '& " (To: " & oMessage.Recipients(LoopCount).OriginalAddress & ")"
			Next
			
			'Message.HTMLBody + "<br>"
			nMessage.HTMLBody = oMessage.HTMLBody
			
			nMessage.From = "Email Scanner"
			nMessage.FromAddress = "no-reply@yourdomain.com"
			nMessage.Subject = "Flagged Word:" &strBadWord & " Sent from:" & oMessage.FromAddress & Recipients
			nMessage.ClearRecipients()
			nMessage.AddRecipient "Name", "found@yourdomain.com"  ' address of emails that have matched words get sent to
			nMessage.Save()
			Exit Do
		
		End If
	
	Loop
	oMessage.Save()
	
	
End Sub


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

Re: Word Search

Post by jimimaseye » 2016-01-26 13:03

bcpaul wrote:The word searched for in the email MUST have a space on either side of the word, or it will not trigger a find.
So if my blocked word is "shit" and email comes in with "This is shit!", then it wont get caught?!

A fundamental flaw I think. As most words are nouns they are VERY likely to be at the end of sentences and therefore punctuated without a trailing space.

Out of interest, what does your script offer that the original script doesnt?

EDIT: see my proposed changes to the original script to tackle the flaw : viewtopic.php?p=182419#p182419
5.7 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

bcpaul
Normal user
Normal user
Posts: 31
Joined: 2014-07-17 23:32

Re: Word Search

Post by bcpaul » 2016-01-28 01:01

jimimaseye wrote:
bcpaul wrote:The word searched for in the email MUST have a space on either side of the word, or it will not trigger a find.
So if my blocked word is "shit" and email comes in with "This is shit!", then it wont get caught?!

A fundamental flaw I think. As most words are nouns they are VERY likely to be at the end of sentences and therefore punctuated without a trailing space.

Out of interest, what does your script offer that the original script doesnt?

EDIT: see my proposed changes to the original script to tackle the flaw : viewtopic.php?p=182419#p182419
lol, yes flawed. Not at all familiar with VB. Thanks. Would be nice to be able to edit my own post....

User avatar
mattg
Moderator
Moderator
Posts: 20971
Joined: 2007-06-14 05:12
Location: 'The Outback' Australia

Re: Word Search

Post by mattg » 2016-01-28 02:46

bcpaul wrote: Would be nice to be able to edit my own post....
Can only do within 30 minutes, if no replies have been made I believe

This is done to stop complete re-writes of posts after they have been answered...thus making the answers provided not relate to the new question.
Just 'cause I link to a page and say little else doesn't mean I am not being nice.
https://www.hmailserver.com/documentation

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

Re: Word Search

Post by jimimaseye » 2016-01-28 10:46

bcpaul wrote:
jimimaseye wrote:
bcpaul wrote:The word searched for in the email MUST have a space on either side of the word, or it will not trigger a find.
So if my blocked word is "shit" and email comes in with "This is shit!", then it wont get caught?!

A fundamental flaw I think. As most words are nouns they are VERY likely to be at the end of sentences and therefore punctuated without a trailing space.

Out of interest, what does your script offer that the original script doesnt?

EDIT: see my proposed changes to the original script to tackle the flaw : viewtopic.php?p=182419#p182419
lol, yes flawed. Not at all familiar with VB. Thanks. Would be nice to be able to edit my own post....
If you want to repost an updated solution then just do another post to the thread in completeness.

Also, I note another problem in your script: you only check HTMLbody. That assume emails being sent in to you have HTMLbodys. What if the incoming message is in plain text format only? (answer: it wont get caught.)
5.7 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