forked from indymedia/epicyon
Word filters
parent
07e751751a
commit
e0f3ac4e8c
22
epicyon.py
22
epicyon.py
|
@ -32,6 +32,8 @@ from session import createSession
|
|||
from session import getJson
|
||||
from blocking import addBlock
|
||||
from blocking import removeBlock
|
||||
from filters import addFilter
|
||||
from filters import removeFilter
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
|
@ -191,6 +193,10 @@ parser.add_argument('--block', dest='block', type=str,default=None, \
|
|||
help='Block a particular address')
|
||||
parser.add_argument('--unblock', dest='unblock', type=str,default=None, \
|
||||
help='Remove a block on a particular address')
|
||||
parser.add_argument('--filter', dest='filterStr', type=str,default=None, \
|
||||
help='Adds a word or phrase which if present will cause a message to be ignored')
|
||||
parser.add_argument('--unfilter', dest='unfilterStr', type=str,default=None, \
|
||||
help='Remove a filter on a particular word or phrase')
|
||||
args = parser.parse_args()
|
||||
|
||||
debug=False
|
||||
|
@ -614,6 +620,22 @@ if args.unblock:
|
|||
print('The block on '+args.block+' was removed by '+args.nickname)
|
||||
sys.exit()
|
||||
|
||||
if args.filterStr:
|
||||
if not args.nickname:
|
||||
print('Please specify a nickname')
|
||||
sys.exit()
|
||||
if addFilter(baseDir,args.nickname,domain,args.filterStr):
|
||||
print('Filter added: '+args.filterStr)
|
||||
sys.exit()
|
||||
|
||||
if args.unfilterStr:
|
||||
if not args.nickname:
|
||||
print('Please specify a nickname')
|
||||
sys.exit()
|
||||
if removeFilter(baseDir,args.nickname,domain,args.unfilterStr):
|
||||
print('Filter removed: '+args.unfilterStr)
|
||||
sys.exit()
|
||||
|
||||
if args.testdata:
|
||||
useBlurhash=False
|
||||
nickname='testuser567'
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
__filename__ = "filters.py"
|
||||
__author__ = "Bob Mottram"
|
||||
__license__ = "AGPL3+"
|
||||
__version__ = "0.0.1"
|
||||
__maintainer__ = "Bob Mottram"
|
||||
__email__ = "bob@freedombone.net"
|
||||
__status__ = "Production"
|
||||
|
||||
import os
|
||||
|
||||
def addFilter(baseDir: str,nickname: str,domain: str,words: str) -> bool:
|
||||
"""Adds a filter for particular words within the content of a incoming posts
|
||||
"""
|
||||
filtersFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/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
|
||||
"""
|
||||
filtersFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/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 words not in line:
|
||||
fpnew.write(line+'\n')
|
||||
if os.path.isfile(filtersFilename+'.new'):
|
||||
os.rename(filtersFilename+'.new',filtersFilename)
|
||||
return True
|
||||
return False
|
||||
|
||||
def isFiltered(baseDir: str,nickname: str,domain: str,content: str) -> bool:
|
||||
"""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
|
||||
"""
|
||||
filtersFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/filters.txt'
|
||||
if os.path.isfile(filtersFilename):
|
||||
with open(filtersFilename, 'r') as fp:
|
||||
for line in fp:
|
||||
filterStr=line.replace('\n','')
|
||||
if '+' not in filterStr:
|
||||
if filterStr in content:
|
||||
return True
|
||||
else:
|
||||
filterWords=filterStr.replace('"','').split('+')
|
||||
for word in filterWords:
|
||||
if not word in content:
|
||||
return False
|
||||
return True
|
||||
return False
|
||||
|
10
inbox.py
10
inbox.py
|
@ -37,6 +37,7 @@ from capabilities import capabilitiesReceiveUpdate
|
|||
from like import updateLikesCollection
|
||||
from like import undoLikesCollectionEntry
|
||||
from blocking import isBlocked
|
||||
from filters import isFiltered
|
||||
|
||||
def getPersonPubKey(session,personUrl: str,personCache: {},debug: bool) -> str:
|
||||
if not personUrl:
|
||||
|
@ -126,7 +127,14 @@ def savePostToInboxQueue(baseDir: str,httpPrefix: str,nickname: str, domain: str
|
|||
postDomain,postPort=getDomainFromActor(postJsonObject['actor'])
|
||||
if isBlocked(baseDir,nickname,domain,postNickname,postDomain):
|
||||
return None
|
||||
|
||||
|
||||
if postJsonObject.get('object'):
|
||||
if isinstance(postJsonObject['object'], dict):
|
||||
if postJsonObject['object'].get('content'):
|
||||
if isinstance(postJsonObject['object']['content'], str):
|
||||
if isFiltered(baseDir,nickname,domain,postJsonObject['object']['content']):
|
||||
return None
|
||||
|
||||
if postJsonObject.get('id'):
|
||||
postId=postJsonObject['id'].replace('/activity','')
|
||||
else:
|
||||
|
|
Loading…
Reference in New Issue