From a5fd8b04315dfbedbb5133fec635acbf078e3023 Mon Sep 17 00:00:00 2001 From: Nyo Date: Wed, 10 Aug 2016 12:00:33 +0200 Subject: [PATCH] .BANCHO. .FIX. Fix chat filters --- .gitignore | 1 + constants/fokabotCommands.py | 5 ++++- filters.txt | 38 +++++++++++++++--------------------- helpers/chatHelper.py | 7 ++----- helpers/filterHelper.py | 31 ----------------------------- objects/chatFilters.py | 35 +++++++++++++++++++++++++++++++++ objects/glob.py | 1 + pep.py | 6 +++--- 8 files changed, 62 insertions(+), 62 deletions(-) delete mode 100644 helpers/filterHelper.py create mode 100644 objects/chatFilters.py diff --git a/.gitignore b/.gitignore index c3b3ef9..8659240 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ **/__pycache__ config.ini +filters.txt .data diff --git a/constants/fokabotCommands.py b/constants/fokabotCommands.py index f6e0b36..2293aec 100644 --- a/constants/fokabotCommands.py +++ b/constants/fokabotCommands.py @@ -296,12 +296,15 @@ def systemShutdown(fro, chan, message): return restartShutdown(False) def systemReload(fro, chan, message): - #Reload settings from bancho_settings + # Reload settings from bancho_settings glob.banchoConf.loadSettings() # Reload channels too glob.channels.loadChannels() + # And chat filters + glob.chatFilters.loadFilters() + # Send new channels and new bottom icon to everyone glob.tokens.enqueueAll(serverPackets.mainMenuIcon(glob.banchoConf.config["menuIcon"])) glob.tokens.enqueueAll(serverPackets.channelInfoEnd()) diff --git a/filters.txt b/filters.txt index 457f49d..3d33b7a 100644 --- a/filters.txt +++ b/filters.txt @@ -1,22 +1,16 @@ -👍= -👎= -👼= -😈= -😀= -😃= -😆= -😊= -😍= -😎= -😑= -😖= -😗= -😛= -😠= -😡= -😥= -😬= -😭= -😮= -😯= -r/osugame=cancer \ No newline at end of file +fuck=firetruck +shit=shish +ass=peach +asses=peaches +bitch=fine lady +bitches=fine ladies +asshole=donkey +ass hole=donkey +cock=chicken +cocks=chickens +dick=eggplant +dicks=eggplants +boobs=bob +tits=teeth +cum=yogurt +cunt=count \ No newline at end of file diff --git a/helpers/chatHelper.py b/helpers/chatHelper.py index 1bf1153..38a79e2 100644 --- a/helpers/chatHelper.py +++ b/helpers/chatHelper.py @@ -6,9 +6,7 @@ from objects import fokabot from helpers import discordBotHelper 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): """ @@ -210,9 +208,8 @@ 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) + # Check for word filters + message = glob.chatFilters.filterMessage(message) # Build packet bytes packet = serverPackets.sendMessage(username, toClient, message) diff --git a/helpers/filterHelper.py b/helpers/filterHelper.py deleted file mode 100644 index 22246bb..0000000 --- a/helpers/filterHelper.py +++ /dev/null @@ -1,31 +0,0 @@ -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/objects/chatFilters.py b/objects/chatFilters.py new file mode 100644 index 0000000..5f04586 --- /dev/null +++ b/objects/chatFilters.py @@ -0,0 +1,35 @@ +import os + +class chatFilters: + def __init__(self, fileName="filters.txt"): + self.filters = {} + self.loadFilters(fileName) + + def loadFilters(self, fileName="filters.txt"): + # Reset chat filters + self.filters = {} + + # Open filters.txt + #with open(os.path.dirname(os.path.realpath(__file__)) + "/../"+fileName+".txt", "r") as f: + with open("filters.txt", "r") as f: + # Read all lines + data = f.readlines() + + # Process each line + for line in data: + # Get old/new word and save it in dictionary + lineSplit = line.split("=") + self.filters[lineSplit[0]] = lineSplit[1].replace("\n", "") + + def filterMessage(self, message): + # Split words by spaces + messageTemp = message.split(" ") + + # Check each word + for word in messageTemp: + # If the word is filtered, replace it + if word in self.filters: + message = message.replace(word, self.filters[word]) + + # Return filtered message + return message diff --git a/objects/glob.py b/objects/glob.py index 93836d7..9aac829 100644 --- a/objects/glob.py +++ b/objects/glob.py @@ -24,6 +24,7 @@ pool = None fLocks = fileLocks.fileLocks() verifiedCache = {} cloudflare = False +chatFilters = None debug = False outputRequestTime = False diff --git a/pep.py b/pep.py index 1310ce7..236a07e 100644 --- a/pep.py +++ b/pep.py @@ -19,11 +19,11 @@ from helpers import configHelper from objects import glob from objects import fokabot from objects import banchoConfig +from objects import chatFilters 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 @@ -109,12 +109,12 @@ if __name__ == "__main__": try: consoleHelper.printNoNl("> Loading chat filters... ") - filters = filterHelper.chatFilters() - filters.loadFilters() + glob.chatFilters = chatFilters.chatFilters() consoleHelper.printDone() except: consoleHelper.printError() consoleHelper.printColored("[!] Error while loading chat filters. Make sure there is a filters.txt file present", bcolors.RED) + raise # Create data folder if needed consoleHelper.printNoNl("> Checking folders... ")