epicyon/acceptreject.py

159 lines
6.0 KiB
Python
Raw Normal View History

2019-07-02 11:31:26 +00:00
__filename__ = "acceptreject.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "0.0.1"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
import json
import commentjson
from utils import getStatusNumber
from utils import createOutboxDir
from utils import urlPermitted
2019-07-06 15:17:21 +00:00
from utils import getDomainFromActor
from utils import getNicknameFromActor
from utils import domainPermitted
2019-07-06 19:24:52 +00:00
from utils import followPerson
2019-07-02 11:31:26 +00:00
2019-07-06 17:00:22 +00:00
def createAcceptReject(baseDir: str,federationList: [],capsList: [], \
nickname: str,domain: str,port: int, \
toUrl: str,ccUrl: str,httpPrefix: str, \
2019-07-06 19:24:52 +00:00
objectJson: {},acceptType: str) -> {}:
2019-07-06 17:00:22 +00:00
"""Accepts or rejects something (eg. a follow request or offer)
2019-07-02 11:31:26 +00:00
Typically toUrl will be https://www.w3.org/ns/activitystreams#Public
2019-07-06 17:00:22 +00:00
and ccUrl might be a specific person favorited or repeated and
the followers url objectUrl is typically the url of the message,
corresponding to url or atomUri in createPostBase
2019-07-02 11:31:26 +00:00
"""
2019-07-06 19:24:52 +00:00
if not objectJson.get('actor'):
return None
if not urlPermitted(objectJson['actor'],federationList,capsList,"inbox:write"):
2019-07-02 11:31:26 +00:00
return None
if port!=80 and port!=443:
domain=domain+':'+str(port)
newAccept = {
'type': acceptType,
2019-07-03 19:00:03 +00:00
'actor': httpPrefix+'://'+domain+'/users/'+nickname,
2019-07-02 11:31:26 +00:00
'to': [toUrl],
'cc': [],
2019-07-06 19:24:52 +00:00
'object': objectJson
2019-07-02 11:31:26 +00:00
}
if ccUrl:
if len(ccUrl)>0:
newAccept['cc']=ccUrl
return newAccept
2019-07-06 17:00:22 +00:00
def createAccept(baseDir: str,federationList: [],capsList: [], \
nickname: str,domain: str,port: int, \
toUrl: str,ccUrl: str,httpPrefix: str, \
2019-07-06 19:24:52 +00:00
objectJson: {}) -> {}:
2019-07-06 17:00:22 +00:00
return createAcceptReject(baseDir,federationList,capsList, \
nickname,domain,port, \
toUrl,ccUrl,httpPrefix, \
2019-07-06 19:24:52 +00:00
objectJson,'Accept')
2019-07-02 11:31:26 +00:00
2019-07-06 17:00:22 +00:00
def createReject(baseDir: str,federationList: [],capsList: [], \
nickname: str,domain: str,port: int, \
toUrl: str,ccUrl: str,httpPrefix: str, \
2019-07-06 19:24:52 +00:00
objectJson: {}) -> {}:
2019-07-06 17:00:22 +00:00
return createAcceptReject(baseDir,federationList,capsList, \
nickname,domain,port, \
toUrl,ccUrl, \
2019-07-06 19:24:52 +00:00
httpPrefix,objectJson,'Reject')
def acceptFollow(baseDir: str,domain : str,messageJson: {}, \
federationList: [],capsList: [],debug : bool) -> None:
if not messageJson.get('object'):
return
if not messageJson['object'].get('type'):
return
if not messageJson['object'].get('actor'):
return
# no, this isn't a mistake
if not messageJson['object'].get('object'):
return
if not messageJson['object']['type']=='Follow':
return
if debug:
print('DEBUG: follow Accept received')
thisActor=messageJson['object']['actor']
nickname=getNicknameFromActor(thisActor)
acceptedDomain,acceptedPort=getDomainFromActor(thisActor)
if not acceptedDomain:
if debug:
print('DEBUG: domain not found in '+thisActor)
return
if acceptedDomain != domain:
if debug:
print('DEBUG: domain mismatch '+acceptedDomain+' != '+domain)
return
if not nickname:
if debug:
print('DEBUG: nickname not found in '+thisActor)
return
if acceptedPort:
if not '/'+domain+':'+str(acceptedPort)+'/users/'+nickname in thisActor:
if debug:
print('DEBUG: unrecognized actor '+thisActor)
return
else:
if not '/'+domain+'/users/'+nickname in thisActor:
if debug:
print('DEBUG: unrecognized actor '+thisActor)
return
followedActor=messageJson['object']['object']
followedDomain,port=getDomainFromActor(followedActor)
if not followedDomain:
return
followedDomainFull=followedDomain
if port:
followedDomainFull=followedDomain+':'+str(port)
followedNickname=getNicknameFromActor(followedActor)
if not followedNickname:
return
if followPerson(baseDir,nickname,domain, \
followedNickname,followedDomain, \
federationList,debug):
if debug:
print('DEBUG: '+nickname+'@'+domain+' followed '+followedNickname+'@'+followedDomain)
else:
if debug:
print('DEBUG: Unable to create follow - '+nickname+'@'+domain+' -> '+followedNickname+'@'+followedDomain)
2019-07-06 15:17:21 +00:00
2019-07-06 19:24:52 +00:00
def receiveAcceptReject(session,baseDir: str, \
httpPrefix: str,domain :str,port: int, \
2019-07-06 17:00:22 +00:00
sendThreads: [],postLog: [],cachedWebfingers: {}, \
personCache: {},messageJson: {},federationList: [], \
capsList: [],debug : bool) -> bool:
2019-07-06 15:17:21 +00:00
"""Receives an Accept or Reject within the POST section of HTTPServer
"""
if messageJson['type']!='Accept' and messageJson['type']!='Reject':
return False
if not messageJson.get('actor'):
if debug:
print('DEBUG: '+messageJson['type']+' has no actor')
return False
if '/users/' not in messageJson['actor']:
if debug:
print('DEBUG: "users" missing from actor in '+messageJson['type'])
return False
domain,tempPort=getDomainFromActor(messageJson['actor'])
if not domainPermitted(domain,federationList):
if debug:
print('DEBUG: '+messageJson['type']+' from domain not permitted - '+domain)
return False
nickname=getNicknameFromActor(messageJson['actor'])
if not nickname:
if debug:
print('DEBUG: '+messageJson['type']+' does not contain a nickname')
return False
handle=nickname.lower()+'@'+domain.lower()
2019-07-06 19:24:52 +00:00
acceptFollow(baseDir,domain,messageJson,federationList,capsList,debug)
2019-07-06 15:17:21 +00:00
if debug:
print('DEBUG: Uh, '+messageJson['type']+', I guess')
return True