2020-04-03 16:45:00 +00:00
|
|
|
__filename__ = "manualapprove.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2021-01-26 10:07:42 +00:00
|
|
|
__version__ = "1.2.0"
|
2020-04-03 16:45:00 +00:00
|
|
|
__maintainer__ = "Bob Mottram"
|
2021-09-10 16:14:50 +00:00
|
|
|
__email__ = "bob@libreserver.org"
|
2020-04-03 16:45:00 +00:00
|
|
|
__status__ = "Production"
|
2021-06-15 15:08:12 +00:00
|
|
|
__module_group__ = "ActivityPub"
|
2019-07-20 18:25:40 +00:00
|
|
|
|
|
|
|
import os
|
|
|
|
from follow import followedAccountAccepts
|
2019-09-09 12:19:00 +00:00
|
|
|
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
|
2021-06-26 14:21:24 +00:00
|
|
|
from utils import removeDomainPort
|
|
|
|
from utils import getPortFromDomain
|
2021-07-04 22:58:01 +00:00
|
|
|
from utils import getUserPaths
|
2021-07-13 21:59:53 +00:00
|
|
|
from utils import acctDir
|
2021-10-23 11:58:38 +00:00
|
|
|
from threads import threadWithTrace
|
2019-07-20 18:25:40 +00:00
|
|
|
|
2020-04-03 16:45:00 +00:00
|
|
|
|
|
|
|
def manualDenyFollowRequest(session, baseDir: str,
|
2019-09-09 12:19:00 +00:00
|
|
|
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,
|
2021-08-31 14:17:11 +00:00
|
|
|
projectVersion: str,
|
|
|
|
signingPrivateKeyPem: str) -> None:
|
2019-07-20 18:25:40 +00:00
|
|
|
"""Manually deny a follow request
|
|
|
|
"""
|
2021-07-13 21:59:53 +00:00
|
|
|
accountsDir = acctDir(baseDir, nickname, domain)
|
2019-09-09 08:50:28 +00:00
|
|
|
|
|
|
|
# has this handle already been rejected?
|
2020-04-03 16:45:00 +00:00
|
|
|
rejectedFollowsFilename = accountsDir + '/followrejects.txt'
|
2019-09-09 08:50:28 +00:00
|
|
|
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)
|
2019-09-09 08:50:28 +00:00
|
|
|
return
|
|
|
|
|
2020-04-03 16:45:00 +00:00
|
|
|
removeFromFollowRequests(baseDir, nickname, domain, denyHandle, debug)
|
2019-09-09 08:50:28 +00:00
|
|
|
|
|
|
|
# Store rejected follows
|
2021-11-25 21:18:53 +00:00
|
|
|
try:
|
|
|
|
with open(rejectedFollowsFilename, 'a+') as rejectsFile:
|
|
|
|
rejectsFile.write(denyHandle + '\n')
|
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to append ' + rejectedFollowsFilename)
|
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
|
2019-09-09 12:19:00 +00:00
|
|
|
if ':' in denyDomain:
|
2021-06-23 21:44:31 +00:00
|
|
|
denyPort = getPortFromDomain(denyDomain)
|
2021-06-23 21:31:50 +00:00
|
|
|
denyDomain = removeDomainPort(denyDomain)
|
2020-04-03 16:45:00 +00:00
|
|
|
followedAccountRejects(session, baseDir, httpPrefix,
|
|
|
|
nickname, domain, port,
|
|
|
|
denyNickname, denyDomain, denyPort,
|
|
|
|
federationList,
|
|
|
|
sendThreads, postLog,
|
|
|
|
cachedWebfingers, personCache,
|
2021-08-31 14:17:11 +00:00
|
|
|
debug, projectVersion,
|
|
|
|
signingPrivateKeyPem)
|
2020-04-03 16:45:00 +00:00
|
|
|
|
|
|
|
print('Follow request from ' + denyHandle + ' was denied.')
|
2019-09-09 08:50:28 +00:00
|
|
|
|
2020-03-22 21:16:02 +00:00
|
|
|
|
2021-10-23 11:58:38 +00:00
|
|
|
def manualDenyFollowRequestThread(session, baseDir: str,
|
|
|
|
httpPrefix: str,
|
|
|
|
nickname: str, domain: str, port: int,
|
|
|
|
denyHandle: str,
|
|
|
|
federationList: [],
|
|
|
|
sendThreads: [], postLog: [],
|
|
|
|
cachedWebfingers: {}, personCache: {},
|
|
|
|
debug: bool,
|
|
|
|
projectVersion: str,
|
|
|
|
signingPrivateKeyPem: str) -> None:
|
|
|
|
"""Manually deny a follow request, within a thread so that the
|
|
|
|
user interface doesn't lag
|
|
|
|
"""
|
|
|
|
thr = \
|
|
|
|
threadWithTrace(target=manualDenyFollowRequest,
|
|
|
|
args=(session, baseDir,
|
|
|
|
httpPrefix,
|
|
|
|
nickname, domain, port,
|
|
|
|
denyHandle,
|
|
|
|
federationList,
|
|
|
|
sendThreads, postLog,
|
|
|
|
cachedWebfingers, personCache,
|
|
|
|
debug,
|
|
|
|
projectVersion,
|
|
|
|
signingPrivateKeyPem), daemon=True)
|
|
|
|
thr.start()
|
|
|
|
sendThreads.append(thr)
|
|
|
|
|
|
|
|
|
2020-12-22 18:06:23 +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():
|
2021-11-25 21:18:53 +00:00
|
|
|
try:
|
|
|
|
with open(approvedFilename, 'a+') as approvedFile:
|
|
|
|
approvedFile.write(approveHandle + '\n')
|
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to append ' + approvedFilename)
|
2019-12-31 09:23:41 +00:00
|
|
|
else:
|
2021-11-25 21:18:53 +00:00
|
|
|
try:
|
|
|
|
with open(approvedFilename, 'w+') as approvedFile:
|
|
|
|
approvedFile.write(approveHandle + '\n')
|
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to write ' + approvedFilename)
|
2020-03-22 21:16:02 +00:00
|
|
|
|
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,
|
2021-08-31 14:17:11 +00:00
|
|
|
projectVersion: str,
|
|
|
|
signingPrivateKeyPem: 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
|
2020-10-24 11:07:22 +00:00
|
|
|
|
2019-12-29 12:59:13 +00:00
|
|
|
# is the handle in the requests file?
|
2021-06-21 22:52:04 +00:00
|
|
|
approveFollowsStr = ''
|
|
|
|
with open(approveFollowsFilename, 'r') as fpFollowers:
|
|
|
|
approveFollowsStr = fpFollowers.read()
|
2020-10-28 10:02:02 +00:00
|
|
|
exists = False
|
2020-10-24 11:07:22 +00:00
|
|
|
approveHandleFull = approveHandle
|
2020-10-28 10:02:02 +00:00
|
|
|
if approveHandle in approveFollowsStr:
|
|
|
|
exists = True
|
2020-10-24 11:07:22 +00:00
|
|
|
elif '@' in approveHandle:
|
2021-07-31 12:24:46 +00:00
|
|
|
groupAccount = False
|
|
|
|
if approveHandle.startswith('!'):
|
|
|
|
groupAccount = True
|
|
|
|
reqNick = approveHandle.split('@')[0].replace('!', '')
|
2020-10-24 11:07:22 +00:00
|
|
|
reqDomain = approveHandle.split('@')[1].strip()
|
|
|
|
reqPrefix = httpPrefix + '://' + reqDomain
|
2021-07-04 22:58:01 +00:00
|
|
|
paths = getUserPaths()
|
2021-07-03 20:15:34 +00:00
|
|
|
for userPath in paths:
|
|
|
|
if reqPrefix + userPath + reqNick in approveFollowsStr:
|
|
|
|
exists = True
|
|
|
|
approveHandleFull = reqPrefix + userPath + reqNick
|
2021-07-31 12:24:46 +00:00
|
|
|
if groupAccount:
|
|
|
|
approveHandleFull = '!' + approveHandleFull
|
2021-07-03 20:15:34 +00:00
|
|
|
break
|
2020-10-24 11:07:22 +00:00
|
|
|
if not exists:
|
2020-10-28 10:02:02 +00:00
|
|
|
print('Manual follow accept: ' + approveHandleFull +
|
|
|
|
' not in requests file "' +
|
|
|
|
approveFollowsStr.replace('\n', ' ') +
|
|
|
|
'" ' + approveFollowsFilename)
|
2019-08-07 13:05:09 +00:00
|
|
|
return
|
2020-03-22 21:16:02 +00:00
|
|
|
|
2021-06-22 12:27:10 +00:00
|
|
|
with open(approveFollowsFilename + '.new', 'w+') as approvefilenew:
|
|
|
|
updateApprovedFollowers = False
|
|
|
|
followActivityfilename = None
|
|
|
|
with open(approveFollowsFilename, 'r') as approvefile:
|
|
|
|
for handleOfFollowRequester in approvefile:
|
|
|
|
# is this the approved follow?
|
|
|
|
if handleOfFollowRequester.startswith(approveHandleFull):
|
|
|
|
handleOfFollowRequester = \
|
|
|
|
handleOfFollowRequester.replace('\n', '')
|
|
|
|
handleOfFollowRequester = \
|
|
|
|
handleOfFollowRequester.replace('\r', '')
|
|
|
|
port2 = port
|
|
|
|
if ':' in handleOfFollowRequester:
|
2021-06-23 21:44:31 +00:00
|
|
|
port2 = getPortFromDomain(handleOfFollowRequester)
|
2021-06-22 12:27:10 +00:00
|
|
|
requestsDir = accountDir + '/requests'
|
|
|
|
followActivityfilename = \
|
|
|
|
requestsDir + '/' + handleOfFollowRequester + '.follow'
|
|
|
|
if os.path.isfile(followActivityfilename):
|
|
|
|
followJson = loadJson(followActivityfilename)
|
|
|
|
if followJson:
|
|
|
|
approveNickname = approveHandle.split('@')[0]
|
|
|
|
approveDomain = approveHandle.split('@')[1]
|
|
|
|
approveDomain = \
|
|
|
|
approveDomain.replace('\n', '')
|
|
|
|
approveDomain = \
|
|
|
|
approveDomain.replace('\r', '')
|
|
|
|
approvePort = port2
|
|
|
|
if ':' in approveDomain:
|
2021-06-23 21:44:31 +00:00
|
|
|
approvePort = getPortFromDomain(approveDomain)
|
2021-06-23 21:31:50 +00:00
|
|
|
approveDomain = removeDomainPort(approveDomain)
|
2021-06-22 12:27:10 +00:00
|
|
|
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,
|
|
|
|
followJson,
|
|
|
|
sendThreads, postLog,
|
|
|
|
cachedWebfingers,
|
|
|
|
personCache,
|
|
|
|
debug,
|
2021-08-31 14:17:11 +00:00
|
|
|
projectVersion, False,
|
|
|
|
signingPrivateKeyPem)
|
2021-06-22 12:27:10 +00:00
|
|
|
updateApprovedFollowers = True
|
|
|
|
else:
|
|
|
|
# this isn't the approved follow so it will remain
|
|
|
|
# in the requests file
|
|
|
|
approvefilenew.write(handleOfFollowRequester)
|
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):
|
2020-10-24 11:07:22 +00:00
|
|
|
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()
|
2020-12-29 20:22:28 +00:00
|
|
|
if approveHandleFull + '\n' not in content:
|
|
|
|
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:
|
2020-10-24 11:07:22 +00:00
|
|
|
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 ' +
|
2020-10-24 11:07:22 +00:00
|
|
|
handle + ' is ' + approveHandleFull)
|
2021-11-25 21:18:53 +00:00
|
|
|
try:
|
|
|
|
with open(followersFilename, 'w+') as followersFile:
|
|
|
|
followersFile.write(approveHandleFull + '\n')
|
|
|
|
except OSError:
|
2021-11-25 22:22:54 +00:00
|
|
|
print('EX: unable to write ' + followersFilename)
|
2019-12-29 12:59:13 +00:00
|
|
|
|
|
|
|
# only update the follow requests file if the follow is confirmed to be
|
|
|
|
# in followers.txt
|
2020-10-24 11:07:22 +00:00
|
|
|
if approveHandleFull in open(followersFilename).read():
|
2019-12-31 09:23:41 +00:00
|
|
|
# mark this handle as approved for following
|
2020-12-22 18:06:23 +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)
|
2019-12-29 13:19:51 +00:00
|
|
|
# remove the .follow file
|
|
|
|
if followActivityfilename:
|
|
|
|
if os.path.isfile(followActivityfilename):
|
2021-09-05 10:17:43 +00:00
|
|
|
try:
|
|
|
|
os.remove(followActivityfilename)
|
2021-11-25 18:42:38 +00:00
|
|
|
except OSError:
|
2021-10-29 18:48:15 +00:00
|
|
|
print('EX: manualApproveFollowRequest unable to delete ' +
|
|
|
|
followActivityfilename)
|
2019-12-29 12:59:13 +00:00
|
|
|
else:
|
2021-09-05 10:17:43 +00:00
|
|
|
try:
|
|
|
|
os.remove(approveFollowsFilename + '.new')
|
2021-11-25 18:42:38 +00:00
|
|
|
except OSError:
|
2021-10-29 18:48:15 +00:00
|
|
|
print('EX: manualApproveFollowRequest unable to delete ' +
|
|
|
|
approveFollowsFilename + '.new')
|
2021-10-23 11:58:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def manualApproveFollowRequestThread(session, baseDir: str,
|
|
|
|
httpPrefix: str,
|
|
|
|
nickname: str, domain: str, port: int,
|
|
|
|
approveHandle: str,
|
|
|
|
federationList: [],
|
|
|
|
sendThreads: [], postLog: [],
|
|
|
|
cachedWebfingers: {}, personCache: {},
|
|
|
|
debug: bool,
|
|
|
|
projectVersion: str,
|
|
|
|
signingPrivateKeyPem: str) -> None:
|
|
|
|
"""Manually approve a follow request, in a thread so as not to cause
|
|
|
|
the UI to lag
|
|
|
|
"""
|
|
|
|
thr = \
|
|
|
|
threadWithTrace(target=manualApproveFollowRequest,
|
|
|
|
args=(session, baseDir,
|
|
|
|
httpPrefix,
|
|
|
|
nickname, domain, port,
|
|
|
|
approveHandle,
|
|
|
|
federationList,
|
|
|
|
sendThreads, postLog,
|
|
|
|
cachedWebfingers, personCache,
|
|
|
|
debug,
|
|
|
|
projectVersion,
|
|
|
|
signingPrivateKeyPem), daemon=True)
|
|
|
|
thr.start()
|
|
|
|
sendThreads.append(thr)
|