2020-04-01 18:16:29 +00:00
|
|
|
__filename__ = "acceptreject.py"
|
|
|
|
__author__ = "Bob Mottram"
|
|
|
|
__license__ = "AGPL3+"
|
2021-01-26 10:07:42 +00:00
|
|
|
__version__ = "1.2.0"
|
2020-04-01 18:16:29 +00:00
|
|
|
__maintainer__ = "Bob Mottram"
|
2021-09-10 16:14:50 +00:00
|
|
|
__email__ = "bob@libreserver.org"
|
2020-04-01 18:16:29 +00:00
|
|
|
__status__ = "Production"
|
2021-06-15 15:08:12 +00:00
|
|
|
__module_group__ = "ActivityPub"
|
2019-07-02 11:31:26 +00:00
|
|
|
|
2020-03-01 00:04:27 +00:00
|
|
|
import os
|
2021-10-13 10:37:52 +00:00
|
|
|
from utils import hasObjectStringObject
|
2020-12-23 10:57:44 +00:00
|
|
|
from utils import hasUsersPath
|
2020-12-16 10:30:54 +00:00
|
|
|
from utils import getFullDomain
|
2019-07-02 11:31:26 +00:00
|
|
|
from utils import urlPermitted
|
2019-07-06 15:17:21 +00:00
|
|
|
from utils import getDomainFromActor
|
|
|
|
from utils import getNicknameFromActor
|
|
|
|
from utils import domainPermitted
|
2019-07-06 19:24:52 +00:00
|
|
|
from utils import followPerson
|
2021-07-13 21:59:53 +00:00
|
|
|
from utils import acctDir
|
2021-07-31 11:56:28 +00:00
|
|
|
from utils import hasGroupType
|
2021-08-14 11:13:39 +00:00
|
|
|
from utils import localActorUrl
|
2021-10-13 09:33:15 +00:00
|
|
|
from utils import hasActor
|
2021-10-13 10:11:02 +00:00
|
|
|
from utils import hasObjectStringType
|
2019-07-02 11:31:26 +00:00
|
|
|
|
2020-04-01 18:16:29 +00:00
|
|
|
|
2021-12-25 23:45:30 +00:00
|
|
|
def _create_accept_reject(base_dir: str, federation_list: [],
|
|
|
|
nickname: str, domain: str, port: int,
|
|
|
|
toUrl: str, ccUrl: str, http_prefix: str,
|
|
|
|
objectJson: {}, acceptType: str) -> {}:
|
2019-07-06 17:00:22 +00:00
|
|
|
"""Accepts or rejects something (eg. a follow request or offer)
|
2019-07-02 11:31:26 +00:00
|
|
|
Typically toUrl will be https://www.w3.org/ns/activitystreams#Public
|
2019-07-06 17:00:22 +00:00
|
|
|
and ccUrl might be a specific person favorited or repeated and
|
|
|
|
the followers url objectUrl is typically the url of the message,
|
|
|
|
corresponding to url or atomUri in createPostBase
|
2019-07-02 11:31:26 +00:00
|
|
|
"""
|
2019-07-06 19:24:52 +00:00
|
|
|
if not objectJson.get('actor'):
|
|
|
|
return None
|
|
|
|
|
2021-12-25 23:45:30 +00:00
|
|
|
if not urlPermitted(objectJson['actor'], federation_list):
|
2019-07-02 11:31:26 +00:00
|
|
|
return None
|
|
|
|
|
2020-12-16 10:30:54 +00:00
|
|
|
domain = getFullDomain(domain, port)
|
2019-07-02 11:31:26 +00:00
|
|
|
|
2021-12-25 23:51:19 +00:00
|
|
|
new_accept = {
|
2019-08-18 11:07:06 +00:00
|
|
|
"@context": "https://www.w3.org/ns/activitystreams",
|
2019-07-02 11:31:26 +00:00
|
|
|
'type': acceptType,
|
2021-12-25 17:09:22 +00:00
|
|
|
'actor': localActorUrl(http_prefix, nickname, domain),
|
2019-07-02 11:31:26 +00:00
|
|
|
'to': [toUrl],
|
|
|
|
'cc': [],
|
2019-07-06 19:24:52 +00:00
|
|
|
'object': objectJson
|
2019-07-02 11:31:26 +00:00
|
|
|
}
|
|
|
|
if ccUrl:
|
2020-04-01 18:16:29 +00:00
|
|
|
if len(ccUrl) > 0:
|
2021-12-25 23:51:19 +00:00
|
|
|
new_accept['cc'] = [ccUrl]
|
|
|
|
return new_accept
|
2019-07-02 11:31:26 +00:00
|
|
|
|
2020-04-01 18:16:29 +00:00
|
|
|
|
2021-12-25 23:45:30 +00:00
|
|
|
def createAccept(base_dir: str, federation_list: [],
|
2020-04-01 18:16:29 +00:00
|
|
|
nickname: str, domain: str, port: int,
|
2021-12-25 17:09:22 +00:00
|
|
|
toUrl: str, ccUrl: str, http_prefix: str,
|
2020-09-27 19:27:24 +00:00
|
|
|
objectJson: {}) -> {}:
|
2021-12-25 23:45:30 +00:00
|
|
|
return _create_accept_reject(base_dir, federation_list,
|
|
|
|
nickname, domain, port,
|
|
|
|
toUrl, ccUrl, http_prefix,
|
|
|
|
objectJson, 'Accept')
|
2020-04-01 18:16:29 +00:00
|
|
|
|
|
|
|
|
2021-12-25 23:45:30 +00:00
|
|
|
def createReject(base_dir: str, federation_list: [],
|
2020-04-01 18:16:29 +00:00
|
|
|
nickname: str, domain: str, port: int,
|
2021-12-25 17:09:22 +00:00
|
|
|
toUrl: str, ccUrl: str, http_prefix: str,
|
2019-07-06 19:24:52 +00:00
|
|
|
objectJson: {}) -> {}:
|
2021-12-25 23:45:30 +00:00
|
|
|
return _create_accept_reject(base_dir, federation_list,
|
|
|
|
nickname, domain, port,
|
|
|
|
toUrl, ccUrl,
|
|
|
|
http_prefix, objectJson, 'Reject')
|
2019-07-06 19:24:52 +00:00
|
|
|
|
2020-04-01 18:16:29 +00:00
|
|
|
|
2021-12-26 00:07:44 +00:00
|
|
|
def _accept_follow(base_dir: str, domain: str, message_json: {},
|
|
|
|
federation_list: [], debug: bool) -> None:
|
2019-07-20 08:33:18 +00:00
|
|
|
"""Receiving a follow Accept activity
|
|
|
|
"""
|
2021-12-25 23:51:19 +00:00
|
|
|
if not hasObjectStringType(message_json, debug):
|
2020-03-22 21:16:02 +00:00
|
|
|
return
|
2021-12-25 23:51:19 +00:00
|
|
|
if not message_json['object']['type'] == 'Follow':
|
|
|
|
if not message_json['object']['type'] == 'Join':
|
2021-02-08 14:48:37 +00:00
|
|
|
return
|
2019-11-12 14:18:23 +00:00
|
|
|
if debug:
|
|
|
|
print('DEBUG: receiving Follow activity')
|
2021-12-25 23:51:19 +00:00
|
|
|
if not message_json['object'].get('actor'):
|
2019-11-12 14:18:23 +00:00
|
|
|
print('DEBUG: no actor in Follow activity')
|
2019-07-06 19:24:52 +00:00
|
|
|
return
|
|
|
|
# no, this isn't a mistake
|
2021-12-25 23:51:19 +00:00
|
|
|
if not hasObjectStringObject(message_json, debug):
|
2020-03-22 21:16:02 +00:00
|
|
|
return
|
2021-12-25 23:51:19 +00:00
|
|
|
if not message_json.get('to'):
|
2019-07-07 13:53:12 +00:00
|
|
|
if debug:
|
|
|
|
print('DEBUG: No "to" parameter in follow Accept')
|
|
|
|
return
|
2019-07-06 19:24:52 +00:00
|
|
|
if debug:
|
|
|
|
print('DEBUG: follow Accept received')
|
2021-12-26 00:07:44 +00:00
|
|
|
this_actor = message_json['object']['actor']
|
|
|
|
nickname = getNicknameFromActor(this_actor)
|
2019-09-02 09:43:43 +00:00
|
|
|
if not nickname:
|
2021-12-26 00:07:44 +00:00
|
|
|
print('WARN: no nickname found in ' + this_actor)
|
2019-09-02 09:43:43 +00:00
|
|
|
return
|
2021-12-26 00:07:44 +00:00
|
|
|
acceptedDomain, acceptedPort = getDomainFromActor(this_actor)
|
2019-07-06 19:24:52 +00:00
|
|
|
if not acceptedDomain:
|
|
|
|
if debug:
|
2021-12-26 00:07:44 +00:00
|
|
|
print('DEBUG: domain not found in ' + this_actor)
|
2019-07-06 19:24:52 +00:00
|
|
|
return
|
|
|
|
if not nickname:
|
|
|
|
if debug:
|
2021-12-26 00:07:44 +00:00
|
|
|
print('DEBUG: nickname not found in ' + this_actor)
|
2019-07-06 19:24:52 +00:00
|
|
|
return
|
|
|
|
if acceptedPort:
|
2020-04-01 18:16:29 +00:00
|
|
|
if '/' + acceptedDomain + ':' + str(acceptedPort) + \
|
2021-12-26 00:07:44 +00:00
|
|
|
'/users/' + nickname not in this_actor:
|
2019-07-06 19:24:52 +00:00
|
|
|
if debug:
|
2020-09-03 09:58:32 +00:00
|
|
|
print('Port: ' + str(acceptedPort))
|
2020-04-01 18:16:29 +00:00
|
|
|
print('Expected: /' + acceptedDomain + ':' +
|
2020-09-03 09:58:32 +00:00
|
|
|
str(acceptedPort) + '/users/' + nickname)
|
2021-12-26 00:07:44 +00:00
|
|
|
print('Actual: ' + this_actor)
|
|
|
|
print('DEBUG: unrecognized actor ' + this_actor)
|
2019-07-06 19:24:52 +00:00
|
|
|
return
|
|
|
|
else:
|
2021-12-26 00:07:44 +00:00
|
|
|
if not '/' + acceptedDomain + '/users/' + nickname in this_actor:
|
2019-07-06 19:24:52 +00:00
|
|
|
if debug:
|
2021-06-22 12:42:52 +00:00
|
|
|
print('Expected: /' + acceptedDomain + '/users/' + nickname)
|
2021-12-26 00:07:44 +00:00
|
|
|
print('Actual: ' + this_actor)
|
|
|
|
print('DEBUG: unrecognized actor ' + this_actor)
|
2020-03-22 21:16:02 +00:00
|
|
|
return
|
2021-12-26 00:07:44 +00:00
|
|
|
followed_actor = message_json['object']['object']
|
|
|
|
followed_domain, port = getDomainFromActor(followed_actor)
|
|
|
|
if not followed_domain:
|
2020-04-01 18:16:29 +00:00
|
|
|
print('DEBUG: no domain found within Follow activity object ' +
|
2021-12-26 00:07:44 +00:00
|
|
|
followed_actor)
|
2019-07-06 19:24:52 +00:00
|
|
|
return
|
2021-12-26 00:07:44 +00:00
|
|
|
followed_domain_full = followed_domain
|
2019-07-06 19:24:52 +00:00
|
|
|
if port:
|
2021-12-26 00:07:44 +00:00
|
|
|
followed_domain_full = followed_domain + ':' + str(port)
|
|
|
|
followed_nickname = getNicknameFromActor(followed_actor)
|
|
|
|
if not followed_nickname:
|
2020-04-01 18:16:29 +00:00
|
|
|
print('DEBUG: no nickname found within Follow activity object ' +
|
2021-12-26 00:07:44 +00:00
|
|
|
followed_actor)
|
2019-07-06 19:24:52 +00:00
|
|
|
return
|
2019-07-07 11:53:32 +00:00
|
|
|
|
2021-12-26 00:07:44 +00:00
|
|
|
accepted_domain_full = acceptedDomain
|
2019-07-16 22:57:45 +00:00
|
|
|
if acceptedPort:
|
2021-12-26 00:07:44 +00:00
|
|
|
accepted_domain_full = acceptedDomain + ':' + str(acceptedPort)
|
2019-07-16 22:57:45 +00:00
|
|
|
|
2020-02-22 10:50:07 +00:00
|
|
|
# has this person already been unfollowed?
|
2021-12-26 00:07:44 +00:00
|
|
|
unfollowed_filename = \
|
|
|
|
acctDir(base_dir, nickname, accepted_domain_full) + '/unfollowed.txt'
|
|
|
|
if os.path.isfile(unfollowed_filename):
|
|
|
|
if followed_nickname + '@' + followed_domain_full in \
|
|
|
|
open(unfollowed_filename).read():
|
2020-02-22 10:50:07 +00:00
|
|
|
if debug:
|
2020-04-01 18:16:29 +00:00
|
|
|
print('DEBUG: follow accept arrived for ' +
|
2021-12-26 00:07:44 +00:00
|
|
|
nickname + '@' + accepted_domain_full +
|
|
|
|
' from ' +
|
|
|
|
followed_nickname + '@' + followed_domain_full +
|
2020-02-22 10:50:07 +00:00
|
|
|
' but they have been unfollowed')
|
2020-03-22 21:16:02 +00:00
|
|
|
return
|
2020-02-22 10:50:07 +00:00
|
|
|
|
2021-07-30 16:06:34 +00:00
|
|
|
# does the url path indicate that this is a group actor
|
2021-12-26 00:07:44 +00:00
|
|
|
group_account = hasGroupType(base_dir, followed_actor, None, debug)
|
2021-07-31 11:56:28 +00:00
|
|
|
if debug:
|
2021-12-26 00:07:44 +00:00
|
|
|
print('Accepted follow is a group: ' + str(group_account) +
|
|
|
|
' ' + followed_actor + ' ' + base_dir)
|
2021-07-30 16:06:34 +00:00
|
|
|
|
2021-12-25 16:17:53 +00:00
|
|
|
if followPerson(base_dir,
|
2021-12-26 00:07:44 +00:00
|
|
|
nickname, accepted_domain_full,
|
|
|
|
followed_nickname, followed_domain_full,
|
|
|
|
federation_list, debug, group_account):
|
2019-07-06 19:24:52 +00:00
|
|
|
if debug:
|
2021-12-26 00:07:44 +00:00
|
|
|
print('DEBUG: ' + nickname + '@' + accepted_domain_full +
|
|
|
|
' followed ' +
|
|
|
|
followed_nickname + '@' + followed_domain_full)
|
2019-07-06 19:24:52 +00:00
|
|
|
else:
|
|
|
|
if debug:
|
2020-04-01 18:16:29 +00:00
|
|
|
print('DEBUG: Unable to create follow - ' +
|
2021-06-22 12:42:52 +00:00
|
|
|
nickname + '@' + acceptedDomain + ' -> ' +
|
2021-12-26 00:07:44 +00:00
|
|
|
followed_nickname + '@' + followed_domain)
|
2020-04-01 18:16:29 +00:00
|
|
|
|
|
|
|
|
2021-12-25 16:17:53 +00:00
|
|
|
def receiveAcceptReject(session, base_dir: str,
|
2021-12-25 17:09:22 +00:00
|
|
|
http_prefix: str, domain: str, port: int,
|
2021-12-25 23:51:19 +00:00
|
|
|
send_threads: [], postLog: [],
|
|
|
|
cached_webfingers: {},
|
|
|
|
person_cache: {}, message_json: {},
|
|
|
|
federation_list: [],
|
2020-04-01 18:16:29 +00:00
|
|
|
debug: bool) -> bool:
|
2019-07-06 15:17:21 +00:00
|
|
|
"""Receives an Accept or Reject within the POST section of HTTPServer
|
|
|
|
"""
|
2021-12-25 23:51:19 +00:00
|
|
|
if message_json['type'] != 'Accept' and message_json['type'] != 'Reject':
|
2019-07-06 15:17:21 +00:00
|
|
|
return False
|
2021-12-25 23:51:19 +00:00
|
|
|
if not hasActor(message_json, debug):
|
2019-07-06 15:17:21 +00:00
|
|
|
return False
|
2021-12-25 23:51:19 +00:00
|
|
|
if not hasUsersPath(message_json['actor']):
|
2019-07-06 15:17:21 +00:00
|
|
|
if debug:
|
2020-04-01 18:16:29 +00:00
|
|
|
print('DEBUG: "users" or "profile" missing from actor in ' +
|
2021-12-25 23:51:19 +00:00
|
|
|
message_json['type'] + '. Assuming single user instance.')
|
2021-12-26 00:07:44 +00:00
|
|
|
domain, _ = getDomainFromActor(message_json['actor'])
|
2021-12-25 23:45:30 +00:00
|
|
|
if not domainPermitted(domain, federation_list):
|
2019-07-06 15:17:21 +00:00
|
|
|
if debug:
|
2021-12-25 23:51:19 +00:00
|
|
|
print('DEBUG: ' + message_json['type'] +
|
2020-04-01 18:16:29 +00:00
|
|
|
' from domain not permitted - ' + domain)
|
2019-07-06 15:17:21 +00:00
|
|
|
return False
|
2021-12-25 23:51:19 +00:00
|
|
|
nickname = getNicknameFromActor(message_json['actor'])
|
2019-07-06 15:17:21 +00:00
|
|
|
if not nickname:
|
2019-10-21 12:27:47 +00:00
|
|
|
# single user instance
|
2020-04-01 18:16:29 +00:00
|
|
|
nickname = 'dev'
|
2019-07-06 15:17:21 +00:00
|
|
|
if debug:
|
2021-12-25 23:51:19 +00:00
|
|
|
print('DEBUG: ' + message_json['type'] +
|
2020-04-01 18:16:29 +00:00
|
|
|
' does not contain a nickname. ' +
|
2020-03-30 19:09:45 +00:00
|
|
|
'Assuming single user instance.')
|
2019-07-20 08:33:18 +00:00
|
|
|
# receive follow accept
|
2021-12-26 00:07:44 +00:00
|
|
|
_accept_follow(base_dir, domain, message_json, federation_list, debug)
|
2019-07-06 15:17:21 +00:00
|
|
|
if debug:
|
2021-12-25 23:51:19 +00:00
|
|
|
print('DEBUG: Uh, ' + message_json['type'] + ', I guess')
|
2019-07-06 15:17:21 +00:00
|
|
|
return True
|