Add new whitelist loading system

This commit is contained in:
Daniel 2015-03-04 20:24:11 +02:00
parent a81e40be30
commit 3e660c2fa6
7 changed files with 36824 additions and 195 deletions

View file

@ -1,55 +1,22 @@
from bisect import bisect_left from bisect import bisect_left
import string
import sys
import os
class WhiteList: class WhiteList:
def __init__(self, words):
def __init__(self, wordlist): self.words = words
self.words = []
for line in wordlist:
self.words.append(line.strip('\n\r').lower())
self.words.sort()
self.numWords = len(self.words) self.numWords = len(self.words)
def cleanText(self, text): def cleanText(self, text):
text = text.strip('.,?!') text = text.strip('.,?!')
text = text.lower() return text.lower()
return text
def isWord(self, text): def isWord(self, text):
try: return self.cleanText(text) in self.words
text = self.cleanText(text)
i = bisect_left(self.words, text)
if i == self.numWords:
return False
return self.words[i] == text
except UnicodeDecodeError:
return False # Lets not open ourselves up to obscure keyboards...
def isPrefix(self, text): def isPrefix(self, text):
text = self.cleanText(text) text = self.cleanText(text)
i = bisect_left(self.words, text) i = bisect_left(self.words, text)
if i == self.numWords: if i == self.numWords:
return False return False
return self.words[i].startswith(text)
def prefixCount(self, text): return self.words[i].startswith(text)
text = self.cleanText(text)
i = bisect_left(self.words, text)
j = i
while j < self.numWords and self.words[j].startswith(text):
j += 1
return j - i
def prefixList(self, text):
text = self.cleanText(text)
i = bisect_left(self.words, text)
j = i
while j < self.numWords and self.words[j].startswith(text):
j += 1
return self.words[i:j]

3
tools/whitelist_tool.bat Normal file
View file

@ -0,0 +1,3 @@
@echo off
python -m whitelist_tool
pause

63
tools/whitelist_tool.py Normal file
View file

@ -0,0 +1,63 @@
import os
os.chdir('../')
from toontown.chat import WhiteListData
def acceptWord():
word = raw_input('> ').rstrip().lower()
if word == 'exit()':
saveChanges()
return
if word.startswith('r '):
word = word.replace('r ', '')
if word not in LOCAL_LIST:
print 'Could not remove unknown word "%s" from the whitelist.' % word
else:
LOCAL_LIST.remove(word)
print 'Removed "%s" from the whitelist.' % word
elif word in LOCAL_LIST:
print 'The word "%s" is already whitelisted.' % word
else:
LOCAL_LIST.append(word)
print 'Added the word "%s" to the whitelist.' % word
acceptWord()
def saveChanges():
print 'Saving the whitelist...'
with open('toontown/chat/WhiteListData.py', 'w') as f:
f.write('WHITELIST = [\n')
LOCAL_LIST.sort()
addedWords = []
for word in LOCAL_LIST:
if word in addedWords:
continue
addedWords.append(word)
if "'" in word:
f.write(' "%s",\n' % word)
else:
f.write(" '%s',\n" % word)
f.write(']')
print 'Your changes have been saved! Make sure to push your changes!'
LOCAL_LIST = WhiteListData.WHITELIST
print 'Welcome to the Toontown Unlimited Whitelist Tool!'
print 'Type any word you want to add to the whitelist.'
print 'If you wish to remove a word, type "r <word>".'
print 'When you are done and want to save your changes, type "exit()".'
acceptWord()

View file

@ -1,159 +1,11 @@
import os
import datetime
from pandac.PandaModules import *
from direct.directnotify import DirectNotifyGlobal
from direct.distributed import DistributedObject
from direct.showbase import AppRunnerGlobal
from otp.chat.WhiteList import WhiteList from otp.chat.WhiteList import WhiteList
from toontown.toonbase import TTLocalizer from toontown.toonbase import TTLocalizer
from toontown.chat import WhiteListData
class TTWhiteList(WhiteList, DistributedObject.DistributedObject): class TTWhiteList(WhiteList):
RedownloadTaskName = 'RedownloadWhitelistTask' notify = directNotify.newCategory('TTWhiteList')
WhitelistBaseDir = config.GetString('whitelist-base-dir', '')
WhitelistStageDir = config.GetString('whitelist-stage-dir', 'whitelist')
WhitelistOverHttp = config.GetBool('whitelist-over-http', False)
WhitelistFileName = config.GetString('whitelist-filename', 'twhitelist.dat')
def __init__(self): def __init__(self):
self.redownloadingWhitelist = False WhiteList.__init__(self, WhiteListData.WHITELIST)
self.startRedownload = datetime.datetime.now()
self.endRedownload = datetime.datetime.now()
self.percentDownloaded = 0.0
self.notify = DirectNotifyGlobal.directNotify.newCategory('TTWhiteList')
vfs = VirtualFileSystem.getGlobalPtr()
filename = Filename('twhitelist.dat')
searchPath = DSearchPath()
searchPath.appendDirectory(Filename('/phase_4/etc'))
if __debug__:
searchPath.appendDirectory(Filename('../resources/phase_4/etc'))
found = vfs.resolveFilename(filename, searchPath)
if not found:
self.notify.info("Couldn't find whitelist data file!")
data = vfs.readFile(filename, 1)
lines = data.split('\n')
WhiteList.__init__(self, lines)
if self.WhitelistOverHttp:
self.redownloadWhitelist()
self.defaultWord = TTLocalizer.ChatGarblerDefault[0] self.defaultWord = TTLocalizer.ChatGarblerDefault[0]
def unload(self):
self.removeDownloadingTextTask()
def redownloadWhitelist(self):
self.percentDownload = 0.0
self.notify.info('starting redownloadWhitelist')
self.startRedownload = datetime.datetime.now()
self.redownloadingWhitelist = True
self.addDownloadingTextTask()
self.whitelistUrl = self.getWhitelistUrl()
self.whitelistDir = Filename(self.findWhitelistDir())
Filename(self.whitelistDir + '/.').makeDir()
http = HTTPClient.getGlobalPtr()
self.url = self.whitelistUrl + self.WhitelistFileName
self.ch = http.makeChannel(True)
localFilename = Filename(self.whitelistDir, 'twhitelist.dat')
self.ch.getHeader(DocumentSpec(self.url))
size = self.ch.getFileSize()
doc = self.ch.getDocumentSpec()
localSize = localFilename.getFileSize()
outOfDate = True
if size == localSize:
if doc.hasDate():
date = doc.getDate()
localDate = HTTPDate(localFilename.getTimestamp())
if localDate.compareTo(date) > 0:
outOfDate = False
self.notify.info('Whitelist is up to date')
taskMgr.remove(self.RedownloadTaskName)
if outOfDate and self.ch.isValid():
self.ch.beginGetDocument(doc)
self.ch.downloadToFile(localFilename)
taskMgr.add(self.downloadWhitelistTask, self.RedownloadTaskName)
else:
self.updateWhitelist()
def getWhitelistUrl(self):
result = base.config.GetString('fallback-whitelist-url', 'http://cdn.toontown.disney.go.com/toontown/en/')
override = base.config.GetString('whitelist-url', '')
if override:
self.notify.info('got an override url, using %s for the whitelist' % override)
result = override
else:
try:
launcherUrl = base.launcher.getValue('GAME_WHITELIST_URL', '')
if launcherUrl:
result = launcherUrl
self.notify.info('got GAME_WHITELIST_URL from launcher using %s' % result)
else:
self.notify.info('blank GAME_WHITELIST_URL from launcher, using %s' % result)
except:
self.notify.warning('got exception getting GAME_WHITELIST_URL from launcher, using %s' % result)
return result
def addDownloadingTextTask(self):
self.removeDownloadingTextTask()
task = taskMgr.doMethodLater(1, self.loadingTextTask, 'WhitelistDownloadingTextTask')
task.startTime = globalClock.getFrameTime()
self.loadingTextTask(task)
def removeDownloadingTextTask(self):
taskMgr.remove('WhitelistDownloadingTextTask')
def loadingTextTask(self, task):
timeIndex = int(globalClock.getFrameTime() - task.startTime) % 3
timeStrs = (TTLocalizer.NewsPageDownloadingNews0, TTLocalizer.NewsPageDownloadingNews1, TTLocalizer.NewsPageDownloadingNews2)
textToDisplay = timeStrs[timeIndex] % int(self.percentDownloaded * 100)
return task.again
def findWhitelistDir(self):
if self.WhitelistOverHttp:
return self.WhitelistStageDir
searchPath = DSearchPath()
if AppRunnerGlobal.appRunner:
searchPath.appendDirectory(Filename.expandFrom('$TT_3_5_ROOT/phase_3.5/models/news'))
else:
basePath = os.path.expandvars('$TTMODELS') or './ttmodels'
searchPath.appendDirectory(Filename.fromOsSpecific(basePath + '/built/' + self.NewsBaseDir))
searchPath.appendDirectory(Filename(self.NewsBaseDir))
pfile = Filename(self.WhitelistFileName)
found = vfs.resolveFilename(pfile, searchPath)
if not found:
self.notify.warning('findWhitelistDir - no path: %s' % self.WhitelistFileName)
self.setErrorMessage(TTLocalizer.NewsPageErrorDownloadingFile % self.WhitelistFileName)
return None
self.notify.debug('found whitelist file %s' % pfile)
realDir = pfile.getDirname()
return realDir
def downloadWhitelistTask(self, task):
if self.ch.run():
return task.cont
doc = self.ch.getDocumentSpec()
date = ''
if doc.hasDate():
date = doc.getDate().getString()
if not self.ch.isValid():
self.notify.warning('Unable to download %s' % self.url)
self.redownloadingWhitelist = False
return task.done
self.notify.info('Done downloading whitelist file')
self.updateWhitelist()
return task.done
def updateWhitelist(self):
localFilename = Filename(self.whitelistDir, 'twhitelist.dat')
if not localFilename.exists():
return
data = vfs.readFile(localFilename, 1)
lines = data.split('\n')
self.words = []
for line in lines:
self.words.append(line.strip('\n\r').lower())
self.words.sort()
self.numWords = len(self.words)
self.defaultWord = TTLocalizer.ChatGarblerDefault[0]
def handleNewWhitelist(self):
self.redownloadWhitelist()

36747
toontown/chat/WhiteListData.py Normal file

File diff suppressed because it is too large Load diff

View file

@ -4,7 +4,6 @@ import os
from direct.distributed.DistributedObjectGlobal import DistributedObjectGlobal from direct.distributed.DistributedObjectGlobal import DistributedObjectGlobal
from direct.distributed.DistributedObject import DistributedObject from direct.distributed.DistributedObject import DistributedObject
from toontown.toonbase import ToontownGlobals from toontown.toonbase import ToontownGlobals
from toontown.uberdog import InGameNewsResponses
class DistributedInGameNewsMgr(DistributedObject): class DistributedInGameNewsMgr(DistributedObject):
notify = directNotify.newCategory('InGameNewsMgr') notify = directNotify.newCategory('InGameNewsMgr')

View file

@ -1,2 +0,0 @@
setLatestIssueFailureXML = '\n<setLatestIssueResponse>\n <success>false</success>\n <error>%s</error>\n</setLatestIssueResponse>\n\r\n'
setLatestIssueSuccessXML = '\n<setLatestIssueResponse>\n <success>true</success>\n <info>%s</info>\n</setLatestIssueResponse>\n\r\n'