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

@ -1,201 +1,219 @@
__filename__="blocking.py" __filename__ = "blocking.py"
__author__="Bob Mottram" __author__ = "Bob Mottram"
__license__="AGPL3+" __license__ = "AGPL3+"
__version__="1.1.0" __version__ = "1.1.0"
__maintainer__="Bob Mottram" __maintainer__ = "Bob Mottram"
__email__="bob@freedombone.net" __email__ = "bob@freedombone.net"
__status__="Production" __status__ = "Production"
import os import os
from utils import isEvil from utils import isEvil
from utils import locatePost
from utils import evilIncarnate 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, \
blockNickname: str,blockDomain: str) -> bool: def addGlobalBlock(baseDir: str,
blockNickname: str, blockDomain: str) -> bool:
"""Global block which applies to all accounts """Global block which applies to all accounts
""" """
blockingFilename=baseDir+'/accounts/blocking.txt' blockingFilename = baseDir + '/accounts/blocking.txt'
if not blockNickname.startswith('#'): if not blockNickname.startswith('#'):
blockHandle=blockNickname+'@'+blockDomain blockHandle = blockNickname + '@' + blockDomain
if os.path.isfile(blockingFilename): if os.path.isfile(blockingFilename):
if blockHandle in open(blockingFilename).read(): if blockHandle in open(blockingFilename).read():
return False return False
blockFile=open(blockingFilename, "a+") blockFile = open(blockingFilename, "a+")
blockFile.write(blockHandle+'\n') blockFile.write(blockHandle + '\n')
blockFile.close() blockFile.close()
else: else:
blockHashtag=blockNickname blockHashtag = blockNickname
if os.path.isfile(blockingFilename): if os.path.isfile(blockingFilename):
if blockHashtag+'\n' in open(blockingFilename).read(): if blockHashtag + '\n' in open(blockingFilename).read():
return False return False
blockFile=open(blockingFilename, "a+") blockFile = open(blockingFilename, "a+")
blockFile.write(blockHashtag+'\n') blockFile.write(blockHashtag + '\n')
blockFile.close() blockFile.close()
return True return True
def addBlock(baseDir: str,nickname: str,domain: str, \
blockNickname: str,blockDomain: str) -> bool: def addBlock(baseDir: str, nickname: str, domain: str,
blockNickname: str, blockDomain: str) -> bool:
"""Block the given account """Block the given account
""" """
if ':' in domain: if ':' in domain:
domain=domain.split(':')[0] domain = domain.split(':')[0]
blockingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/blocking.txt' blockingFilename = baseDir + '/accounts/' + \
blockHandle=blockNickname+'@'+blockDomain nickname + '@' + domain + '/blocking.txt'
blockHandle = blockNickname + '@' + blockDomain
if os.path.isfile(blockingFilename): if os.path.isfile(blockingFilename):
if blockHandle in open(blockingFilename).read(): if blockHandle in open(blockingFilename).read():
return False return False
blockFile=open(blockingFilename, "a+") blockFile = open(blockingFilename, "a+")
blockFile.write(blockHandle+'\n') blockFile.write(blockHandle + '\n')
blockFile.close() blockFile.close()
return True return True
def removeGlobalBlock(baseDir: str, \
unblockNickname: str, \ def removeGlobalBlock(baseDir: str,
unblockNickname: str,
unblockDomain: str) -> bool: unblockDomain: str) -> bool:
"""Unblock the given global block """Unblock the given global block
""" """
unblockingFilename=baseDir+'/accounts/blocking.txt' unblockingFilename = baseDir + '/accounts/blocking.txt'
if not unblockNickname.startswith('#'): if not unblockNickname.startswith('#'):
unblockHandle=unblockNickname+'@'+unblockDomain unblockHandle = unblockNickname + '@' + unblockDomain
if os.path.isfile(unblockingFilename): if os.path.isfile(unblockingFilename):
if unblockHandle in open(unblockingFilename).read(): if unblockHandle in open(unblockingFilename).read():
with open(unblockingFilename, 'r') as fp: with open(unblockingFilename, 'r') as fp:
with open(unblockingFilename+'.new', 'w') as fpnew: with open(unblockingFilename + '.new', 'w') as fpnew:
for line in fp: for line in fp:
handle=line.replace('\n','') handle = line.replace('\n', '')
if unblockHandle not in line: if unblockHandle not in line:
fpnew.write(handle+'\n') fpnew.write(handle + '\n')
if os.path.isfile(unblockingFilename+'.new'): if os.path.isfile(unblockingFilename + '.new'):
os.rename(unblockingFilename+'.new',unblockingFilename) os.rename(unblockingFilename + '.new', unblockingFilename)
return True return True
else: else:
unblockHashtag=unblockNickname unblockHashtag = unblockNickname
if os.path.isfile(unblockingFilename): if os.path.isfile(unblockingFilename):
if unblockHashtag+'\n' in open(unblockingFilename).read(): if unblockHashtag + '\n' in open(unblockingFilename).read():
with open(unblockingFilename, 'r') as fp: with open(unblockingFilename, 'r') as fp:
with open(unblockingFilename+'.new', 'w') as fpnew: with open(unblockingFilename + '.new', 'w') as fpnew:
for line in fp: for line in fp:
blockLine=line.replace('\n','') blockLine = line.replace('\n', '')
if unblockHashtag not in line: if unblockHashtag not in line:
fpnew.write(blockLine+'\n') fpnew.write(blockLine + '\n')
if os.path.isfile(unblockingFilename+'.new'): if os.path.isfile(unblockingFilename + '.new'):
os.rename(unblockingFilename+'.new',unblockingFilename) os.rename(unblockingFilename + '.new', unblockingFilename)
return True return True
return False return False
def removeBlock(baseDir: str,nickname: str,domain: str, \
unblockNickname: str,unblockDomain: str) -> bool: def removeBlock(baseDir: str, nickname: str, domain: str,
unblockNickname: str, unblockDomain: str) -> bool:
"""Unblock the given account """Unblock the given account
""" """
if ':' in domain: if ':' in domain:
domain=domain.split(':')[0] domain = domain.split(':')[0]
unblockingFilename=baseDir+'/accounts/'+nickname+'@'+domain+'/blocking.txt' unblockingFilename = baseDir + '/accounts/' + \
unblockHandle=unblockNickname+'@'+unblockDomain nickname + '@' + domain + '/blocking.txt'
unblockHandle = unblockNickname + '@' + unblockDomain
if os.path.isfile(unblockingFilename): if os.path.isfile(unblockingFilename):
if unblockHandle in open(unblockingFilename).read(): if unblockHandle in open(unblockingFilename).read():
with open(unblockingFilename, 'r') as fp: with open(unblockingFilename, 'r') as fp:
with open(unblockingFilename+'.new', 'w') as fpnew: with open(unblockingFilename + '.new', 'w') as fpnew:
for line in fp: for line in fp:
handle=line.replace('\n','') handle = line.replace('\n', '')
if unblockHandle not in line: if unblockHandle not in line:
fpnew.write(handle+'\n') fpnew.write(handle + '\n')
if os.path.isfile(unblockingFilename+'.new'): if os.path.isfile(unblockingFilename + '.new'):
os.rename(unblockingFilename+'.new',unblockingFilename) os.rename(unblockingFilename + '.new', unblockingFilename)
return True return True
return False return False
def isBlockedHashtag(baseDir: str,hashtag: str) -> bool:
def isBlockedHashtag(baseDir: str, hashtag: str) -> bool:
"""Is the given hashtag blocked? """Is the given hashtag blocked?
""" """
globalBlockingFilename=baseDir+'/accounts/blocking.txt' globalBlockingFilename = baseDir + '/accounts/blocking.txt'
if os.path.isfile(globalBlockingFilename): if os.path.isfile(globalBlockingFilename):
hashtag=hashtag.strip('\n') hashtag = hashtag.strip('\n')
if hashtag+'\n' in open(globalBlockingFilename).read(): if hashtag + '\n' in open(globalBlockingFilename).read():
return True return True
return False return False
def getDomainBlocklist(baseDir: str) -> str: def getDomainBlocklist(baseDir: str) -> str:
"""Returns all globally blocked domains as a string """Returns all globally blocked domains as a string
This can be used for fast matching to mitigate flooding This can be used for fast matching to mitigate flooding
""" """
blockedStr='' blockedStr = ''
evilDomains=evilIncarnate() evilDomains = evilIncarnate()
for evil in evilDomains: for evil in evilDomains:
blockedStr+=evil+'\n' blockedStr += evil + '\n'
globalBlockingFilename=baseDir+'/accounts/blocking.txt' globalBlockingFilename = baseDir + '/accounts/blocking.txt'
if not os.path.isfile(globalBlockingFilename): if not os.path.isfile(globalBlockingFilename):
return blockedStr return blockedStr
with open(globalBlockingFilename, 'r') as file: with open(globalBlockingFilename, 'r') as file:
blockedStr += file.read() blockedStr += file.read()
return blockedStr return blockedStr
def isBlockedDomain(baseDir: str,domain: str) -> bool:
def isBlockedDomain(baseDir: str, domain: str) -> bool:
"""Is the given domain blocked? """Is the given domain blocked?
""" """
if isEvil(domain): if isEvil(domain):
return True return True
globalBlockingFilename=baseDir+'/accounts/blocking.txt' globalBlockingFilename = baseDir + '/accounts/blocking.txt'
if os.path.isfile(globalBlockingFilename): if os.path.isfile(globalBlockingFilename):
if '*@'+domain in open(globalBlockingFilename).read(): if '*@' + domain in open(globalBlockingFilename).read():
return True return True
return False return False
def isBlocked(baseDir: str,nickname: str,domain: str, \
blockNickname: str,blockDomain: str) -> bool: def isBlocked(baseDir: str, nickname: str, domain: str,
blockNickname: str, blockDomain: str) -> bool:
"""Is the given nickname blocked? """Is the given nickname blocked?
""" """
if isEvil(blockDomain): if isEvil(blockDomain):
return True return True
globalBlockingFilename=baseDir+'/accounts/blocking.txt' globalBlockingFilename = baseDir + '/accounts/blocking.txt'
if os.path.isfile(globalBlockingFilename): if os.path.isfile(globalBlockingFilename):
if '*@'+blockDomain in open(globalBlockingFilename).read(): if '*@' + blockDomain in open(globalBlockingFilename).read():
return True return True
if blockNickname: if blockNickname:
blockHandle=blockNickname+'@'+blockDomain blockHandle = blockNickname + '@' + blockDomain
if blockHandle in open(globalBlockingFilename).read(): if blockHandle in open(globalBlockingFilename).read():
return True return True
allowFilename= \ allowFilename = baseDir + '/accounts/' + \
baseDir+'/accounts/'+nickname+'@'+domain+'/allowedinstances.txt' 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= \ blockingFilename = baseDir + '/accounts/' + \
baseDir+'/accounts/'+nickname+'@'+domain+'/blocking.txt' nickname + '@' + domain + '/blocking.txt'
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
if blockNickname: if blockNickname:
blockHandle=blockNickname+'@'+blockDomain blockHandle = blockNickname + '@' + blockDomain
if blockHandle in open(blockingFilename).read(): if blockHandle in open(blockingFilename).read():
return True return True
return False return False
def sendBlockViaServer(baseDir: str,session, \
fromNickname: str,password: str, \ def sendBlockViaServer(baseDir: str, session,
fromDomain: str,fromPort: int, \ fromNickname: str, password: str,
httpPrefix: str,blockedUrl: str, \ fromDomain: str, fromPort: int,
cachedWebfingers: {},personCache: {}, \ httpPrefix: str, blockedUrl: str,
debug: bool,projectVersion: str) -> {}: cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str) -> {}:
"""Creates a block via c2s """Creates a block via c2s
""" """
if not session: if not session:
print('WARN: No session for sendBlockViaServer') print('WARN: No session for sendBlockViaServer')
return 6 return 6
fromDomainFull=fromDomain fromDomainFull = fromDomain
if fromPort: if fromPort:
if fromPort!=80 and fromPort!=443: if fromPort != 80 and fromPort != 443:
if ':' not in fromDomain: if ':' not in fromDomain:
fromDomainFull=fromDomain+':'+str(fromPort) fromDomainFull = fromDomain + ':' + str(fromPort)
toUrl= 'https://www.w3.org/ns/activitystreams#Public' toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl= \ ccUrl = httpPrefix + '://' + fromDomainFull + '/users/' + \
httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers' fromNickname + '/followers'
blockActor=httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname blockActor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname
newBlockJson={ newBlockJson = {
"@context": "https://www.w3.org/ns/activitystreams", "@context": "https://www.w3.org/ns/activitystreams",
'type': 'Block', 'type': 'Block',
'actor': blockActor, 'actor': blockActor,
@ -204,73 +222,79 @@ def sendBlockViaServer(baseDir: str,session, \
'cc': [ccUrl] 'cc': [ccUrl]
} }
handle=httpPrefix+'://'+fromDomainFull+'/@'+fromNickname handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
# lookup the inbox for the To handle # lookup the inbox for the To handle
wfRequest= \ wfRequest = webfingerHandle(session, handle, httpPrefix,
webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \ cachedWebfingers,
fromDomain,projectVersion) fromDomain, projectVersion)
if not wfRequest: if not wfRequest:
if debug: if debug:
print('DEBUG: announce webfinger failed for '+handle) print('DEBUG: announce webfinger failed for ' + handle)
return 1 return 1
postToBox='outbox' postToBox = 'outbox'
# get the actor inbox for the To handle # get the actor inbox for the To handle
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \ (inboxUrl, pubKeyId, pubKey,
getPersonBox(baseDir,session,wfRequest,personCache, \ fromPersonId, sharedInbox,
projectVersion,httpPrefix,fromNickname, \ capabilityAcquisition, avatarUrl,
fromDomain,postToBox) displayName) = getPersonBox(baseDir, session, wfRequest,
personCache,
projectVersion, httpPrefix, fromNickname,
fromDomain, postToBox)
if not inboxUrl: if not inboxUrl:
if debug: if debug:
print('DEBUG: No '+postToBox+' was found for '+handle) print('DEBUG: No ' + postToBox + ' was found for ' + handle)
return 3 return 3
if not fromPersonId: if not fromPersonId:
if debug: if debug:
print('DEBUG: No actor was found for '+handle) print('DEBUG: No actor was found for ' + handle)
return 4 return 4
authHeader=createBasicAuthHeader(fromNickname,password) authHeader = createBasicAuthHeader(fromNickname, password)
headers={ headers = {
'host': fromDomain, \ 'host': fromDomain,
'Content-type': 'application/json', \ 'Content-type': 'application/json',
'Authorization': authHeader 'Authorization': authHeader
} }
postResult= \ postResult = postJson(session, newBlockJson, [], inboxUrl,
postJson(session,newBlockJson,[],inboxUrl,headers,"inbox:write") headers, "inbox:write")
if not postResult:
print('WARN: Unable to post block')
if debug: if debug:
print('DEBUG: c2s POST block success') print('DEBUG: c2s POST block success')
return newBlockJson return newBlockJson
def sendUndoBlockViaServer(baseDir: str,session, \
fromNickname: str,password: str, \ def sendUndoBlockViaServer(baseDir: str, session,
fromDomain: str,fromPort: int, \ fromNickname: str, password: str,
httpPrefix: str,blockedUrl: str, \ fromDomain: str, fromPort: int,
cachedWebfingers: {},personCache: {}, \ httpPrefix: str, blockedUrl: str,
debug: bool,projectVersion: str) -> {}: cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str) -> {}:
"""Creates a block via c2s """Creates a block via c2s
""" """
if not session: if not session:
print('WARN: No session for sendBlockViaServer') print('WARN: No session for sendBlockViaServer')
return 6 return 6
fromDomainFull=fromDomain fromDomainFull = fromDomain
if fromPort: if fromPort:
if fromPort!=80 and fromPort!=443: if fromPort != 80 and fromPort != 443:
if ':' not in fromDomain: if ':' not in fromDomain:
fromDomainFull=fromDomain+':'+str(fromPort) fromDomainFull = fromDomain + ':' + str(fromPort)
toUrl= 'https://www.w3.org/ns/activitystreams#Public' toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl= \ ccUrl = httpPrefix + '://' + fromDomainFull + '/users/' + \
httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname+'/followers' fromNickname + '/followers'
blockActor=httpPrefix+'://'+fromDomainFull+'/users/'+fromNickname blockActor = httpPrefix + '://' + fromDomainFull + '/users/' + fromNickname
newBlockJson={ newBlockJson = {
"@context": "https://www.w3.org/ns/activitystreams", "@context": "https://www.w3.org/ns/activitystreams",
'type': 'Undo', 'type': 'Undo',
'actor': blockActor, 'actor': blockActor,
@ -283,59 +307,64 @@ def sendUndoBlockViaServer(baseDir: str,session, \
} }
} }
handle=httpPrefix+'://'+fromDomainFull+'/@'+fromNickname handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
# lookup the inbox for the To handle # lookup the inbox for the To handle
wfRequest= \ wfRequest = webfingerHandle(session, handle, httpPrefix,
webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \ cachedWebfingers,
fromDomain,projectVersion) fromDomain, projectVersion)
if not wfRequest: if not wfRequest:
if debug: if debug:
print('DEBUG: announce webfinger failed for '+handle) print('DEBUG: announce webfinger failed for ' + handle)
return 1 return 1
postToBox='outbox' postToBox = 'outbox'
# get the actor inbox for the To handle # get the actor inbox for the To handle
inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName= \ (inboxUrl, pubKeyId, pubKey,
getPersonBox(baseDir,session,wfRequest,personCache, \ fromPersonId, sharedInbox,
projectVersion,httpPrefix,fromNickname, \ capabilityAcquisition, avatarUrl,
fromDomain,postToBox) displayName) = getPersonBox(baseDir, session, wfRequest, personCache,
projectVersion, httpPrefix, fromNickname,
fromDomain, postToBox)
if not inboxUrl: if not inboxUrl:
if debug: if debug:
print('DEBUG: No '+postToBox+' was found for '+handle) print('DEBUG: No ' + postToBox + ' was found for ' + handle)
return 3 return 3
if not fromPersonId: if not fromPersonId:
if debug: if debug:
print('DEBUG: No actor was found for '+handle) print('DEBUG: No actor was found for ' + handle)
return 4 return 4
authHeader=createBasicAuthHeader(fromNickname,password) authHeader = createBasicAuthHeader(fromNickname, password)
headers={ headers = {
'host': fromDomain, \ 'host': fromDomain,
'Content-type': 'application/json', \ 'Content-type': 'application/json',
'Authorization': authHeader 'Authorization': authHeader
} }
postResult= \ postResult = postJson(session, newBlockJson, [], inboxUrl,
postJson(session,newBlockJson,[],inboxUrl,headers,"inbox:write") headers, "inbox:write")
if not postResult:
print('WARN: Unable to post block')
if debug: if debug:
print('DEBUG: c2s POST block success') print('DEBUG: c2s POST block success')
return newBlockJson return newBlockJson
def outboxBlock(baseDir: str,httpPrefix: str, \
nickname: str,domain: str,port: int, \ def outboxBlock(baseDir: str, httpPrefix: str,
messageJson: {},debug: bool) -> None: nickname: str, domain: str, port: int,
messageJson: {}, debug: bool) -> None:
""" When a block request is received by the outbox from c2s """ When a block request is received by the outbox from c2s
""" """
if not messageJson.get('type'): if not messageJson.get('type'):
if debug: if debug:
print('DEBUG: block - no type') print('DEBUG: block - no type')
return return
if not messageJson['type']=='Block': if not messageJson['type'] == 'Block':
if debug: if debug:
print('DEBUG: not a block') print('DEBUG: not a block')
return return
@ -350,7 +379,7 @@ def outboxBlock(baseDir: str,httpPrefix: str, \
if debug: if debug:
print('DEBUG: c2s block request arrived in outbox') print('DEBUG: c2s block request arrived in outbox')
messageId=messageJson['object'].replace('/activity','') messageId = messageJson['object'].replace('/activity', '')
if '/statuses/' not in messageId: if '/statuses/' not in messageId:
if debug: if debug:
print('DEBUG: c2s block object is not a status') print('DEBUG: c2s block object is not a status')
@ -362,40 +391,41 @@ def outboxBlock(baseDir: str,httpPrefix: str, \
print('DEBUG: c2s block object has no nickname') print('DEBUG: c2s block object has no nickname')
return return
if ':' in domain: if ':' in domain:
domain=domain.split(':')[0] domain = domain.split(':')[0]
postFilename=locatePost(baseDir,nickname,domain,messageId) postFilename = locatePost(baseDir, nickname, domain, messageId)
if not postFilename: if not postFilename:
if debug: if debug:
print('DEBUG: c2s block post not found in inbox or outbox') print('DEBUG: c2s block post not found in inbox or outbox')
print(messageId) print(messageId)
return return
nicknameBlocked=getNicknameFromActor(messageJson['object']) nicknameBlocked = getNicknameFromActor(messageJson['object'])
if not nicknameBlocked: if not nicknameBlocked:
print('WARN: unable to find nickname in '+messageJson['object']) print('WARN: unable to find nickname in ' + messageJson['object'])
return return
domainBlocked,portBlocked=getDomainFromActor(messageJson['object']) domainBlocked, portBlocked = getDomainFromActor(messageJson['object'])
domainBlockedFull=domainBlocked domainBlockedFull = domainBlocked
if portBlocked: if portBlocked:
if portBlocked!=80 and portBlocked!=443: if portBlocked != 80 and portBlocked != 443:
if ':' not in domainBlocked: if ':' not in domainBlocked:
domainBlockedFull=domainBlocked+':'+str(portBlocked) domainBlockedFull = domainBlocked + ':' + str(portBlocked)
addBlock(baseDir,nickname,domain, \ addBlock(baseDir, nickname, domain,
nicknameBlocked,domainBlockedFull) nicknameBlocked, domainBlockedFull)
if debug: if debug:
print('DEBUG: post blocked via c2s - '+postFilename) 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,
messageJson: {},debug: bool) -> None: nickname: str, domain: str, port: int,
messageJson: {}, debug: bool) -> None:
""" When an undo block request is received by the outbox from c2s """ When an undo block request is received by the outbox from c2s
""" """
if not messageJson.get('type'): if not messageJson.get('type'):
if debug: if debug:
print('DEBUG: undo block - no type') print('DEBUG: undo block - no type')
return return
if not messageJson['type']=='Undo': if not messageJson['type'] == 'Undo':
if debug: if debug:
print('DEBUG: not an undo block') print('DEBUG: not an undo block')
return return
@ -412,7 +442,7 @@ def outboxUndoBlock(baseDir: str,httpPrefix: str, \
if debug: if debug:
print('DEBUG: undo block - no type') print('DEBUG: undo block - no type')
return return
if not messageJson['object']['type']=='Block': if not messageJson['object']['type'] == 'Block':
if debug: if debug:
print('DEBUG: not an undo block') print('DEBUG: not an undo block')
return return
@ -427,7 +457,7 @@ def outboxUndoBlock(baseDir: str,httpPrefix: str, \
if debug: if debug:
print('DEBUG: c2s undo block request arrived in outbox') print('DEBUG: c2s undo block request arrived in outbox')
messageId=messageJson['object']['object'].replace('/activity','') messageId = messageJson['object']['object'].replace('/activity', '')
if '/statuses/' not in messageId: if '/statuses/' not in messageId:
if debug: if debug:
print('DEBUG: c2s undo block object is not a status') print('DEBUG: c2s undo block object is not a status')
@ -439,27 +469,27 @@ def outboxUndoBlock(baseDir: str,httpPrefix: str, \
print('DEBUG: c2s undo block object has no nickname') print('DEBUG: c2s undo block object has no nickname')
return return
if ':' in domain: if ':' in domain:
domain=domain.split(':')[0] domain = domain.split(':')[0]
postFilename=locatePost(baseDir,nickname,domain,messageId) postFilename = locatePost(baseDir, nickname, domain, messageId)
if not postFilename: if not postFilename:
if debug: if debug:
print('DEBUG: c2s undo block post not found in inbox or outbox') print('DEBUG: c2s undo block post not found in inbox or outbox')
print(messageId) print(messageId)
return return
nicknameBlocked=getNicknameFromActor(messageJson['object']['object']) nicknameBlocked = getNicknameFromActor(messageJson['object']['object'])
if not nicknameBlocked: if not nicknameBlocked:
print('WARN: unable to find nickname in '+ \ print('WARN: unable to find nickname in ' +
messageJson['object']['object']) messageJson['object']['object'])
return return
domainBlocked,portBlocked= \ domainObject = messageJson['object']['object']
getDomainFromActor(messageJson['object']['object']) domainBlocked, portBlocked = getDomainFromActor(domainObject)
domainBlockedFull=domainBlocked domainBlockedFull = domainBlocked
if portBlocked: if portBlocked:
if portBlocked!=80 and portBlocked!=443: if portBlocked != 80 and portBlocked != 443:
if ':' not in domainBlocked: if ':' not in domainBlocked:
domainBlockedFull=domainBlocked+':'+str(portBlocked) domainBlockedFull = domainBlocked + ':' + str(portBlocked)
removeBlock(baseDir,nickname,domain, \ removeBlock(baseDir, nickname, domain,
nicknameBlocked,domainBlockedFull) nicknameBlocked, domainBlockedFull)
if debug: if debug:
print('DEBUG: post undo blocked via c2s - '+postFilename) print('DEBUG: post undo blocked via c2s - ' + postFilename)