Move functions

merge-requests/8/head
Bob Mottram 2020-09-25 14:21:56 +01:00
parent 6ac45978e8
commit e5eaf42a36
5 changed files with 111 additions and 109 deletions

View File

@ -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:

102
follow.py
View File

@ -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:

View File

@ -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: [],

View File

@ -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

View File

@ -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
"""