From 283c630b8284e560b6f91b87ddd40645f123ff3e Mon Sep 17 00:00:00 2001 From: n9k Date: Sun, 3 Jul 2022 08:50:29 +0000 Subject: [PATCH] Optionally force captcha every n messages By default every 40 messages. 0 means never force captcha in this way. --- anonstream/chat.py | 7 +++++++ anonstream/config.py | 2 ++ anonstream/helpers/user.py | 1 + anonstream/user.py | 4 ++++ config.toml | 1 + 5 files changed, 15 insertions(+) diff --git a/anonstream/chat.py b/anonstream/chat.py index e791221..c10fbc0 100644 --- a/anonstream/chat.py +++ b/anonstream/chat.py @@ -107,9 +107,16 @@ def add_chat_message(user, nonce, comment, ignore_empty=False): } MESSAGES_BY_ID[message_id] = message + # Limit number of stored messages while len(MESSAGES_BY_ID) > CONFIG['MAX_CHAT_MESSAGES']: MESSAGES_BY_ID.popitem(last=False) + # Deverify user every n messages + if CONFIG['CHAT_DEVERIFY_CLOCK'] is not None: + user['clock'] = (user['clock'] + 1) % CONFIG['CHAT_DEVERIFY_CLOCK'] + if user['clock'] == 0: + user['verified'] = False + # Notify event sockets that a chat message was added notify_event_sockets({ 'type': 'message', diff --git a/anonstream/config.py b/anonstream/config.py index 755d84e..8ebda05 100644 --- a/anonstream/config.py +++ b/anonstream/config.py @@ -119,6 +119,7 @@ def toml_to_flask_section_presence(config): def toml_to_flask_section_chat(config): cfg = config['chat'] + assert cfg['force_captcha_every'] >= 0 return { 'CHAT_COMMENT_MAX_LENGTH': cfg['max_comment_length'], 'CHAT_COMMENT_MAX_LINES': cfg['max_comment_lines'], @@ -127,6 +128,7 @@ def toml_to_flask_section_chat(config): 'CHAT_BACKGROUND_COLOUR': color_to_colour(cfg['background_color']), 'CHAT_TRIPCODE_PASSWORD_MAX_LENGTH': cfg['max_tripcode_password_length'], 'CHAT_LEGACY_TRIPCODE_ALGORITHM': cfg['legacy_tripcode_algorithm'], + 'CHAT_DEVERIFY_CLOCK': cfg['force_captcha_every'] or None, } def toml_to_flask_section_flood(config): diff --git a/anonstream/helpers/user.py b/anonstream/helpers/user.py index 7bc6e63..4668d29 100644 --- a/anonstream/helpers/user.py +++ b/anonstream/helpers/user.py @@ -56,6 +56,7 @@ def generate_user( 'current': {}, }, 'headers': headers, + 'clock': 0, } def get_default_name(user): diff --git a/anonstream/user.py b/anonstream/user.py index 021a986..c27c56e 100644 --- a/anonstream/user.py +++ b/anonstream/user.py @@ -174,6 +174,10 @@ def verify(user, digest, answer): return verification_happened def deverify(user, timestamp=None): + ''' + Try to deverify a user. The user is deverified iff they have + exceeded the message flood threshold. + ''' if timestamp is None: timestamp = get_timestamp() if user['verified'] and not user['broadcaster']: diff --git a/config.toml b/config.toml index bb59ee7..a12737e 100644 --- a/config.toml +++ b/config.toml @@ -63,6 +63,7 @@ min_name_contrast = 3.0 background_color = "#232327" max_tripcode_password_length = 1024 legacy_tripcode_algorithm = false +force_captcha_every = 40 [flood.messages] duration = 20.0