Use appropriate session to send follow rejects

merge-requests/30/head
Bob Mottram 2022-06-01 15:26:50 +01:00
parent 63194d2f0c
commit 5eef7c72e9
9 changed files with 51 additions and 59 deletions

View File

@ -469,8 +469,7 @@ def outbox_block(base_dir: str, http_prefix: str,
return True return True
def outbox_undo_block(base_dir: str, http_prefix: str, def outbox_undo_block(base_dir: str, nickname: str, domain: str,
nickname: str, domain: str, port: int,
message_json: {}, debug: bool) -> None: message_json: {}, debug: bool) -> None:
""" When an undo block request is received by the outbox from c2s """ When an undo block request is received by the outbox from c2s
""" """

View File

@ -1273,8 +1273,9 @@ def save_media_in_form_post(media_bytes, debug: bool,
extension = 'mp3' extension = 'mp3'
if filename_base: if filename_base:
filename = filename_base + '.' + extension filename = filename_base + '.' + extension
search_lst = search_str.decode().split('/', maxsplit=1)
attachment_media_type = \ attachment_media_type = \
search_str.decode().split('/')[0].replace('Content-Type: ', '') search_lst[0].replace('Content-Type: ', '')
detected_extension = extension detected_extension = extension
break break
@ -1331,15 +1332,15 @@ def save_media_in_form_post(media_bytes, debug: bool,
def extract_text_fields_in_post(post_bytes, boundary: str, debug: bool, def extract_text_fields_in_post(post_bytes, boundary: str, debug: bool,
unit_testData: str = None) -> {}: unit_test_data: str = None) -> {}:
"""Returns a dictionary containing the text fields of a http form POST """Returns a dictionary containing the text fields of a http form POST
The boundary argument comes from the http header The boundary argument comes from the http header
""" """
if not unit_testData: if not unit_test_data:
msg_bytes = email.parser.BytesParser().parsebytes(post_bytes) msg_bytes = email.parser.BytesParser().parsebytes(post_bytes)
message_fields = msg_bytes.get_payload(decode=True).decode('utf-8') message_fields = msg_bytes.get_payload(decode=True).decode('utf-8')
else: else:
message_fields = unit_testData message_fields = unit_test_data
if debug: if debug:
print('DEBUG: POST arriving ' + message_fields) print('DEBUG: POST arriving ' + message_fields)
@ -1690,30 +1691,31 @@ def remove_script(content: str, log_filename: str,
for sep in separators: for sep in separators:
prefix = sep[0] + 'script' prefix = sep[0] + 'script'
ending = '/script' + sep[1] ending = '/script' + sep[1]
if prefix in content: if prefix not in content:
sections = content.split(prefix) continue
ctr = 0 sections = content.split(prefix)
for text in sections: ctr = 0
if ctr == 0: for text in sections:
ctr += 1 if ctr == 0:
ctr += 1
continue
if ending not in text:
if '/' + sep[1] not in text:
continue continue
if ending not in text: if ending in text:
if '/' + sep[1] not in text: text = prefix + text.split(ending)[0] + ending
continue else:
if ending in text: text = prefix + text.split('/' + sep[1])[0] + '/' + sep[1]
text = prefix + text.split(ending)[0] + ending if log_filename and actor:
else: # write the detected script to a log file
text = prefix + text.split('/' + sep[1])[0] + '/' + sep[1] log_str = actor + ' ' + url + ' ' + text + '\n'
if log_filename and actor: write_type = 'a+'
# write the detected script to a log file if os.path.isfile(log_filename):
log_str = actor + ' ' + url + ' ' + text + '\n' write_type = 'w+'
write_type = 'a+' try:
if os.path.isfile(log_filename): with open(log_filename, write_type) as fp_log:
write_type = 'w+' fp_log.write(log_str)
try: except OSError:
with open(log_filename, write_type) as fp_log: print('EX: cannot append to svg script log')
fp_log.write(log_str) content = content.replace(text, '')
except OSError:
print('EX: cannot append to svg script log')
content = content.replace(text, '')
return content return content

View File

@ -5998,12 +5998,12 @@ class PubServer(BaseHTTPRequestHandler):
current_show_languages = get_actor_languages(actor_json) current_show_languages = get_actor_languages(actor_json)
if fields.get('showLanguages'): if fields.get('showLanguages'):
if fields['showLanguages'] != current_show_languages: if fields['showLanguages'] != current_show_languages:
set_actor_languages(base_dir, actor_json, set_actor_languages(actor_json,
fields['showLanguages']) fields['showLanguages'])
actor_changed = True actor_changed = True
else: else:
if current_show_languages: if current_show_languages:
set_actor_languages(base_dir, actor_json, '') set_actor_languages(actor_json, '')
actor_changed = True actor_changed = True
# change time zone # change time zone

View File

@ -386,7 +386,6 @@ def clear_followers(base_dir: str, nickname: str, domain: str) -> None:
def _get_no_of_follows(base_dir: str, nickname: str, domain: str, def _get_no_of_follows(base_dir: str, nickname: str, domain: str,
authenticated: bool,
follow_file='following.txt') -> int: follow_file='following.txt') -> int:
"""Returns the number of follows or followers """Returns the number of follows or followers
""" """
@ -422,13 +421,10 @@ def _get_no_of_follows(base_dir: str, nickname: str, domain: str,
return ctr return ctr
def get_no_of_followers(base_dir: str, def get_no_of_followers(base_dir: str, nickname: str, domain: str) -> int:
nickname: str, domain: str,
authenticated: bool) -> int:
"""Returns the number of followers of the given person """Returns the number of followers of the given person
""" """
return _get_no_of_follows(base_dir, nickname, domain, return _get_no_of_follows(base_dir, nickname, domain, 'followers.txt')
authenticated, 'followers.txt')
def get_following_feed(base_dir: str, domain: str, port: int, path: str, def get_following_feed(base_dir: str, domain: str, port: int, path: str,
@ -482,7 +478,7 @@ def get_following_feed(base_dir: str, domain: str, port: int, path: str,
id_str = \ id_str = \
local_actor_url(http_prefix, nickname, domain) + '/' + follow_file local_actor_url(http_prefix, nickname, domain) + '/' + follow_file
total_str = \ total_str = \
_get_no_of_follows(base_dir, nickname, domain, authorized) _get_no_of_follows(base_dir, nickname, domain)
following = { following = {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
'first': first_str, 'first': first_str,
@ -599,7 +595,6 @@ def follow_approval_required(base_dir: str, nickname_to_follow: str,
def no_of_follow_requests(base_dir: str, def no_of_follow_requests(base_dir: str,
nickname_to_follow: str, domain_to_follow: str, nickname_to_follow: str, domain_to_follow: str,
nickname: str, domain: str, from_port: int,
follow_type: str) -> int: follow_type: str) -> int:
"""Returns the current number of follow requests """Returns the current number of follow requests
""" """
@ -846,8 +841,13 @@ def followed_account_rejects(session, session_onion, session_i2p,
except OSError: except OSError:
print('EX: followed_account_rejects unable to delete ' + print('EX: followed_account_rejects unable to delete ' +
follow_activity_filename) follow_activity_filename)
curr_session = session
if domain.endswith('.onion') and session_onion:
curr_session = session_onion
elif domain.endswith('.i2p') and session_i2p:
curr_session = session_i2p
# send the reject activity # send the reject activity
return send_signed_json(reject_json, session, base_dir, return send_signed_json(reject_json, curr_session, base_dir,
nickname_to_follow, domain_to_follow, port, nickname_to_follow, domain_to_follow, port,
nickname, domain, from_port, nickname, domain, from_port,
http_prefix, client_to_server, http_prefix, client_to_server,
@ -981,7 +981,7 @@ def send_follow_request_via_server(base_dir: str, session,
followed_id = \ followed_id = \
http_prefix + '://' + follow_domain_full + '/@' + follow_nickname http_prefix + '://' + follow_domain_full + '/@' + follow_nickname
status_number, published = get_status_number() status_number, _ = get_status_number()
new_follow_json = { new_follow_json = {
'@context': 'https://www.w3.org/ns/activitystreams', '@context': 'https://www.w3.org/ns/activitystreams',
'id': follow_actor + '/statuses/' + str(status_number), 'id': follow_actor + '/statuses/' + str(status_number),

View File

@ -4649,9 +4649,8 @@ def _receive_follow_request(session, session_onion, session_i2p,
nickname_to_follow) nickname_to_follow)
return True return True
if max_followers > 0: if max_followers > 0:
if get_no_of_followers(base_dir, if get_no_of_followers(base_dir, nickname_to_follow,
nickname_to_follow, domain_to_follow, domain_to_follow) > max_followers:
True) > max_followers:
print('WARN: ' + nickname_to_follow + print('WARN: ' + nickname_to_follow +
' has reached their maximum number of followers') ' has reached their maximum number of followers')
return True return True
@ -4716,21 +4715,18 @@ def _receive_follow_request(session, session_onion, session_i2p,
if domain.endswith('.onion'): if domain.endswith('.onion'):
if no_of_follow_requests(base_dir, if no_of_follow_requests(base_dir,
nickname_to_follow, domain_to_follow, nickname_to_follow, domain_to_follow,
nickname, domain, from_port,
'onion') > 5: 'onion') > 5:
print('Too many follow requests from onion addresses') print('Too many follow requests from onion addresses')
return False return False
elif domain.endswith('.i2p'): elif domain.endswith('.i2p'):
if no_of_follow_requests(base_dir, if no_of_follow_requests(base_dir,
nickname_to_follow, domain_to_follow, nickname_to_follow, domain_to_follow,
nickname, domain, from_port,
'i2p') > 5: 'i2p') > 5:
print('Too many follow requests from i2p addresses') print('Too many follow requests from i2p addresses')
return False return False
else: else:
if no_of_follow_requests(base_dir, if no_of_follow_requests(base_dir,
nickname_to_follow, domain_to_follow, nickname_to_follow, domain_to_follow,
nickname, domain, from_port,
'') > 10: '') > 10:
print('Too many follow requests') print('Too many follow requests')
return False return False

View File

@ -46,8 +46,7 @@ def get_understood_languages(base_dir: str, http_prefix: str,
return get_actor_languages_list(actor_json) return get_actor_languages_list(actor_json)
def set_actor_languages(base_dir: str, actor_json: {}, def set_actor_languages(actor_json: {}, languages_str: str) -> None:
languages_str: str) -> None:
"""Sets the languages understood by the given actor """Sets the languages understood by the given actor
""" """
languages_str = languages_str.strip() languages_str = languages_str.strip()

View File

@ -351,8 +351,7 @@ def send_undo_like_via_server(base_dir: str, session,
def outbox_like(recent_posts_cache: {}, def outbox_like(recent_posts_cache: {},
base_dir: str, http_prefix: str, base_dir: str, nickname: str, domain: str,
nickname: str, domain: str, port: int,
message_json: {}, debug: bool) -> None: message_json: {}, debug: bool) -> None:
""" When a like request is received by the outbox from c2s """ When a like request is received by the outbox from c2s
""" """

View File

@ -576,8 +576,7 @@ def post_message_to_outbox(session, translate: {},
if debug: if debug:
print('DEBUG: handle any like requests') print('DEBUG: handle any like requests')
outbox_like(recent_posts_cache, outbox_like(recent_posts_cache,
base_dir, http_prefix, base_dir, post_to_nickname, domain,
post_to_nickname, domain, port,
message_json, debug) message_json, debug)
if debug: if debug:
print('DEBUG: handle any undo like requests') print('DEBUG: handle any undo like requests')
@ -636,9 +635,7 @@ def post_message_to_outbox(session, translate: {},
if debug: if debug:
print('DEBUG: handle undo block requests') print('DEBUG: handle undo block requests')
outbox_undo_block(base_dir, http_prefix, outbox_undo_block(base_dir, post_to_nickname, domain, message_json, debug)
post_to_nickname, domain,
port, message_json, debug)
if debug: if debug:
print('DEBUG: handle mute requests') print('DEBUG: handle mute requests')

View File

@ -6389,7 +6389,7 @@ def _test_set_actor_language():
actor_json = { actor_json = {
"attachment": [] "attachment": []
} }
set_actor_languages(None, actor_json, 'es, fr, en') set_actor_languages(actor_json, 'es, fr, en')
assert len(actor_json['attachment']) == 1 assert len(actor_json['attachment']) == 1
assert actor_json['attachment'][0]['name'] == 'Languages' assert actor_json['attachment'][0]['name'] == 'Languages'
assert actor_json['attachment'][0]['type'] == 'PropertyValue' assert actor_json['attachment'][0]['type'] == 'PropertyValue'