From 5eef7c72e99e79fc5d547d0cadc079ef56bd8948 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 1 Jun 2022 15:26:50 +0100 Subject: [PATCH] Use appropriate session to send follow rejects --- blocking.py | 3 +-- content.py | 60 +++++++++++++++++++++++++++------------------------- daemon.py | 4 ++-- follow.py | 20 +++++++++--------- inbox.py | 8 ++----- languages.py | 3 +-- like.py | 3 +-- outbox.py | 7 ++---- tests.py | 2 +- 9 files changed, 51 insertions(+), 59 deletions(-) diff --git a/blocking.py b/blocking.py index 3218bf60e..f73bc93d1 100644 --- a/blocking.py +++ b/blocking.py @@ -469,8 +469,7 @@ def outbox_block(base_dir: str, http_prefix: str, return True -def outbox_undo_block(base_dir: str, http_prefix: str, - nickname: str, domain: str, port: int, +def outbox_undo_block(base_dir: str, nickname: str, domain: str, message_json: {}, debug: bool) -> None: """ When an undo block request is received by the outbox from c2s """ diff --git a/content.py b/content.py index 24533e741..5e5c1b769 100644 --- a/content.py +++ b/content.py @@ -1273,8 +1273,9 @@ def save_media_in_form_post(media_bytes, debug: bool, extension = 'mp3' if filename_base: filename = filename_base + '.' + extension + search_lst = search_str.decode().split('/', maxsplit=1) attachment_media_type = \ - search_str.decode().split('/')[0].replace('Content-Type: ', '') + search_lst[0].replace('Content-Type: ', '') detected_extension = extension 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, - unit_testData: str = None) -> {}: + unit_test_data: str = None) -> {}: """Returns a dictionary containing the text fields of a http form POST 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) message_fields = msg_bytes.get_payload(decode=True).decode('utf-8') else: - message_fields = unit_testData + message_fields = unit_test_data if debug: print('DEBUG: POST arriving ' + message_fields) @@ -1690,30 +1691,31 @@ def remove_script(content: str, log_filename: str, for sep in separators: prefix = sep[0] + 'script' ending = '/script' + sep[1] - if prefix in content: - sections = content.split(prefix) - ctr = 0 - for text in sections: - if ctr == 0: - ctr += 1 + if prefix not in content: + continue + sections = content.split(prefix) + ctr = 0 + for text in sections: + if ctr == 0: + ctr += 1 + continue + if ending not in text: + if '/' + sep[1] not in text: continue - if ending not in text: - if '/' + sep[1] not in text: - continue - if ending in text: - text = prefix + text.split(ending)[0] + ending - else: - text = prefix + text.split('/' + sep[1])[0] + '/' + sep[1] - if log_filename and actor: - # write the detected script to a log file - log_str = actor + ' ' + url + ' ' + text + '\n' - write_type = 'a+' - if os.path.isfile(log_filename): - write_type = 'w+' - try: - with open(log_filename, write_type) as fp_log: - fp_log.write(log_str) - except OSError: - print('EX: cannot append to svg script log') - content = content.replace(text, '') + if ending in text: + text = prefix + text.split(ending)[0] + ending + else: + text = prefix + text.split('/' + sep[1])[0] + '/' + sep[1] + if log_filename and actor: + # write the detected script to a log file + log_str = actor + ' ' + url + ' ' + text + '\n' + write_type = 'a+' + if os.path.isfile(log_filename): + write_type = 'w+' + try: + with open(log_filename, write_type) as fp_log: + fp_log.write(log_str) + except OSError: + print('EX: cannot append to svg script log') + content = content.replace(text, '') return content diff --git a/daemon.py b/daemon.py index f6f404482..deca689f4 100644 --- a/daemon.py +++ b/daemon.py @@ -5998,12 +5998,12 @@ class PubServer(BaseHTTPRequestHandler): current_show_languages = get_actor_languages(actor_json) if fields.get('showLanguages'): if fields['showLanguages'] != current_show_languages: - set_actor_languages(base_dir, actor_json, + set_actor_languages(actor_json, fields['showLanguages']) actor_changed = True else: if current_show_languages: - set_actor_languages(base_dir, actor_json, '') + set_actor_languages(actor_json, '') actor_changed = True # change time zone diff --git a/follow.py b/follow.py index 154e17e1d..403d6d0ca 100644 --- a/follow.py +++ b/follow.py @@ -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, - authenticated: bool, follow_file='following.txt') -> int: """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 -def get_no_of_followers(base_dir: str, - nickname: str, domain: str, - authenticated: bool) -> int: +def get_no_of_followers(base_dir: str, nickname: str, domain: str) -> int: """Returns the number of followers of the given person """ - return _get_no_of_follows(base_dir, nickname, domain, - authenticated, 'followers.txt') + return _get_no_of_follows(base_dir, nickname, domain, 'followers.txt') 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 = \ local_actor_url(http_prefix, nickname, domain) + '/' + follow_file total_str = \ - _get_no_of_follows(base_dir, nickname, domain, authorized) + _get_no_of_follows(base_dir, nickname, domain) following = { '@context': 'https://www.w3.org/ns/activitystreams', '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, nickname_to_follow: str, domain_to_follow: str, - nickname: str, domain: str, from_port: int, follow_type: str) -> int: """Returns the current number of follow requests """ @@ -846,8 +841,13 @@ def followed_account_rejects(session, session_onion, session_i2p, except OSError: print('EX: followed_account_rejects unable to delete ' + 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 - 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, domain, from_port, http_prefix, client_to_server, @@ -981,7 +981,7 @@ def send_follow_request_via_server(base_dir: str, session, followed_id = \ http_prefix + '://' + follow_domain_full + '/@' + follow_nickname - status_number, published = get_status_number() + status_number, _ = get_status_number() new_follow_json = { '@context': 'https://www.w3.org/ns/activitystreams', 'id': follow_actor + '/statuses/' + str(status_number), diff --git a/inbox.py b/inbox.py index 8df1f3623..3617676b0 100644 --- a/inbox.py +++ b/inbox.py @@ -4649,9 +4649,8 @@ def _receive_follow_request(session, session_onion, session_i2p, nickname_to_follow) return True if max_followers > 0: - if get_no_of_followers(base_dir, - nickname_to_follow, domain_to_follow, - True) > max_followers: + if get_no_of_followers(base_dir, nickname_to_follow, + domain_to_follow) > max_followers: print('WARN: ' + nickname_to_follow + ' has reached their maximum number of followers') return True @@ -4716,21 +4715,18 @@ def _receive_follow_request(session, session_onion, session_i2p, if domain.endswith('.onion'): if no_of_follow_requests(base_dir, nickname_to_follow, domain_to_follow, - nickname, domain, from_port, 'onion') > 5: print('Too many follow requests from onion addresses') return False elif domain.endswith('.i2p'): if no_of_follow_requests(base_dir, nickname_to_follow, domain_to_follow, - nickname, domain, from_port, 'i2p') > 5: print('Too many follow requests from i2p addresses') return False else: if no_of_follow_requests(base_dir, nickname_to_follow, domain_to_follow, - nickname, domain, from_port, '') > 10: print('Too many follow requests') return False diff --git a/languages.py b/languages.py index d586f96e9..97918f737 100644 --- a/languages.py +++ b/languages.py @@ -46,8 +46,7 @@ def get_understood_languages(base_dir: str, http_prefix: str, return get_actor_languages_list(actor_json) -def set_actor_languages(base_dir: str, actor_json: {}, - languages_str: str) -> None: +def set_actor_languages(actor_json: {}, languages_str: str) -> None: """Sets the languages understood by the given actor """ languages_str = languages_str.strip() diff --git a/like.py b/like.py index a4fbe7c3b..75814e882 100644 --- a/like.py +++ b/like.py @@ -351,8 +351,7 @@ def send_undo_like_via_server(base_dir: str, session, def outbox_like(recent_posts_cache: {}, - base_dir: str, http_prefix: str, - nickname: str, domain: str, port: int, + base_dir: str, nickname: str, domain: str, message_json: {}, debug: bool) -> None: """ When a like request is received by the outbox from c2s """ diff --git a/outbox.py b/outbox.py index 27761ed4e..703bc9930 100644 --- a/outbox.py +++ b/outbox.py @@ -576,8 +576,7 @@ def post_message_to_outbox(session, translate: {}, if debug: print('DEBUG: handle any like requests') outbox_like(recent_posts_cache, - base_dir, http_prefix, - post_to_nickname, domain, port, + base_dir, post_to_nickname, domain, message_json, debug) if debug: print('DEBUG: handle any undo like requests') @@ -636,9 +635,7 @@ def post_message_to_outbox(session, translate: {}, if debug: print('DEBUG: handle undo block requests') - outbox_undo_block(base_dir, http_prefix, - post_to_nickname, domain, - port, message_json, debug) + outbox_undo_block(base_dir, post_to_nickname, domain, message_json, debug) if debug: print('DEBUG: handle mute requests') diff --git a/tests.py b/tests.py index f69d1333b..07a8c3de1 100644 --- a/tests.py +++ b/tests.py @@ -6389,7 +6389,7 @@ def _test_set_actor_language(): actor_json = { "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 actor_json['attachment'][0]['name'] == 'Languages' assert actor_json['attachment'][0]['type'] == 'PropertyValue'