Global block and unblock addresses or domains from moderation timeline

master
Bob Mottram 2019-08-13 16:45:47 +01:00
parent febb4258bd
commit 812ad29de2
2 changed files with 60 additions and 1 deletions

View File

@ -8,6 +8,20 @@ __status__ = "Production"
import os import os
def addGlobalBlock(baseDir: str, \
blockNickname: str,blockDomain: str) -> bool:
"""Global block which applies to all accounts
"""
blockingFilename=baseDir+'/accounts/blocking.txt'
blockHandle=blockNickName+'@'+blockDomain
if os.path.isfile(blockingFilename):
if blockHandle in open(blockingFilename).read():
return False
blockFile=open(blockingFilename, "a+")
blockFile.write(blockHandle+'\n')
blockFile.close()
return True
def addBlock(baseDir: str,nickname: str,domain: str, \ def addBlock(baseDir: str,nickname: str,domain: str, \
blockNickname: str,blockDomain: str) -> bool: blockNickname: str,blockDomain: str) -> bool:
"""Block the given account """Block the given account
@ -24,6 +38,26 @@ def addBlock(baseDir: str,nickname: str,domain: str, \
blockFile.close() blockFile.close()
return True return True
def removeGlobalBlock(baseDir: str, \
unblockNickname: str, \
unblockDomain: str) -> bool:
"""Unblock the given global block
"""
unblockingFilename=baseDir+'/accounts/blocking.txt'
unblockHandle=unblockNickName+'@'+unblockDomain
if os.path.isfile(unblockingFilename):
if unblockHandle in open(unblockingFilename).read():
with open(unblockingFilename, 'r') as fp:
with open(unblockingFilename+'.new', 'w') as fpnew:
for line in fp:
handle=line.replace('\n','')
if unblockHandle not in line:
fpnew.write(handle+'\n')
if os.path.isfile(unblockingFilename+'.new'):
os.rename(unblockingFilename+'.new',unblockingFilename)
return True
return False
def removeBlock(baseDir: str,nickname: str,domain: str, \ def removeBlock(baseDir: str,nickname: str,domain: str, \
unblockNickname: str,unblockDomain: str) -> bool: unblockNickname: str,unblockDomain: str) -> bool:
"""Unblock the given account """Unblock the given account
@ -49,15 +83,22 @@ def isBlocked(baseDir: str,nickname: str,domain: str, \
blockNickname: str,blockDomain: str) -> bool: blockNickname: str,blockDomain: str) -> bool:
"""Is the given nickname blocked? """Is the given nickname blocked?
""" """
globalBlockingFilename=baseDir+'/accounts/blocking.txt'
if os.path.isfile(globalBlockingFilename):
if '*@'+blockDomain in open(globalBlockingFilename).read():
return True
blockHandle=blockNickname+'@'+blockDomain
if blockHandle in open(globalBlockingFilename).read():
return True
allowFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/allowedinstances.txt' allowFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/allowedinstances.txt'
if os.path.isfile(allowFilename): if os.path.isfile(allowFilename):
if blockDomain not in open(allowFilename).read(): if blockDomain not in open(allowFilename).read():
return True return True
blockingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/blocking.txt' blockingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/blocking.txt'
blockHandle=blockNickname+'@'+blockDomain
if os.path.isfile(blockingFilename): if os.path.isfile(blockingFilename):
if '*@'+blockDomain in open(blockingFilename).read(): if '*@'+blockDomain in open(blockingFilename).read():
return True return True
blockHandle=blockNickname+'@'+blockDomain
if blockHandle in open(blockingFilename).read(): if blockHandle in open(blockingFilename).read():
return True return True
return False return False

View File

@ -64,6 +64,8 @@ from blocking import outboxBlock
from blocking import outboxUndoBlock from blocking import outboxUndoBlock
from blocking import addBlock from blocking import addBlock
from blocking import removeBlock from blocking import removeBlock
from blocking import addGlobalBlock
from blocking import removeGlobalBlock
from config import setConfigParam from config import setConfigParam
from config import getConfigParam from config import getConfigParam
from roles import outboxDelegate from roles import outboxDelegate
@ -2141,6 +2143,22 @@ class PubServer(BaseHTTPRequestHandler):
suspendAccount(self.server.baseDir,nickname,self.server.salts) suspendAccount(self.server.baseDir,nickname,self.server.salts)
if moderationButton=='unsuspend': if moderationButton=='unsuspend':
unsuspendAccount(self.server.baseDir,nickname) unsuspendAccount(self.server.baseDir,nickname)
if moderationButton=='block':
blockDomain,blockPort=getDomainFromActor(moderationText)
fullBlockDomain=blockDomain
if blockPort:
if blockPort!=80 and blockPort!=443:
fullBlockDomain=blockDomain+':'+str(blockPort)
addGlobalBlock(self.server.baseDir, \
nickname,fullBlockDomain)
if moderationButton=='unblock':
blockDomain,blockPort=getDomainFromActor(moderationText)
fullBlockDomain=blockDomain
if blockPort:
if blockPort!=80 and blockPort!=443:
fullBlockDomain=blockDomain+':'+str(blockPort)
removeGlobalBlock(self.server.baseDir, \
nickname,fullBlockDomain)
if moderationButton=='remove': if moderationButton=='remove':
if '/statuses/' not in moderationText: if '/statuses/' not in moderationText:
removeAccount(self.server.baseDir, \ removeAccount(self.server.baseDir, \