diff --git a/blocking.py b/blocking.py index 86048f36..96cf4820 100644 --- a/blocking.py +++ b/blocking.py @@ -13,13 +13,22 @@ def addGlobalBlock(baseDir: str, \ """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() + if not blockNickname.startswith('#'): + 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() + else: + blockHashtag=blockNickname + if os.path.isfile(blockingFilename): + if blockHashtag+'\n' in open(blockingFilename).read(): + return False + blockFile=open(blockingFilename, "a+") + blockFile.write(blockHashtag+'\n') + blockFile.close() return True def addBlock(baseDir: str,nickname: str,domain: str, \ @@ -44,18 +53,32 @@ def removeGlobalBlock(baseDir: str, \ """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 + if not unblockNickname.startswith('#'): + 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 + else: + unblockHashtag=unblockNickname + if os.path.isfile(unblockingFilename): + if unblockHashtag+'\n' in open(unblockingFilename).read(): + with open(unblockingFilename, 'r') as fp: + with open(unblockingFilename+'.new', 'w') as fpnew: + for line in fp: + blockLine=line.replace('\n','') + if unblockHashtag not in line: + fpnew.write(blockLine+'\n') + if os.path.isfile(unblockingFilename+'.new'): + os.rename(unblockingFilename+'.new',unblockingFilename) + return True return False def removeBlock(baseDir: str,nickname: str,domain: str, \ @@ -78,7 +101,17 @@ def removeBlock(baseDir: str,nickname: str,domain: str, \ os.rename(unblockingFilename+'.new',unblockingFilename) return True return False - + +def isBlockedHashtag(baseDir: str,hashtag: str) -> bool: + """Is the given hashtag blocked? + """ + globalBlockingFilename=baseDir+'/accounts/blocking.txt' + if os.path.isfile(globalBlockingFilename): + hashtag=hashtag.strip('\n') + if hashtag+'\n' in open(globalBlockingFilename).read(): + return True + return False + def isBlocked(baseDir: str,nickname: str,domain: str, \ blockNickname: str,blockDomain: str) -> bool: """Is the given nickname blocked? diff --git a/daemon.py b/daemon.py index 7598edbb..6565e66c 100644 --- a/daemon.py +++ b/daemon.py @@ -66,6 +66,7 @@ from blocking import addBlock from blocking import removeBlock from blocking import addGlobalBlock from blocking import removeGlobalBlock +from blocking import isBlockedHashtag from config import setConfigParam from config import getConfigParam from roles import outboxDelegate @@ -92,6 +93,7 @@ from webinterface import htmlTermsOfService from webinterface import htmlHashtagSearch from webinterface import htmlModerationInfo from webinterface import htmlSearchSharedItems +from webinterface import htmlHashtagBlocked from shares import getSharesFeedForPerson from shares import outboxShareUpload from shares import outboxUndoShareUpload @@ -706,6 +708,11 @@ class PubServer(BaseHTTPRequestHandler): hashtag=self.path.split('/tags/')[1] if '?page=' in hashtag: hashtag=hashtag.split('?page=')[0] + if isBlockedHashtag(self.server.baseDir,hashtag): + self._login_headers('text/html') + self.wfile.write(htmlHashtagBlocked(self.server.baseDir).encode('utf-8')) + self.server.GETbusy=False + return hashtagStr= \ htmlHashtagSearch(self.server.baseDir,hashtag,pageNumber, \ maxPostsInFeed,self.server.session, \ @@ -2160,7 +2167,7 @@ class PubServer(BaseHTTPRequestHandler): fullBlockDomain=blockDomain+':'+str(blockPort) if '@' in moderationText: fullBlockDomain=moderationText.split('@')[1] - if fullBlockDomain: + if fullBlockDomain or nickname.startswith('#'): addGlobalBlock(self.server.baseDir, \ nickname,fullBlockDomain) if moderationButton=='unblock': @@ -2174,7 +2181,7 @@ class PubServer(BaseHTTPRequestHandler): fullBlockDomain=blockDomain+':'+str(blockPort) if '@' in moderationText: fullBlockDomain=moderationText.split('@')[1] - if fullBlockDomain: + if fullBlockDomain or nickname.startswith('#'): removeGlobalBlock(self.server.baseDir, \ nickname,fullBlockDomain) if moderationButton=='remove': diff --git a/webinterface.py b/webinterface.py index 945d7765..1a3e5337 100644 --- a/webinterface.py +++ b/webinterface.py @@ -451,6 +451,20 @@ def htmlTermsOfService(baseDir: str,httpPrefix: str,domainFull: str) -> str: TOSForm+=htmlFooter() return TOSForm +def htmlHashtagBlocked(baseDir: str) -> str: + """Show the screen for a blocked hashtag + """ + blockedHashtagForm='' + with open(baseDir+'/epicyon-suspended.css', 'r') as cssFile: + blockedHashtagCSS=cssFile.read() + blockedHashtagForm=htmlHeader(blockedHashtagCSS) + blockedHashtagForm+='
Hashtag Blocked
' + blockedHashtagForm+='See Terms of Service
' + blockedHashtagForm+='