Nickname blocks can include wildcards

main
Bob Mottram 2025-05-03 20:27:25 +01:00
parent ef97b3c02f
commit 7b2750f061
2 changed files with 35 additions and 10 deletions

View File

@ -10,6 +10,7 @@ __module_group__ = "Core"
import os import os
import json import json
import time import time
import fnmatch
from session import get_json_valid from session import get_json_valid
from session import create_session from session import create_session
from flags import is_evil from flags import is_evil
@ -774,26 +775,39 @@ def is_blocked_nickname(base_dir: str, nickname: str,
blocked_cache: [] = None) -> bool: blocked_cache: [] = None) -> bool:
"""Is the given nickname blocked? """Is the given nickname blocked?
""" """
search_str = nickname + '@*' if not blocked_cache:
if blocked_cache:
for blocked_str in blocked_cache:
if blocked_str == search_str:
return True
else:
# instance-wide block list # instance-wide block list
blocked_cache = []
global_blocking_filename = data_dir(base_dir) + '/blocking.txt' global_blocking_filename = data_dir(base_dir) + '/blocking.txt'
if os.path.isfile(global_blocking_filename): if os.path.isfile(global_blocking_filename):
search_str += '\n'
try: try:
with open(global_blocking_filename, 'r', with open(global_blocking_filename, 'r',
encoding='utf-8') as fp_blocked: encoding='utf-8') as fp_blocked:
blocked_str = fp_blocked.read() blocked_cache = fp_blocked.read().split('\n')
if search_str in blocked_str:
return True
except OSError as ex: except OSError as ex:
print('EX: is_blocked_nickname unable to read ' + print('EX: is_blocked_nickname unable to read ' +
global_blocking_filename + ' ' + str(ex)) global_blocking_filename + ' ' + str(ex))
if blocked_cache:
search_str = nickname + '@*'
for blocked_str in blocked_cache:
blocked_str = blocked_str.strip()
if not blocked_str:
continue
if blocked_str.startswith('#'):
# skip over commented out blocklist entries
continue
if not blocked_str.endswith('@*'):
continue
blocked_nick = blocked_str.split('@*')[0]
if '?' not in blocked_nick and '*' not in blocked_nick:
if blocked_str == search_str:
return True
else:
# allow wildcards within nickname blocks
if fnmatch.fnmatchcase(nickname, blocked_nick):
return True
return False return False

View File

@ -229,6 +229,7 @@ from reading import store_book_events
from conversation import conversation_tag_to_convthread_id from conversation import conversation_tag_to_convthread_id
from conversation import convthread_id_to_conversation_tag from conversation import convthread_id_to_conversation_tag
from webapp_utils import add_emoji_to_display_name from webapp_utils import add_emoji_to_display_name
from blocking import is_blocked_nickname
TEST_SERVER_GROUP_RUNNING = False TEST_SERVER_GROUP_RUNNING = False
@ -9220,6 +9221,15 @@ def _test_conversation_to_convthread() -> None:
assert conversation_id2 == conversation_id assert conversation_id2 == conversation_id
def _test_blocking_nick(base_dir: str) -> None:
print('blocking nickname')
blocked_cache = ['weasel@*', 'badger@*', 'chud*@*']
assert not is_blocked_nickname(base_dir, 'cessil', blocked_cache)
assert is_blocked_nickname(base_dir, 'weasel', blocked_cache)
assert is_blocked_nickname(base_dir, 'chud', blocked_cache)
assert is_blocked_nickname(base_dir, 'chud674', blocked_cache)
def run_all_tests(): def run_all_tests():
base_dir = os.getcwd() base_dir = os.getcwd()
data_dir_testing(base_dir) data_dir_testing(base_dir)
@ -9238,6 +9248,7 @@ def run_all_tests():
_test_checkbox_names() _test_checkbox_names()
_test_thread_functions() _test_thread_functions()
_test_functions() _test_functions()
_test_blocking_nick(base_dir)
_test_conversation_to_convthread() _test_conversation_to_convthread()
_test_bridgy() _test_bridgy()
_test_link_tracking() _test_link_tracking()