epicyon/filters.py

85 lines
3.0 KiB
Python
Raw Normal View History

2020-04-03 10:11:54 +00:00
__filename__ = "filters.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2019-07-14 20:50:27 +00:00
import os
2020-04-03 10:11:54 +00:00
def addFilter(baseDir: str, nickname: str, domain: str, words: str) -> bool:
2019-07-14 20:50:27 +00:00
"""Adds a filter for particular words within the content of a incoming posts
"""
2020-04-03 10:11:54 +00:00
filtersFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/filters.txt'
2019-07-14 20:50:27 +00:00
if os.path.isfile(filtersFilename):
if words in open(filtersFilename).read():
return False
2020-04-03 10:11:54 +00:00
filtersFile = open(filtersFilename, "a+")
filtersFile.write(words + '\n')
2019-07-14 20:50:27 +00:00
filtersFile.close()
return True
2020-04-03 10:11:54 +00:00
def removeFilter(baseDir: str, nickname: str, domain: str,
2019-07-14 20:50:27 +00:00
words: str) -> bool:
"""Removes a word filter
"""
2020-04-03 10:11:54 +00:00
filtersFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/filters.txt'
2019-07-14 20:50:27 +00:00
if os.path.isfile(filtersFilename):
if words in open(filtersFilename).read():
with open(filtersFilename, 'r') as fp:
2020-07-12 20:04:58 +00:00
with open(filtersFilename + '.new', 'w+') as fpnew:
2019-07-14 20:50:27 +00:00
for line in fp:
2020-04-03 10:11:54 +00:00
line = line.replace('\n', '')
if line != words:
fpnew.write(line + '\n')
if os.path.isfile(filtersFilename + '.new'):
os.rename(filtersFilename + '.new', filtersFilename)
2019-07-14 20:50:27 +00:00
return True
return False
2020-02-05 14:57:10 +00:00
2020-04-03 10:11:54 +00:00
2020-02-05 14:57:10 +00:00
def isTwitterPost(content: str) -> bool:
"""Returns true if the given post content is a retweet or twitter crosspost
"""
2020-02-05 16:56:45 +00:00
if '/twitter.' in content or '@twitter.' in content:
2020-02-05 14:57:10 +00:00
return True
2020-02-05 16:56:45 +00:00
elif '>RT <' in content:
2020-02-05 14:57:10 +00:00
return True
return False
2020-04-03 10:11:54 +00:00
def isFiltered(baseDir: str, nickname: str, domain: str, content: str) -> bool:
2019-07-14 20:50:27 +00:00
"""Should the given content be filtered out?
This is a simple type of filter which just matches words, not a regex
You can add individual words or use word1+word2 to indicate that two
words must be present although not necessarily adjacent
"""
2020-02-05 14:57:10 +00:00
# optionally remove retweets
2020-04-03 10:11:54 +00:00
removeTwitter = baseDir + '/accounts/' + \
nickname + '@' + domain + '/.removeTwitter'
2020-02-05 14:57:10 +00:00
if os.path.isfile(removeTwitter):
if isTwitterPost(content):
return True
2020-04-03 10:11:54 +00:00
filtersFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/filters.txt'
2019-07-14 20:50:27 +00:00
if os.path.isfile(filtersFilename):
with open(filtersFilename, 'r') as fp:
for line in fp:
2020-05-22 11:32:38 +00:00
filterStr = line.replace('\n', '').replace('\r', '')
2019-07-14 20:50:27 +00:00
if '+' not in filterStr:
if filterStr in content:
return True
else:
2020-04-03 10:11:54 +00:00
filterWords = filterStr.replace('"', '').split('+')
2019-07-14 20:50:27 +00:00
for word in filterWords:
2020-04-03 10:11:54 +00:00
if word not in content:
2019-07-14 20:50:27 +00:00
return False
return True
return False