From e5eaf42a36daf61aaf456dedf24c618d2ee649de Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Fri, 25 Sep 2020 14:21:56 +0100 Subject: [PATCH] Move functions --- epicyon.py | 14 ++++---- follow.py | 102 +---------------------------------------------------- posts.py | 72 +++++++++++++++++++++++++++++++++++++ tests.py | 2 +- utils.py | 30 ++++++++++++++++ 5 files changed, 111 insertions(+), 109 deletions(-) diff --git a/epicyon.py b/epicyon.py index f34f09fa8..7654515f9 100644 --- a/epicyon.py +++ b/epicyon.py @@ -25,6 +25,7 @@ from posts import archivePosts from posts import sendPostViaServer from posts import getPublicPostsOfPerson from posts import getUserUrl +from posts import checkDomains from session import createSession from session import getJson from filters import addFilter @@ -35,7 +36,6 @@ import sys import time from pprint import pprint from daemon import runDaemon -from follow import checkDomains from follow import clearFollows from follow import followerOfPerson from follow import sendFollowRequestViaServer @@ -555,12 +555,12 @@ if args.checkDomains: elif args.gnunet: proxyType = 'gnunet' maxBlockedDomains = 2 -# checkDomains(None, -# baseDir, nickname, domain, -# proxyType, args.port, -# httpPrefix, debug, -# __version__, -# maxBlockedDomains, False) + checkDomains(None, + baseDir, nickname, domain, + proxyType, args.port, + httpPrefix, debug, + __version__, + maxBlockedDomains, False) sys.exit() if args.socnet: diff --git a/follow.py b/follow.py index 2d0534f30..014458237 100644 --- a/follow.py +++ b/follow.py @@ -8,6 +8,7 @@ __status__ = "Production" from pprint import pprint import os +from utils import getFollowersOfPerson from utils import validNickname from utils import domainPermitted from utils import getDomainFromActor @@ -25,61 +26,6 @@ from auth import createBasicAuthHeader from session import postJson -def checkDomains(session, baseDir: str, - nickname: str, domain: str, - proxyType: str, port: int, httpPrefix: str, - debug: bool, projectVersion: str, - maxBlockedDomains: int, singleCheck: bool): - """Checks follower accounts for references to globally blocked domains - """ - nonMutuals = getNonMutualsOfPerson(baseDir, nickname, domain) - if not nonMutuals: - return - followerWarningFilename = baseDir + '/accounts/followerWarnings.txt' - updateFollowerWarnings = False - followerWarningStr = '' - if os.path.isfile(followerWarningFilename): - with open(followerWarningFilename, 'r') as fp: - followerWarningStr = fp.read() - - if singleCheck: - # checks a single random non-mutual - index = random.randrange(0, len(nonMutuals)) - domainName = nonMutuals[index] - blockedDomains = \ - getPublicPostDomainsBlocked(session, baseDir, - nickname, domain, - proxyType, port, httpPrefix, - debug, projectVersion, []) - if blockedDomains: - if len(blockedDomains) > maxBlockedDomains: - followerWarningStr += domainName + '\n' - updateFollowerWarnings = True - else: - # checks all non-mutuals - for domainName in nonMutuals: - if domainName in followerWarningStr: - continue - blockedDomains = \ - getPublicPostDomainsBlocked(session, baseDir, - nickname, domain, - proxyType, port, httpPrefix, - debug, projectVersion, []) - if blockedDomains: - print(domainName) - for d in blockedDomains: - print(' ' + d) - if len(blockedDomains) > maxBlockedDomains: - followerWarningStr += domainName + '\n' - updateFollowerWarnings = True - - if updateFollowerWarnings and followerWarningStr: - with open(followerWarningFilename, 'w+') as fp: - fp.write(followerWarningStr) - if not singleCheck: - print(followerWarningStr) - - def preApprovedFollower(baseDir: str, nickname: str, domain: str, approveHandle: str) -> bool: @@ -182,52 +128,6 @@ def getMutualsOfPerson(baseDir: str, return mutuals -def getNonMutualsOfPerson(baseDir: str, - nickname: str, domain: str) -> []: - """Returns the followers who are not mutuals of a person - i.e. accounts which follow you but you don't follow them - """ - followers = \ - getFollowersOfPerson(baseDir, nickname, domain, 'followers') - following = \ - getFollowersOfPerson(baseDir, nickname, domain, 'following') - nonMutuals = [] - for handle in following: - if handle not in followers: - nonMutuals.append(handle) - return nonMutuals - - -def getFollowersOfPerson(baseDir: str, - nickname: str, domain: str, - 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 - """ - followers = [] - if ':' in domain: - domain = domain.split(':')[0] - handle = nickname + '@' + domain - if not os.path.isdir(baseDir + '/accounts/' + handle): - return followers - for subdir, dirs, files in os.walk(baseDir + '/accounts'): - for account in dirs: - 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: - followingHandle2 = followingHandle.replace('\n', '') - followingHandle2 = followingHandle2.replace('\r', '') - if followingHandle2 == handle: - if account not in followers: - followers.append(account) - break - return followers - - def followerOfPerson(baseDir: str, nickname: str, domain: str, followerNickname: str, followerDomain: str, federationList: [], debug: bool) -> bool: diff --git a/posts.py b/posts.py index b65305b68..9f637f84f 100644 --- a/posts.py +++ b/posts.py @@ -30,6 +30,7 @@ from session import postJsonString from session import postImage from webfinger import webfingerHandle from httpsig import createSignedHeader +from utils import getFollowersOfPerson from utils import isEvil from utils import removeIdEnding from utils import siteIsActive @@ -3331,6 +3332,77 @@ def getPublicPostDomainsBlocked(session, baseDir: str, return blockedDomains +def getNonMutualsOfPerson(baseDir: str, + nickname: str, domain: str) -> []: + """Returns the followers who are not mutuals of a person + i.e. accounts which follow you but you don't follow them + """ + followers = \ + getFollowersOfPerson(baseDir, nickname, domain, 'followers') + following = \ + getFollowersOfPerson(baseDir, nickname, domain, 'following') + nonMutuals = [] + for handle in following: + if handle not in followers: + nonMutuals.append(handle) + return nonMutuals + + +def checkDomains(session, baseDir: str, + nickname: str, domain: str, + proxyType: str, port: int, httpPrefix: str, + debug: bool, projectVersion: str, + maxBlockedDomains: int, singleCheck: bool): + """Checks follower accounts for references to globally blocked domains + """ + nonMutuals = getNonMutualsOfPerson(baseDir, nickname, domain) + if not nonMutuals: + return + followerWarningFilename = baseDir + '/accounts/followerWarnings.txt' + updateFollowerWarnings = False + followerWarningStr = '' + if os.path.isfile(followerWarningFilename): + with open(followerWarningFilename, 'r') as fp: + followerWarningStr = fp.read() + + if singleCheck: + # checks a single random non-mutual + index = random.randrange(0, len(nonMutuals)) + domainName = nonMutuals[index] + blockedDomains = \ + getPublicPostDomainsBlocked(session, baseDir, + nickname, domain, + proxyType, port, httpPrefix, + debug, projectVersion, []) + if blockedDomains: + if len(blockedDomains) > maxBlockedDomains: + followerWarningStr += domainName + '\n' + updateFollowerWarnings = True + else: + # checks all non-mutuals + for domainName in nonMutuals: + if domainName in followerWarningStr: + continue + blockedDomains = \ + getPublicPostDomainsBlocked(session, baseDir, + nickname, domain, + proxyType, port, httpPrefix, + debug, projectVersion, []) + if blockedDomains: + print(domainName) + for d in blockedDomains: + print(' ' + d) + if len(blockedDomains) > maxBlockedDomains: + followerWarningStr += domainName + '\n' + updateFollowerWarnings = True + + if updateFollowerWarnings and followerWarningStr: + with open(followerWarningFilename, 'w+') as fp: + fp.write(followerWarningStr) + if not singleCheck: + print(followerWarningStr) + + def sendCapabilitiesUpdate(session, baseDir: str, httpPrefix: str, nickname: str, domain: str, port: int, followerUrl, updateCaps: [], diff --git a/tests.py b/tests.py index abb35f43e..7996c3ba9 100644 --- a/tests.py +++ b/tests.py @@ -42,10 +42,10 @@ from utils import copytree from utils import loadJson from utils import saveJson from utils import getStatusNumber +from utils import getFollowersOfPerson from follow import followerOfPerson from follow import unfollowPerson from follow import unfollowerOfPerson -from follow import getFollowersOfPerson from follow import sendFollowRequest from person import createPerson from person import setDisplayNickname diff --git a/utils.py b/utils.py index 8a18c7534..2be770b0a 100644 --- a/utils.py +++ b/utils.py @@ -19,6 +19,36 @@ from calendar import monthrange from followingCalendar import addPersonToCalendar +def getFollowersOfPerson(baseDir: str, + nickname: str, domain: str, + 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 + """ + followers = [] + if ':' in domain: + domain = domain.split(':')[0] + handle = nickname + '@' + domain + if not os.path.isdir(baseDir + '/accounts/' + handle): + return followers + for subdir, dirs, files in os.walk(baseDir + '/accounts'): + for account in dirs: + 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: + followingHandle2 = followingHandle.replace('\n', '') + followingHandle2 = followingHandle2.replace('\r', '') + if followingHandle2 == handle: + if account not in followers: + followers.append(account) + break + return followers + + def removeIdEnding(idStr: str) -> str: """Removes endings such as /activity and /undo """