From 7c561a309d8e955b6e103b131c929f7c559183c6 Mon Sep 17 00:00:00 2001 From: Bob Mottram Date: Wed, 23 Nov 2022 13:57:38 +0000 Subject: [PATCH] Store reasons on global domain blocks --- blocking.py | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++- daemon.py | 32 ++++++++++++++++--------- 2 files changed, 89 insertions(+), 12 deletions(-) diff --git a/blocking.py b/blocking.py index d047e0311..0def95e80 100644 --- a/blocking.py +++ b/blocking.py @@ -39,10 +39,77 @@ from conversation import mute_conversation from conversation import unmute_conversation +def _add_global_block_reason(base_dir: str, + block_nickname: str, block_domain: str, + reason: str) -> bool: + """Store a global block reason + """ + if not reason: + return False + + blocking_reasons_filename = \ + base_dir + '/accounts/blocking_reasons.txt' + + if not block_nickname.startswith('#'): + # is the handle already blocked? + block_id = block_nickname + '@' + block_domain + else: + block_id = block_nickname + + reason = reason.replace('\n', '').strip() + reason_line = block_id + ' ' + reason + '\n' + + if os.path.isfile(blocking_reasons_filename): + if not text_in_file(block_id, + blocking_reasons_filename): + try: + with open(blocking_reasons_filename, 'a+', + encoding='utf-8') as reas_file: + reas_file.write(reason_line) + except OSError: + print('EX: unable to add blocking reason ' + + block_id) + else: + reasons_str = '' + try: + with open(blocking_reasons_filename, 'r', + encoding='utf-8') as reas_file: + reasons_str = reas_file.read() + except OSError: + print('EX: unable to read blocking reasons') + reasons_lines = reasons_str.split('\n') + new_reasons_str = '' + for line in reasons_lines: + if not line.startswith(block_id + ' '): + new_reasons_str += line + '\n' + continue + new_reasons_str = reason_line + try: + with open(blocking_reasons_filename, 'w+', + encoding='utf-8') as reas_file: + reas_file.write(new_reasons_str) + except OSError: + print('EX: unable to save blocking reasons' + + blocking_reasons_filename) + else: + try: + with open(blocking_reasons_filename, 'w+', + encoding='utf-8') as reas_file: + reas_file.write(reason_line) + except OSError: + print('EX: unable to save blocking reason ' + + block_id + ' ' + blocking_reasons_filename) + + def add_global_block(base_dir: str, - block_nickname: str, block_domain: str) -> bool: + block_nickname: str, block_domain: str, + reason: str) -> bool: """Global block which applies to all accounts """ + _add_global_block_reason(base_dir, + block_nickname, block_domain, + reason) + blocking_filename = base_dir + '/accounts/blocking.txt' if not block_nickname.startswith('#'): # is the handle already blocked? diff --git a/daemon.py b/daemon.py index 6130c8589..6eb2e82cb 100644 --- a/daemon.py +++ b/daemon.py @@ -2611,25 +2611,35 @@ class PubServer(BaseHTTPRequestHandler): remove_global_filter(base_dir, moderation_text) if moderation_button == 'block': full_block_domain = None - if moderation_text.startswith('http') or \ - moderation_text.startswith('ipfs') or \ - moderation_text.startswith('ipns') or \ - moderation_text.startswith('hyper'): + moderation_text = moderation_text.strip() + moderation_reason = None + if ' ' in moderation_text: + moderation_domain = moderation_text.split(' ', 1)[0] + moderation_reason = moderation_text.split(' ', 1)[1] + else: + moderation_domain = moderation_text + if moderation_domain.startswith('http') or \ + moderation_domain.startswith('ipfs') or \ + moderation_domain.startswith('ipns') or \ + moderation_domain.startswith('hyper'): # https://domain block_domain, block_port = \ - get_domain_from_actor(moderation_text) + get_domain_from_actor(moderation_domain) full_block_domain = \ get_full_domain(block_domain, block_port) - if '@' in moderation_text: + if '@' in moderation_domain: # nick@domain or *@domain - full_block_domain = moderation_text.split('@')[1] + full_block_domain = \ + moderation_domain.split('@')[1] else: # assume the text is a domain name - if not full_block_domain and '.' in moderation_text: + if not full_block_domain and '.' in moderation_domain: nickname = '*' - full_block_domain = moderation_text.strip() + full_block_domain = \ + moderation_domain.strip() if full_block_domain or nickname.startswith('#'): - add_global_block(base_dir, nickname, full_block_domain) + add_global_block(base_dir, nickname, + full_block_domain, moderation_reason) if moderation_button == 'unblock': full_block_domain = None if moderation_text.startswith('http') or \ @@ -18880,7 +18890,7 @@ class PubServer(BaseHTTPRequestHandler): block_domain = urllib.parse.unquote_plus(block_domain.strip()) if '?' in block_domain: block_domain = block_domain.split('?')[0] - add_global_block(self.server.base_dir, '*', block_domain) + add_global_block(self.server.base_dir, '*', block_domain, None) msg = \ html_account_info(self.server.translate, self.server.base_dir,