epicyon/delete.py

296 lines
10 KiB
Python
Raw Normal View History

2020-04-03 08:50:43 +00:00
__filename__ = "delete.py"
__author__ = "Bob Mottram"
__license__ = "AGPL3+"
__version__ = "1.1.0"
__maintainer__ = "Bob Mottram"
__email__ = "bob@freedombone.net"
__status__ = "Production"
2019-07-11 21:38:28 +00:00
from utils import getStatusNumber
from utils import urlPermitted
from utils import getNicknameFromActor
from utils import getDomainFromActor
2019-07-17 17:16:48 +00:00
from utils import locatePost
from utils import deletePost
2019-08-12 18:02:29 +00:00
from utils import removeModerationPostFromIndex
2019-07-11 21:38:28 +00:00
from posts import sendSignedJson
2019-07-17 17:16:48 +00:00
from session import postJson
from webfinger import webfingerHandle
from auth import createBasicAuthHeader
from posts import getPersonBox
2019-07-11 21:38:28 +00:00
2020-04-03 08:50:43 +00:00
def createDelete(session, baseDir: str, federationList: [],
nickname: str, domain: str, port: int,
toUrl: str, ccUrl: str, httpPrefix: str,
objectUrl: str, clientToServer: bool,
sendThreads: [], postLog: [],
personCache: {}, cachedWebfingers: {},
2019-07-11 21:38:28 +00:00
debug: bool) -> {}:
2019-07-12 09:49:12 +00:00
"""Creates a delete message
2019-07-11 21:38:28 +00:00
Typically toUrl will be https://www.w3.org/ns/activitystreams#Public
and ccUrl might be a specific person whose post is to be deleted
objectUrl is typically the url of the message, corresponding to url
or atomUri in createPostBase
"""
2020-04-03 08:50:43 +00:00
if not urlPermitted(objectUrl, federationList, "inbox:write"):
2019-07-11 21:38:28 +00:00
return None
if ':' in domain:
2020-04-03 08:50:43 +00:00
domain = domain.split(':')[0]
fullDomain = domain
if port:
2020-04-03 08:50:43 +00:00
if port != 80 and port != 443:
if ':' not in domain:
2020-04-03 08:50:43 +00:00
fullDomain = domain + ':' + str(port)
2019-07-11 21:38:28 +00:00
2020-04-03 08:50:43 +00:00
statusNumber, published = getStatusNumber()
newDeleteId = \
httpPrefix + '://' + fullDomain + '/users/' + \
nickname + '/statuses/' + statusNumber
newDelete = {
2019-08-18 11:07:06 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
2019-07-11 21:38:28 +00:00
'actor': httpPrefix+'://'+fullDomain+'/users/'+nickname,
2020-04-03 08:50:43 +00:00
'atomUri': newDeleteId,
2019-07-11 21:38:28 +00:00
'cc': [],
2020-04-03 08:50:43 +00:00
'id': newDeleteId + '/activity',
2019-07-11 21:38:28 +00:00
'object': objectUrl,
'published': published,
'to': [toUrl],
'type': 'Delete'
}
if ccUrl:
2020-04-03 08:50:43 +00:00
if len(ccUrl) > 0:
newDelete['cc'] = [ccUrl]
2019-07-11 21:38:28 +00:00
2020-04-03 08:50:43 +00:00
deleteNickname = None
deleteDomain = None
deletePort = None
2019-10-17 22:26:47 +00:00
if '/users/' in objectUrl or \
'/channel/' in objectUrl or \
'/profile/' in objectUrl:
2020-04-03 08:50:43 +00:00
deleteNickname = getNicknameFromActor(objectUrl)
deleteDomain, deletePort = getDomainFromActor(objectUrl)
2019-07-11 21:38:28 +00:00
if deleteNickname and deleteDomain:
2020-04-03 08:50:43 +00:00
sendSignedJson(newDelete, session, baseDir,
nickname, domain, port,
deleteNickname, deleteDomain, deletePort,
'https://www.w3.org/ns/activitystreams#Public',
httpPrefix, True, clientToServer, federationList,
sendThreads, postLog, cachedWebfingers,
personCache, debug)
2020-03-22 21:16:02 +00:00
2019-07-11 21:38:28 +00:00
return newDelete
2020-04-03 08:50:43 +00:00
def sendDeleteViaServer(baseDir: str, session,
fromNickname: str, password: str,
fromDomain: str, fromPort: int,
httpPrefix: str, deleteObjectUrl: str,
cachedWebfingers: {}, personCache: {},
debug: bool, projectVersion: str) -> {}:
2019-07-17 17:16:48 +00:00
"""Creates a delete request message via c2s
"""
if not session:
print('WARN: No session for sendDeleteViaServer')
return 6
2020-04-03 08:50:43 +00:00
fromDomainFull = fromDomain
if fromPort:
2020-04-03 08:50:43 +00:00
if fromPort != 80 and fromPort != 443:
if ':' not in fromDomain:
2020-04-03 08:50:43 +00:00
fromDomainFull = fromDomain + ':' + str(fromPort)
2019-07-17 17:16:48 +00:00
2020-04-03 08:50:43 +00:00
actor = httpPrefix + '://' + fromDomainFull + \
'/users/' + fromNickname
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl = actor + '/followers'
2019-07-17 17:16:48 +00:00
2020-04-03 08:50:43 +00:00
newDeleteJson = {
2019-08-18 11:07:06 +00:00
"@context": "https://www.w3.org/ns/activitystreams",
2020-04-03 08:50:43 +00:00
'actor': actor,
2019-07-17 17:16:48 +00:00
'cc': [ccUrl],
'object': deleteObjectUrl,
'to': [toUrl],
'type': 'Delete'
}
2020-04-03 08:50:43 +00:00
handle = httpPrefix + '://' + fromDomainFull + '/@' + fromNickname
2019-07-17 17:16:48 +00:00
# lookup the inbox for the To handle
2020-04-03 08:50:43 +00:00
wfRequest = \
webfingerHandle(session, handle, httpPrefix, cachedWebfingers,
fromDomain, projectVersion)
2019-07-17 17:16:48 +00:00
if not wfRequest:
if debug:
2020-04-03 08:50:43 +00:00
print('DEBUG: announce webfinger failed for ' + handle)
2019-07-17 17:16:48 +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-17 17:16:48 +00:00
2020-04-03 08:50:43 +00:00
postToBox = 'outbox'
2019-07-17 17:16:48 +00:00
# get the actor inbox for the To handle
2020-04-03 08:50:43 +00:00
(inboxUrl, pubKeyId, pubKey,
fromPersonId, sharedInbox,
capabilityAcquisition, avatarUrl,
displayName) = getPersonBox(baseDir, session, wfRequest, personCache,
projectVersion, httpPrefix, fromNickname,
fromDomain, postToBox)
2020-03-22 21:16:02 +00:00
2019-07-17 17:16:48 +00:00
if not inboxUrl:
if debug:
2020-04-03 08:50:43 +00:00
print('DEBUG: No ' + postToBox + ' was found for ' + handle)
2019-07-17 17:16:48 +00:00
return 3
if not fromPersonId:
if debug:
2020-04-03 08:50:43 +00:00
print('DEBUG: No actor was found for ' + handle)
2019-07-17 17:16:48 +00:00
return 4
2020-03-22 21:16:02 +00:00
2020-04-03 08:50:43 +00:00
authHeader = createBasicAuthHeader(fromNickname, password)
2020-03-22 21:16:02 +00:00
2020-04-03 08:50:43 +00:00
headers = {
'host': fromDomain,
'Content-type': 'application/json',
2020-03-22 20:36:19 +00:00
'Authorization': authHeader
}
2020-04-03 08:50:43 +00:00
postResult = \
postJson(session, newDeleteJson, [], inboxUrl, headers, "inbox:write")
if not postResult:
if debug:
print('DEBUG: POST announce failed for c2s to ' + inboxUrl)
return 5
2019-07-17 17:16:48 +00:00
if debug:
print('DEBUG: c2s POST delete request success')
return newDeleteJson
2020-04-03 08:50:43 +00:00
def deletePublic(session, baseDir: str, federationList: [],
nickname: str, domain: str, port: int, httpPrefix: str,
objectUrl: str, clientToServer: bool,
sendThreads: [], postLog: [],
personCache: {}, cachedWebfingers: {},
2019-07-11 21:38:28 +00:00
debug: bool) -> {}:
"""Makes a public delete activity
"""
2020-04-03 08:50:43 +00:00
fromDomain = domain
if port:
2020-04-03 08:50:43 +00:00
if port != 80 and port != 443:
if ':' not in domain:
2020-04-03 08:50:43 +00:00
fromDomain = domain + ':' + str(port)
2019-07-11 21:38:28 +00:00
2020-04-03 08:50:43 +00:00
toUrl = 'https://www.w3.org/ns/activitystreams#Public'
ccUrl = httpPrefix + '://' + fromDomain + \
'/users/' + nickname + '/followers'
return createDelete(session, baseDir, federationList,
nickname, domain, port,
toUrl, ccUrl, httpPrefix,
objectUrl, clientToServer,
sendThreads, postLog,
personCache, cachedWebfingers,
2019-07-11 21:38:28 +00:00
debug)
2020-04-03 08:50:43 +00:00
def deletePostPub(session, baseDir: str, federationList: [],
nickname: str, domain: str, port: int, httpPrefix: str,
deleteNickname: str, deleteDomain: str,
deletePort: int, deleteHttpsPrefix: str,
deleteStatusNumber: int, clientToServer: bool,
sendThreads: [], postLog: [],
personCache: {}, cachedWebfingers: {},
2019-07-17 17:16:48 +00:00
debug: bool) -> {}:
2019-07-11 21:38:28 +00:00
"""Deletes a given status post
"""
2020-04-03 08:50:43 +00:00
deletedDomain = deleteDomain
if deletePort:
2020-04-03 08:50:43 +00:00
if deletePort != 80 and deletePort != 443:
if ':' not in deletedDomain:
2020-04-03 08:50:43 +00:00
deletedDomain = deletedDomain + ':' + str(deletePort)
2019-07-11 21:38:28 +00:00
2020-04-03 08:50:43 +00:00
objectUrl = \
deleteHttpsPrefix + '://' + deletedDomain + '/users/' + \
deleteNickname + '/statuses/' + str(deleteStatusNumber)
2019-07-11 21:38:28 +00:00
2020-04-03 08:50:43 +00:00
return deletePublic(session, baseDir, federationList,
nickname, domain, port, httpPrefix,
objectUrl, clientToServer,
sendThreads, postLog,
personCache, cachedWebfingers,
2019-07-11 21:38:28 +00:00
debug)
2020-04-03 08:50:43 +00:00
def outboxDelete(baseDir: str, httpPrefix: str,
nickname: str, domain: str,
messageJson: {}, debug: bool,
allowDeletion: bool,
recentPostsCache: {}) -> None:
""" When a delete request is received by the outbox from c2s
2019-07-17 17:16:48 +00:00
"""
if not messageJson.get('type'):
if debug:
print('DEBUG: delete - no type')
return
2020-04-03 08:50:43 +00:00
if not messageJson['type'] == 'Delete':
2019-07-17 17:16:48 +00:00
if debug:
print('DEBUG: not a delete')
return
if not messageJson.get('object'):
if debug:
print('DEBUG: no object in delete')
return
if not isinstance(messageJson['object'], str):
if debug:
print('DEBUG: delete object is not string')
return
if debug:
print('DEBUG: c2s delete request arrived in outbox')
2020-04-03 08:50:43 +00:00
deletePrefix = httpPrefix + '://' + domain
if (not allowDeletion and
(not messageJson['object'].startswith(deletePrefix) or
not messageJson['actor'].startswith(deletePrefix))):
2019-08-12 18:02:29 +00:00
if debug:
print('DEBUG: delete not permitted from other instances')
return
2020-04-03 08:50:43 +00:00
messageId = messageJson['object'].replace('/activity', '')
2019-07-17 17:16:48 +00:00
if '/statuses/' not in messageId:
if debug:
print('DEBUG: c2s delete object is not a status')
return
2019-10-17 22:26:47 +00:00
if '/users/' not in messageId and \
'/channel/' not in messageId and \
'/profile/' not in messageId:
2019-07-17 17:16:48 +00:00
if debug:
print('DEBUG: c2s delete object has no nickname')
return
2020-04-03 08:50:43 +00:00
deleteNickname = getNicknameFromActor(messageId)
if deleteNickname != nickname:
if debug:
2020-04-03 08:50:43 +00:00
print("DEBUG: you can't delete a post which " +
"wasn't created by you (nickname does not match)")
2020-03-22 21:16:02 +00:00
return
2020-04-03 08:50:43 +00:00
deleteDomain, deletePort = getDomainFromActor(messageId)
if ':' in domain:
2020-04-03 08:50:43 +00:00
domain = domain.split(':')[0]
if deleteDomain != domain:
if debug:
2020-04-03 08:50:43 +00:00
print("DEBUG: you can't delete a post which " +
"wasn't created by you (domain does not match)")
2020-03-22 21:16:02 +00:00
return
2020-04-03 08:50:43 +00:00
removeModerationPostFromIndex(baseDir, messageId, debug)
postFilename = locatePost(baseDir, deleteNickname, deleteDomain,
messageId)
2019-07-17 17:16:48 +00:00
if not postFilename:
if debug:
print('DEBUG: c2s delete post not found in inbox or outbox')
print(messageId)
return True
2020-04-03 08:50:43 +00:00
deletePost(baseDir, httpPrefix, deleteNickname, deleteDomain,
postFilename, debug, recentPostsCache)
2019-07-17 17:16:48 +00:00
if debug:
2020-04-03 08:50:43 +00:00
print('DEBUG: post deleted via c2s - ' + postFilename)