epicyon/availability.py

161 lines
5.2 KiB
Python
Raw Normal View History

2020-04-01 19:39:27 +00:00
__filename__ = "availability.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
2021-01-26 10:07:42 +00:00
__version__ = "1.2.0"
2020-04-01 19:39:27 +00:00
__maintainer__ = "Bob Mottram"
2021-09-10 16:14:50 +00:00
__email__ = "bob@libreserver.org"
2020-04-01 19:39:27 +00:00
__status__ = "Production"
2021-06-26 11:16:41 +00:00
__module_group__ = "Profile Metadata"
2020-04-01 19:39:27 +00:00
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
2021-10-13 11:15:06 +00:00
from utils import hasObjectString
2021-12-26 12:45:03 +00:00
from utils import get_full_domain
2019-07-19 11:38:37 +00:00
from utils import getNicknameFromActor
from utils import getDomainFromActor
2019-10-22 11:55:06 +00:00
from utils import loadJson
from utils import saveJson
2021-12-26 12:02:29 +00:00
from utils import acct_dir
2021-12-26 10:19:59 +00:00
from utils import local_actor_url
from utils import hasActor
2019-07-19 11:38:37 +00:00
2020-04-01 19:39:27 +00:00
2021-12-25 16:17:53 +00:00
def setAvailability(base_dir: 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
2021-12-26 12:02:29 +00:00
actorFilename = acct_dir(base_dir, nickname, domain) + '.json'
2019-07-19 11:38:37 +00:00
if not os.path.isfile(actorFilename):
return False
2021-12-26 10:29:52 +00:00
actor_json = loadJson(actorFilename)
if actor_json:
actor_json['availability'] = status
saveJson(actor_json, actorFilename)
2019-07-19 11:38:37 +00:00
return True
2020-04-01 19:39:27 +00:00
2021-12-25 16:17:53 +00:00
def getAvailability(base_dir: str, nickname: str, domain: str) -> str:
2019-07-19 11:38:37 +00:00
"""Returns the availability for a given person
"""
2021-12-26 12:02:29 +00:00
actorFilename = acct_dir(base_dir, nickname, domain) + '.json'
2019-07-19 11:38:37 +00:00
if not os.path.isfile(actorFilename):
return False
2021-12-26 10:29:52 +00:00
actor_json = loadJson(actorFilename)
if actor_json:
if not actor_json.get('availability'):
2019-07-19 11:38:37 +00:00
return None
2021-12-26 10:29:52 +00:00
return actor_json['availability']
2019-07-19 11:38:37 +00:00
return None
2020-04-01 19:39:27 +00:00
2021-12-25 23:51:19 +00:00
def outboxAvailability(base_dir: str, nickname: str, message_json: {},
2019-11-03 15:27:29 +00:00
debug: bool) -> bool:
2019-07-19 11:38:37 +00:00
"""Handles receiving an availability update
"""
2021-12-25 23:51:19 +00:00
if not message_json.get('type'):
2019-07-19 11:38:37 +00:00
return False
2021-12-25 23:51:19 +00:00
if not message_json['type'] == 'Availability':
2019-07-19 11:38:37 +00:00
return False
2021-12-25 23:51:19 +00:00
if not hasActor(message_json, debug):
2019-07-19 11:38:37 +00:00
return False
2021-12-25 23:51:19 +00:00
if not hasObjectString(message_json, debug):
2019-07-19 11:38:37 +00:00
return False
2021-12-25 23:51:19 +00:00
actorNickname = getNicknameFromActor(message_json['actor'])
2020-04-01 19:39:27 +00:00
if actorNickname != nickname:
2019-07-19 11:38:37 +00:00
return False
2021-12-25 23:51:19 +00:00
domain, port = getDomainFromActor(message_json['actor'])
status = message_json['object'].replace('"', '')
2020-04-01 19:39:27 +00:00
2021-12-25 16:17:53 +00:00
return setAvailability(base_dir, nickname, domain, status)
2020-04-01 19:39:27 +00:00
2021-12-25 16:17:53 +00:00
def sendAvailabilityViaServer(base_dir: str, session,
2020-04-01 19:39:27 +00:00
nickname: str, password: str,
domain: str, port: int,
2021-12-25 17:09:22 +00:00
http_prefix: str,
2020-04-01 19:39:27 +00:00
status: str,
2021-12-25 22:28:18 +00:00
cached_webfingers: {}, person_cache: {},
2021-12-25 20:34:38 +00:00
debug: bool, project_version: str,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem: 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
2021-12-26 12:45:03 +00:00
domain_full = get_full_domain(domain, port)
2020-03-22 21:16:02 +00:00
2021-12-26 10:19:59 +00:00
toUrl = local_actor_url(http_prefix, nickname, domain_full)
2021-08-14 11:13:39 +00:00
ccUrl = toUrl + '/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',
2021-08-14 11:13:39 +00:00
'actor': toUrl,
2021-06-22 12:42:52 +00:00
'object': '"' + status + '"',
2019-07-19 11:38:37 +00:00
'to': [toUrl],
'cc': [ccUrl]
}
2021-12-26 10:00:46 +00:00
handle = http_prefix + '://' + domain_full + '/@' + nickname
2019-07-19 11:38:37 +00:00
# lookup the inbox for the To handle
2021-12-25 17:09:22 +00:00
wfRequest = webfingerHandle(session, handle, http_prefix,
2021-12-25 22:28:18 +00:00
cached_webfingers,
2021-12-25 20:34:38 +00:00
domain, project_version, debug, False,
2021-12-25 23:03:28 +00:00
signing_priv_key_pem)
2019-07-19 11:38:37 +00:00
if not wfRequest:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: availability 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):
2021-03-18 10:01:01 +00:00
print('WARN: availability webfinger for ' + handle +
' did not return a dict. ' + str(wfRequest))
2020-06-23 10:41:12 +00:00
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
2021-09-15 14:05:08 +00:00
originDomain = domain
(inboxUrl, pubKeyId, pubKey, fromPersonId, sharedInbox, avatarUrl,
2021-12-25 23:03:28 +00:00
displayName, _) = getPersonBox(signing_priv_key_pem,
originDomain,
2021-12-25 16:17:53 +00:00
base_dir, session, wfRequest,
2021-12-25 22:17:49 +00:00
person_cache, project_version,
2021-12-25 17:09:22 +00:00
http_prefix, nickname,
domain, postToBox, 57262)
2020-03-22 21:16:02 +00:00
2019-07-19 11:38:37 +00:00
if not inboxUrl:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: availability no ' + postToBox +
' was found for ' + handle)
2019-07-19 11:38:37 +00:00
return 3
if not fromPersonId:
if debug:
2021-03-18 10:01:01 +00:00
print('DEBUG: availability 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
}
2021-12-26 10:00:46 +00:00
postResult = postJson(http_prefix, domain_full,
2021-06-20 13:39:53 +00:00
session, newAvailabilityJson, [],
2021-03-10 19:24:52 +00:00
inboxUrl, headers, 30, True)
2020-04-01 19:39:27 +00:00
if not postResult:
2021-03-18 10:01:01 +00:00
print('WARN: availability failed to post')
2019-07-19 11:38:37 +00:00
if debug:
print('DEBUG: c2s POST availability success')
return newAvailabilityJson