epicyon/availability.py

156 lines
4.9 KiB
Python
Raw Normal View History

2020-04-01 19:39:27 +00:00
__filename__ = "availability.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2019-07-19 11:38:37 +00:00
import os
from webfinger import webfingerHandle
from auth import createBasicAuthHeader
from posts import getPersonBox
from session import postJson
from utils import getNicknameFromActor
from utils import getDomainFromActor
2019-10-22 11:55:06 +00:00
from utils import loadJson
from utils import saveJson
2019-07-19 11:38:37 +00:00
2020-04-01 19:39:27 +00:00
def setAvailability(baseDir: str, nickname: str, domain: str,
2019-07-19 11:38:37 +00:00
status: str) -> bool:
"""Set an availability status
"""
# avoid giant strings
2020-04-01 19:39:27 +00:00
if len(status) > 128:
2019-07-19 11:38:37 +00:00
return False
2020-04-01 19:39:27 +00:00
actorFilename = baseDir + '/accounts/' + nickname + '@' + domain + '.json'
2019-07-19 11:38:37 +00:00
if not os.path.isfile(actorFilename):
return False
2020-04-01 19:39:27 +00:00
actorJson = loadJson(actorFilename)
2019-09-30 22:39:02 +00:00
if actorJson:
2020-04-01 19:39:27 +00:00
actorJson['availability'] = status
saveJson(actorJson, actorFilename)
2019-07-19 11:38:37 +00:00
return True
2020-04-01 19:39:27 +00:00
def getAvailability(baseDir: str, nickname: str, domain: str) -> str:
2019-07-19 11:38:37 +00:00
"""Returns the availability for a given person
"""
2020-04-01 19:39:27 +00:00
actorFilename = baseDir + '/accounts/' + nickname + '@' + domain + '.json'
2019-07-19 11:38:37 +00:00
if not os.path.isfile(actorFilename):
return False
2020-04-01 19:39:27 +00:00
actorJson = loadJson(actorFilename)
2019-09-30 22:39:02 +00:00
if actorJson:
2019-07-19 11:38:37 +00:00
if not actorJson.get('availability'):
return None
return actorJson['availability']
return None
2020-04-01 19:39:27 +00:00
def outboxAvailability(baseDir: str, nickname: str, messageJson: {},
2019-11-03 15:27:29 +00:00
debug: bool) -> bool:
2019-07-19 11:38:37 +00:00
"""Handles receiving an availability update
"""
if not messageJson.get('type'):
return False
2020-04-01 19:39:27 +00:00
if not messageJson['type'] == 'Availability':
2019-07-19 11:38:37 +00:00
return False
if not messageJson.get('actor'):
return False
if not messageJson.get('object'):
return False
if not isinstance(messageJson['object'], str):
return False
2020-04-01 19:39:27 +00:00
actorNickname = getNicknameFromActor(messageJson['actor'])
if actorNickname != nickname:
2019-07-19 11:38:37 +00:00
return False
2020-04-01 19:39:27 +00:00
domain, port = getDomainFromActor(messageJson['actor'])
status = messageJson['object'].replace('"', '')
return setAvailability(baseDir, nickname, domain, status)
def sendAvailabilityViaServer(baseDir: str, session,
nickname: str, password: str,
domain: str, port: int,
httpPrefix: str,
status: str,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str) -> {}:
2019-07-19 11:38:37 +00:00
"""Sets the availability for a person via c2s
"""
if not session:
print('WARN: No session for sendAvailabilityViaServer')
return 6
2020-04-01 19:39:27 +00:00
domainFull = domain
if port:
2020-04-01 19:39:27 +00:00
if port != 80 and port != 443:
if ':' not in domain:
2020-04-01 19:39:27 +00:00
domainFull = domain + ':' + str(port)
2020-03-22 21:16:02 +00:00
2020-04-01 19:39:27 +00:00
toUrl = httpPrefix + '://' + domainFull + '/users/' + nickname
ccUrl = httpPrefix + '://' + domainFull + '/users/' + nickname + \
'/followers'
2019-07-19 11:38:37 +00:00
2020-04-01 19:39:27 +00:00
newAvailabilityJson = {
2019-07-19 11:38:37 +00:00
'type': 'Availability',
'actor': httpPrefix+'://'+domainFull+'/users/'+nickname,
'object': '"'+status+'"',
'to': [toUrl],
'cc': [ccUrl]
}
2020-04-01 19:39:27 +00:00
handle = httpPrefix + '://' + domainFull + '/@' + nickname
2019-07-19 11:38:37 +00:00
# lookup the inbox for the To handle
2020-04-01 19:39:27 +00:00
wfRequest = webfingerHandle(session, handle, httpPrefix,
cachedWebfingers,
domain, projectVersion)
2019-07-19 11:38:37 +00:00
if not wfRequest:
if debug:
2020-04-01 19:39:27 +00:00
print('DEBUG: announce webfinger failed for ' + handle)
2019-07-19 11:38:37 +00:00
return 1
2020-06-23 10:41:12 +00:00
if not isinstance(wfRequest, dict):
print('WARN: Webfinger for ' + handle + ' did not return a dict. ' +
str(wfRequest))
return 1
2019-07-19 11:38:37 +00:00
2020-04-01 19:39:27 +00:00
postToBox = 'outbox'
2019-07-19 11:38:37 +00:00
# get the actor inbox for the To handle
2020-04-01 19:39:27 +00:00
(inboxUrl, pubKeyId, pubKey,
fromPersonId, sharedInbox,
avatarUrl, displayName) = getPersonBox(baseDir, session, wfRequest,
personCache, projectVersion,
httpPrefix, nickname,
domain, postToBox)
2020-03-22 21:16:02 +00:00
2019-07-19 11:38:37 +00:00
if not inboxUrl:
if debug:
2020-04-01 19:39:27 +00:00
print('DEBUG: No ' + postToBox + ' was found for ' + handle)
2019-07-19 11:38:37 +00:00
return 3
if not fromPersonId:
if debug:
2020-04-01 19:39:27 +00:00
print('DEBUG: No actor was found for ' + handle)
2019-07-19 11:38:37 +00:00
return 4
2020-03-22 21:16:02 +00:00
2020-04-01 19:39:27 +00:00
authHeader = createBasicAuthHeader(nickname, password)
2020-03-22 21:16:02 +00:00
2020-04-01 19:39:27 +00:00
headers = {
'host': domain,
'Content-type': 'application/json',
2020-03-22 20:36:19 +00:00
'Authorization': authHeader
}
2020-04-01 19:39:27 +00:00
postResult = postJson(session, newAvailabilityJson, [],
2020-09-27 19:27:24 +00:00
inboxUrl, headers)
2020-04-01 19:39:27 +00:00
if not postResult:
print('WARN: failed to post availability')
2019-07-19 11:38:37 +00:00
if debug:
print('DEBUG: c2s POST availability success')
return newAvailabilityJson