epicyon/manualapprove.py

157 lines
7.3 KiB
Python
Raw Normal View History

2019-07-20 18:25:40 +00:00
__filename__ = "manualapprove.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2019-12-14 10:52:19 +00:00
__version__ = "1.1.0"
2019-07-20 18:25:40 +00:00
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
import os
import json
2019-10-12 09:37:21 +00:00
import time
2019-07-20 18:25:40 +00:00
from follow import followedAccountAccepts
from follow import followedAccountRejects
2019-09-18 17:04:19 +00:00
from follow import removeFromFollowRequests
2019-10-22 11:55:06 +00:00
from utils import loadJson
from utils import saveJson
2019-07-20 18:25:40 +00:00
def manualDenyFollowRequest(session,baseDir: str, \
httpPrefix: str,
nickname: str,domain: str,port: int, \
denyHandle: str, \
federationList: [], \
sendThreads: [],postLog: [], \
cachedWebfingers: {},personCache: {}, \
debug: bool, \
projectVersion: str) -> None:
2019-07-20 18:25:40 +00:00
"""Manually deny a follow request
"""
2019-08-07 13:42:23 +00:00
handle=nickname+'@'+domain
2019-07-20 18:25:40 +00:00
accountsDir=baseDir+'/accounts/'+handle
# has this handle already been rejected?
rejectedFollowsFilename=accountsDir+'/followrejects.txt'
if os.path.isfile(rejectedFollowsFilename):
if denyHandle in open(rejectedFollowsFilename).read():
2019-10-15 09:12:58 +00:00
removeFromFollowRequests(baseDir,nickname,domain,denyHandle,debug)
print(denyHandle+' has already been rejected as a follower of '+nickname)
return
2019-10-15 09:12:58 +00:00
removeFromFollowRequests(baseDir,nickname,domain,denyHandle,debug)
# Store rejected follows
rejectsFile=open(rejectedFollowsFilename, "a+")
rejectsFile.write(denyHandle+'\n')
rejectsFile.close()
denyNickname=denyHandle.split('@')[0]
2019-11-28 09:39:53 +00:00
denyDomain=denyHandle.split('@')[1].replace('\n','')
denyPort=port
if ':' in denyDomain:
denyPort=denyDomain.split(':')[1]
denyDomain=denyDomain.split(':')[0]
2019-12-16 10:01:57 +00:00
followedAccountRejects(session,baseDir,httpPrefix, \
nickname,domain,port, \
denyNickname,denyDomain,denyPort, \
federationList, \
sendThreads,postLog, \
cachedWebfingers,personCache, \
debug,projectVersion)
2019-08-07 13:05:09 +00:00
print('Follow request from '+denyHandle+' was denied.')
2019-07-20 18:25:40 +00:00
def manualApproveFollowRequest(session,baseDir: str, \
httpPrefix: str,
nickname: str,domain: str,port: int, \
approveHandle: str, \
federationList: [], \
sendThreads: [],postLog: [], \
cachedWebfingers: {},personCache: {}, \
acceptedCaps: [], \
2019-08-14 20:12:27 +00:00
debug: bool, \
projectVersion: str) -> None:
2019-07-20 18:25:40 +00:00
"""Manually approve a follow request
"""
handle=nickname+'@'+domain
2019-12-29 12:59:13 +00:00
print('Manual follow accept: '+handle+' approving follow request from '+approveHandle)
accountDir=baseDir+'/accounts/'+handle
approveFollowsFilename=accountDir+'/followrequests.txt'
2019-08-07 13:05:09 +00:00
if not os.path.isfile(approveFollowsFilename):
2019-12-29 13:05:33 +00:00
print('Manual follow accept: follow requests file '+approveFollowsFilename+' not found')
2019-08-07 13:05:09 +00:00
return
2019-12-29 12:59:13 +00:00
# is the handle in the requests file?
2019-08-07 13:31:11 +00:00
if approveHandle not in open(approveFollowsFilename).read():
2019-12-29 13:05:33 +00:00
print('Manual follow accept: '+approveHandle+' not in requests file '+approveFollowsFilename)
2019-08-07 13:05:09 +00:00
return
2019-08-07 13:52:38 +00:00
approvefilenew = open(approveFollowsFilename+'.new', 'w+')
2019-08-31 14:21:59 +00:00
updateApprovedFollowers=False
followActivityfilename=None
2019-08-07 13:51:54 +00:00
with open(approveFollowsFilename, 'r') as approvefile:
2019-12-29 12:59:13 +00:00
for handleOfFollowRequester in approvefile:
# is this the approved follow?
if handleOfFollowRequester.startswith(approveHandle):
handleOfFollowRequester=handleOfFollowRequester.replace('\n','')
2019-08-07 13:51:54 +00:00
port2=port
2019-12-29 12:59:13 +00:00
if ':' in handleOfFollowRequester:
port2=int(handleOfFollowRequester.split(':')[1])
requestsDir=accountDir+'/requests'
followActivityfilename=requestsDir+'/'+handleOfFollowRequester+'.follow'
2019-08-07 13:51:54 +00:00
if os.path.isfile(followActivityfilename):
2019-10-22 11:55:06 +00:00
followJson=loadJson(followActivityfilename)
2019-09-30 22:39:02 +00:00
if followJson:
2019-08-07 13:51:54 +00:00
approveNickname=approveHandle.split('@')[0]
approveDomain=approveHandle.split('@')[1].replace('\n','')
approvePort=port2
if ':' in approveDomain:
approvePort=approveDomain.split(':')[1]
approveDomain=approveDomain.split(':')[0]
2019-12-29 12:59:13 +00:00
print('Manual follow accept: Sending Accept for '+handle+' follow request from '+approveNickname+'@'+approveDomain)
2019-08-07 13:51:54 +00:00
followedAccountAccepts(session,baseDir,httpPrefix, \
nickname,domain,port, \
approveNickname,approveDomain,approvePort, \
followJson['actor'],federationList, \
followJson,acceptedCaps, \
sendThreads,postLog, \
cachedWebfingers,personCache, \
debug,projectVersion,False)
updateApprovedFollowers=True
2019-08-07 13:51:54 +00:00
else:
2019-12-29 12:59:13 +00:00
# this isn't the approved follow so it will remain
# in the requests file
approvefilenew.write(handleOfFollowRequester)
2019-08-07 13:49:21 +00:00
approvefilenew.close()
2019-08-31 14:21:59 +00:00
2019-12-29 12:59:13 +00:00
followersFilename=accountDir+'/followers.txt'
2019-08-31 14:21:59 +00:00
if updateApprovedFollowers:
# update the followers
2019-12-29 12:59:13 +00:00
print('Manual follow accept: updating '+followersFilename)
2019-08-31 14:21:59 +00:00
if os.path.isfile(followersFilename):
if approveHandle not in open(followersFilename).read():
2019-11-05 13:13:55 +00:00
try:
with open(followersFilename, 'r+') as followersFile:
content = followersFile.read()
followersFile.seek(0, 0)
followersFile.write(approveHandle+'\n'+content)
except Exception as e:
2019-12-29 12:59:13 +00:00
print('WARN: Manual follow accept. Failed to write entry to followers file '+str(e))
else:
print('WARN: Manual follow accept: '+approveHandle+' already exists in '+followersFilename)
2019-08-31 14:21:59 +00:00
else:
2019-12-29 12:59:13 +00:00
print('Manual follow accept: first follower accepted for '+handle+' is '+approveHandle)
2019-08-31 14:21:59 +00:00
followersFile=open(followersFilename, "w+")
followersFile.write(approveHandle+'\n')
followersFile.close()
2019-12-29 12:59:13 +00:00
# only update the follow requests file if the follow is confirmed to be
# in followers.txt
if approveHandle in open(followersFilename).read():
# update the follow requests with the handles not yet approved
os.rename(approveFollowsFilename+'.new',approveFollowsFilename)
# remove the .follow file
if followActivityfilename:
if os.path.isfile(followActivityfilename):
os.remove(followActivityfilename)
2019-12-29 12:59:13 +00:00
else:
os.remove(approveFollowsFilename+'.new')