diff --git a/daemon.py b/daemon.py index ac627d49..0b18109b 100644 --- a/daemon.py +++ b/daemon.py @@ -239,6 +239,8 @@ from newswire import loadHashtagCategories from newsdaemon import runNewswireWatchdog from newsdaemon import runNewswireDaemon from filters import isFiltered +from filters import addGlobalFilter +from filters import removeGlobalFilter import os @@ -1506,6 +1508,10 @@ class PubServer(BaseHTTPRequestHandler): moderationButton = 'block' elif moderationStr.startswith('submitUnblock'): moderationButton = 'unblock' + elif moderationStr.startswith('submitFilter'): + moderationButton = 'filter' + elif moderationStr.startswith('submitUnfilter'): + moderationButton = 'unfilter' elif moderationStr.startswith('submitSuspend'): moderationButton = 'suspend' elif moderationStr.startswith('submitUnsuspend'): @@ -1526,6 +1532,10 @@ class PubServer(BaseHTTPRequestHandler): suspendAccount(baseDir, nickname, domain) if moderationButton == 'unsuspend': unsuspendAccount(baseDir, nickname) + if moderationButton == 'filter': + addGlobalFilter(baseDir, moderationText) + if moderationButton == 'unfilter': + removeGlobalFilter(baseDir, moderationText) if moderationButton == 'block': fullBlockDomain = None if moderationText.startswith('http') or \ diff --git a/filters.py b/filters.py index 38885711..42db4959 100644 --- a/filters.py +++ b/filters.py @@ -23,6 +23,20 @@ def addFilter(baseDir: str, nickname: str, domain: str, words: str) -> bool: return True +def addGlobalFilter(baseDir: str, words: str) -> bool: + """Adds a global filter for particular words within + the content of a incoming posts + """ + filtersFilename = baseDir + '/accounts/filters.txt' + if os.path.isfile(filtersFilename): + if words in open(filtersFilename).read(): + return False + filtersFile = open(filtersFilename, "a+") + filtersFile.write(words + '\n') + filtersFile.close() + return True + + def removeFilter(baseDir: str, nickname: str, domain: str, words: str) -> bool: """Removes a word filter @@ -43,6 +57,24 @@ def removeFilter(baseDir: str, nickname: str, domain: str, return False +def removeGlobalFilter(baseDir: str, words: str) -> bool: + """Removes a global word filter + """ + filtersFilename = baseDir + '/accounts/filters.txt' + if os.path.isfile(filtersFilename): + if words in open(filtersFilename).read(): + with open(filtersFilename, 'r') as fp: + with open(filtersFilename + '.new', 'w+') as fpnew: + for line in fp: + line = line.replace('\n', '') + if line != words: + fpnew.write(line + '\n') + if os.path.isfile(filtersFilename + '.new'): + os.rename(filtersFilename + '.new', filtersFilename) + return True + return False + + def isTwitterPost(content: str) -> bool: """Returns true if the given post content is a retweet or twitter crosspost """ @@ -66,9 +98,9 @@ def isFiltered(baseDir: str, nickname: str, domain: str, content: str) -> bool: if isTwitterPost(content): return True - instanceFiltersFilename = baseDir + '/accounts/filters.txt' - if os.path.isfile(instanceFiltersFilename): - if content + '\n' in open(instanceFiltersFilename).read(): + globalFiltersFilename = baseDir + '/accounts/filters.txt' + if os.path.isfile(globalFiltersFilename): + if content + '\n' in open(globalFiltersFilename).read(): return True accountFiltersFilename = baseDir + '/accounts/' + \