manualapprove.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 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. followJson['actor'],federationList, \
  51. followJson, \
  52. sendThreads,postLog, \
  53. cachedWebfingers,personCache, \
  54. debug,projectVersion)
  55. print('Follow request from '+denyHandle+' was denied.')
  56. def manualApproveFollowRequest(session,baseDir: str, \
  57. httpPrefix: str,
  58. nickname: str,domain: str,port: int, \
  59. approveHandle: str, \
  60. federationList: [], \
  61. sendThreads: [],postLog: [], \
  62. cachedWebfingers: {},personCache: {}, \
  63. acceptedCaps: [], \
  64. debug: bool, \
  65. projectVersion: str) -> None:
  66. """Manually approve a follow request
  67. """
  68. print('Manually approving follow request from '+approveHandle)
  69. handle=nickname+'@'+domain
  70. accountsDir=baseDir+'/accounts/'+handle
  71. approveFollowsFilename=accountsDir+'/followrequests.txt'
  72. if not os.path.isfile(approveFollowsFilename):
  73. if debug:
  74. print('WARN: Follow requests file '+approveFollowsFilename+' not found')
  75. return
  76. if approveHandle not in open(approveFollowsFilename).read():
  77. if debug:
  78. print(handle+' not in '+approveFollowsFilename)
  79. return
  80. approvefilenew = open(approveFollowsFilename+'.new', 'w+')
  81. updateApprovedFollowers=False
  82. with open(approveFollowsFilename, 'r') as approvefile:
  83. for handle in approvefile:
  84. if handle.startswith(approveHandle):
  85. handle=handle.replace('\n','')
  86. port2=port
  87. if ':' in handle:
  88. port2=int(handle.split(':')[1])
  89. requestsDir=accountsDir+'/requests'
  90. followActivityfilename=requestsDir+'/'+handle+'.follow'
  91. if os.path.isfile(followActivityfilename):
  92. followJson=loadJson(followActivityfilename)
  93. if followJson:
  94. approveNickname=approveHandle.split('@')[0]
  95. approveDomain=approveHandle.split('@')[1].replace('\n','')
  96. approvePort=port2
  97. if ':' in approveDomain:
  98. approvePort=approveDomain.split(':')[1]
  99. approveDomain=approveDomain.split(':')[0]
  100. print('Sending Accept for '+handle+' follow request from '+approveHandle)
  101. followedAccountAccepts(session,baseDir,httpPrefix, \
  102. nickname,domain,port, \
  103. approveNickname,approveDomain,approvePort, \
  104. followJson['actor'],federationList, \
  105. followJson,acceptedCaps, \
  106. sendThreads,postLog, \
  107. cachedWebfingers,personCache, \
  108. debug,projectVersion)
  109. os.remove(followActivityfilename)
  110. updateApprovedFollowers=True
  111. else:
  112. approvefilenew.write(handle)
  113. approvefilenew.close()
  114. os.rename(approveFollowsFilename+'.new',approveFollowsFilename)
  115. if updateApprovedFollowers:
  116. # update the followers
  117. followersFilename=accountsDir+'/followers.txt'
  118. if os.path.isfile(followersFilename):
  119. if approveHandle not in open(followersFilename).read():
  120. try:
  121. with open(followersFilename, 'r+') as followersFile:
  122. content = followersFile.read()
  123. followersFile.seek(0, 0)
  124. followersFile.write(approveHandle+'\n'+content)
  125. except Exception as e:
  126. print('WARN: Failed to write entry to followers file '+str(e))
  127. else:
  128. followersFile=open(followersFilename, "w+")
  129. followersFile.write(approveHandle+'\n')
  130. followersFile.close()