From 10e9b3701ab34463fab7847613575c69b4dd73d9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Fri, 23 Jun 2023 16:05:13 +0200 Subject: [PATCH] Continued: - changed 'silence' to 'silenced', makes more sense - bot POSTs are now centralized in fba.commands.fetch_blocks() - more logging added --- fba/commands.py | 22 +++++++++++++++++----- fba/models/blocks.py | 14 +++++++------- fba/networks/pleroma.py | 12 ++++++------ 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/fba/commands.py b/fba/commands.py index da62329..6a77376 100644 --- a/fba/commands.py +++ b/fba/commands.py @@ -172,7 +172,6 @@ def fetch_blocks(args: argparse.Namespace) -> int: logger.info("Checking %d entries ...", len(rows)) for blocker, software, origin, nodeinfo_url in rows: logger.debug("blocker='%s',software='%s',origin='%s',nodeinfo_url='%s'", blocker, software, origin, nodeinfo_url) - blockdict = list() blocker = tidyup.domain(blocker) logger.debug("blocker='%s' - AFTER!", blocker) @@ -191,6 +190,7 @@ def fetch_blocks(args: argparse.Namespace) -> int: instances.set_has_obfuscation(blocker, False) blocking = list() + blockdict = list() if software == "pleroma": logger.info("blocker='%s',software='%s'", blocker, software) blocking = pleroma.fetch_blocks(blocker, nodeinfo_url) @@ -260,6 +260,9 @@ def fetch_blocks(args: argparse.Namespace) -> int: if not utils.is_domain_wanted(block['blocked']): logger.debug("blocked='%s' is not wanted - SKIPPED!", block['blocked']) continue + elif block['block_level'] in ["accept", "accepted"]: + logger.debug("blocked='%s' is accepted, not wanted here - SKIPPED!", block['blocked']) + continue elif not instances.is_registered(block['blocked']): logger.debug("Hash wasn't found, adding: blocked='%s',blocker='%s'", block['blocked'], blocker) try: @@ -268,10 +271,17 @@ def fetch_blocks(args: argparse.Namespace) -> int: logger.warning("Exception during adding blocked='%s',blocker='%s': '%s'", block['blocked'], blocker, type(exception)) continue + if block['block_level'] == "silence": + logger.debug("Block level 'silence' has been changed to 'silenced'") + block['block_level'] = "silenced" + if not blocks.is_instance_blocked(blocker, block['blocked'], block['block_level']): + logger.debug("Invoking blocks.add_instance(%s, %s, %s, %s)", blocker, block['blocked'], block['reason'], block['block_level']) blocks.add_instance(blocker, block['blocked'], block['reason'], block['block_level']) - if block['block_level'] == "reject": + logger.debug("block_level='%s',config[bot_enabled]=%s", block['block_level'], config.get("bot_enabled")) + if block['block_level'] == "reject" and config.get("bot_enabled"): + logger.debug("blocker='%s' has blocked '%s' with reason='%s' - Adding to bot notification ...", blocker, block['blocked'], block['reason']) blockdict.append({ "blocked": block['blocked'], "reason" : block['reason'], @@ -291,12 +301,14 @@ def fetch_blocks(args: argparse.Namespace) -> int: logger.debug("Invoking commit() ...") database.connection.commit() - if config.get("bot_enabled") and len(blockdict) > 0: - network.send_bot_post(blocker, blockdict) - logger.debug("Invoking cookies.clear(%s) ...", blocker) cookies.clear(blocker) + logger.debug("config[bot_enabled]='%s',blockdict()=%d'", config.get("bot_enabled"), len(blockdict)) + if config.get("bot_enabled") and len(blockdict) > 0: + logger.info("Sending bot POST for blocker='%s,blockdict()=%d ...", blocker, len(blockdict)) + network.send_bot_post(blocker, blockdict) + logger.debug("Success! - EXIT!") return 0 diff --git a/fba/models/blocks.py b/fba/models/blocks.py index 589c6d1..67f36d3 100644 --- a/fba/models/blocks.py +++ b/fba/models/blocks.py @@ -47,7 +47,7 @@ def update_reason(reason: str, blocker: str, blocked: str, block_level: str): raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not 'str'") elif block_level == "": raise ValueError("Parameter 'block_level' is empty") - elif block_level in ["accept", "suspended", "silenced"]: + elif block_level in ["accept", "suspended", "silence"]: raise ValueError(f"block_level='{block_level}' is not wanted.") logger.debug("Updating block reason='%s',blocker='%s',blocked='%s',block_level='%s'", reason, blocker, blocked, block_level) @@ -81,8 +81,8 @@ def update_last_seen(blocker: str, blocked: str, block_level: str): raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not 'str'") elif block_level == "": raise ValueError("Parameter 'block_level' is empty") - elif block_level in ["accept", "suspended", "silenced"]: - raise ValueError("Accepted domains are not wanted here") + elif block_level in ["accept", "suspended", "silence"]: + raise ValueError(f"blocked='{blocked}' has unwanted block_level='{block_level}'") database.cursor.execute( "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1", @@ -113,8 +113,8 @@ def is_instance_blocked(blocker: str, blocked: str, block_level: str) -> bool: raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not of type 'str'") elif block_level == "": raise ValueError("Parameter 'block_level' is empty") - elif block_level in ["accept", "suspended", "silenced"]: - raise ValueError("Accepted domains are not wanted here") + elif block_level in ["accept", "suspended", "silence"]: + raise ValueError(f"blocked='{blocked}' has unwanted block_level='{block_level}'") database.cursor.execute( "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1", @@ -143,8 +143,8 @@ def add_instance(blocker: str, blocked: str, reason: str, block_level: str): raise Exception(f"blocker='{blocker}' is blacklisted but function invoked") elif blacklist.is_blacklisted(blocked): raise Exception(f"blocked='{blocked}' is blacklisted but function invoked") - elif block_level in ["accept", "suspended", "silenced"]: - raise ValueError("Accepted domains are not wanted here") + elif block_level in ["accept", "suspended", "silence"]: + raise ValueError(f"blocked='{blocked}' has unwanted block_level='{block_level}'") if reason is not None: # Maybe needs cleaning diff --git a/fba/networks/pleroma.py b/fba/networks/pleroma.py index f132ba3..c616cfd 100644 --- a/fba/networks/pleroma.py +++ b/fba/networks/pleroma.py @@ -95,9 +95,9 @@ def fetch_blocks(domain: str, nodeinfo_url: str) -> list: elif block_level == "suspended": logger.debug("domain='%s', mapping 'suspended' to 'suspend'", domain) block_level = "suspend" - elif block_level == "silenced": - logger.debug("domain='%s', mapping 'silenced' to 'silence'", domain) - block_level = "silence" + elif block_level == "silence": + logger.debug("domain='%s', mapping 'silence' to 'silenced'", domain) + block_level = "silenced" logger.debug("Checking %d entries from domain='%s',block_level='%s' ...", len(blocklist), domain, block_level) if len(blocklist) > 0: @@ -247,9 +247,9 @@ def fetch_blocks(domain: str, nodeinfo_url: str) -> list: elif block_level == "suspended": logger.debug("domain='%s', mapping 'suspended' to 'suspend'", domain) block_level = "suspend" - elif block_level == "silenced": - logger.debug("domain='%s', mapping 'silenced' to 'silence'", domain) - block_level = "silence" + elif block_level == "silence": + logger.debug("domain='%s', mapping 'silence' to 'silenced'", domain) + block_level = "silenced" logger.debug("Checking %d entries from domain='%s',block_level='%s' ...", len(info.items()), domain, block_level) for blocked, reason in info.items(): -- 2.39.5