manualapprove.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. __filename__ = "manualapprove.py"
  2. __author__ = "Bob Mottram"
  3. __license__ = "AGPL3+"
  4. __version__ = "1.0.0"
  5. __maintainer__ = "Bob Mottram"
  6. __email__ = "bob@freedombone.net"
  7. __status__ = "Production"
  8. import os
  9. import json
  10. import commentjson
  11. import time
  12. from follow import followedAccountAccepts
  13. from follow import followedAccountRejects
  14. from follow import removeFromFollowRequests
  15. from utils import loadJson
  16. from utils import saveJson
  17. def manualDenyFollowRequest(session,baseDir: str, \
  18. httpPrefix: str,
  19. nickname: str,domain: str,port: int, \
  20. denyHandle: str, \
  21. federationList: [], \
  22. sendThreads: [],postLog: [], \
  23. cachedWebfingers: {},personCache: {}, \
  24. debug: bool, \
  25. projectVersion: str) -> None:
  26. """Manually deny a follow request
  27. """
  28. handle=nickname+'@'+domain
  29. accountsDir=baseDir+'/accounts/'+handle
  30. # has this handle already been rejected?
  31. rejectedFollowsFilename=accountsDir+'/followrejects.txt'
  32. if os.path.isfile(rejectedFollowsFilename):
  33. if denyHandle in open(rejectedFollowsFilename).read():
  34. removeFromFollowRequests(baseDir,nickname,domain,denyHandle,debug)
  35. print(denyHandle+' has already been rejected as a follower of '+nickname)
  36. return
  37. removeFromFollowRequests(baseDir,nickname,domain,denyHandle,debug)
  38. # Store rejected follows
  39. rejectsFile=open(rejectedFollowsFilename, "a+")
  40. rejectsFile.write(denyHandle+'\n')
  41. rejectsFile.close()
  42. denyNickname=denyHandle.split('@')[0]
  43. approveDomain=denyHandle.split('@')[1].replace('\n','')
  44. approvePort=port
  45. if ':' in denyDomain:
  46. denyPort=denyDomain.split(':')[1]
  47. denyDomain=denyDomain.split(':')[0]
  48. followedAccountRejects(session,baseDir,httpPrefix, \
  49. nickname,domain,port, \
  50. denyNickname,denyDomain,denyPort, \
  51. followJson['actor'],federationList, \
  52. followJson, \
  53. sendThreads,postLog, \
  54. cachedWebfingers,personCache, \
  55. debug,projectVersion)
  56. print('Follow request from '+denyHandle+' was denied.')
  57. def manualApproveFollowRequest(session,baseDir: str, \
  58. httpPrefix: str,
  59. nickname: str,domain: str,port: int, \
  60. approveHandle: str, \
  61. federationList: [], \
  62. sendThreads: [],postLog: [], \
  63. cachedWebfingers: {},personCache: {}, \
  64. acceptedCaps: [], \
  65. debug: bool, \
  66. projectVersion: str) -> None:
  67. """Manually approve a follow request
  68. """
  69. print('Manually approving follow request from '+approveHandle)
  70. handle=nickname+'@'+domain
  71. accountsDir=baseDir+'/accounts/'+handle
  72. approveFollowsFilename=accountsDir+'/followrequests.txt'
  73. if not os.path.isfile(approveFollowsFilename):
  74. if debug:
  75. print('WARN: Follow requests file '+approveFollowsFilename+' not found')
  76. return
  77. if approveHandle not in open(approveFollowsFilename).read():
  78. if debug:
  79. print(handle+' not in '+approveFollowsFilename)
  80. return
  81. approvefilenew = open(approveFollowsFilename+'.new', 'w+')
  82. updateApprovedFollowers=False
  83. with open(approveFollowsFilename, 'r') as approvefile:
  84. for handle in approvefile:
  85. if handle.startswith(approveHandle):
  86. handle=handle.replace('\n','')
  87. port2=port
  88. if ':' in handle:
  89. port2=int(handle.split(':')[1])
  90. requestsDir=accountsDir+'/requests'
  91. followActivityfilename=requestsDir+'/'+handle+'.follow'
  92. if os.path.isfile(followActivityfilename):
  93. followJson=loadJson(followActivityfilename)
  94. if followJson:
  95. approveNickname=approveHandle.split('@')[0]
  96. approveDomain=approveHandle.split('@')[1].replace('\n','')
  97. approvePort=port2
  98. if ':' in approveDomain:
  99. approvePort=approveDomain.split(':')[1]
  100. approveDomain=approveDomain.split(':')[0]
  101. print('Sending Accept for '+handle+' follow request from '+approveHandle)
  102. followedAccountAccepts(session,baseDir,httpPrefix, \
  103. nickname,domain,port, \
  104. approveNickname,approveDomain,approvePort, \
  105. followJson['actor'],federationList, \
  106. followJson,acceptedCaps, \
  107. sendThreads,postLog, \
  108. cachedWebfingers,personCache, \
  109. debug,projectVersion)
  110. os.remove(followActivityfilename)
  111. updateApprovedFollowers=True
  112. else:
  113. approvefilenew.write(handle)
  114. approvefilenew.close()
  115. os.rename(approveFollowsFilename+'.new',approveFollowsFilename)
  116. if updateApprovedFollowers:
  117. # update the followers
  118. followersFilename=accountsDir+'/followers.txt'
  119. if os.path.isfile(followersFilename):
  120. if approveHandle not in open(followersFilename).read():
  121. try:
  122. with open(followersFilename, 'r+') as followersFile:
  123. content = followersFile.read()
  124. followersFile.seek(0, 0)
  125. followersFile.write(approveHandle+'\n'+content)
  126. except Exception as e:
  127. print('WARN: Failed to write entry to followers file '+str(e))
  128. else:
  129. followersFile=open(followersFilename, "w+")
  130. followersFile.write(approveHandle+'\n')
  131. followersFile.close()