availability.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. __filename__ = "availability.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 json
  9. import commentjson
  10. import os
  11. from webfinger import webfingerHandle
  12. from auth import createBasicAuthHeader
  13. from posts import getPersonBox
  14. from session import postJson
  15. from utils import getNicknameFromActor
  16. from utils import getDomainFromActor
  17. def setAvailability(baseDir: str,nickname: str,domain: str, \
  18. status: str) -> bool:
  19. """Set an availability status
  20. """
  21. # avoid giant strings
  22. if len(status)>128:
  23. return False
  24. actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
  25. if not os.path.isfile(actorFilename):
  26. return False
  27. with open(actorFilename, 'r') as fp:
  28. actorJson=commentjson.load(fp)
  29. actorJson['availability']=status
  30. with open(actorFilename, 'w') as fp:
  31. commentjson.dump(actorJson, fp, indent=4, sort_keys=False)
  32. return True
  33. def getAvailability(baseDir: str,nickname: str,domain: str) -> str:
  34. """Returns the availability for a given person
  35. """
  36. actorFilename=baseDir+'/accounts/'+nickname+'@'+domain+'.json'
  37. if not os.path.isfile(actorFilename):
  38. return False
  39. with open(actorFilename, 'r') as fp:
  40. actorJson=commentjson.load(fp)
  41. if not actorJson.get('availability'):
  42. return None
  43. return actorJson['availability']
  44. return None
  45. def outboxAvailability(baseDir: str,nickname: str,messageJson: {},debug: bool) -> bool:
  46. """Handles receiving an availability update
  47. """
  48. if not messageJson.get('type'):
  49. return False
  50. if not messageJson['type']=='Availability':
  51. return False
  52. if not messageJson.get('actor'):
  53. return False
  54. if not messageJson.get('object'):
  55. return False
  56. if not isinstance(messageJson['object'], str):
  57. return False
  58. actorNickname=getNicknameFromActor(messageJson['actor'])
  59. if actorNickname!=nickname:
  60. return False
  61. domain,port=getDomainFromActor(messageJson['actor'])
  62. status=messageJson['object'].replace('"','')
  63. return setAvailability(baseDir,nickname,domain,status)
  64. def sendAvailabilityViaServer(baseDir: str,session, \
  65. nickname: str,password: str, \
  66. domain: str,port: int, \
  67. httpPrefix: str, \
  68. status: str, \
  69. cachedWebfingers: {},personCache: {}, \
  70. debug: bool,projectVersion: str) -> {}:
  71. """Sets the availability for a person via c2s
  72. """
  73. if not session:
  74. print('WARN: No session for sendAvailabilityViaServer')
  75. return 6
  76. domainFull=domain
  77. if port:
  78. if port!=80 and port!=443:
  79. if ':' not in domain:
  80. domainFull=domain+':'+str(port)
  81. toUrl = httpPrefix+'://'+domainFull+'/users/'+nickname
  82. ccUrl = httpPrefix+'://'+domainFull+'/users/'+nickname+'/followers'
  83. newAvailabilityJson = {
  84. 'type': 'Availability',
  85. 'actor': httpPrefix+'://'+domainFull+'/users/'+nickname,
  86. 'object': '"'+status+'"',
  87. 'to': [toUrl],
  88. 'cc': [ccUrl]
  89. }
  90. handle=httpPrefix+'://'+domainFull+'/@'+nickname
  91. # lookup the inbox for the To handle
  92. wfRequest = webfingerHandle(session,handle,httpPrefix,cachedWebfingers, \
  93. domain,projectVersion)
  94. if not wfRequest:
  95. if debug:
  96. print('DEBUG: announce webfinger failed for '+handle)
  97. return 1
  98. postToBox='outbox'
  99. # get the actor inbox for the To handle
  100. inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition,avatarUrl,displayName = \
  101. getPersonBox(baseDir,session,wfRequest,personCache, \
  102. projectVersion,httpPrefix,domain,postToBox)
  103. if not inboxUrl:
  104. if debug:
  105. print('DEBUG: No '+postToBox+' was found for '+handle)
  106. return 3
  107. if not fromPersonId:
  108. if debug:
  109. print('DEBUG: No actor was found for '+handle)
  110. return 4
  111. authHeader=createBasicAuthHeader(Nickname,password)
  112. headers = {'host': domain, \
  113. 'Content-type': 'application/json', \
  114. 'Authorization': authHeader}
  115. postResult = \
  116. postJson(session,newAvailabilityJson,[],inboxUrl,headers,"inbox:write")
  117. #if not postResult:
  118. # if debug:
  119. # print('DEBUG: POST announce failed for c2s to '+inboxUrl)
  120. # return 5
  121. if debug:
  122. print('DEBUG: c2s POST availability success')
  123. return newAvailabilityJson