diff --git a/blocking.py b/blocking.py index 2902af43f..364fc068c 100644 --- a/blocking.py +++ b/blocking.py @@ -243,19 +243,33 @@ def isBlockedDomain(baseDir: str, domain: str, def isBlocked(baseDir: str, nickname: str, domain: str, - blockNickname: str, blockDomain: str) -> bool: + blockNickname: str, blockDomain: str, + blockedCache: [] = None) -> bool: """Is the given nickname blocked? """ if isEvil(blockDomain): return True - globalBlockingFilename = baseDir + '/accounts/blocking.txt' - if os.path.isfile(globalBlockingFilename): - if '*@' + blockDomain in open(globalBlockingFilename).read(): - return True - if blockNickname: - blockHandle = blockNickname + '@' + blockDomain - if blockHandle in open(globalBlockingFilename).read(): + + blockHandle = None + if blockNickname and blockDomain: + blockHandle = blockNickname + '@' + blockDomain + + if blockedCache: + for blockedStr in blockedCache: + if '*@' + domain in blockedStr: return True + if blockHandle: + if blockHandle in blockedStr: + return True + else: + globalBlockingFilename = baseDir + '/accounts/blocking.txt' + if os.path.isfile(globalBlockingFilename): + if '*@' + blockDomain in open(globalBlockingFilename).read(): + return True + if blockHandle: + if blockHandle in open(globalBlockingFilename).read(): + return True + allowFilename = baseDir + '/accounts/' + \ nickname + '@' + domain + '/allowedinstances.txt' if os.path.isfile(allowFilename): @@ -266,8 +280,7 @@ def isBlocked(baseDir: str, nickname: str, domain: str, if os.path.isfile(blockingFilename): if '*@' + blockDomain in open(blockingFilename).read(): return True - if blockNickname: - blockHandle = blockNickname + '@' + blockDomain + if blockHandle: if blockHandle in open(blockingFilename).read(): return True return False diff --git a/daemon.py b/daemon.py index b53021918..0cfd93532 100644 --- a/daemon.py +++ b/daemon.py @@ -1277,6 +1277,13 @@ class PubServer(BaseHTTPRequestHandler): beginSaveTime = time.time() # save the json for later queue processing messageBytesDecoded = messageBytes.decode('utf-8') + + self.server.blockedCacheLastUpdated = \ + updateBlockedCache(self.server.baseDir, + self.server.blockedCache, + self.server.blockedCacheLastUpdated, + self.server.blockedCacheUpdateSecs) + queueFilename = \ savePostToInboxQueue(self.server.baseDir, self.server.httpPrefix, @@ -1286,7 +1293,8 @@ class PubServer(BaseHTTPRequestHandler): messageBytesDecoded, headersDict, self.path, - self.server.debug) + self.server.debug, + self.server.blockedCache) if queueFilename: # add json to the queue if queueFilename not in self.server.inboxQueue: diff --git a/inbox.py b/inbox.py index f73a9eae1..9e9751f26 100644 --- a/inbox.py +++ b/inbox.py @@ -353,7 +353,8 @@ def savePostToInboxQueue(baseDir: str, httpPrefix: str, originalPostJsonObject: {}, messageBytes: str, httpHeaders: {}, - postPath: str, debug: bool) -> str: + postPath: str, debug: bool, + blockedCache: []) -> str: """Saves the give json to the inbox queue for the person keyId specifies the actor sending the post """ @@ -384,7 +385,8 @@ def savePostToInboxQueue(baseDir: str, httpPrefix: str, pprint(postJsonObject) print('No post Domain in actor') return None - if isBlocked(baseDir, nickname, domain, postNickname, postDomain): + if isBlocked(baseDir, nickname, domain, + postNickname, postDomain, blockedCache): if debug: print('DEBUG: post from ' + postNickname + ' blocked') return None @@ -398,7 +400,7 @@ def savePostToInboxQueue(baseDir: str, httpPrefix: str, postJsonObject['object']['inReplyTo'] replyDomain, replyPort = \ getDomainFromActor(inReplyTo) - if isBlockedDomain(baseDir, replyDomain): + if isBlockedDomain(baseDir, replyDomain, blockedCache): if debug: print('WARN: post contains reply from ' + str(actor) + @@ -409,7 +411,8 @@ def savePostToInboxQueue(baseDir: str, httpPrefix: str, getNicknameFromActor(inReplyTo) if replyNickname and replyDomain: if isBlocked(baseDir, nickname, domain, - replyNickname, replyDomain): + replyNickname, replyDomain, + blockedCache): if debug: print('WARN: post contains reply from ' + str(actor) +