From 73d30730600ff422514bff7f0df1418778877c13 Mon Sep 17 00:00:00 2001 From: avail Date: Mon, 8 Aug 2016 05:19:52 +0200 Subject: [PATCH] Implement word filter --- filters.txt | 0 helpers/chatHelper.py | 5 +++++ helpers/filterHelper.py | 31 +++++++++++++++++++++++++++++++ pep.py | 11 ++++++++++- 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 filters.txt create mode 100644 helpers/filterHelper.py diff --git a/filters.txt b/filters.txt new file mode 100644 index 0000000..e69de29 diff --git a/helpers/chatHelper.py b/helpers/chatHelper.py index 9f5f2bd..1bf1153 100644 --- a/helpers/chatHelper.py +++ b/helpers/chatHelper.py @@ -8,6 +8,7 @@ from helpers import userHelper from events import logoutEvent from events import channelJoinEvent from constants import messageTemplates +from helpers import filterHelper 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 message = message[:2048]+"..." if len(message) > 2048 else message + # check for word filters + filters = filterHelper.chatFilters() + message = filters.checkFilters(message) + # Build packet bytes packet = serverPackets.sendMessage(username, toClient, message) diff --git a/helpers/filterHelper.py b/helpers/filterHelper.py new file mode 100644 index 0000000..22246bb --- /dev/null +++ b/helpers/filterHelper.py @@ -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 diff --git a/pep.py b/pep.py index 9f9e528..1310ce7 100644 --- a/pep.py +++ b/pep.py @@ -23,7 +23,7 @@ from helpers import consoleHelper from helpers import databaseHelperNew from helpers import generalFunctions from helpers import logHelper as log - +from helpers import filterHelper from handlers import mainHandler from handlers import apiIsOnlineHandler @@ -107,6 +107,15 @@ if __name__ == "__main__": consoleHelper.printError() 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 consoleHelper.printNoNl("> Checking folders... ") paths = [".data"]