epicyon/manualapprove.py

218 lines
9.8 KiB
Python
Raw Normal View History

2020-04-03 16:45:00 +00:00
__filename__ = "manualapprove.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2019-07-20 18:25:40 +00:00
import os
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
2019-07-20 18:25:40 +00:00
2020-04-03 16:45:00 +00:00
def manualDenyFollowRequest(session, baseDir: str,
httpPrefix: str,
2020-04-03 16:45:00 +00:00
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
"""
2020-04-03 16:45:00 +00:00
handle = nickname + '@' + domain
accountsDir = baseDir + '/accounts/' + handle
# has this handle already been rejected?
2020-04-03 16:45:00 +00:00
rejectedFollowsFilename = accountsDir + '/followrejects.txt'
if os.path.isfile(rejectedFollowsFilename):
if denyHandle in open(rejectedFollowsFilename).read():
2020-04-03 16:45:00 +00:00
removeFromFollowRequests(baseDir, nickname, domain,
denyHandle, debug)
print(denyHandle + ' has already been rejected as a follower of ' +
nickname)
return
2020-04-03 16:45:00 +00:00
removeFromFollowRequests(baseDir, nickname, domain, denyHandle, debug)
# Store rejected follows
2020-04-03 16:45:00 +00:00
rejectsFile = open(rejectedFollowsFilename, "a+")
rejectsFile.write(denyHandle + '\n')
rejectsFile.close()
2020-03-22 21:16:02 +00:00
2020-04-03 16:45:00 +00:00
denyNickname = denyHandle.split('@')[0]
2020-05-22 11:32:38 +00:00
denyDomain = \
denyHandle.split('@')[1].replace('\n', '').replace('\r', '')
2020-04-03 16:45:00 +00:00
denyPort = port
if ':' in denyDomain:
2020-04-03 16:45:00 +00:00
denyPort = denyDomain.split(':')[1]
denyDomain = denyDomain.split(':')[0]
followedAccountRejects(session, baseDir, httpPrefix,
nickname, domain, port,
denyNickname, denyDomain, denyPort,
federationList,
sendThreads, postLog,
cachedWebfingers, personCache,
debug, projectVersion)
print('Follow request from ' + denyHandle + ' was denied.')
2020-03-22 21:16:02 +00:00
2020-04-03 16:45:00 +00:00
def approveFollowerHandle(accountDir: str, approveHandle: str) -> None:
2019-12-31 09:23:41 +00:00
""" Record manually approved handles so that if they unfollow and then
re-follow later then they don't need to be manually approved again
2020-03-22 21:16:02 +00:00
"""
2020-04-03 16:45:00 +00:00
approvedFilename = accountDir + '/approved.txt'
2019-12-31 09:23:41 +00:00
if os.path.isfile(approvedFilename):
if approveHandle not in open(approvedFilename).read():
2020-04-03 16:45:00 +00:00
approvedFile = open(approvedFilename, "a+")
approvedFile.write(approveHandle + '\n')
2019-12-31 09:23:41 +00:00
approvedFile.close()
else:
2020-04-03 16:45:00 +00:00
approvedFile = open(approvedFilename, "w+")
approvedFile.write(approveHandle + '\n')
2020-03-22 21:16:02 +00:00
approvedFile.close()
2020-04-03 16:45:00 +00:00
def manualApproveFollowRequest(session, baseDir: str,
2019-07-20 18:25:40 +00:00
httpPrefix: str,
2020-04-03 16:45:00 +00:00
nickname: str, domain: str, port: int,
approveHandle: str,
federationList: [],
sendThreads: [], postLog: [],
cachedWebfingers: {}, personCache: {},
debug: bool,
2019-08-14 20:12:27 +00:00
projectVersion: str) -> None:
2019-07-20 18:25:40 +00:00
"""Manually approve a follow request
"""
2020-04-03 16:45:00 +00:00
handle = nickname + '@' + domain
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):
2020-04-03 16:45:00 +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?
approveFollowsStr = ''
with open(approveFollowsFilename, 'r') as fpFollowers:
approveFollowsStr = fpFollowers.read()
exists = True
approveHandleFull = approveHandle
if approveHandle not in approveFollowsStr:
2020-10-28 09:43:55 +00:00
print('Manual follow accept: ' + approveHandle +
' not in requests file ' + approveFollowsFilename +
2020-10-28 09:48:44 +00:00
'\n"' + approveFollowsStr + '"')
exists = False
elif '@' in approveHandle:
reqNick = approveHandle.split('@')[0]
reqDomain = approveHandle.split('@')[1].strip()
reqPrefix = httpPrefix + '://' + reqDomain
if reqPrefix + '/profile/' + reqNick not in approveFollowsStr:
exists = False
approveHandleFull = reqPrefix + '/profile/' + reqNick
elif reqPrefix + '/channel/' + reqNick not in approveFollowsStr:
exists = False
approveHandleFull = reqPrefix + '/channel/' + reqNick
elif reqPrefix + '/accounts/' + reqNick not in approveFollowsStr:
exists = False
approveHandleFull = reqPrefix + '/accounts/' + reqNick
2020-10-28 09:43:55 +00:00
if not exists:
print('Manual follow accept: ' + approveHandleFull +
2020-10-28 09:48:44 +00:00
' not in requests file "' + approveFollowsStr +
'" ' + approveFollowsFilename)
if not exists:
2019-08-07 13:05:09 +00:00
return
2020-03-22 21:16:02 +00:00
2020-04-03 16:45:00 +00:00
approvefilenew = open(approveFollowsFilename + '.new', 'w+')
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(approveHandleFull):
2020-04-03 16:45:00 +00:00
handleOfFollowRequester = \
2020-05-22 11:32:38 +00:00
handleOfFollowRequester.replace('\n', '').replace('\r', '')
2020-04-03 16:45:00 +00:00
port2 = port
2019-12-29 12:59:13 +00:00
if ':' in handleOfFollowRequester:
2020-04-03 16:45:00 +00:00
port2Str = handleOfFollowRequester.split(':')[1]
2020-03-01 10:18:08 +00:00
if port2Str.isdigit():
2020-04-03 16:45:00 +00:00
port2 = int(port2Str)
requestsDir = accountDir + '/requests'
followActivityfilename = \
requestsDir + '/' + handleOfFollowRequester + '.follow'
2019-08-07 13:51:54 +00:00
if os.path.isfile(followActivityfilename):
2020-04-03 16:45:00 +00:00
followJson = loadJson(followActivityfilename)
2019-09-30 22:39:02 +00:00
if followJson:
2020-04-03 16:45:00 +00:00
approveNickname = approveHandle.split('@')[0]
2020-05-22 11:32:38 +00:00
approveDomain = approveHandle.split('@')[1]
2020-04-03 16:45:00 +00:00
approveDomain = \
2020-05-22 11:32:38 +00:00
approveDomain.replace('\n', '').replace('\r', '')
2020-04-03 16:45:00 +00:00
approvePort = port2
2019-08-07 13:51:54 +00:00
if ':' in approveDomain:
2020-04-03 16:45:00 +00:00
approvePort = approveDomain.split(':')[1]
approveDomain = approveDomain.split(':')[0]
print('Manual follow accept: Sending Accept for ' +
handle + ' follow request from ' +
approveNickname + '@' + approveDomain)
followedAccountAccepts(session, baseDir, httpPrefix,
nickname, domain, port,
approveNickname, approveDomain,
approvePort,
followJson['actor'],
federationList,
2020-09-27 19:27:24 +00:00
followJson,
2020-04-03 16:45:00 +00:00
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
2020-04-03 16:45:00 +00:00
followersFilename = accountDir + '/followers.txt'
2019-08-31 14:21:59 +00:00
if updateApprovedFollowers:
# update the followers
2020-04-03 16:45:00 +00:00
print('Manual follow accept: updating ' + followersFilename)
2019-08-31 14:21:59 +00:00
if os.path.isfile(followersFilename):
if approveHandleFull not in open(followersFilename).read():
2019-11-05 13:13:55 +00:00
try:
with open(followersFilename, 'r+') as followersFile:
2020-04-03 16:45:00 +00:00
content = followersFile.read()
2019-11-05 13:13:55 +00:00
followersFile.seek(0, 0)
followersFile.write(approveHandleFull + '\n' + content)
2019-11-05 13:13:55 +00:00
except Exception as e:
2020-04-03 16:45:00 +00:00
print('WARN: Manual follow accept. ' +
'Failed to write entry to followers file ' + str(e))
2019-12-29 12:59:13 +00:00
else:
print('WARN: Manual follow accept: ' + approveHandleFull +
2020-04-03 16:45:00 +00:00
' already exists in ' + followersFilename)
2019-08-31 14:21:59 +00:00
else:
2020-04-03 16:45:00 +00:00
print('Manual follow accept: first follower accepted for ' +
handle + ' is ' + approveHandleFull)
2020-04-03 16:45:00 +00:00
followersFile = open(followersFilename, "w+")
followersFile.write(approveHandleFull + '\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 approveHandleFull in open(followersFilename).read():
2019-12-31 09:23:41 +00:00
# mark this handle as approved for following
2020-04-03 16:45:00 +00:00
approveFollowerHandle(accountDir, approveHandle)
2019-12-29 12:59:13 +00:00
# update the follow requests with the handles not yet approved
2020-04-03 16:45:00 +00:00
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:
2020-04-03 16:45:00 +00:00
os.remove(approveFollowsFilename + '.new')