epicyon/follow.py

1211 lines
47 KiB
Python
Raw Normal View History

2020-04-03 11:38:44 +00:00
__filename__ = "follow.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2019-06-29 18:23:13 +00:00
from pprint import pprint
import os
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
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
from session import postJson
2019-06-29 18:23:13 +00:00
2020-04-03 11:38:44 +00:00
def preApprovedFollower(baseDir: str,
nickname: str, domain: str,
2019-12-31 09:23:41 +00:00
approveHandle: str) -> bool:
"""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,
2019-10-15 09:12:58 +00:00
debug: bool) -> None:
2019-10-06 09:48:37 +00:00
"""Removes a handle 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
2019-10-06 09:48:37 +00:00
if acceptOrDenyHandle not in open(approveFollowsFilename).read():
2019-09-18 17:04:19 +00:00
return
2020-04-03 11:38:44 +00:00
approvefilenew = open(approveFollowsFilename + '.new', 'w+')
2019-09-18 17:04:19 +00:00
with open(approveFollowsFilename, 'r') as approvefile:
for approveHandle in approvefile:
2019-10-06 09:48:37 +00:00
if not approveHandle.startswith(acceptOrDenyHandle):
2019-09-18 17:04:19 +00:00
approvefilenew.write(approveHandle)
approvefilenew.close()
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
"""
2020-04-03 11:38:44 +00:00
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
"""
2020-04-03 11:38:44 +00:00
removeFromFollowBase(baseDir, nickname, domain,
acceptHandle, 'followrejects', debug)
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?
2019-07-29 19:46:30 +00:00
"""
if ':' in domain:
2020-04-03 11:38:44 +00:00
domain = domain.split(':')[0]
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)
followingHandle = followingNickname + '@' + followingDomain
2019-08-31 12:25:42 +00:00
if followingPort:
2020-04-03 11:38:44 +00:00
if followingPort != 80 and followingPort != 443:
2019-08-31 12:25:42 +00:00
if ':' not in followingHandle:
2020-04-03 11:38:44 +00:00
followingHandle += ':' + str(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,
nickname: str, domain: str,
2020-01-13 16:06:31 +00:00
followFile='following.txt') -> []:
"""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 = \
getFollowersOfPerson(baseDir, nickname, domain, 'followers')
following = \
getFollowersOfPerson(baseDir, nickname, domain, 'following')
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 getFollowersOfPerson(baseDir: str,
nickname: str, domain: str,
2019-07-06 17:00:22 +00:00
followFile='following.txt') -> []:
"""Returns a list containing the followers of the given person
Used by the shared inbox to know who to send incoming mail to
"""
2020-04-03 11:38:44 +00:00
followers = []
2019-07-11 12:29:31 +00:00
if ':' in domain:
2020-04-03 11:38:44 +00:00
domain = domain.split(':')[0]
handle = nickname + '@' + domain
if not os.path.isdir(baseDir + '/accounts/' + handle):
return followers
2020-04-03 11:38:44 +00:00
for subdir, dirs, files in os.walk(baseDir + '/accounts'):
for account in dirs:
2020-04-03 11:38:44 +00:00
filename = os.path.join(subdir, account) + '/' + followFile
if account == handle or account.startswith('inbox@'):
continue
if not os.path.isfile(filename):
continue
with open(filename, 'r') as followingfile:
for followingHandle in followingfile:
2020-05-22 11:32:38 +00:00
followingHandle2 = followingHandle.replace('\n', '')
followingHandle2 = followingHandle2.replace('\r', '')
if followingHandle2 == handle:
if account not in followers:
followers.append(account)
break
return followers
2020-04-03 11:38:44 +00:00
def followerOfPerson(baseDir: str, nickname: str, domain: str,
followerNickname: str, followerDomain: str,
federationList: [], debug: bool) -> bool:
"""Adds a follower of the given person
"""
2020-04-03 11:38:44 +00:00
return followPerson(baseDir, nickname, domain,
followerNickname, followerDomain,
federationList, debug, 'followers.txt')
2019-06-29 18:23:13 +00:00
2020-04-03 11:38:44 +00:00
def isFollowerOfPerson(baseDir: str, nickname: str, domain: str,
2019-08-31 15:17:07 +00:00
followerNickname: str, followerDomain: str) -> bool:
"""is the given nickname a follower of followerNickname?
"""
if ':' in domain:
2020-04-03 11:38:44 +00:00
domain = domain.split(':')[0]
followersFile = baseDir + '/accounts/' + \
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
return handle in open(followersFile).read()
2019-08-31 15:17:07 +00:00
2020-04-03 11:38:44 +00:00
def unfollowPerson(baseDir: str, nickname: str, domain: str,
followNickname: str, followDomain: str,
followFile='following.txt',
2019-07-17 11:54:13 +00:00
debug=False) -> bool:
2019-06-29 18:23:13 +00:00
"""Removes a person to the follow list
"""
2019-07-17 11:54:13 +00:00
if ':' in domain:
2020-04-03 11:38:44 +00:00
domain = domain.split(':')[0]
handle = nickname + '@' + domain
handleToUnfollow = followNickname + '@' + followDomain
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
with open(filename, "r") as f:
2020-04-03 11:38:44 +00:00
lines = f.readlines()
2020-08-29 11:14:19 +00:00
with open(filename, 'w+') as f:
2019-07-17 10:34:00 +00:00
for line in lines:
2020-07-14 21:48:39 +00:00
if line.strip("\n").strip("\r").lower() != handleToUnfollowLower:
2019-07-17 10:34:00 +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():
with open(filename, "a+") as f:
2020-04-03 11:38:44 +00:00
f.write(handleToUnfollow + '\n')
else:
2020-02-22 11:04:08 +00:00
with open(unfollowedFilename, "w+") as f:
2020-04-03 11:38:44 +00:00
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
def unfollowerOfPerson(baseDir: str, nickname: str, domain: str,
followerNickname: str, followerDomain: str,
2019-07-17 11:54:13 +00:00
debug=False) -> bool:
"""Remove a follower of a person
"""
2020-04-03 11:38:44 +00:00
return unfollowPerson(baseDir, nickname, domain,
followerNickname, followerDomain,
'followers.txt', debug)
2019-06-29 18:23:13 +00:00
2020-04-03 11:38:44 +00:00
def clearFollows(baseDir: str, nickname: str, domain: str,
2019-07-06 17:00:22 +00:00
followFile='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):
os.remove(filename)
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
2020-04-03 11:38:44 +00:00
def getNoOfFollows(baseDir: str, nickname: str, domain: str,
authenticated: bool,
2019-07-06 17:00:22 +00:00
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
2019-06-29 20:21:37 +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:
2020-04-03 11:38:44 +00:00
if '@' in line and \
'.' in line and \
not line.startswith('http'):
2019-06-29 20:21:37 +00:00
ctr += 1
elif line.startswith('http') and '/users/' in line:
ctr += 1
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
"""
2020-04-03 11:38:44 +00:00
return getNoOfFollows(baseDir, nickname, domain,
authenticated, 'followers.txt')
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,
httpPrefix: str, authenticated: bool,
2020-04-03 11:38:44 +00:00
followsPerPage=12,
2019-07-06 17:00:22 +00:00
followFile='following') -> {}:
"""Returns the following and followers feeds from GET requests
"""
# Show a small number of follows to non-authenticated viewers
if not authenticated:
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]
if pageNumber == 'true' or not authenticated:
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
if port:
2020-04-03 11:38:44 +00:00
if port != 80 and port != 443:
if ':' not in domain:
2020-04-03 11:38:44 +00:00
domain = domain + ':' + str(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 = \
httpPrefix + '://' + domain + '/users/' + \
nickname + '/' + followFile + '?page=1'
idStr = \
httpPrefix + '://' + domain + '/users/' + \
nickname + '/' + followFile
totalStr = \
getNoOfFollows(baseDir, nickname, domain, authenticated)
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 = \
httpPrefix + '://' + domain + '/users/' + \
nickname + '/' + followFile + '?page=' + str(pageNumber)
partOfStr = \
httpPrefix + '://' + domain + '/users/' + nickname + '/' + followFile
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
if ':' in handleDomain:
2020-04-03 11:38:44 +00:00
handleDomain = domain.split(':')[0]
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
2019-06-29 20:21:37 +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'):
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', '')
2020-04-03 11:38:44 +00:00
url = httpPrefix + '://' + \
2020-05-22 11:32:38 +00:00
line2.split('@')[1] + \
2020-04-03 11:38:44 +00:00
'/users/' + \
2020-05-22 11:32:38 +00:00
line2.split('@')[0]
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
line.startswith('dat')) and '/users/' in line):
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'] = \
httpPrefix + '://' + domain + '/users/' + \
nickname + '/' + 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,
2019-12-31 09:23:41 +00:00
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?
2020-04-03 11:38:44 +00:00
if preApprovedFollower(baseDir, nicknameToFollow, domainToFollow,
2019-12-31 09:23:41 +00:00
followRequestHandle):
return False
2020-04-03 11:38:44 +00:00
manuallyApproveFollows = False
2019-08-31 14:42:35 +00:00
if ':' in domainToFollow:
2020-04-03 11:38:44 +00:00
domainToFollow = domainToFollow.split(':')[0]
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
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) -> 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-04-03 11:38:44 +00:00
approveHandle = nickname + '@' + domain
if fromPort:
2020-04-03 11:38:44 +00:00
if fromPort != 80 and fromPort != 443:
if ':' not in domain:
2020-04-03 11:38:44 +00:00
approveHandle = nickname + '@' + domain + ':' + str(fromPort)
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):
if approveHandle in open(followersFilename).read():
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'
if os.path.isfile(approveFollowsFilename):
if approveHandle not in open(approveFollowsFilename).read():
2020-08-20 11:34:39 +00:00
with open(approveFollowsFilename, 'a+') as fp:
2020-04-03 11:38:44 +00:00
fp.write(approveHandle + '\n')
else:
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: ' + approveHandle +
' is already awaiting approval')
else:
2019-12-29 12:59:13 +00:00
with open(approveFollowsFilename, "w+") as fp:
2020-04-03 11:38:44 +00:00
fp.write(approveHandle + '\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,
acceptedCaps=["inbox:write", "objects:read"]) -> 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'):
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
2019-10-17 22:26:47 +00:00
if '/users/' not in messageJson['actor'] and \
2020-08-13 16:19:35 +00:00
'/accounts/' not in messageJson['actor'] and \
2019-10-17 22:26:47 +00:00
'/channel/' not in messageJson['actor'] and \
'/profile/' not in messageJson['actor']:
2019-07-06 13:49:25 +00:00
if debug:
2020-03-22 21:16:02 +00:00
print('DEBUG: "users" or "profile" 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
domainFull = domain
2019-07-06 15:17:21 +00:00
if tempPort:
2020-04-03 11:38:44 +00:00
fromPort = tempPort
if tempPort != 80 and tempPort != 443:
if ':' not in domain:
2020-04-03 11:38:44 +00:00
domainFull = domain + ':' + str(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']
2019-10-17 22:26:47 +00:00
if '/users/' not in messageJson['object'] and \
2020-08-13 16:19:35 +00:00
'/accounts/' not in messageJson['object'] and \
2019-10-17 22:26:47 +00:00
'/channel/' not in messageJson['object'] and \
'/profile/' not in messageJson['object']:
2019-07-06 13:49:25 +00:00
if debug:
2019-09-09 09:41:31 +00:00
print('DEBUG: "users" or "profile" 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-04-03 11:38:44 +00:00
domainToFollowFull = domainToFollow
2019-07-16 22:57:45 +00:00
if tempPort:
2020-04-03 11:38:44 +00:00
if tempPort != 80 and tempPort != 443:
if ':' not in domainToFollow:
2020-04-03 11:38:44 +00:00
domainToFollowFull = domainToFollow + ':' + str(tempPort)
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-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
2020-04-03 11:38:44 +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'):
2020-04-03 11:38:44 +00:00
if noOfFollowRequests(baseDir,
nicknameToFollow, domainToFollow,
nickname, domain, fromPort,
2020-06-03 20:21:44 +00:00
'onion') > 5:
print('Too many follow requests from onion addresses')
return False
elif domain.endswith('.i2p'):
if noOfFollowRequests(baseDir,
nicknameToFollow, domainToFollow,
nickname, domain, fromPort,
'i2p') > 5:
print('Too many follow requests from i2p addresses')
return False
else:
2020-04-03 11:38:44 +00:00
if noOfFollowRequests(baseDir,
nicknameToFollow, domainToFollow,
nickname, domain, fromPort,
2020-06-03 20:21:44 +00:00
'') > 10:
print('Too many follow requests')
return False
2019-08-15 16:05:28 +00:00
print('Storing follow request for approval')
2020-04-03 11:38:44 +00:00
return storeFollowRequest(baseDir,
nicknameToFollow, domainToFollow, port,
nickname, domain, fromPort,
messageJson, debug)
2019-08-31 14:42:35 +00:00
else:
2019-09-01 20:03:20 +00:00
print('Follow request does not require approval')
2019-09-01 20:28:43 +00:00
# update the followers
2020-04-03 11:38:44 +00:00
if os.path.isdir(baseDir + '/accounts/' +
nicknameToFollow + '@' + domainToFollow):
followersFilename = \
baseDir + '/accounts/' + \
nicknameToFollow + '@' + domainToFollow + '/followers.txt'
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():
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()
2019-10-26 15:15:38 +00:00
followersFile.seek(0, 0)
2020-04-03 11:38:44 +00:00
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:
2020-04-03 11:38:44 +00:00
followersFile = open(followersFilename, "w+")
followersFile.write(approveHandle + '\n')
2019-09-01 20:28:43 +00:00
followersFile.close()
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,
messageJson, acceptedCaps,
sendThreads, postLog,
cachedWebfingers, personCache,
debug, projectVersion, True)
def followedAccountAccepts(session, baseDir: str, httpPrefix: str,
nicknameToFollow: str, domainToFollow: str,
port: int,
nickname: str, domain: str, fromPort: int,
personUrl: str, federationList: [],
followJson: {}, acceptedCaps: [],
sendThreads: [], postLog: [],
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str,
removeFollowActivity: bool):
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,
followJson, acceptedCaps)
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 = \
baseDir + '/accounts/' + \
nicknameToFollow + '@' + domainToFollow + '/requests/' + \
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
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)
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):
"""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 = \
baseDir + '/accounts/' + \
nicknameToFollow + '@' + domainToFollow + '/requests/' + \
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
denyHandle = nickname + '@' + domain
2019-09-18 17:30:19 +00:00
if fromPort:
2020-04-03 11:38:44 +00:00
if fromPort != 80 and fromPort != 443:
denyHandle = denyHandle + ':' + str(fromPort)
# 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)
def sendFollowRequest(session, baseDir: str,
nickname: str, domain: str, port: int, httpPrefix: str,
followNickname: str, followDomain: str,
followPort: int, followHttpPrefix: str,
clientToServer: bool, federationList: [],
sendThreads: [], postLog: [], cachedWebfingers: {},
personCache: {}, debug: bool,
2019-08-14 20:12:27 +00:00
projectVersion: 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
"""
2020-04-03 11:38:44 +00:00
if not domainPermitted(followDomain, federationList):
2019-07-02 18:38:51 +00:00
return None
2020-03-22 21:16:02 +00:00
2020-04-03 11:38:44 +00:00
fullDomain = domain
followActor = httpPrefix + '://' + domain + '/users/' + nickname
if port:
2020-04-03 11:38:44 +00:00
if port != 80 and port != 443:
if ':' not in domain:
2020-04-03 11:38:44 +00:00
fullDomain = domain + ':' + str(port)
followActor = httpPrefix + '://' + \
fullDomain + '/users/' + nickname
2019-07-02 18:38:51 +00:00
2020-04-03 11:38:44 +00:00
requestDomain = followDomain
if followPort:
2020-04-03 11:38:44 +00:00
if followPort != 80 and followPort != 443:
if ':' not in followDomain:
2020-04-03 11:38:44 +00:00
requestDomain = followDomain + ':' + str(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
2019-10-21 14:12:22 +00:00
if followNickname:
2020-04-03 11:38:44 +00:00
followedId = followHttpPrefix + '://' + \
requestDomain + '/users/' + followNickname
followHandle = followNickname + '@' + requestDomain
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
}
2019-07-02 19:05:59 +00:00
2020-04-03 11:38:44 +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.
2020-04-03 11:38:44 +00:00
removeFromFollowRejects(baseDir,
nickname, domain,
followHandle, debug)
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)
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) -> {}:
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-04-03 11:38:44 +00:00
fromDomainFull = fromDomain
if fromPort:
2020-04-03 11:38:44 +00:00
if fromPort != 80 and fromPort != 443:
if ':' not in fromDomain:
2020-04-03 11:38:44 +00:00
fromDomainFull = fromDomain + ':' + str(fromPort)
2019-07-18 11:35:48 +00:00
2020-04-03 11:38:44 +00:00
followDomainFull = followDomain
if followPort:
2020-04-03 11:38:44 +00:00
if followPort != 80 and followPort != 443:
if ':' not in followDomain:
2020-04-03 11:38:44 +00:00
followDomainFull = followDomain + ':' + str(followPort)
2019-07-16 21:38:06 +00:00
2020-04-03 11:38:44 +00:00
followActor = httpPrefix + '://' + \
fromDomainFull + '/users/' + fromNickname
followedId = httpPrefix + '://' + \
followDomainFull + '/users/' + 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)
2019-07-16 21:38:06 +00:00
if not wfRequest:
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: announce 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):
print('WARN: Webfinger for ' + handle + ' did not return a dict. ' +
str(wfRequest))
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
2020-04-03 11:38:44 +00:00
(inboxUrl, pubKeyId, pubKey,
fromPersonId, sharedInbox,
capabilityAcquisition, avatarUrl,
displayName) = getPersonBox(baseDir, session, wfRequest, personCache,
projectVersion, httpPrefix, fromNickname,
fromDomain, postToBox)
2020-03-22 21:16:02 +00:00
2019-07-16 21:38:06 +00:00
if not inboxUrl:
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: No ' + postToBox + ' was found for ' + handle)
2019-07-16 21:38:06 +00:00
return 3
if not fromPersonId:
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: 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 = \
postJson(session, newFollowJson, [], inboxUrl, headers, "inbox:write")
if not postResult:
if debug:
print('DEBUG: POST announce failed for c2s to ' + inboxUrl)
return 5
2019-07-16 21:38:06 +00:00
if debug:
print('DEBUG: c2s POST follow success')
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) -> {}:
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-04-03 11:38:44 +00:00
fromDomainFull = fromDomain
if fromPort:
2020-04-03 11:38:44 +00:00
if fromPort != 80 and fromPort != 443:
if ':' not in fromDomain:
2020-04-03 11:38:44 +00:00
fromDomainFull = fromDomain + ':' + str(fromPort)
followDomainFull = followDomain
if followPort:
2020-04-03 11:38:44 +00:00
if followPort != 80 and followPort != 443:
if ':' not in followDomain:
2020-04-03 11:38:44 +00:00
followDomainFull = followDomain + ':' + str(followPort)
2019-07-17 10:34:00 +00:00
2020-04-03 11:38:44 +00:00
followActor = httpPrefix + '://' + \
fromDomainFull + '/users/' + fromNickname
followedId = httpPrefix + '://' + \
followDomainFull + '/users/' + followNickname
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)
2019-07-17 10:34:00 +00:00
if not wfRequest:
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: announce 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):
print('WARN: Webfinger for ' + handle + ' did not return a dict. ' +
str(wfRequest))
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
2020-04-03 11:38:44 +00:00
(inboxUrl, pubKeyId, pubKey,
fromPersonId, sharedInbox,
capabilityAcquisition, avatarUrl,
displayName) = getPersonBox(baseDir, session, wfRequest, personCache,
projectVersion, httpPrefix, fromNickname,
fromDomain, postToBox)
2020-03-22 21:16:02 +00:00
2019-07-17 10:34:00 +00:00
if not inboxUrl:
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: No ' + postToBox + ' was found for ' + handle)
2019-07-17 10:34:00 +00:00
return 3
if not fromPersonId:
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: 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 = \
postJson(session, unfollowJson, [], inboxUrl, headers, "inbox:write")
if not postResult:
if debug:
print('DEBUG: POST announce failed for c2s to ' + inboxUrl)
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
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
and also the corresponding capability id if it exists
"""
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
httpPrefix = actor.split(':')[0]
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)
ocapFilename = baseDir + '/accounts/' + \
account + '/ocap/accept/' + httpPrefix + \
':##' + domain + ':' + str(port) + \
'#users#' + nickname + '.json'
2019-07-11 12:29:31 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: checking capabilities of' + account)
2019-09-30 22:39:02 +00:00
if os.path.isfile(ocapFilename):
2020-04-03 11:38:44 +00:00
ocapJson = loadJson(ocapFilename)
2019-09-30 22:39:02 +00:00
if ocapJson:
2019-07-11 12:29:31 +00:00
if ocapJson.get('id'):
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: ' +
'capabilities id found for ' +
account)
2020-03-22 21:16:02 +00:00
2020-04-03 11:38:44 +00:00
recipientsDict[account] = ocapJson['id']
2019-07-11 12:29:31 +00:00
else:
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: ' +
'capabilities has no ' +
'id attribute')
recipientsDict[account] = None
2019-07-08 17:15:55 +00:00
else:
2019-07-11 12:29:31 +00:00
if debug:
2020-04-03 11:38:44 +00:00
print('DEBUG: ' +
'No capabilities file found for ' +
account + ' granted by ' + actorHandle)
2019-07-11 12:29:31 +00:00
print(ocapFilename)
2020-04-03 11:38:44 +00:00
recipientsDict[account] = None
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 messageJson.get('object'):
return
if not isinstance(messageJson['object'], dict):
return
if not messageJson['object'].get('type'):
return
2020-04-03 11:38:44 +00:00
if not messageJson['object']['type'] == 'Follow':
2019-07-17 10:34:00 +00:00
return
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'])
domainFollowerFull = domainFollower
2019-07-17 10:34:00 +00:00
if portFollower:
2020-04-03 11:38:44 +00:00
if portFollower != 80 and portFollower != 443:
if ':' not in domainFollower:
2020-04-03 11:38:44 +00:00
domainFollowerFull = domainFollower + ':' + str(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'])
domainFollowingFull = domainFollowing
2019-07-17 10:34:00 +00:00
if portFollowing:
2020-04-03 11:38:44 +00:00
if portFollowing != 80 and portFollowing != 443:
if ':' not in domainFollowing:
2020-04-03 11:38:44 +00:00
domainFollowingFull = \
domainFollowing + ':' + str(portFollowing)
2019-07-17 10:34:00 +00:00
2020-04-03 11:38:44 +00:00
if unfollowPerson(baseDir, nicknameFollower, domainFollowerFull,
nicknameFollowing, domainFollowingFull):
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)