manualapprove.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. __filename__ = "manualapprove.py"
  2. __author__ = "Bob Mottram"
  3. __license__ = "AGPL3+"
  4. __version__ = "1.1.0"
  5. __maintainer__ = "Bob Mottram"
  6. __email__ = "bob@freedombone.net"
  7. __status__ = "Production"
  8. import os
  9. import json
  10. import time
  11. from follow import followedAccountAccepts
  12. from follow import followedAccountRejects
  13. from follow import removeFromFollowRequests
  14. from utils import loadJson
  15. from utils import saveJson
  16. def manualDenyFollowRequest(session,baseDir: str, \
  17. httpPrefix: str,
  18. nickname: str,domain: str,port: int, \
  19. denyHandle: str, \
  20. federationList: [], \
  21. sendThreads: [],postLog: [], \
  22. cachedWebfingers: {},personCache: {}, \
  23. debug: bool, \
  24. projectVersion: str) -> None:
  25. """Manually deny a follow request
  26. """
  27. handle=nickname+'@'+domain
  28. accountsDir=baseDir+'/accounts/'+handle
  29. # has this handle already been rejected?
  30. rejectedFollowsFilename=accountsDir+'/followrejects.txt'
  31. if os.path.isfile(rejectedFollowsFilename):
  32. if denyHandle in open(rejectedFollowsFilename).read():
  33. removeFromFollowRequests(baseDir,nickname,domain,denyHandle,debug)
  34. print(denyHandle+' has already been rejected as a follower of '+nickname)
  35. return
  36. removeFromFollowRequests(baseDir,nickname,domain,denyHandle,debug)
  37. # Store rejected follows
  38. rejectsFile=open(rejectedFollowsFilename, "a+")
  39. rejectsFile.write(denyHandle+'\n')
  40. rejectsFile.close()
  41. denyNickname=denyHandle.split('@')[0]
  42. denyDomain=denyHandle.split('@')[1].replace('\n','')
  43. denyPort=port
  44. if ':' in denyDomain:
  45. denyPort=denyDomain.split(':')[1]
  46. denyDomain=denyDomain.split(':')[0]
  47. followedAccountRejects(session,baseDir,httpPrefix, \
  48. nickname,domain,port, \
  49. denyNickname,denyDomain,denyPort, \
  50. federationList, \
  51. sendThreads,postLog, \
  52. cachedWebfingers,personCache, \
  53. debug,projectVersion)
  54. print('Follow request from '+denyHandle+' was denied.')
  55. def approveFollowerHandle(accountDir: str,approveHandle: str) -> None:
  56. """ Record manually approved handles so that if they unfollow and then
  57. re-follow later then they don't need to be manually approved again
  58. """
  59. approvedFilename=accountDir+'/approved.txt'
  60. if os.path.isfile(approvedFilename):
  61. if approveHandle not in open(approvedFilename).read():
  62. approvedFile=open(approvedFilename, "a+")
  63. approvedFile.write(approveHandle+'\n')
  64. approvedFile.close()
  65. else:
  66. approvedFile=open(approvedFilename, "w+")
  67. approvedFile.write(approveHandle+'\n')
  68. approvedFile.close()
  69. def manualApproveFollowRequest(session,baseDir: str, \
  70. httpPrefix: str,
  71. nickname: str,domain: str,port: int, \
  72. approveHandle: str, \
  73. federationList: [], \
  74. sendThreads: [],postLog: [], \
  75. cachedWebfingers: {},personCache: {}, \
  76. acceptedCaps: [], \
  77. debug: bool, \
  78. projectVersion: str) -> None:
  79. """Manually approve a follow request
  80. """
  81. handle=nickname+'@'+domain
  82. print('Manual follow accept: '+handle+' approving follow request from '+approveHandle)
  83. accountDir=baseDir+'/accounts/'+handle
  84. approveFollowsFilename=accountDir+'/followrequests.txt'
  85. if not os.path.isfile(approveFollowsFilename):
  86. print('Manual follow accept: follow requests file '+approveFollowsFilename+' not found')
  87. return
  88. # is the handle in the requests file?
  89. if approveHandle not in open(approveFollowsFilename).read():
  90. print('Manual follow accept: '+approveHandle+' not in requests file '+approveFollowsFilename)
  91. return
  92. approvefilenew = open(approveFollowsFilename+'.new', 'w+')
  93. updateApprovedFollowers=False
  94. followActivityfilename=None
  95. with open(approveFollowsFilename, 'r') as approvefile:
  96. for handleOfFollowRequester in approvefile:
  97. # is this the approved follow?
  98. if handleOfFollowRequester.startswith(approveHandle):
  99. handleOfFollowRequester=handleOfFollowRequester.replace('\n','')
  100. port2=port
  101. if ':' in handleOfFollowRequester:
  102. port2=int(handleOfFollowRequester.split(':')[1])
  103. requestsDir=accountDir+'/requests'
  104. followActivityfilename=requestsDir+'/'+handleOfFollowRequester+'.follow'
  105. if os.path.isfile(followActivityfilename):
  106. followJson=loadJson(followActivityfilename)
  107. if followJson:
  108. approveNickname=approveHandle.split('@')[0]
  109. approveDomain=approveHandle.split('@')[1].replace('\n','')
  110. approvePort=port2
  111. if ':' in approveDomain:
  112. approvePort=approveDomain.split(':')[1]
  113. approveDomain=approveDomain.split(':')[0]
  114. print('Manual follow accept: Sending Accept for '+handle+' follow request from '+approveNickname+'@'+approveDomain)
  115. followedAccountAccepts(session,baseDir,httpPrefix, \
  116. nickname,domain,port, \
  117. approveNickname,approveDomain,approvePort, \
  118. followJson['actor'],federationList, \
  119. followJson,acceptedCaps, \
  120. sendThreads,postLog, \
  121. cachedWebfingers,personCache, \
  122. debug,projectVersion,False)
  123. updateApprovedFollowers=True
  124. else:
  125. # this isn't the approved follow so it will remain
  126. # in the requests file
  127. approvefilenew.write(handleOfFollowRequester)
  128. approvefilenew.close()
  129. followersFilename=accountDir+'/followers.txt'
  130. if updateApprovedFollowers:
  131. # update the followers
  132. print('Manual follow accept: updating '+followersFilename)
  133. if os.path.isfile(followersFilename):
  134. if approveHandle not in open(followersFilename).read():
  135. try:
  136. with open(followersFilename, 'r+') as followersFile:
  137. content = followersFile.read()
  138. followersFile.seek(0, 0)
  139. followersFile.write(approveHandle+'\n'+content)
  140. except Exception as e:
  141. print('WARN: Manual follow accept. Failed to write entry to followers file '+str(e))
  142. else:
  143. print('WARN: Manual follow accept: '+approveHandle+' already exists in '+followersFilename)
  144. else:
  145. print('Manual follow accept: first follower accepted for '+handle+' is '+approveHandle)
  146. followersFile=open(followersFilename, "w+")
  147. followersFile.write(approveHandle+'\n')
  148. followersFile.close()
  149. # only update the follow requests file if the follow is confirmed to be
  150. # in followers.txt
  151. if approveHandle in open(followersFilename).read():
  152. # mark this handle as approved for following
  153. approveFollowerHandle(accountDir,approveHandle)
  154. # update the follow requests with the handles not yet approved
  155. os.rename(approveFollowsFilename+'.new',approveFollowsFilename)
  156. # remove the .follow file
  157. if followActivityfilename:
  158. if os.path.isfile(followActivityfilename):
  159. os.remove(followActivityfilename)
  160. else:
  161. os.remove(approveFollowsFilename+'.new')