From 812ad29de29976f2edc05a8d4f614f9a26c153bc Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Tue, 13 Aug 2019 16:45:47 +0100 Subject: [PATCH] Global block and unblock addresses or domains from moderation timeline --- blocking.py | 43 ++++++++++++++++++++++++++++++++++++++++++- daemon.py | 18 ++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/blocking.py b/blocking.py index b565ec61..d664000b 100644 --- a/blocking.py +++ b/blocking.py @@ -8,6 +8,20 @@ __status__ = "Production" 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, \ blockNickname: str,blockDomain: str) -> bool: """Block the given account @@ -24,6 +38,26 @@ def addBlock(baseDir: str,nickname: str,domain: str, \ blockFile.close() 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, \ unblockNickname: str,unblockDomain: str) -> bool: """Unblock the given account @@ -49,15 +83,22 @@ def isBlocked(baseDir: str,nickname: str,domain: str, \ blockNickname: str,blockDomain: str) -> bool: """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' if os.path.isfile(allowFilename): if blockDomain not in open(allowFilename).read(): return True blockingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/blocking.txt' - blockHandle=blockNickname+'@'+blockDomain if os.path.isfile(blockingFilename): if '*@'+blockDomain in open(blockingFilename).read(): return True + blockHandle=blockNickname+'@'+blockDomain if blockHandle in open(blockingFilename).read(): return True return False diff --git a/daemon.py b/daemon.py index e0be9287..c089ff78 100644 --- a/daemon.py +++ b/daemon.py @@ -64,6 +64,8 @@ from blocking import outboxBlock from blocking import outboxUndoBlock from blocking import addBlock from blocking import removeBlock +from blocking import addGlobalBlock +from blocking import removeGlobalBlock from config import setConfigParam from config import getConfigParam from roles import outboxDelegate @@ -2141,6 +2143,22 @@ class PubServer(BaseHTTPRequestHandler): suspendAccount(self.server.baseDir,nickname,self.server.salts) if moderationButton=='unsuspend': 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 '/statuses/' not in moderationText: removeAccount(self.server.baseDir, \