mirror of https://gitlab.com/bashrc2/epicyon
flake8 style
parent
abe216a2b3
commit
ae5b9f579f
195
acceptreject.py
195
acceptreject.py
|
@ -1,27 +1,25 @@
|
||||||
__filename__="acceptreject.py"
|
__filename__ = "acceptreject.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
|
||||||
import json
|
|
||||||
from capabilities import capabilitiesAccept
|
from capabilities import capabilitiesAccept
|
||||||
from capabilities import capabilitiesGrantedSave
|
from capabilities import capabilitiesGrantedSave
|
||||||
from utils import getStatusNumber
|
|
||||||
from utils import createOutboxDir
|
|
||||||
from utils import urlPermitted
|
from utils import urlPermitted
|
||||||
from utils import getDomainFromActor
|
from utils import getDomainFromActor
|
||||||
from utils import getNicknameFromActor
|
from utils import getNicknameFromActor
|
||||||
from utils import domainPermitted
|
from utils import domainPermitted
|
||||||
from utils import followPerson
|
from utils import followPerson
|
||||||
|
|
||||||
def createAcceptReject(baseDir: str,federationList: [], \
|
|
||||||
nickname: str,domain: str,port: int, \
|
def createAcceptReject(baseDir: str, federationList: [],
|
||||||
toUrl: str,ccUrl: str,httpPrefix: str, \
|
nickname: str, domain: str, port: int,
|
||||||
objectJson: {},ocapJson,acceptType: str) -> {}:
|
toUrl: str, ccUrl: str, httpPrefix: str,
|
||||||
|
objectJson: {}, ocapJson, acceptType: str) -> {}:
|
||||||
"""Accepts or rejects something (eg. a follow request or offer)
|
"""Accepts or rejects something (eg. a follow request or offer)
|
||||||
Typically toUrl will be https://www.w3.org/ns/activitystreams#Public
|
Typically toUrl will be https://www.w3.org/ns/activitystreams#Public
|
||||||
and ccUrl might be a specific person favorited or repeated and
|
and ccUrl might be a specific person favorited or repeated and
|
||||||
|
@ -31,15 +29,15 @@ def createAcceptReject(baseDir: str,federationList: [], \
|
||||||
if not objectJson.get('actor'):
|
if not objectJson.get('actor'):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if not urlPermitted(objectJson['actor'],federationList,"inbox:write"):
|
if not urlPermitted(objectJson['actor'], federationList, "inbox:write"):
|
||||||
return None
|
return None
|
||||||
|
|
||||||
if port:
|
if port:
|
||||||
if port!=80 and port!=443:
|
if port != 80 and port != 443:
|
||||||
if ':' not in domain:
|
if ':' not in domain:
|
||||||
domain=domain+':'+str(port)
|
domain = domain + ':' + str(port)
|
||||||
|
|
||||||
newAccept={
|
newAccept = {
|
||||||
"@context": "https://www.w3.org/ns/activitystreams",
|
"@context": "https://www.w3.org/ns/activitystreams",
|
||||||
'type': acceptType,
|
'type': acceptType,
|
||||||
'actor': httpPrefix+'://'+domain+'/users/'+nickname,
|
'actor': httpPrefix+'://'+domain+'/users/'+nickname,
|
||||||
|
@ -48,45 +46,48 @@ def createAcceptReject(baseDir: str,federationList: [], \
|
||||||
'object': objectJson
|
'object': objectJson
|
||||||
}
|
}
|
||||||
if ccUrl:
|
if ccUrl:
|
||||||
if len(ccUrl)>0:
|
if len(ccUrl) > 0:
|
||||||
newAccept['cc']=[ccUrl]
|
newAccept['cc'] = [ccUrl]
|
||||||
# attach capabilities for follow accept
|
# attach capabilities for follow accept
|
||||||
if ocapJson:
|
if ocapJson:
|
||||||
newAccept['capabilities']=ocapJson
|
newAccept['capabilities'] = ocapJson
|
||||||
return newAccept
|
return newAccept
|
||||||
|
|
||||||
def createAccept(baseDir: str,federationList: [], \
|
|
||||||
nickname: str,domain: str,port: int, \
|
def createAccept(baseDir: str, federationList: [],
|
||||||
toUrl: str,ccUrl: str,httpPrefix: str, \
|
nickname: str, domain: str, port: int,
|
||||||
objectJson: {}, \
|
toUrl: str, ccUrl: str, httpPrefix: str,
|
||||||
acceptedCaps=["inbox:write","objects:read"]) -> {}:
|
objectJson: {},
|
||||||
|
acceptedCaps=["inbox:write", "objects:read"]) -> {}:
|
||||||
# create capabilities accept
|
# create capabilities accept
|
||||||
ocapNew= \
|
ocapNew = capabilitiesAccept(baseDir, httpPrefix,
|
||||||
capabilitiesAccept(baseDir,httpPrefix, \
|
nickname, domain, port,
|
||||||
nickname,domain,port,toUrl,True,acceptedCaps)
|
toUrl, True, acceptedCaps)
|
||||||
return createAcceptReject(baseDir,federationList, \
|
return createAcceptReject(baseDir, federationList,
|
||||||
nickname,domain,port, \
|
nickname, domain, port,
|
||||||
toUrl,ccUrl,httpPrefix, \
|
toUrl, ccUrl, httpPrefix,
|
||||||
objectJson,ocapNew,'Accept')
|
objectJson, ocapNew, 'Accept')
|
||||||
|
|
||||||
def createReject(baseDir: str,federationList: [], \
|
|
||||||
nickname: str,domain: str,port: int, \
|
def createReject(baseDir: str, federationList: [],
|
||||||
toUrl: str,ccUrl: str,httpPrefix: str, \
|
nickname: str, domain: str, port: int,
|
||||||
|
toUrl: str, ccUrl: str, httpPrefix: str,
|
||||||
objectJson: {}) -> {}:
|
objectJson: {}) -> {}:
|
||||||
return createAcceptReject(baseDir,federationList, \
|
return createAcceptReject(baseDir, federationList,
|
||||||
nickname,domain,port, \
|
nickname, domain, port,
|
||||||
toUrl,ccUrl, \
|
toUrl, ccUrl,
|
||||||
httpPrefix,objectJson,None,'Reject')
|
httpPrefix, objectJson, None, 'Reject')
|
||||||
|
|
||||||
def acceptFollow(baseDir: str,domain : str,messageJson: {}, \
|
|
||||||
federationList: [],debug : bool) -> None:
|
def acceptFollow(baseDir: str, domain: str, messageJson: {},
|
||||||
|
federationList: [], debug: bool) -> None:
|
||||||
"""Receiving a follow Accept activity
|
"""Receiving a follow Accept activity
|
||||||
"""
|
"""
|
||||||
if not messageJson.get('object'):
|
if not messageJson.get('object'):
|
||||||
return
|
return
|
||||||
if not messageJson['object'].get('type'):
|
if not messageJson['object'].get('type'):
|
||||||
return
|
return
|
||||||
if not messageJson['object']['type']=='Follow':
|
if not messageJson['object']['type'] == 'Follow':
|
||||||
return
|
return
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: receiving Follow activity')
|
print('DEBUG: receiving Follow activity')
|
||||||
|
@ -103,12 +104,12 @@ def acceptFollow(baseDir: str,domain : str,messageJson: {}, \
|
||||||
return
|
return
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: follow Accept received')
|
print('DEBUG: follow Accept received')
|
||||||
thisActor=messageJson['object']['actor']
|
thisActor = messageJson['object']['actor']
|
||||||
nickname=getNicknameFromActor(thisActor)
|
nickname = getNicknameFromActor(thisActor)
|
||||||
if not nickname:
|
if not nickname:
|
||||||
print('WARN: no nickname found in '+thisActor)
|
print('WARN: no nickname found in '+thisActor)
|
||||||
return
|
return
|
||||||
acceptedDomain,acceptedPort=getDomainFromActor(thisActor)
|
acceptedDomain, acceptedPort = getDomainFromActor(thisActor)
|
||||||
if not acceptedDomain:
|
if not acceptedDomain:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: domain not found in '+thisActor)
|
print('DEBUG: domain not found in '+thisActor)
|
||||||
|
@ -118,108 +119,110 @@ def acceptFollow(baseDir: str,domain : str,messageJson: {}, \
|
||||||
print('DEBUG: nickname not found in '+thisActor)
|
print('DEBUG: nickname not found in '+thisActor)
|
||||||
return
|
return
|
||||||
if acceptedPort:
|
if acceptedPort:
|
||||||
if '/'+acceptedDomain+':'+str(acceptedPort)+'/users/'+nickname not in thisActor:
|
if '/' + acceptedDomain + ':' + str(acceptedPort) + \
|
||||||
|
'/users/' + nickname not in thisActor:
|
||||||
if debug:
|
if debug:
|
||||||
print('Port: '+str(acceptedPort))
|
print('Port: '+str(acceptedPort))
|
||||||
print('Expected: /'+acceptedDomain+':'+str(acceptedPort)+ \
|
print('Expected: /' + acceptedDomain + ':' +
|
||||||
'/users/'+nickname)
|
str(acceptedPort) + '/users/'+nickname)
|
||||||
print('Actual: '+thisActor)
|
print('Actual: '+thisActor)
|
||||||
print('DEBUG: unrecognized actor '+thisActor)
|
print('DEBUG: unrecognized actor '+thisActor)
|
||||||
return
|
return
|
||||||
else:
|
else:
|
||||||
if not '/'+acceptedDomain+'/users/'+nickname in thisActor:
|
if not '/' + acceptedDomain+'/users/' + nickname in thisActor:
|
||||||
if debug:
|
if debug:
|
||||||
print('Expected: /'+acceptedDomain+'/users/'+nickname)
|
print('Expected: /'+acceptedDomain+'/users/'+nickname)
|
||||||
print('Actual: '+thisActor)
|
print('Actual: '+thisActor)
|
||||||
print('DEBUG: unrecognized actor '+thisActor)
|
print('DEBUG: unrecognized actor '+thisActor)
|
||||||
return
|
return
|
||||||
followedActor=messageJson['object']['object']
|
followedActor = messageJson['object']['object']
|
||||||
followedDomain,port=getDomainFromActor(followedActor)
|
followedDomain, port = getDomainFromActor(followedActor)
|
||||||
if not followedDomain:
|
if not followedDomain:
|
||||||
print('DEBUG: no domain found within Follow activity object '+ \
|
print('DEBUG: no domain found within Follow activity object ' +
|
||||||
followedActor)
|
followedActor)
|
||||||
return
|
return
|
||||||
followedDomainFull=followedDomain
|
followedDomainFull = followedDomain
|
||||||
if port:
|
if port:
|
||||||
followedDomainFull=followedDomain+':'+str(port)
|
followedDomainFull = followedDomain+':' + str(port)
|
||||||
followedNickname=getNicknameFromActor(followedActor)
|
followedNickname = getNicknameFromActor(followedActor)
|
||||||
if not followedNickname:
|
if not followedNickname:
|
||||||
print('DEBUG: no nickname found within Follow activity object '+ \
|
print('DEBUG: no nickname found within Follow activity object ' +
|
||||||
followedActor)
|
followedActor)
|
||||||
return
|
return
|
||||||
|
|
||||||
acceptedDomainFull=acceptedDomain
|
acceptedDomainFull = acceptedDomain
|
||||||
if acceptedPort:
|
if acceptedPort:
|
||||||
acceptedDomainFull=acceptedDomain+':'+str(acceptedPort)
|
acceptedDomainFull = acceptedDomain + ':' + str(acceptedPort)
|
||||||
|
|
||||||
# are capabilities attached? If so then store them
|
# are capabilities attached? If so then store them
|
||||||
if messageJson.get('capabilities'):
|
if messageJson.get('capabilities'):
|
||||||
if isinstance(messageJson['capabilities'], dict):
|
if isinstance(messageJson['capabilities'], dict):
|
||||||
capabilitiesGrantedSave(baseDir, \
|
capabilitiesGrantedSave(baseDir,
|
||||||
nickname,acceptedDomainFull, \
|
nickname, acceptedDomainFull,
|
||||||
messageJson['capabilities'])
|
messageJson['capabilities'])
|
||||||
|
|
||||||
# has this person already been unfollowed?
|
# has this person already been unfollowed?
|
||||||
unfollowedFilename= \
|
unfollowedFilename = baseDir + '/accounts/' + \
|
||||||
baseDir+'/accounts/'+nickname+'@'+acceptedDomainFull+'/unfollowed.txt'
|
nickname + '@' + acceptedDomainFull + '/unfollowed.txt'
|
||||||
if os.path.isfile(unfollowedFilename):
|
if os.path.isfile(unfollowedFilename):
|
||||||
if followedNickname+'@'+followedDomainFull in open(unfollowedFilename).read():
|
if followedNickname + '@' + followedDomainFull in \
|
||||||
|
open(unfollowedFilename).read():
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: follow accept arrived for '+ \
|
print('DEBUG: follow accept arrived for ' +
|
||||||
nickname+'@'+acceptedDomainFull+ \
|
nickname + '@' + acceptedDomainFull +
|
||||||
' from '+followedNickname+'@'+followedDomainFull+ \
|
' from ' + followedNickname + '@' + followedDomainFull +
|
||||||
' but they have been unfollowed')
|
' but they have been unfollowed')
|
||||||
return
|
return
|
||||||
|
|
||||||
if followPerson(baseDir, \
|
if followPerson(baseDir,
|
||||||
nickname,acceptedDomainFull, \
|
nickname, acceptedDomainFull,
|
||||||
followedNickname,followedDomainFull, \
|
followedNickname, followedDomainFull,
|
||||||
federationList,debug):
|
federationList, debug):
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: '+nickname+'@'+acceptedDomainFull+ \
|
print('DEBUG: ' + nickname + '@' + acceptedDomainFull +
|
||||||
' followed '+followedNickname+'@'+followedDomainFull)
|
' followed ' + followedNickname + '@' + followedDomainFull)
|
||||||
else:
|
else:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: Unable to create follow - '+ \
|
print('DEBUG: Unable to create follow - ' +
|
||||||
nickname+'@'+acceptedDomain+' -> '+ \
|
nickname + '@' + acceptedDomain+' -> ' +
|
||||||
followedNickname+'@'+followedDomain)
|
followedNickname + '@' + followedDomain)
|
||||||
|
|
||||||
def receiveAcceptReject(session,baseDir: str, \
|
|
||||||
httpPrefix: str,domain :str,port: int, \
|
def receiveAcceptReject(session, baseDir: str,
|
||||||
sendThreads: [],postLog: [],cachedWebfingers: {}, \
|
httpPrefix: str, domain: str, port: int,
|
||||||
personCache: {},messageJson: {},federationList: [], \
|
sendThreads: [], postLog: [], cachedWebfingers: {},
|
||||||
debug : bool) -> bool:
|
personCache: {}, messageJson: {}, federationList: [],
|
||||||
|
debug: bool) -> bool:
|
||||||
"""Receives an Accept or Reject within the POST section of HTTPServer
|
"""Receives an Accept or Reject within the POST section of HTTPServer
|
||||||
"""
|
"""
|
||||||
if messageJson['type']!='Accept' and messageJson['type']!='Reject':
|
if messageJson['type'] != 'Accept' and messageJson['type'] != 'Reject':
|
||||||
return False
|
return False
|
||||||
if not messageJson.get('actor'):
|
if not messageJson.get('actor'):
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: '+messageJson['type']+' has no actor')
|
print('DEBUG: ' + messageJson['type'] + ' has no actor')
|
||||||
return False
|
return False
|
||||||
if '/users/' not in messageJson['actor'] and \
|
if '/users/' not in messageJson['actor'] and \
|
||||||
'/channel/' not in messageJson['actor'] and \
|
'/channel/' not in messageJson['actor'] and \
|
||||||
'/profile/' not in messageJson['actor']:
|
'/profile/' not in messageJson['actor']:
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: "users" or "profile" missing from actor in '+ \
|
print('DEBUG: "users" or "profile" missing from actor in ' +
|
||||||
messageJson['type']+'. Assuming single user instance.')
|
messageJson['type'] + '. Assuming single user instance.')
|
||||||
domain,tempPort=getDomainFromActor(messageJson['actor'])
|
domain, tempPort = getDomainFromActor(messageJson['actor'])
|
||||||
if not domainPermitted(domain,federationList):
|
if not domainPermitted(domain, federationList):
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: '+messageJson['type']+ \
|
print('DEBUG: ' + messageJson['type'] +
|
||||||
' from domain not permitted - '+domain)
|
' from domain not permitted - ' + domain)
|
||||||
return False
|
return False
|
||||||
nickname=getNicknameFromActor(messageJson['actor'])
|
nickname = getNicknameFromActor(messageJson['actor'])
|
||||||
if not nickname:
|
if not nickname:
|
||||||
# single user instance
|
# single user instance
|
||||||
nickname='dev'
|
nickname = 'dev'
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: '+messageJson['type']+ \
|
print('DEBUG: ' + messageJson['type'] +
|
||||||
' does not contain a nickname. '+ \
|
' does not contain a nickname. ' +
|
||||||
'Assuming single user instance.')
|
'Assuming single user instance.')
|
||||||
handle=nickname.lower()+'@'+domain.lower()
|
|
||||||
# receive follow accept
|
# receive follow accept
|
||||||
acceptFollow(baseDir,domain,messageJson,federationList,debug)
|
acceptFollow(baseDir, domain, messageJson, federationList, debug)
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: Uh, '+messageJson['type']+', I guess')
|
print('DEBUG: Uh, ' + messageJson['type'] + ', I guess')
|
||||||
return True
|
return True
|
||||||
|
|
Loading…
Reference in New Issue