Implement word filter
This commit is contained in:
parent
8497a7c318
commit
73d3073060
0
filters.txt
Normal file
0
filters.txt
Normal file
|
@ -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)
|
||||
|
||||
|
|
31
helpers/filterHelper.py
Normal file
31
helpers/filterHelper.py
Normal 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
11
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"]
|
||||
|
|
Loading…
Reference in New Issue
Block a user