manualapprove.py 6.4 KB

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