flake8 style

main
Bob Mottram 2020-04-01 20:06:27 +00:00
parent 2c6169fa64
commit 8dd64c187f
1 changed files with 200 additions and 170 deletions

View File

@ -8,9 +8,17 @@ __status__="Production"
import os
from utils import isEvil
from utils import locatePost
from utils import evilIncarnate
from utils import getDomainFromActor
from utils import getNicknameFromActor
from session import postJson
from auth import createBasicAuthHeader
from posts import getPersonBox
from webfinger import webfingerHandle
def addGlobalBlock(baseDir: str, \
def addGlobalBlock(baseDir: str,
blockNickname: str, blockDomain: str) -> bool:
"""Global block which applies to all accounts
"""
@ -33,13 +41,15 @@ def addGlobalBlock(baseDir: str, \
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:
"""Block the given account
"""
if ':' in domain:
domain = domain.split(':')[0]
blockingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/blocking.txt'
blockingFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/blocking.txt'
blockHandle = blockNickname + '@' + blockDomain
if os.path.isfile(blockingFilename):
if blockHandle in open(blockingFilename).read():
@ -49,8 +59,9 @@ def addBlock(baseDir: str,nickname: str,domain: str, \
blockFile.close()
return True
def removeGlobalBlock(baseDir: str, \
unblockNickname: str, \
def removeGlobalBlock(baseDir: str,
unblockNickname: str,
unblockDomain: str) -> bool:
"""Unblock the given global block
"""
@ -83,13 +94,15 @@ def removeGlobalBlock(baseDir: str, \
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:
"""Unblock the given account
"""
if ':' in domain:
domain = domain.split(':')[0]
unblockingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/blocking.txt'
unblockingFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/blocking.txt'
unblockHandle = unblockNickname + '@' + unblockDomain
if os.path.isfile(unblockingFilename):
if unblockHandle in open(unblockingFilename).read():
@ -104,6 +117,7 @@ def removeBlock(baseDir: str,nickname: str,domain: str, \
return True
return False
def isBlockedHashtag(baseDir: str, hashtag: str) -> bool:
"""Is the given hashtag blocked?
"""
@ -114,6 +128,7 @@ def isBlockedHashtag(baseDir: str,hashtag: str) -> bool:
return True
return False
def getDomainBlocklist(baseDir: str) -> str:
"""Returns all globally blocked domains as a string
This can be used for fast matching to mitigate flooding
@ -131,6 +146,7 @@ def getDomainBlocklist(baseDir: str) -> str:
blockedStr += file.read()
return blockedStr
def isBlockedDomain(baseDir: str, domain: str) -> bool:
"""Is the given domain blocked?
"""
@ -142,7 +158,8 @@ def isBlockedDomain(baseDir: str,domain: str) -> bool:
return True
return False
def isBlocked(baseDir: str,nickname: str,domain: str, \
def isBlocked(baseDir: str, nickname: str, domain: str,
blockNickname: str, blockDomain: str) -> bool:
"""Is the given nickname blocked?
"""
@ -156,13 +173,13 @@ def isBlocked(baseDir: str,nickname: str,domain: str, \
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 blockDomain not in open(allowFilename).read():
return True
blockingFilename= \
baseDir+'/accounts/'+nickname+'@'+domain+'/blocking.txt'
blockingFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/blocking.txt'
if os.path.isfile(blockingFilename):
if '*@' + blockDomain in open(blockingFilename).read():
return True
@ -172,11 +189,12 @@ def isBlocked(baseDir: str,nickname: str,domain: str, \
return True
return False
def sendBlockViaServer(baseDir: str,session, \
fromNickname: str,password: str, \
fromDomain: str,fromPort: int, \
httpPrefix: str,blockedUrl: str, \
cachedWebfingers: {},personCache: {}, \
def sendBlockViaServer(baseDir: str, session,
fromNickname: str, password: str,
fromDomain: str, fromPort: int,
httpPrefix: str, blockedUrl: str,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str) -> {}:
"""Creates a block via c2s
"""
@ -191,8 +209,8 @@ def sendBlockViaServer(baseDir: str,session, \
fromDomainFull = fromDomain + ':' + str(fromPort)
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl= \
httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers'
ccUrl = httpPrefix + '://' + fromDomainFull + '/users/' + \
fromNickname + '/followers'
blockActor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname
newBlockJson = {
@ -207,8 +225,8 @@ def sendBlockViaServer(baseDir: str,session, \
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
# lookup the inbox for the To handle
wfRequest= \
webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \
wfRequest = webfingerHandle(session, handle, httpPrefix,
cachedWebfingers,
fromDomain, projectVersion)
if not wfRequest:
if debug:
@ -218,9 +236,12 @@ def sendBlockViaServer(baseDir: str,session, \
postToBox = 'outbox'
# get the actor inbox for the To handle
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \
getPersonBox(baseDir,session,wfRequest,personCache, \
projectVersion,httpPrefix,fromNickname, \
(inboxUrl, pubKeyId, pubKey,
fromPersonId, sharedInbox,
capabilityAcquisition, avatarUrl,
displayName) = getPersonBox(baseDir, session, wfRequest,
personCache,
projectVersion, httpPrefix, fromNickname,
fromDomain, postToBox)
if not inboxUrl:
@ -235,23 +256,26 @@ def sendBlockViaServer(baseDir: str,session, \
authHeader = createBasicAuthHeader(fromNickname, password)
headers = {
'host': fromDomain, \
'Content-type': 'application/json', \
'host': fromDomain,
'Content-type': 'application/json',
'Authorization': authHeader
}
postResult= \
postJson(session,newBlockJson,[],inboxUrl,headers,"inbox:write")
postResult = postJson(session, newBlockJson, [], inboxUrl,
headers, "inbox:write")
if not postResult:
print('WARN: Unable to post block')
if debug:
print('DEBUG: c2s POST block success')
return newBlockJson
def sendUndoBlockViaServer(baseDir: str,session, \
fromNickname: str,password: str, \
fromDomain: str,fromPort: int, \
httpPrefix: str,blockedUrl: str, \
cachedWebfingers: {},personCache: {}, \
def sendUndoBlockViaServer(baseDir: str, session,
fromNickname: str, password: str,
fromDomain: str, fromPort: int,
httpPrefix: str, blockedUrl: str,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str) -> {}:
"""Creates a block via c2s
"""
@ -266,8 +290,8 @@ def sendUndoBlockViaServer(baseDir: str,session, \
fromDomainFull = fromDomain + ':' + str(fromPort)
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl= \
httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers'
ccUrl = httpPrefix + '://' + fromDomainFull + '/users/' + \
fromNickname + '/followers'
blockActor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname
newBlockJson = {
@ -286,8 +310,8 @@ def sendUndoBlockViaServer(baseDir: str,session, \
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
# lookup the inbox for the To handle
wfRequest= \
webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \
wfRequest = webfingerHandle(session, handle, httpPrefix,
cachedWebfingers,
fromDomain, projectVersion)
if not wfRequest:
if debug:
@ -297,9 +321,11 @@ def sendUndoBlockViaServer(baseDir: str,session, \
postToBox = 'outbox'
# get the actor inbox for the To handle
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \
getPersonBox(baseDir,session,wfRequest,personCache, \
projectVersion,httpPrefix,fromNickname, \
(inboxUrl, pubKeyId, pubKey,
fromPersonId, sharedInbox,
capabilityAcquisition, avatarUrl,
displayName) = getPersonBox(baseDir, session, wfRequest, personCache,
projectVersion, httpPrefix, fromNickname,
fromDomain, postToBox)
if not inboxUrl:
@ -314,20 +340,23 @@ def sendUndoBlockViaServer(baseDir: str,session, \
authHeader = createBasicAuthHeader(fromNickname, password)
headers = {
'host': fromDomain, \
'Content-type': 'application/json', \
'host': fromDomain,
'Content-type': 'application/json',
'Authorization': authHeader
}
postResult= \
postJson(session,newBlockJson,[],inboxUrl,headers,"inbox:write")
postResult = postJson(session, newBlockJson, [], inboxUrl,
headers, "inbox:write")
if not postResult:
print('WARN: Unable to post block')
if debug:
print('DEBUG: c2s POST block success')
return newBlockJson
def outboxBlock(baseDir: str,httpPrefix: str, \
nickname: str,domain: str,port: int, \
def outboxBlock(baseDir: str, httpPrefix: str,
nickname: str, domain: str, port: int,
messageJson: {}, debug: bool) -> None:
""" When a block request is received by the outbox from c2s
"""
@ -380,14 +409,15 @@ def outboxBlock(baseDir: str,httpPrefix: str, \
if ':' not in domainBlocked:
domainBlockedFull = domainBlocked + ':' + str(portBlocked)
addBlock(baseDir,nickname,domain, \
addBlock(baseDir, nickname, domain,
nicknameBlocked, domainBlockedFull)
if debug:
print('DEBUG: post blocked via c2s - ' + postFilename)
def outboxUndoBlock(baseDir: str,httpPrefix: str, \
nickname: str,domain: str,port: int, \
def outboxUndoBlock(baseDir: str, httpPrefix: str,
nickname: str, domain: str, port: int,
messageJson: {}, debug: bool) -> None:
""" When an undo block request is received by the outbox from c2s
"""
@ -448,18 +478,18 @@ def outboxUndoBlock(baseDir: str,httpPrefix: str, \
return
nicknameBlocked = getNicknameFromActor(messageJson['object']['object'])
if not nicknameBlocked:
print('WARN: unable to find nickname in '+ \
print('WARN: unable to find nickname in ' +
messageJson['object']['object'])
return
domainBlocked,portBlocked= \
getDomainFromActor(messageJson['object']['object'])
domainObject = messageJson['object']['object']
domainBlocked, portBlocked = getDomainFromActor(domainObject)
domainBlockedFull = domainBlocked
if portBlocked:
if portBlocked != 80 and portBlocked != 443:
if ':' not in domainBlocked:
domainBlockedFull = domainBlocked + ':' + str(portBlocked)
removeBlock(baseDir,nickname,domain, \
removeBlock(baseDir, nickname, domain,
nicknameBlocked, domainBlockedFull)
if debug:
print('DEBUG: post undo blocked via c2s - ' + postFilename)