epicyon/follow.py

1552 lines
59 KiB
Python
Raw Normal View History

2020-04-03 11:38:44 +00:00
__filename__ = "follow.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-04-03 11:38:44 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-03 11:38:44 +00:00
__status__ = "Production"
2021-06-15 15:08:12 +00:00
__module_group__ = "ActivityPub"
2020-04-03 11:38:44 +00:00
2019-06-29 18:23:13 +00:00
from pprint import pprint
import os
2021-06-26 14:21:24 +00:00
from utils import removeDomainPort
from utils import hasObjectDict
2020-12-23 10:57:44 +00:00
from utils import hasUsersPath
2020-12-16 10:30:54 +00:00
from utils import getFullDomain
2020-10-23 19:05:17 +00:00
from utils import isSystemAccount
2020-09-25 14:14:59 +00:00
from utils import getFollowersList
2019-07-27 22:48:34 +00:00
from utils import validNickname
2019-07-02 10:39:55 +00:00
from utils import domainPermitted
2019-07-06 15:17:21 +00:00
from utils import getDomainFromActor
from utils import getNicknameFromActor
2019-07-06 19:24:52 +00:00
from utils import getStatusNumber
from utils import followPerson
2019-07-05 18:57:19 +00:00
from posts import sendSignedJson
2019-07-16 21:38:06 +00:00
from posts import getPersonBox
2019-10-22 11:55:06 +00:00
from utils import loadJson
from utils import saveJson
from utils import isAccountDir
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-07-31 11:56:28 +00:00
from utils import hasGroupType
from utils import isGroupAccount
2021-08-14 11:13:39 +00:00
from utils import localActorUrl
2019-07-06 13:49:25 +00:00
from acceptreject import createAccept
from acceptreject import createReject
2019-07-16 21:38:06 +00:00
from webfinger import webfingerHandle
from auth import createBasicAuthHeader
2021-03-24 12:43:24 +00:00
from session import getJson
2019-07-16 21:38:06 +00:00
from session import postJson
2021-07-31 11:56:28 +00:00
from cache import getPersonPubKey
2019-06-29 18:23:13 +00:00
2020-04-03 11:38:44 +00:00
2020-12-13 22:01:10 +00:00
def createInitialLastSeen(baseDir: str, httpPrefix: str) -> None:
2021-06-07 08:59:43 +00:00
"""Creates initial lastseen files for all follows.
The lastseen files are used to generate the Zzz icons on
follows/following lists on the profile screen.
2020-12-13 22:01:10 +00:00
"""
for subdir, dirs, files in os.walk(baseDir + '/accounts'):
for acct in dirs:
if not isAccountDir(acct):
2020-12-13 22:01:10 +00:00
continue
accountDir = os.path.join(baseDir + '/accounts', acct)
followingFilename = accountDir + '/following.txt'
if not os.path.isfile(followingFilename):
continue
lastSeenDir = accountDir + '/lastseen'
if not os.path.isdir(lastSeenDir):
os.mkdir(lastSeenDir)
with open(followingFilename, 'r') as fp:
followingHandles = fp.readlines()
2020-12-13 22:01:10 +00:00
for handle in followingHandles:
if '#' in handle:
continue
if '@' not in handle:
continue
2020-12-13 22:24:02 +00:00
handle = handle.replace('\n', '')
2020-12-13 22:01:10 +00:00
nickname = handle.split('@')[0]
2020-12-13 22:23:39 +00:00
domain = handle.split('@')[1]
2021-07-30 16:06:34 +00:00
if nickname.startswith('!'):
nickname = nickname[1:]
2021-08-14 11:13:39 +00:00
actor = localActorUrl(httpPrefix, nickname, domain)
2020-12-13 22:01:10 +00:00
lastSeenFilename = \
lastSeenDir + '/' + actor.replace('/', '#') + '.txt'
if not os.path.isfile(lastSeenFilename):
with open(lastSeenFilename, 'w+') as fp:
fp.write(str(100))
2020-12-13 22:01:10 +00:00
break
def _preApprovedFollower(baseDir: str,
nickname: str, domain: str,
approveHandle: str) -> bool:
2019-12-31 09:23:41 +00:00
"""Is the given handle an already manually approved follower?
"""
2020-04-03 11:38:44 +00:00
handle = nickname + '@' + domain
accountDir = baseDir + '/accounts/' + handle
approvedFilename = accountDir + '/approved.txt'
2019-12-31 09:23:41 +00:00
if os.path.isfile(approvedFilename):
if approveHandle in open(approvedFilename).read():
return True
return False
2020-04-03 11:38:44 +00:00
def _removeFromFollowBase(baseDir: str,
nickname: str, domain: str,
acceptOrDenyHandle: str, followFile: str,
debug: bool) -> None:
"""Removes a handle/actor from follow requests or rejects file
2019-09-18 17:04:19 +00:00
"""
2020-04-03 11:38:44 +00:00
handle = nickname + '@' + domain
accountsDir = baseDir + '/accounts/' + handle
approveFollowsFilename = accountsDir + '/' + followFile + '.txt'
2019-09-18 17:04:19 +00:00
if not os.path.isfile(approveFollowsFilename):
if debug:
2020-04-03 11:38:44 +00:00
print('WARN: Approve follow requests file ' +
approveFollowsFilename + ' not found')
2019-09-18 17:04:19 +00:00
return
acceptDenyActor = None
2019-10-06 09:48:37 +00:00
if acceptOrDenyHandle not in open(approveFollowsFilename).read():
# is this stored in the file as an actor rather than a handle?
acceptDenyNickname = acceptOrDenyHandle.split('@')[0]
acceptDenyDomain = acceptOrDenyHandle.split('@')[1]
# for each possible users path construct an actor and
# check if it exists in teh file
usersPaths = getUserPaths()
actorFound = False
for usersName in usersPaths:
acceptDenyActor = \
'://' + acceptDenyDomain + usersName + acceptDenyNickname
if acceptDenyActor in open(approveFollowsFilename).read():
actorFound = True
break
if not actorFound:
return
2021-06-22 12:27:10 +00:00
with open(approveFollowsFilename + '.new', 'w+') as approvefilenew:
with open(approveFollowsFilename, 'r') as approvefile:
if not acceptDenyActor:
for approveHandle in approvefile:
if not approveHandle.startswith(acceptOrDenyHandle):
approvefilenew.write(approveHandle)
else:
for approveHandle in approvefile:
if acceptDenyActor not in approveHandle:
approvefilenew.write(approveHandle)
2020-04-03 11:38:44 +00:00
os.rename(approveFollowsFilename + '.new', approveFollowsFilename)
2019-09-18 17:04:19 +00:00
2020-04-03 11:38:44 +00:00
def removeFromFollowRequests(baseDir: str,
nickname: str, domain: str,
denyHandle: str, debug: bool) -> None:
2019-10-06 09:48:37 +00:00
"""Removes a handle from follow requests
"""
_removeFromFollowBase(baseDir, nickname, domain,
denyHandle, 'followrequests', debug)
2019-10-06 09:48:37 +00:00
2020-04-03 11:38:44 +00:00
def _removeFromFollowRejects(baseDir: str,
nickname: str, domain: str,
acceptHandle: str, debug: bool) -> None:
2019-10-06 09:48:37 +00:00
"""Removes a handle from follow rejects
"""
_removeFromFollowBase(baseDir, nickname, domain,
acceptHandle, 'followrejects', debug)
2020-04-03 11:38:44 +00:00
2019-10-06 09:48:37 +00:00
2020-04-03 11:38:44 +00:00
def isFollowingActor(baseDir: str,
nickname: str, domain: str, actor: str) -> bool:
2020-07-14 09:09:33 +00:00
"""Is the given nickname following the given actor?
The actor can also be a handle: nickname@domain
2019-07-29 19:46:30 +00:00
"""
domain = removeDomainPort(domain)
2020-04-03 11:38:44 +00:00
handle = nickname + '@' + domain
if not os.path.isdir(baseDir + '/accounts/' + handle):
2019-07-29 19:46:30 +00:00
return False
2020-04-03 11:38:44 +00:00
followingFile = baseDir + '/accounts/' + handle + '/following.txt'
2019-08-31 12:25:42 +00:00
if not os.path.isfile(followingFile):
2019-07-29 19:46:30 +00:00
return False
2020-07-14 20:55:47 +00:00
if actor.lower() in open(followingFile).read().lower():
2019-07-29 19:46:30 +00:00
return True
2020-04-03 11:38:44 +00:00
followingNickname = getNicknameFromActor(actor)
2019-09-02 09:43:43 +00:00
if not followingNickname:
2020-04-03 11:38:44 +00:00
print('WARN: unable to find nickname in ' + actor)
2019-09-02 09:43:43 +00:00
return False
2020-04-03 11:38:44 +00:00
followingDomain, followingPort = getDomainFromActor(actor)
2020-12-16 10:30:54 +00:00
followingHandle = \
getFullDomain(followingNickname + '@' + followingDomain, followingPort)
2020-07-14 20:55:47 +00:00
if followingHandle.lower() in open(followingFile).read().lower():
2019-07-29 19:46:30 +00:00
return True
return False
2020-04-03 11:38:44 +00:00
def getMutualsOfPerson(baseDir: str,
2020-09-25 10:20:58 +00:00
nickname: str, domain: str) -> []:
2020-01-13 16:06:31 +00:00
"""Returns the mutuals of a person
i.e. accounts which they follow and which also follow back
"""
2020-04-03 11:38:44 +00:00
followers = \
2020-09-25 14:14:59 +00:00
getFollowersList(baseDir, nickname, domain, 'followers.txt')
2020-04-03 11:38:44 +00:00
following = \
2020-09-25 14:14:59 +00:00
getFollowersList(baseDir, nickname, domain, 'following.txt')
2020-04-03 11:38:44 +00:00
mutuals = []
2020-01-13 16:06:31 +00:00
for handle in following:
if handle in followers:
mutuals.append(handle)
return mutuals
2020-04-03 11:38:44 +00:00
def followerOfPerson(baseDir: str, nickname: str, domain: str,
followerNickname: str, followerDomain: str,
2021-07-30 16:06:34 +00:00
federationList: [], debug: bool,
groupAccount: bool) -> bool:
"""Adds a follower of the given person
"""
2020-04-03 11:38:44 +00:00
return followPerson(baseDir, nickname, domain,
followerNickname, followerDomain,
2021-07-30 16:06:34 +00:00
federationList, debug, groupAccount, 'followers.txt')
2020-04-03 11:38:44 +00:00
2019-06-29 18:23:13 +00:00
def isFollowerOfPerson(baseDir: str, nickname: str, domain: str,
followerNickname: str, followerDomain: str) -> bool:
2019-08-31 15:17:07 +00:00
"""is the given nickname a follower of followerNickname?
"""
2021-09-13 13:41:29 +00:00
if not followerDomain:
print('No followerDomain')
return False
if not followerNickname:
print('No followerNickname for ' + followerDomain)
return False
domain = removeDomainPort(domain)
2021-07-13 21:59:53 +00:00
followersFile = acctDir(baseDir, nickname, domain) + '/followers.txt'
2019-08-31 15:17:07 +00:00
if not os.path.isfile(followersFile):
return False
2020-04-03 11:38:44 +00:00
handle = followerNickname + '@' + followerDomain
alreadyFollowing = False
followersStr = ''
with open(followersFile, 'r') as fpFollowers:
followersStr = fpFollowers.read()
if handle in followersStr:
alreadyFollowing = True
2021-07-03 17:51:58 +00:00
else:
2021-07-04 22:58:01 +00:00
paths = getUserPaths()
2021-07-03 17:51:58 +00:00
for userPath in paths:
url = '://' + followerDomain + userPath + followerNickname
if url in followersStr:
alreadyFollowing = True
break
2020-10-24 11:11:14 +00:00
return alreadyFollowing
2019-08-31 15:17:07 +00:00
2020-04-03 11:38:44 +00:00
2020-12-22 13:57:24 +00:00
def unfollowAccount(baseDir: str, nickname: str, domain: str,
followNickname: str, followDomain: str,
2021-07-30 16:06:34 +00:00
debug: bool, groupAccount: bool,
followFile: str = 'following.txt') -> bool:
2019-06-29 18:23:13 +00:00
"""Removes a person to the follow list
"""
domain = removeDomainPort(domain)
2020-04-03 11:38:44 +00:00
handle = nickname + '@' + domain
handleToUnfollow = followNickname + '@' + followDomain
2021-07-30 16:06:34 +00:00
if groupAccount:
handleToUnfollow = '!' + handleToUnfollow
2020-04-03 11:38:44 +00:00
if not os.path.isdir(baseDir + '/accounts'):
os.mkdir(baseDir + '/accounts')
if not os.path.isdir(baseDir + '/accounts/' + handle):
os.mkdir(baseDir + '/accounts/' + handle)
filename = baseDir + '/accounts/' + handle + '/' + followFile
2019-07-17 10:34:00 +00:00
if not os.path.isfile(filename):
2019-07-17 11:54:13 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: follow file ' + filename + ' was not found')
2019-07-17 10:34:00 +00:00
return False
2020-08-20 12:11:07 +00:00
handleToUnfollowLower = handleToUnfollow.lower()
if handleToUnfollowLower not in open(filename).read().lower():
2019-07-17 11:54:13 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: handle to unfollow ' + handleToUnfollow +
' is not in ' + filename)
2019-07-17 10:34:00 +00:00
return
2021-07-13 14:40:49 +00:00
with open(filename, 'r') as f:
2020-04-03 11:38:44 +00:00
lines = f.readlines()
2020-11-06 18:56:53 +00:00
with open(filename, 'w+') as f:
for line in lines:
2021-07-30 16:06:34 +00:00
checkHandle = line.strip("\n").strip("\r").lower()
if checkHandle != handleToUnfollowLower and \
checkHandle != '!' + handleToUnfollowLower:
2020-11-06 18:56:53 +00:00
f.write(line)
# write to an unfollowed file so that if a follow accept
# later arrives then it can be ignored
2020-04-03 11:38:44 +00:00
unfollowedFilename = baseDir + '/accounts/' + handle + '/unfollowed.txt'
if os.path.isfile(unfollowedFilename):
2020-07-14 21:48:39 +00:00
if handleToUnfollowLower not in \
open(unfollowedFilename).read().lower():
2021-07-13 14:40:49 +00:00
with open(unfollowedFilename, 'a+') as f:
f.write(handleToUnfollow + '\n')
else:
2021-07-13 14:40:49 +00:00
with open(unfollowedFilename, 'w+') as f:
f.write(handleToUnfollow + '\n')
2019-10-30 21:44:16 +00:00
return True
2019-06-29 18:23:13 +00:00
2020-04-03 11:38:44 +00:00
2020-12-22 13:57:24 +00:00
def unfollowerOfAccount(baseDir: str, nickname: str, domain: str,
followerNickname: str, followerDomain: str,
2021-07-30 16:06:34 +00:00
debug: bool, groupAccount: bool) -> bool:
"""Remove a follower of a person
"""
2020-12-22 13:57:24 +00:00
return unfollowAccount(baseDir, nickname, domain,
followerNickname, followerDomain,
2021-07-30 16:06:34 +00:00
debug, groupAccount, 'followers.txt')
2019-06-29 18:23:13 +00:00
2020-04-03 11:38:44 +00:00
def clearFollows(baseDir: str, nickname: str, domain: str,
2021-07-30 19:20:49 +00:00
followFile: str = 'following.txt') -> None:
2019-06-29 18:23:13 +00:00
"""Removes all follows
"""
2020-09-15 09:16:03 +00:00
handle = nickname + '@' + domain
2020-04-03 11:38:44 +00:00
if not os.path.isdir(baseDir + '/accounts'):
os.mkdir(baseDir + '/accounts')
if not os.path.isdir(baseDir + '/accounts/' + handle):
os.mkdir(baseDir + '/accounts/' + handle)
filename = baseDir + '/accounts/' + handle + '/' + followFile
2019-06-29 18:23:13 +00:00
if os.path.isfile(filename):
try:
os.remove(filename)
except BaseException:
pass
2019-06-29 18:23:13 +00:00
2020-04-03 11:38:44 +00:00
def clearFollowers(baseDir: str, nickname: str, domain: str) -> None:
"""Removes all followers
"""
2020-04-03 11:38:44 +00:00
clearFollows(baseDir, nickname, domain, 'followers.txt')
2019-06-29 20:21:37 +00:00
def _getNoOfFollows(baseDir: str, nickname: str, domain: str,
authenticated: bool,
followFile='following.txt') -> int:
"""Returns the number of follows or followers
"""
# only show number of followers to authenticated
# account holders
2020-04-03 11:38:44 +00:00
# if not authenticated:
# return 9999
2020-09-15 09:16:03 +00:00
handle = nickname + '@' + domain
2020-04-03 11:38:44 +00:00
filename = baseDir + '/accounts/' + handle + '/' + followFile
2019-06-29 20:21:37 +00:00
if not os.path.isfile(filename):
return 0
2020-04-03 11:38:44 +00:00
ctr = 0
2021-07-13 14:40:49 +00:00
with open(filename, 'r') as f:
2020-04-03 11:38:44 +00:00
lines = f.readlines()
2019-06-29 20:21:37 +00:00
for line in lines:
if '#' in line:
continue
if '@' in line and \
'.' in line and \
not line.startswith('http'):
ctr += 1
elif ((line.startswith('http') or
2021-07-01 17:59:24 +00:00
line.startswith('hyper')) and
2020-12-23 10:57:44 +00:00
hasUsersPath(line)):
ctr += 1
2019-06-29 20:21:37 +00:00
return ctr
2020-04-03 11:38:44 +00:00
def _getNoOfFollowers(baseDir: str,
nickname: str, domain: str, authenticated: bool) -> int:
"""Returns the number of followers of the given person
"""
return _getNoOfFollows(baseDir, nickname, domain,
authenticated, 'followers.txt')
2020-04-03 11:38:44 +00:00
2019-06-29 20:21:37 +00:00
2020-04-03 11:38:44 +00:00
def getFollowingFeed(baseDir: str, domain: str, port: int, path: str,
2021-03-24 13:15:43 +00:00
httpPrefix: str, authorized: bool,
2020-04-03 11:38:44 +00:00
followsPerPage=12,
2019-07-06 17:00:22 +00:00
followFile='following') -> {}:
2020-10-24 09:28:21 +00:00
"""Returns the following and followers feeds from GET requests.
This accesses the following.txt or followers.txt and builds a collection.
"""
2021-03-24 13:15:43 +00:00
# Show a small number of follows to non-authorized viewers
if not authorized:
2020-04-03 11:38:44 +00:00
followsPerPage = 6
2020-04-03 11:38:44 +00:00
if '/' + followFile not in path:
2019-06-29 20:21:37 +00:00
return None
# handle page numbers
2020-04-03 11:38:44 +00:00
headerOnly = True
pageNumber = None
2019-06-29 20:21:37 +00:00
if '?page=' in path:
2020-04-03 11:38:44 +00:00
pageNumber = path.split('?page=')[1]
2021-03-24 13:15:43 +00:00
if pageNumber == 'true' or not authorized:
2020-04-03 11:38:44 +00:00
pageNumber = 1
2019-06-29 20:21:37 +00:00
else:
try:
2020-04-03 11:38:44 +00:00
pageNumber = int(pageNumber)
except BaseException:
2019-06-29 20:21:37 +00:00
pass
2020-04-03 11:38:44 +00:00
path = path.split('?page=')[0]
headerOnly = False
2020-03-22 21:16:02 +00:00
2020-04-03 11:38:44 +00:00
if not path.endswith('/' + followFile):
2019-06-29 20:21:37 +00:00
return None
2020-04-03 11:38:44 +00:00
nickname = None
2019-06-29 20:21:37 +00:00
if path.startswith('/users/'):
2020-04-03 11:38:44 +00:00
nickname = path.replace('/users/', '', 1).replace('/' + followFile, '')
2019-06-29 20:21:37 +00:00
if path.startswith('/@'):
2020-04-03 11:38:44 +00:00
nickname = path.replace('/@', '', 1).replace('/' + followFile, '')
2019-07-03 09:40:27 +00:00
if not nickname:
2019-06-29 20:21:37 +00:00
return None
2020-04-03 11:38:44 +00:00
if not validNickname(domain, nickname):
2019-06-29 20:21:37 +00:00
return None
2020-12-16 10:30:54 +00:00
domain = getFullDomain(domain, port)
2019-06-30 19:01:43 +00:00
2019-06-29 20:21:37 +00:00
if headerOnly:
2020-04-03 11:38:44 +00:00
firstStr = \
2021-08-14 11:13:39 +00:00
localActorUrl(httpPrefix, nickname, domain) + \
'/' + followFile + '?page=1'
2020-04-03 11:38:44 +00:00
idStr = \
2021-08-14 11:13:39 +00:00
localActorUrl(httpPrefix, nickname, domain) + '/' + followFile
2020-04-03 11:38:44 +00:00
totalStr = \
2021-03-24 13:15:43 +00:00
_getNoOfFollows(baseDir, nickname, domain, authorized)
2020-04-03 11:38:44 +00:00
following = {
2019-06-29 20:21:37 +00:00
'@context': 'https://www.w3.org/ns/activitystreams',
2020-04-03 11:38:44 +00:00
'first': firstStr,
'id': idStr,
'totalItems': totalStr,
2020-03-22 20:36:19 +00:00
'type': 'OrderedCollection'
}
2019-06-29 20:21:37 +00:00
return following
if not pageNumber:
2020-04-03 11:38:44 +00:00
pageNumber = 1
nextPageNumber = int(pageNumber + 1)
idStr = \
2021-08-14 11:13:39 +00:00
localActorUrl(httpPrefix, nickname, domain) + \
'/' + followFile + '?page=' + str(pageNumber)
2020-04-03 11:38:44 +00:00
partOfStr = \
2021-08-14 11:13:39 +00:00
localActorUrl(httpPrefix, nickname, domain) + '/' + followFile
2020-04-03 11:38:44 +00:00
following = {
2019-06-29 20:21:37 +00:00
'@context': 'https://www.w3.org/ns/activitystreams',
2020-04-03 11:38:44 +00:00
'id': idStr,
2019-06-29 20:21:37 +00:00
'orderedItems': [],
2020-04-03 11:38:44 +00:00
'partOf': partOfStr,
2019-06-29 20:21:37 +00:00
'totalItems': 0,
2020-03-22 20:36:19 +00:00
'type': 'OrderedCollectionPage'
}
2019-06-29 20:21:37 +00:00
2020-04-03 11:38:44 +00:00
handleDomain = domain
handleDomain = removeDomainPort(handleDomain)
2020-09-15 09:16:03 +00:00
handle = nickname + '@' + handleDomain
2020-04-03 11:38:44 +00:00
filename = baseDir + '/accounts/' + handle + '/' + followFile + '.txt'
2019-06-29 20:21:37 +00:00
if not os.path.isfile(filename):
return following
2020-04-03 11:38:44 +00:00
currPage = 1
pageCtr = 0
totalCtr = 0
2021-07-13 14:40:49 +00:00
with open(filename, 'r') as f:
2020-04-03 11:38:44 +00:00
lines = f.readlines()
2019-06-29 20:21:37 +00:00
for line in lines:
if '#' not in line:
if '@' in line and not line.startswith('http'):
2020-10-24 09:28:21 +00:00
# nickname@domain
2019-06-29 20:21:37 +00:00
pageCtr += 1
totalCtr += 1
2020-04-03 11:38:44 +00:00
if currPage == pageNumber:
2020-05-22 11:32:38 +00:00
line2 = \
line.lower().replace('\n', '').replace('\r', '')
2021-07-30 19:20:49 +00:00
nick = line2.split('@')[0]
dom = line2.split('@')[1]
if not nick.startswith('!'):
# person actor
2021-08-14 11:13:39 +00:00
url = localActorUrl(httpPrefix, nick, dom)
2021-07-30 19:20:49 +00:00
else:
# group actor
url = httpPrefix + '://' + dom + '/c/' + nick
2019-06-29 20:21:37 +00:00
following['orderedItems'].append(url)
2020-04-03 11:38:44 +00:00
elif ((line.startswith('http') or
2021-07-01 17:59:24 +00:00
line.startswith('hyper')) and
2020-12-23 10:57:44 +00:00
hasUsersPath(line)):
2020-10-24 09:28:21 +00:00
# https://domain/users/nickname
2019-06-29 20:21:37 +00:00
pageCtr += 1
totalCtr += 1
2020-04-03 11:38:44 +00:00
if currPage == pageNumber:
2020-05-22 11:32:38 +00:00
appendStr = \
line.lower().replace('\n', '').replace('\r', '')
2020-04-03 11:38:44 +00:00
following['orderedItems'].append(appendStr)
if pageCtr >= followsPerPage:
pageCtr = 0
2019-06-29 20:21:37 +00:00
currPage += 1
2020-04-03 11:38:44 +00:00
following['totalItems'] = totalCtr
lastPage = int(totalCtr / followsPerPage)
if lastPage < 1:
lastPage = 1
if nextPageNumber > lastPage:
following['next'] = \
2021-08-14 11:13:39 +00:00
localActorUrl(httpPrefix, nickname, domain) + \
'/' + followFile + '?page=' + str(lastPage)
2019-06-29 20:21:37 +00:00
return following
2019-07-02 18:17:04 +00:00
2020-04-03 11:38:44 +00:00
def _followApprovalRequired(baseDir: str, nicknameToFollow: str,
domainToFollow: str, debug: bool,
followRequestHandle: str) -> bool:
2019-07-19 20:03:50 +00:00
""" Returns the policy for follower approvals
"""
2020-01-02 22:42:06 +00:00
# has this handle already been manually approved?
if _preApprovedFollower(baseDir, nicknameToFollow, domainToFollow,
followRequestHandle):
2019-12-31 09:23:41 +00:00
return False
2020-04-03 11:38:44 +00:00
manuallyApproveFollows = False
domainToFollow = removeDomainPort(domainToFollow)
2020-04-03 11:38:44 +00:00
actorFilename = baseDir + '/accounts/' + \
nicknameToFollow + '@' + domainToFollow + '.json'
2019-07-19 20:03:50 +00:00
if os.path.isfile(actorFilename):
2020-04-03 11:38:44 +00:00
actor = loadJson(actorFilename)
2019-09-30 22:39:02 +00:00
if actor:
2019-07-19 20:03:50 +00:00
if actor.get('manuallyApprovesFollowers'):
2020-04-03 11:38:44 +00:00
manuallyApproveFollows = actor['manuallyApprovesFollowers']
2019-07-19 20:03:50 +00:00
else:
if debug:
2020-04-03 11:38:44 +00:00
print(nicknameToFollow + '@' + domainToFollow +
' automatically approves followers')
2019-07-19 20:03:50 +00:00
else:
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: Actor file not found: ' + actorFilename)
2019-07-19 20:03:50 +00:00
return manuallyApproveFollows
2020-04-03 11:38:44 +00:00
def _noOfFollowRequests(baseDir: str,
nicknameToFollow: str, domainToFollow: str,
nickname: str, domain: str, fromPort: int,
followType: str) -> int:
"""Returns the current number of follow requests
"""
2020-04-03 11:38:44 +00:00
accountsDir = baseDir + '/accounts/' + \
nicknameToFollow + '@' + domainToFollow
approveFollowsFilename = accountsDir + '/followrequests.txt'
if not os.path.isfile(approveFollowsFilename):
return 0
2020-04-03 11:38:44 +00:00
ctr = 0
2021-07-13 14:40:49 +00:00
with open(approveFollowsFilename, 'r') as f:
2020-04-03 11:38:44 +00:00
lines = f.readlines()
2020-06-03 20:21:44 +00:00
if followType == "onion":
for fileLine in lines:
if '.onion' in fileLine:
ctr += 1
elif followType == "i2p":
for fileLine in lines:
if '.i2p' in fileLine:
ctr += 1
else:
return len(lines)
return ctr
2020-04-03 11:38:44 +00:00
def _storeFollowRequest(baseDir: str,
nicknameToFollow: str, domainToFollow: str, port: int,
nickname: str, domain: str, fromPort: int,
followJson: {},
debug: bool, personUrl: str,
groupAccount: bool) -> bool:
"""Stores the follow request for later use
"""
2020-04-03 11:38:44 +00:00
accountsDir = baseDir + '/accounts/' + \
nicknameToFollow + '@' + domainToFollow
2019-08-07 11:49:38 +00:00
if not os.path.isdir(accountsDir):
return False
2020-12-16 10:30:54 +00:00
domainFull = getFullDomain(domain, fromPort)
2020-12-16 11:29:35 +00:00
approveHandle = getFullDomain(nickname + '@' + domain, fromPort)
if groupAccount:
approveHandle = '!' + approveHandle
2020-04-03 11:38:44 +00:00
followersFilename = accountsDir + '/followers.txt'
2019-08-26 22:38:09 +00:00
if os.path.isfile(followersFilename):
alreadyFollowing = False
followersStr = ''
with open(followersFilename, 'r') as fpFollowers:
followersStr = fpFollowers.read()
if approveHandle in followersStr:
alreadyFollowing = True
2021-07-30 19:20:49 +00:00
else:
usersPaths = getUserPaths()
for possibleUsersPath in usersPaths:
url = '://' + domainFull + possibleUsersPath + nickname
if url in followersStr:
alreadyFollowing = True
break
if alreadyFollowing:
2019-08-26 22:38:09 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: ' +
nicknameToFollow + '@' + domainToFollow +
' already following ' + approveHandle)
2019-08-26 22:38:09 +00:00
return True
# should this follow be denied?
2020-04-03 11:38:44 +00:00
denyFollowsFilename = accountsDir + '/followrejects.txt'
if os.path.isfile(denyFollowsFilename):
2019-09-18 19:05:08 +00:00
if approveHandle in open(denyFollowsFilename).read():
2020-04-03 11:38:44 +00:00
removeFromFollowRequests(baseDir, nicknameToFollow,
domainToFollow, approveHandle, debug)
print(approveHandle + ' was already denied as a follower of ' +
nicknameToFollow)
return True
# add to a file which contains a list of requests
2020-04-03 11:38:44 +00:00
approveFollowsFilename = accountsDir + '/followrequests.txt'
# store either nick@domain or the full person/actor url
approveHandleStored = approveHandle
if '/users/' not in personUrl:
approveHandleStored = personUrl
if groupAccount:
approveHandle = '!' + approveHandle
if os.path.isfile(approveFollowsFilename):
if approveHandle not in open(approveFollowsFilename).read():
with open(approveFollowsFilename, 'a+') as fp:
fp.write(approveHandleStored + '\n')
else:
if debug:
print('DEBUG: ' + approveHandleStored +
2020-04-03 11:38:44 +00:00
' is already awaiting approval')
else:
2021-07-13 14:40:49 +00:00
with open(approveFollowsFilename, 'w+') as fp:
fp.write(approveHandleStored + '\n')
# store the follow request in its own directory
# We don't rely upon the inbox because items in there could expire
2020-04-03 11:38:44 +00:00
requestsDir = accountsDir + '/requests'
if not os.path.isdir(requestsDir):
os.mkdir(requestsDir)
2020-04-03 11:38:44 +00:00
followActivityfilename = requestsDir + '/' + approveHandle + '.follow'
return saveJson(followJson, followActivityfilename)
def receiveFollowRequest(session, baseDir: str, httpPrefix: str,
port: int, sendThreads: [], postLog: [],
cachedWebfingers: {}, personCache: {},
messageJson: {}, federationList: [],
debug: bool, projectVersion: str,
maxFollowers: int, onionDomain: str,
signingPrivateKeyPem: str) -> bool:
2019-07-02 18:38:51 +00:00
"""Receives a follow request within the POST section of HTTPServer
"""
2019-07-02 18:17:04 +00:00
if not messageJson['type'].startswith('Follow'):
if not messageJson['type'].startswith('Join'):
return False
2019-08-15 16:05:28 +00:00
print('Receiving follow request')
2019-07-06 13:49:25 +00:00
if not messageJson.get('actor'):
if debug:
print('DEBUG: follow request has no actor')
return False
2020-12-23 10:57:44 +00:00
if not hasUsersPath(messageJson['actor']):
2019-07-06 13:49:25 +00:00
if debug:
print('DEBUG: users/profile/accounts/channel missing from actor')
2019-07-02 18:17:04 +00:00
return False
2020-04-03 11:38:44 +00:00
domain, tempPort = getDomainFromActor(messageJson['actor'])
fromPort = port
2020-12-16 11:29:35 +00:00
domainFull = getFullDomain(domain, tempPort)
2019-07-06 15:17:21 +00:00
if tempPort:
2020-04-03 11:38:44 +00:00
fromPort = tempPort
if not domainPermitted(domain, federationList):
2019-07-06 13:49:25 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: follower from domain not permitted - ' + domain)
2019-07-02 18:17:04 +00:00
return False
2020-04-03 11:38:44 +00:00
nickname = getNicknameFromActor(messageJson['actor'])
2019-07-06 15:17:21 +00:00
if not nickname:
# single user instance
2020-04-03 11:38:44 +00:00
nickname = 'dev'
2019-07-06 15:17:21 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: follow request does not contain a ' +
2020-03-02 21:28:22 +00:00
'nickname. Assuming single user instance.')
if not messageJson.get('to'):
2020-04-03 11:38:44 +00:00
messageJson['to'] = messageJson['object']
2020-12-23 10:57:44 +00:00
if not hasUsersPath(messageJson['object']):
2019-07-06 13:49:25 +00:00
if debug:
print('DEBUG: users/profile/channel/accounts ' +
'not found within object')
2019-07-02 18:17:04 +00:00
return False
2020-04-03 11:38:44 +00:00
domainToFollow, tempPort = getDomainFromActor(messageJson['object'])
if not domainPermitted(domainToFollow, federationList):
2019-07-06 13:49:25 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: follow domain not permitted ' + domainToFollow)
return True
2020-12-16 10:30:54 +00:00
domainToFollowFull = getFullDomain(domainToFollow, tempPort)
2020-04-03 11:38:44 +00:00
nicknameToFollow = getNicknameFromActor(messageJson['object'])
2019-07-06 15:17:21 +00:00
if not nicknameToFollow:
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: follow request does not contain a ' +
'nickname for the account followed')
return True
2020-10-23 19:05:17 +00:00
if isSystemAccount(nicknameToFollow):
if debug:
print('DEBUG: Cannot follow system account - ' +
nicknameToFollow)
return True
if maxFollowers > 0:
if _getNoOfFollowers(baseDir,
nicknameToFollow, domainToFollow,
True) > maxFollowers:
print('WARN: ' + nicknameToFollow +
' has reached their maximum number of followers')
return True
2020-04-03 11:38:44 +00:00
handleToFollow = nicknameToFollow + '@' + domainToFollow
if domainToFollow == domain:
if not os.path.isdir(baseDir + '/accounts/' + handleToFollow):
2019-07-06 13:49:25 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: followed account not found - ' +
baseDir + '/accounts/' + handleToFollow)
return True
2020-03-22 21:16:02 +00:00
if isFollowerOfPerson(baseDir,
nicknameToFollow, domainToFollowFull,
nickname, domainFull):
2019-07-06 13:49:25 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: ' + nickname + '@' + domain +
' is already a follower of ' +
nicknameToFollow + '@' + domainToFollow)
return True
2020-03-22 21:16:02 +00:00
2019-07-19 20:03:50 +00:00
# what is the followers policy?
2020-04-03 11:38:44 +00:00
approveHandle = nickname + '@' + domainFull
if _followApprovalRequired(baseDir, nicknameToFollow,
domainToFollow, debug, approveHandle):
2020-02-19 12:51:14 +00:00
print('Follow approval is required')
2020-06-03 20:21:44 +00:00
if domain.endswith('.onion'):
if _noOfFollowRequests(baseDir,
nicknameToFollow, domainToFollow,
nickname, domain, fromPort,
'onion') > 5:
2020-06-03 20:21:44 +00:00
print('Too many follow requests from onion addresses')
return False
elif domain.endswith('.i2p'):
if _noOfFollowRequests(baseDir,
nicknameToFollow, domainToFollow,
nickname, domain, fromPort,
'i2p') > 5:
2020-06-03 20:21:44 +00:00
print('Too many follow requests from i2p addresses')
return False
else:
if _noOfFollowRequests(baseDir,
nicknameToFollow, domainToFollow,
nickname, domain, fromPort,
'') > 10:
2020-06-03 20:21:44 +00:00
print('Too many follow requests')
return False
# Get the actor for the follower and add it to the cache.
# Getting their public key has the same result
if debug:
print('Obtaining the following actor: ' + messageJson['actor'])
if not getPersonPubKey(baseDir, session, messageJson['actor'],
personCache, debug, projectVersion,
httpPrefix, domainToFollow, onionDomain,
signingPrivateKeyPem):
if debug:
print('Unable to obtain following actor: ' +
messageJson['actor'])
groupAccount = \
2021-08-12 17:13:18 +00:00
hasGroupType(baseDir, messageJson['actor'], personCache)
if groupAccount and isGroupAccount(baseDir, nickname, domain):
print('Group cannot follow a group')
return False
2019-08-15 16:05:28 +00:00
print('Storing follow request for approval')
return _storeFollowRequest(baseDir,
nicknameToFollow, domainToFollow, port,
nickname, domain, fromPort,
messageJson, debug, messageJson['actor'],
groupAccount)
2019-08-31 14:42:35 +00:00
else:
2021-08-02 17:05:08 +00:00
print('Follow request does not require approval ' + approveHandle)
2019-09-01 20:28:43 +00:00
# update the followers
2021-07-31 11:56:28 +00:00
accountToBeFollowed = \
acctDir(baseDir, nicknameToFollow, domainToFollow)
if os.path.isdir(accountToBeFollowed):
followersFilename = accountToBeFollowed + '/followers.txt'
# for actors which don't follow the mastodon
# /users/ path convention store the full actor
if '/users/' not in messageJson['actor']:
approveHandle = messageJson['actor']
2021-07-31 11:56:28 +00:00
# Get the actor for the follower and add it to the cache.
# Getting their public key has the same result
if debug:
print('Obtaining the following actor: ' + messageJson['actor'])
if not getPersonPubKey(baseDir, session, messageJson['actor'],
personCache, debug, projectVersion,
httpPrefix, domainToFollow, onionDomain,
signingPrivateKeyPem):
2021-07-31 11:56:28 +00:00
if debug:
print('Unable to obtain following actor: ' +
messageJson['actor'])
2020-04-03 11:38:44 +00:00
print('Updating followers file: ' +
followersFilename + ' adding ' + approveHandle)
2019-09-01 20:28:43 +00:00
if os.path.isfile(followersFilename):
if approveHandle not in open(followersFilename).read():
2021-07-31 11:56:28 +00:00
groupAccount = \
2021-08-12 17:13:18 +00:00
hasGroupType(baseDir,
messageJson['actor'], personCache)
2021-08-01 13:25:11 +00:00
if debug:
print(approveHandle + ' / ' + messageJson['actor'] +
' is Group: ' + str(groupAccount))
if groupAccount and \
isGroupAccount(baseDir, nickname, domain):
print('Group cannot follow a group')
return False
2019-10-26 15:15:38 +00:00
try:
with open(followersFilename, 'r+') as followersFile:
2020-04-03 11:38:44 +00:00
content = followersFile.read()
if approveHandle + '\n' not in content:
followersFile.seek(0, 0)
2021-07-30 19:20:49 +00:00
if not groupAccount:
followersFile.write(approveHandle +
'\n' + content)
else:
followersFile.write('!' + approveHandle +
'\n' + content)
2019-10-26 15:15:38 +00:00
except Exception as e:
2020-04-03 11:38:44 +00:00
print('WARN: ' +
'Failed to write entry to followers file ' +
str(e))
2019-09-01 20:28:43 +00:00
else:
2021-06-22 12:27:10 +00:00
with open(followersFilename, 'w+') as followersFile:
followersFile.write(approveHandle + '\n')
2019-08-15 16:05:28 +00:00
print('Beginning follow accept')
2020-04-03 11:38:44 +00:00
return followedAccountAccepts(session, baseDir, httpPrefix,
nicknameToFollow, domainToFollow, port,
nickname, domain, fromPort,
messageJson['actor'], federationList,
2020-09-27 19:27:24 +00:00
messageJson, sendThreads, postLog,
2020-04-03 11:38:44 +00:00
cachedWebfingers, personCache,
debug, projectVersion, True,
signingPrivateKeyPem)
2020-04-03 11:38:44 +00:00
def followedAccountAccepts(session, baseDir: str, httpPrefix: str,
nicknameToFollow: str, domainToFollow: str,
port: int,
nickname: str, domain: str, fromPort: int,
personUrl: str, federationList: [],
2020-09-27 19:27:24 +00:00
followJson: {}, sendThreads: [], postLog: [],
2020-04-03 11:38:44 +00:00
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str,
removeFollowActivity: bool,
signingPrivateKeyPem: str):
2019-07-20 08:33:18 +00:00
"""The person receiving a follow request accepts the new follower
and sends back an Accept activity
"""
2020-04-03 11:38:44 +00:00
acceptHandle = nickname + '@' + domain
2019-10-06 09:57:49 +00:00
2019-07-05 18:57:19 +00:00
# send accept back
2019-07-06 13:49:25 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: sending Accept activity for ' +
'follow request which arrived at ' +
nicknameToFollow + '@' + domainToFollow +
' back to ' + acceptHandle)
acceptJson = createAccept(baseDir, federationList,
nicknameToFollow, domainToFollow, port,
personUrl, '', httpPrefix,
2020-09-27 19:27:24 +00:00
followJson)
2019-07-06 15:17:21 +00:00
if debug:
pprint(acceptJson)
2020-04-03 11:38:44 +00:00
print('DEBUG: sending follow Accept from ' +
nicknameToFollow + '@' + domainToFollow +
' port ' + str(port) + ' to ' +
acceptHandle + ' port ' + str(fromPort))
clientToServer = False
2019-12-16 10:19:21 +00:00
if removeFollowActivity:
# remove the follow request json
2020-04-03 11:38:44 +00:00
followActivityfilename = \
2021-07-13 21:59:53 +00:00
acctDir(baseDir, nicknameToFollow, domainToFollow) + \
'/requests/' + \
2020-04-03 11:38:44 +00:00
nickname + '@' + domain + '.follow'
if os.path.isfile(followActivityfilename):
try:
os.remove(followActivityfilename)
2020-04-03 11:38:44 +00:00
except BaseException:
pass
2019-12-16 10:19:21 +00:00
groupAccount = False
if followJson:
if followJson.get('actor'):
if hasGroupType(baseDir, followJson['actor'], personCache):
groupAccount = True
2020-04-03 11:38:44 +00:00
return sendSignedJson(acceptJson, session, baseDir,
nicknameToFollow, domainToFollow, port,
nickname, domain, fromPort, '',
httpPrefix, True, clientToServer,
federationList,
sendThreads, postLog, cachedWebfingers,
personCache, debug, projectVersion, None,
2021-09-20 16:05:11 +00:00
groupAccount, signingPrivateKeyPem,
7856837)
2020-04-03 11:38:44 +00:00
def followedAccountRejects(session, baseDir: str, httpPrefix: str,
nicknameToFollow: str, domainToFollow: str,
port: int,
nickname: str, domain: str, fromPort: int,
federationList: [],
sendThreads: [], postLog: [],
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str,
signingPrivateKeyPem: str):
"""The person receiving a follow request rejects the new follower
and sends back a Reject activity
"""
# send reject back
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: sending Reject activity for ' +
'follow request which arrived at ' +
nicknameToFollow + '@' + domainToFollow +
' back to ' + nickname + '@' + domain)
2019-12-16 10:01:57 +00:00
# get the json for the original follow request
2020-04-03 11:38:44 +00:00
followActivityfilename = \
2021-07-13 21:59:53 +00:00
acctDir(baseDir, nicknameToFollow, domainToFollow) + '/requests/' + \
2020-04-03 11:38:44 +00:00
nickname + '@' + domain + '.follow'
followJson = loadJson(followActivityfilename)
2019-12-16 10:01:57 +00:00
if not followJson:
2020-04-03 11:38:44 +00:00
print('No follow request json was found for ' +
2019-12-16 10:19:21 +00:00
followActivityfilename)
2019-12-16 10:01:57 +00:00
return None
# actor who made the follow request
2020-04-03 11:38:44 +00:00
personUrl = followJson['actor']
2019-12-16 10:01:57 +00:00
# create the reject activity
2020-04-03 11:38:44 +00:00
rejectJson = \
createReject(baseDir, federationList,
nicknameToFollow, domainToFollow, port,
personUrl, '', httpPrefix, followJson)
if debug:
pprint(rejectJson)
2020-04-03 11:38:44 +00:00
print('DEBUG: sending follow Reject from ' +
nicknameToFollow + '@' + domainToFollow +
' port ' + str(port) + ' to ' +
nickname + '@' + domain + ' port ' + str(fromPort))
clientToServer = False
2020-12-16 10:30:54 +00:00
denyHandle = getFullDomain(nickname + '@' + domain, fromPort)
groupAccount = False
if hasGroupType(baseDir, personUrl, personCache):
groupAccount = True
# remove from the follow requests file
2020-04-03 11:38:44 +00:00
removeFromFollowRequests(baseDir, nicknameToFollow, domainToFollow,
denyHandle, debug)
# remove the follow request json
try:
2019-12-16 10:19:21 +00:00
os.remove(followActivityfilename)
2020-04-03 11:38:44 +00:00
except BaseException:
pass
# send the reject activity
2020-04-03 11:38:44 +00:00
return sendSignedJson(rejectJson, session, baseDir,
nicknameToFollow, domainToFollow, port,
nickname, domain, fromPort, '',
httpPrefix, True, clientToServer,
federationList,
sendThreads, postLog, cachedWebfingers,
personCache, debug, projectVersion, None,
2021-09-20 16:05:11 +00:00
groupAccount, signingPrivateKeyPem,
6393063)
2020-04-03 11:38:44 +00:00
def sendFollowRequest(session, baseDir: str,
nickname: str, domain: str, port: int, httpPrefix: str,
followNickname: str, followDomain: str,
followedActor: str,
2020-04-03 11:38:44 +00:00
followPort: int, followHttpPrefix: str,
clientToServer: bool, federationList: [],
sendThreads: [], postLog: [], cachedWebfingers: {},
personCache: {}, debug: bool,
projectVersion: str, signingPrivateKeyPem: str) -> {}:
2019-07-02 20:54:22 +00:00
"""Gets the json object for sending a follow request
2020-03-22 21:16:02 +00:00
"""
2021-09-20 16:14:24 +00:00
if not signingPrivateKeyPem:
print('WARN: follow request without signing key')
2020-04-03 11:38:44 +00:00
if not domainPermitted(followDomain, federationList):
print('You are not permitted to follow the domain ' + followDomain)
2019-07-02 18:38:51 +00:00
return None
2020-03-22 21:16:02 +00:00
2020-12-16 10:30:54 +00:00
fullDomain = getFullDomain(domain, port)
2021-08-14 11:13:39 +00:00
followActor = localActorUrl(httpPrefix, nickname, fullDomain)
2019-07-02 18:38:51 +00:00
2020-12-16 10:30:54 +00:00
requestDomain = getFullDomain(followDomain, followPort)
2019-07-06 10:33:57 +00:00
2020-04-03 11:38:44 +00:00
statusNumber, published = getStatusNumber()
2020-03-22 21:16:02 +00:00
2021-07-30 19:20:49 +00:00
groupAccount = False
2019-10-21 14:12:22 +00:00
if followNickname:
followedId = followedActor
2020-04-03 11:38:44 +00:00
followHandle = followNickname + '@' + requestDomain
2021-07-31 11:56:28 +00:00
groupAccount = hasGroupType(baseDir, followedActor, personCache)
2021-07-30 19:20:49 +00:00
if groupAccount:
followHandle = '!' + followHandle
2021-08-02 20:57:44 +00:00
print('Follow request being sent to group account')
2019-10-21 14:12:22 +00:00
else:
if debug:
print('DEBUG: sendFollowRequest - assuming single user instance')
2020-04-03 11:38:44 +00:00
followedId = followHttpPrefix + '://' + requestDomain
singleUserNickname = 'dev'
followHandle = singleUserNickname + '@' + requestDomain
2019-07-06 19:24:52 +00:00
2020-04-03 11:38:44 +00:00
newFollowJson = {
2019-08-16 21:52:11 +00:00
'@context': 'https://www.w3.org/ns/activitystreams',
2020-04-03 11:38:44 +00:00
'id': followActor + '/statuses/' + str(statusNumber),
2019-07-02 18:38:51 +00:00
'type': 'Follow',
2019-07-06 10:33:57 +00:00
'actor': followActor,
2019-08-16 21:52:11 +00:00
'object': followedId
2019-07-02 18:38:51 +00:00
}
2021-08-02 20:57:44 +00:00
if groupAccount:
2021-08-02 21:09:22 +00:00
newFollowJson['to'] = followedId
2021-08-02 20:57:44 +00:00
print('Follow request: ' + str(newFollowJson))
2019-07-02 19:05:59 +00:00
if _followApprovalRequired(baseDir, nickname, domain, debug,
followHandle):
2020-02-19 12:51:14 +00:00
# Remove any follow requests rejected for the account being followed.
# It's assumed that if you are following someone then you are
# ok with them following back. If this isn't the case then a rejected
# follow request will block them again.
_removeFromFollowRejects(baseDir,
nickname, domain,
followHandle, debug)
2020-04-03 11:38:44 +00:00
sendSignedJson(newFollowJson, session, baseDir, nickname, domain, port,
followNickname, followDomain, followPort,
'https://www.w3.org/ns/activitystreams#Public',
httpPrefix, True, clientToServer,
federationList,
sendThreads, postLog, cachedWebfingers, personCache,
debug, projectVersion, None, groupAccount,
2021-09-20 16:05:11 +00:00
signingPrivateKeyPem, 8234389)
2019-07-05 20:32:21 +00:00
return newFollowJson
2020-04-03 11:38:44 +00:00
def sendFollowRequestViaServer(baseDir: str, session,
fromNickname: str, password: str,
fromDomain: str, fromPort: int,
followNickname: str, followDomain: str,
followPort: int,
httpPrefix: str,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str,
signingPrivateKeyPem: str) -> {}:
2019-07-16 21:38:06 +00:00
"""Creates a follow request via c2s
"""
if not session:
print('WARN: No session for sendFollowRequestViaServer')
return 6
2020-12-16 10:30:54 +00:00
fromDomainFull = getFullDomain(fromDomain, fromPort)
2019-07-18 11:35:48 +00:00
2020-12-16 10:30:54 +00:00
followDomainFull = getFullDomain(followDomain, followPort)
2019-07-16 21:38:06 +00:00
2021-08-14 11:13:39 +00:00
followActor = localActorUrl(httpPrefix, fromNickname, fromDomainFull)
2021-08-14 12:00:32 +00:00
followedId = \
httpPrefix + '://' + followDomainFull + '/@' + followNickname
2019-07-16 21:38:06 +00:00
2020-04-03 11:38:44 +00:00
statusNumber, published = getStatusNumber()
newFollowJson = {
2019-08-16 21:52:11 +00:00
'@context': 'https://www.w3.org/ns/activitystreams',
2020-04-03 11:38:44 +00:00
'id': followActor + '/statuses/' + str(statusNumber),
2019-07-16 21:38:06 +00:00
'type': 'Follow',
'actor': followActor,
2019-08-16 21:52:11 +00:00
'object': followedId
2019-07-16 21:38:06 +00:00
}
2020-04-03 11:38:44 +00:00
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
2019-07-16 21:38:06 +00:00
# lookup the inbox for the To handle
2020-04-03 11:38:44 +00:00
wfRequest = \
webfingerHandle(session, handle, httpPrefix, cachedWebfingers,
fromDomain, projectVersion, debug, False,
signingPrivateKeyPem)
2019-07-16 21:38:06 +00:00
if not wfRequest:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: follow request webfinger failed for ' + handle)
2019-07-16 21:38:06 +00:00
return 1
2020-06-23 10:41:12 +00:00
if not isinstance(wfRequest, dict):
2021-03-18 10:01:01 +00:00
print('WARN: follow request Webfinger for ' + handle +
' did not return a dict. ' + str(wfRequest))
2020-06-23 10:41:12 +00:00
return 1
2019-07-16 21:38:06 +00:00
2020-04-03 11:38:44 +00:00
postToBox = 'outbox'
2019-07-16 21:38:06 +00:00
# get the actor inbox for the To handle
2021-09-15 14:05:08 +00:00
originDomain = fromDomain
2020-04-03 11:38:44 +00:00
(inboxUrl, pubKeyId, pubKey,
2020-09-27 19:27:24 +00:00
fromPersonId, sharedInbox, avatarUrl,
2021-09-15 14:05:08 +00:00
displayName) = getPersonBox(signingPrivateKeyPem, originDomain,
baseDir, session, wfRequest, personCache,
2020-04-03 11:38:44 +00:00
projectVersion, httpPrefix, fromNickname,
2020-12-18 17:49:17 +00:00
fromDomain, postToBox, 52025)
2020-03-22 21:16:02 +00:00
2019-07-16 21:38:06 +00:00
if not inboxUrl:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: follow request no ' + postToBox +
' was found for ' + handle)
2019-07-16 21:38:06 +00:00
return 3
if not fromPersonId:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: follow request no actor was found for ' + handle)
2019-07-16 21:38:06 +00:00
return 4
2020-03-22 21:16:02 +00:00
2020-04-03 11:38:44 +00:00
authHeader = createBasicAuthHeader(fromNickname, password)
2020-03-22 21:16:02 +00:00
2020-04-03 11:38:44 +00:00
headers = {
'host': fromDomain,
'Content-type': 'application/json',
2020-03-22 20:36:19 +00:00
'Authorization': authHeader
}
2020-04-03 11:38:44 +00:00
postResult = \
2021-06-20 13:39:53 +00:00
postJson(httpPrefix, fromDomainFull,
session, newFollowJson, [], inboxUrl, headers, 3, True)
2020-04-03 11:38:44 +00:00
if not postResult:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: POST follow request failed for c2s to ' + inboxUrl)
2020-04-03 11:38:44 +00:00
return 5
2019-07-16 21:38:06 +00:00
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: c2s POST follow request success')
2019-07-16 21:38:06 +00:00
return newFollowJson
2020-04-03 11:38:44 +00:00
def sendUnfollowRequestViaServer(baseDir: str, session,
fromNickname: str, password: str,
fromDomain: str, fromPort: int,
followNickname: str, followDomain: str,
followPort: int,
httpPrefix: str,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str,
signingPrivateKeyPem: str) -> {}:
2019-07-17 10:34:00 +00:00
"""Creates a unfollow request via c2s
"""
if not session:
print('WARN: No session for sendUnfollowRequestViaServer')
return 6
2020-12-16 10:30:54 +00:00
fromDomainFull = getFullDomain(fromDomain, fromPort)
followDomainFull = getFullDomain(followDomain, followPort)
2019-07-17 10:34:00 +00:00
2021-08-14 11:13:39 +00:00
followActor = localActorUrl(httpPrefix, fromNickname, fromDomainFull)
2021-08-14 12:00:32 +00:00
followedId = \
httpPrefix + '://' + followDomainFull + '/@' + followNickname
2020-04-03 11:38:44 +00:00
statusNumber, published = getStatusNumber()
2019-07-17 10:34:00 +00:00
2020-04-03 11:38:44 +00:00
unfollowJson = {
2019-08-16 21:52:11 +00:00
'@context': 'https://www.w3.org/ns/activitystreams',
2020-04-03 11:38:44 +00:00
'id': followActor + '/statuses/' + str(statusNumber) + '/undo',
2019-07-17 10:34:00 +00:00
'type': 'Undo',
'actor': followActor,
'object': {
2020-04-03 11:38:44 +00:00
'id': followActor + '/statuses/' + str(statusNumber),
2019-07-17 10:34:00 +00:00
'type': 'Follow',
'actor': followActor,
2019-08-16 21:52:11 +00:00
'object': followedId
2019-07-17 10:34:00 +00:00
}
}
2020-04-03 11:38:44 +00:00
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
2019-07-17 10:34:00 +00:00
# lookup the inbox for the To handle
2020-04-03 11:38:44 +00:00
wfRequest = \
webfingerHandle(session, handle, httpPrefix, cachedWebfingers,
fromDomain, projectVersion, debug, False,
signingPrivateKeyPem)
2019-07-17 10:34:00 +00:00
if not wfRequest:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: unfollow webfinger failed for ' + handle)
2019-07-17 10:34:00 +00:00
return 1
2020-06-23 10:41:12 +00:00
if not isinstance(wfRequest, dict):
2021-03-18 10:01:01 +00:00
print('WARN: unfollow webfinger for ' + handle +
' did not return a dict. ' + str(wfRequest))
2020-06-23 10:41:12 +00:00
return 1
2019-07-17 10:34:00 +00:00
2020-04-03 11:38:44 +00:00
postToBox = 'outbox'
2019-07-17 10:34:00 +00:00
# get the actor inbox for the To handle
2021-09-15 14:05:08 +00:00
originDomain = fromDomain
2020-04-03 11:38:44 +00:00
(inboxUrl, pubKeyId, pubKey,
fromPersonId, sharedInbox,
avatarUrl, displayName) = getPersonBox(signingPrivateKeyPem,
2021-09-15 14:05:08 +00:00
originDomain,
baseDir, session,
2020-09-27 19:27:24 +00:00
wfRequest, personCache,
projectVersion, httpPrefix,
fromNickname,
2020-12-18 17:49:17 +00:00
fromDomain, postToBox,
76536)
2020-03-22 21:16:02 +00:00
2019-07-17 10:34:00 +00:00
if not inboxUrl:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: unfollow no ' + postToBox +
' was found for ' + handle)
2019-07-17 10:34:00 +00:00
return 3
if not fromPersonId:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: unfollow no actor was found for ' + handle)
2019-07-17 10:34:00 +00:00
return 4
2020-03-22 21:16:02 +00:00
2020-04-03 11:38:44 +00:00
authHeader = createBasicAuthHeader(fromNickname, password)
2020-03-22 21:16:02 +00:00
2020-04-03 11:38:44 +00:00
headers = {
'host': fromDomain,
'Content-type': 'application/json',
2020-03-22 20:36:19 +00:00
'Authorization': authHeader
}
2020-04-03 11:38:44 +00:00
postResult = \
2021-06-20 13:39:53 +00:00
postJson(httpPrefix, fromDomainFull,
session, unfollowJson, [], inboxUrl, headers, 3, True)
2020-04-03 11:38:44 +00:00
if not postResult:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: POST unfollow failed for c2s to ' + inboxUrl)
2020-04-03 11:38:44 +00:00
return 5
2019-07-17 10:34:00 +00:00
if debug:
print('DEBUG: c2s POST unfollow success')
return unfollowJson
2020-04-03 11:38:44 +00:00
2021-03-24 12:43:24 +00:00
def getFollowingViaServer(baseDir: str, session,
nickname: str, password: str,
domain: str, port: int,
httpPrefix: str, pageNumber: int,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str,
signingPrivateKeyPem: str) -> {}:
2021-03-24 12:43:24 +00:00
"""Gets a page from the following collection as json
"""
if not session:
print('WARN: No session for getFollowingViaServer')
return 6
domainFull = getFullDomain(domain, port)
2021-08-14 11:13:39 +00:00
followActor = localActorUrl(httpPrefix, nickname, domainFull)
2021-03-24 12:43:24 +00:00
authHeader = createBasicAuthHeader(nickname, password)
headers = {
'host': domain,
'Content-type': 'application/json',
'Authorization': authHeader
}
if pageNumber < 1:
pageNumber = 1
url = followActor + '/following?page=' + str(pageNumber)
followingJson = \
getJson(signingPrivateKeyPem, session, url, headers, {}, debug,
__version__, httpPrefix, domain, 10, True)
2021-03-24 12:43:24 +00:00
if not followingJson:
if debug:
print('DEBUG: GET following list failed for c2s to ' + url)
return 5
if debug:
print('DEBUG: c2s GET following list request success')
return followingJson
2021-03-24 13:52:20 +00:00
def getFollowersViaServer(baseDir: str, session,
nickname: str, password: str,
domain: str, port: int,
httpPrefix: str, pageNumber: int,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str,
signingPrivateKeyPem: str) -> {}:
2021-03-24 13:52:20 +00:00
"""Gets a page from the followers collection as json
"""
if not session:
print('WARN: No session for getFollowersViaServer')
return 6
domainFull = getFullDomain(domain, port)
2021-08-14 11:13:39 +00:00
followActor = localActorUrl(httpPrefix, nickname, domainFull)
2021-03-24 13:52:20 +00:00
authHeader = createBasicAuthHeader(nickname, password)
headers = {
'host': domain,
'Content-type': 'application/json',
'Authorization': authHeader
}
if pageNumber < 1:
pageNumber = 1
url = followActor + '/followers?page=' + str(pageNumber)
followersJson = \
getJson(signingPrivateKeyPem, session, url, headers, {}, debug,
2021-03-24 13:52:20 +00:00
__version__, httpPrefix, domain, 10, True)
if not followersJson:
if debug:
print('DEBUG: GET followers list failed for c2s to ' + url)
return 5
if debug:
print('DEBUG: c2s GET followers list request success')
return followersJson
def getFollowRequestsViaServer(baseDir: str, session,
nickname: str, password: str,
domain: str, port: int,
httpPrefix: str, pageNumber: int,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str,
signingPrivateKeyPem: str) -> {}:
"""Gets a page from the follow requests collection as json
"""
if not session:
print('WARN: No session for getFollowRequestsViaServer')
return 6
domainFull = getFullDomain(domain, port)
2021-08-14 11:13:39 +00:00
followActor = localActorUrl(httpPrefix, nickname, domainFull)
authHeader = createBasicAuthHeader(nickname, password)
headers = {
'host': domain,
'Content-type': 'application/json',
'Authorization': authHeader
}
if pageNumber < 1:
pageNumber = 1
url = followActor + '/followrequests?page=' + str(pageNumber)
followersJson = \
getJson(signingPrivateKeyPem, session, url, headers, {}, debug,
__version__, httpPrefix, domain, 10, True)
if not followersJson:
if debug:
print('DEBUG: GET follow requests list failed for c2s to ' + url)
return 5
if debug:
print('DEBUG: c2s GET follow requests list request success')
return followersJson
def approveFollowRequestViaServer(baseDir: str, session,
nickname: str, password: str,
domain: str, port: int,
httpPrefix: str, approveHandle: int,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str,
signingPrivateKeyPem: str) -> str:
"""Approves a follow request
This is not exactly via c2s though. It simulates pressing the Approve
button on the web interface
"""
if not session:
print('WARN: No session for approveFollowRequestViaServer')
return 6
domainFull = getFullDomain(domain, port)
2021-08-14 11:13:39 +00:00
actor = localActorUrl(httpPrefix, nickname, domainFull)
authHeader = createBasicAuthHeader(nickname, password)
headers = {
'host': domain,
'Content-type': 'text/html; charset=utf-8',
'Authorization': authHeader
}
url = actor + '/followapprove=' + approveHandle
approveHtml = \
getJson(signingPrivateKeyPem, session, url, headers, {}, debug,
__version__, httpPrefix, domain, 10, True)
if not approveHtml:
if debug:
print('DEBUG: GET approve follow request failed for c2s to ' + url)
return 5
if debug:
print('DEBUG: c2s GET approve follow request request success')
return approveHtml
def denyFollowRequestViaServer(baseDir: str, session,
nickname: str, password: str,
domain: str, port: int,
httpPrefix: str, denyHandle: int,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str,
signingPrivateKeyPem: str) -> str:
"""Denies a follow request
This is not exactly via c2s though. It simulates pressing the Deny
button on the web interface
"""
if not session:
print('WARN: No session for denyFollowRequestViaServer')
return 6
domainFull = getFullDomain(domain, port)
2021-08-14 11:13:39 +00:00
actor = localActorUrl(httpPrefix, nickname, domainFull)
authHeader = createBasicAuthHeader(nickname, password)
headers = {
'host': domain,
'Content-type': 'text/html; charset=utf-8',
'Authorization': authHeader
}
url = actor + '/followdeny=' + denyHandle
denyHtml = \
getJson(signingPrivateKeyPem, session, url, headers, {}, debug,
__version__, httpPrefix, domain, 10, True)
if not denyHtml:
if debug:
print('DEBUG: GET deny follow request failed for c2s to ' + url)
return 5
if debug:
print('DEBUG: c2s GET deny follow request request success')
return denyHtml
2020-04-03 11:38:44 +00:00
def getFollowersOfActor(baseDir: str, actor: str, debug: bool) -> {}:
"""In a shared inbox if we receive a post we know who it's from
2019-07-08 17:15:55 +00:00
and if it's addressed to followers then we need to get a list of those.
This returns a list of account handles which follow the given actor
"""
2019-07-11 12:29:31 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: getting followers of ' + actor)
recipientsDict = {}
2019-07-08 17:15:55 +00:00
if ':' not in actor:
2019-07-08 22:12:24 +00:00
return recipientsDict
2020-04-03 11:38:44 +00:00
nickname = getNicknameFromActor(actor)
if not nickname:
2019-07-11 12:29:31 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: no nickname found in ' + actor)
2019-07-08 22:12:24 +00:00
return recipientsDict
2020-04-03 11:38:44 +00:00
domain, port = getDomainFromActor(actor)
if not domain:
2019-07-11 12:29:31 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: no domain found in ' + actor)
2019-07-08 22:12:24 +00:00
return recipientsDict
2020-04-03 11:38:44 +00:00
actorHandle = nickname + '@' + domain
2019-07-11 12:29:31 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: searching for handle ' + actorHandle)
# for each of the accounts
2020-04-03 11:38:44 +00:00
for subdir, dirs, files in os.walk(baseDir + '/accounts'):
for account in dirs:
if '@' in account and not account.startswith('inbox@'):
2020-04-03 11:38:44 +00:00
followingFilename = \
os.path.join(subdir, account) + '/following.txt'
2019-07-11 12:29:31 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: examining follows of ' + account)
2019-07-11 12:29:31 +00:00
print(followingFilename)
if os.path.isfile(followingFilename):
# does this account follow the given actor?
2019-07-11 12:29:31 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: checking if ' + actorHandle +
' in ' + followingFilename)
2019-07-08 17:15:55 +00:00
if actorHandle in open(followingFilename).read():
2019-07-11 12:29:31 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: ' + account +
' follows ' + actorHandle)
2020-09-27 18:35:35 +00:00
recipientsDict[account] = None
2020-12-13 22:13:45 +00:00
break
2019-07-08 22:12:24 +00:00
return recipientsDict
2019-07-17 10:34:00 +00:00
2020-04-03 11:38:44 +00:00
def outboxUndoFollow(baseDir: str, messageJson: {}, debug: bool) -> None:
2019-07-17 10:34:00 +00:00
"""When an unfollow request is received by the outbox from c2s
This removes the followed handle from the following.txt file
of the relevant account
"""
if not messageJson.get('type'):
return
2020-04-03 11:38:44 +00:00
if not messageJson['type'] == 'Undo':
2019-07-17 10:34:00 +00:00
return
if not hasObjectDict(messageJson):
2019-07-17 10:34:00 +00:00
return
if not messageJson['object'].get('type'):
return
2020-04-03 11:38:44 +00:00
if not messageJson['object']['type'] == 'Follow':
if not messageJson['object']['type'] == 'Join':
return
2019-07-17 10:34:00 +00:00
if not messageJson['object'].get('object'):
return
if not messageJson['object'].get('actor'):
return
if not isinstance(messageJson['object']['object'], str):
return
if debug:
print('DEBUG: undo follow arrived in outbox')
2020-04-03 11:38:44 +00:00
nicknameFollower = getNicknameFromActor(messageJson['object']['actor'])
2019-09-02 09:43:43 +00:00
if not nicknameFollower:
2020-04-03 11:38:44 +00:00
print('WARN: unable to find nickname in ' +
messageJson['object']['actor'])
2019-09-02 09:43:43 +00:00
return
2020-04-03 11:38:44 +00:00
domainFollower, portFollower = \
getDomainFromActor(messageJson['object']['actor'])
2020-12-16 10:30:54 +00:00
domainFollowerFull = getFullDomain(domainFollower, portFollower)
2020-03-22 21:16:02 +00:00
2020-04-03 11:38:44 +00:00
nicknameFollowing = getNicknameFromActor(messageJson['object']['object'])
2019-09-02 09:43:43 +00:00
if not nicknameFollowing:
2020-04-03 11:38:44 +00:00
print('WARN: unable to find nickname in ' +
messageJson['object']['object'])
2019-09-02 09:43:43 +00:00
return
2020-04-03 11:38:44 +00:00
domainFollowing, portFollowing = \
getDomainFromActor(messageJson['object']['object'])
2020-12-16 10:30:54 +00:00
domainFollowingFull = getFullDomain(domainFollowing, portFollowing)
2019-07-17 10:34:00 +00:00
2021-07-31 11:56:28 +00:00
groupAccount = hasGroupType(baseDir, messageJson['object']['object'], None)
2020-12-22 13:57:24 +00:00
if unfollowAccount(baseDir, nicknameFollower, domainFollowerFull,
2021-07-30 16:06:34 +00:00
nicknameFollowing, domainFollowingFull,
debug, groupAccount):
2019-07-17 10:34:00 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: ' + nicknameFollower + ' unfollowed ' +
nicknameFollowing + '@' + domainFollowingFull)
2019-07-17 10:34:00 +00:00
else:
if debug:
2020-04-03 11:38:44 +00:00
print('WARN: ' + nicknameFollower + ' could not unfollow ' +
nicknameFollowing + '@' + domainFollowingFull)
2020-11-09 15:40:24 +00:00
def followerApprovalActive(baseDir: str, nickname: str, domain: str) -> bool:
"""Returns true if the given account requires follower approval
"""
manuallyApprovesFollowers = False
2021-07-13 21:59:53 +00:00
actorFilename = acctDir(baseDir, nickname, domain) + '.json'
2020-11-09 15:40:24 +00:00
if os.path.isfile(actorFilename):
actorJson = loadJson(actorFilename)
if actorJson:
if actorJson.get('manuallyApprovesFollowers'):
manuallyApprovesFollowers = \
actorJson['manuallyApprovesFollowers']
return manuallyApprovesFollowers