Implement word filter

This commit is contained in:
avail 2016-08-08 05:19:52 +02:00
parent 8497a7c318
commit 73d3073060
4 changed files with 46 additions and 1 deletions

0
filters.txt Normal file
View File

View File

@ -8,6 +8,7 @@ from helpers import userHelper
from events import logoutEvent from events import logoutEvent
from events import channelJoinEvent from events import channelJoinEvent
from constants import messageTemplates from constants import messageTemplates
from helpers import filterHelper
def joinChannel(userID = 0, channel = "", token = None, toIRC = True): def joinChannel(userID = 0, channel = "", token = None, toIRC = True):
""" """
@ -209,6 +210,10 @@ def sendMessage(fro = "", to = "", message = "", token = None, toIRC = True):
# Truncate message if > 2048 characters # Truncate message if > 2048 characters
message = message[:2048]+"..." if len(message) > 2048 else message message = message[:2048]+"..." if len(message) > 2048 else message
# check for word filters
filters = filterHelper.chatFilters()
message = filters.checkFilters(message)
# Build packet bytes # Build packet bytes
packet = serverPackets.sendMessage(username, toClient, message) packet = serverPackets.sendMessage(username, toClient, message)

31
helpers/filterHelper.py Normal file
View File

@ -0,0 +1,31 @@
import os
class chatFilters():
oldWords = () # have to use tuples as a
newWords = () # dictionary broke fairly hard.
def loadFilters(self):
filterFile = open(os.path.dirname(os.path.realpath(__file__)) + "/filters.txt", "r")
for line in filterFile:
lineSplit = line.split("=")
#self.filters[lineSplit[0]] = lineSplit[1]
self.oldWords += (lineSplit[0],)
self.newWords += (lineSplit[1].replace("\n", ""),)
def checkFilters(self, message):
if " " in message:
messageTemp = message.split(" ") # split word by spaces
else:
messageTemp = message
for word in messageTemp:
if word in self.oldWords:
oldIdx = self.oldWords.index(word)
message = message.replace(word, self.newWords[oldIdx]) # replace the bad word with our filtered word
return message

11
pep.py
View File

@ -23,7 +23,7 @@ from helpers import consoleHelper
from helpers import databaseHelperNew from helpers import databaseHelperNew
from helpers import generalFunctions from helpers import generalFunctions
from helpers import logHelper as log from helpers import logHelper as log
from helpers import filterHelper
from handlers import mainHandler from handlers import mainHandler
from handlers import apiIsOnlineHandler from handlers import apiIsOnlineHandler
@ -107,6 +107,15 @@ if __name__ == "__main__":
consoleHelper.printError() consoleHelper.printError()
consoleHelper.printColored("[!] Error while creating threads pool. Please check your config.ini and run the server again", bcolors.RED) consoleHelper.printColored("[!] Error while creating threads pool. Please check your config.ini and run the server again", bcolors.RED)
try:
consoleHelper.printNoNl("> Loading chat filters... ")
filters = filterHelper.chatFilters()
filters.loadFilters()
consoleHelper.printDone()
except:
consoleHelper.printError()
consoleHelper.printColored("[!] Error while loading chat filters. Make sure there is a filters.txt file present", bcolors.RED)
# Create data folder if needed # Create data folder if needed
consoleHelper.printNoNl("> Checking folders... ") consoleHelper.printNoNl("> Checking folders... ")
paths = [".data"] paths = [".data"]