__all__ = [
+ 'blocks',
'boot',
'cache',
'commands',
--- /dev/null
+# Fedi API Block - An aggregator for fetching blocking data from fediverse nodes
+# Copyright (C) 2023 Free Software Foundation
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+import sys
+import time
+import validators
+
+from fba import fba
+
+def update_reason(reason: str, blocker: str, blocked: str, block_level: str):
+ # DEBUG: print(f"DEBUG: reason='{reason}',blocker={blocker},blocked={blocked},block_level={block_level} - CALLED!")
+ if type(reason) != str and reason != None:
+ raise ValueError(f"Parameter reason[]='{type(reason)}' is not 'str'")
+ elif type(blocker) != str:
+ raise ValueError(f"Parameter blocker[]='{type(blocker)}' is not 'str'")
+ elif blocker == "":
+ raise ValueError("Parameter 'blocker' is empty")
+ elif type(blocked) != str:
+ raise ValueError(f"Parameter blocked[]='{type(blocked)}' is not 'str'")
+ elif blocked == "":
+ raise ValueError("Parameter 'blocked' is empty")
+ elif type(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")
+
+ # DEBUG: print("DEBUG: Updating block reason:", reason, blocker, blocked, block_level)
+ try:
+ fba.cursor.execute(
+ "UPDATE blocks SET reason = ?, last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason IN ('','unknown') LIMIT 1",
+ (
+ reason,
+ time.time(),
+ blocker,
+ blocked,
+ block_level
+ ),
+ )
+
+ # DEBUG: print(f"DEBUG: fba.cursor.rowcount={fba.cursor.rowcount}")
+ if fba.cursor.rowcount == 0:
+ # DEBUG: print(f"DEBUG: Did not update any rows: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',reason='{reason}' - EXIT!")
+ return
+
+ except BaseException as e:
+ print(f"ERROR: failed SQL query: reason='{reason}',blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
+ sys.exit(255)
+
+ # DEBUG: print("DEBUG: EXIT!")
+
+def update_last_seen(blocker: str, blocked: str, block_level: str):
+ # DEBUG: print("DEBUG: Updating last_seen for:", blocker, blocked, block_level)
+ if type(blocker) != str:
+ raise ValueError(f"Parameter blocker[]='{type(blocker)}' is not 'str'")
+ elif blocker == "":
+ raise ValueError("Parameter 'blocker' is empty")
+ elif type(blocked) != str:
+ raise ValueError(f"Parameter blocked[]='{type(blocked)}' is not 'str'")
+ elif blocked == "":
+ raise ValueError("Parameter 'blocked' is empty")
+ elif type(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")
+
+ try:
+ fba.cursor.execute(
+ "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
+ (
+ time.time(),
+ blocker,
+ blocked,
+ block_level
+ )
+ )
+
+ # DEBUG: print(f"DEBUG: fba.cursor.rowcount={fba.cursor.rowcount}")
+ if fba.cursor.rowcount == 0:
+ # DEBUG: print(f"DEBUG: Did not update any rows: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' - EXIT!")
+ return
+
+ except BaseException as e:
+ print(f"ERROR: failed SQL query: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
+ sys.exit(255)
+
+ # DEBUG: print("DEBUG: EXIT!")
+
+def is_instance_blocked(blocker: str, blocked: str, block_level: str) -> bool:
+ # DEBUG: print(f"DEBUG: blocker={blocker},blocked={blocked},block_level={block_level} - CALLED!")
+ if type(blocker) != str:
+ raise ValueError(f"Parameter blocker[]={type(blocker)} is not of type 'str'")
+ elif blocker == "":
+ raise ValueError("Parameter 'blocker' is empty")
+ elif type(blocked) != str:
+ raise ValueError(f"Parameter blocked[]={type(blocked)} is not of type 'str'")
+ elif blocked == "":
+ raise ValueError("Parameter 'blocked' is empty")
+ elif type(block_level) != str:
+ raise ValueError(f"Parameter block_level[]={type(block_level)} is not of type 'str'")
+ elif block_level == "":
+ raise ValueError("Parameter 'block_level' is empty")
+
+ fba.cursor.execute(
+ "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
+ (
+ blocker,
+ blocked,
+ block_level
+ ),
+ )
+
+ is_blocked = fba.cursor.fetchone() != None
+
+ # DEBUG: print(f"DEBUG: is_blocked='{is_blocked}' - EXIT!")
+ return is_blocked
+
+def add_instance(blocker: str, blocked: str, reason: str, block_level: str):
+ # DEBUG: print("DEBUG: blocker,blocked,reason,block_level:", blocker, blocked, reason, block_level)
+ if type(blocker) != str:
+ raise ValueError(f"Parameter blocker[]={type(blocker)} is not 'str'")
+ elif blocker == "":
+ raise ValueError(f"Parameter 'blocker' is empty")
+ elif not validators.domain(blocker.split("/")[0]):
+ raise ValueError(f"Bad blocker='{blocker}'")
+ elif type(blocked) != str:
+ raise ValueError(f"Parameter blocked[]={type(blocked)} is not 'str'")
+ elif blocked == "":
+ raise ValueError(f"Parameter 'blocked' is empty")
+ elif not validators.domain(blocked.split("/")[0]):
+ raise ValueError(f"Bad blocked='{blocked}'")
+ elif fba.is_blacklisted(blocker):
+ raise Exception(f"blocker='{blocker}' is blacklisted but function invoked")
+ elif fba.is_blacklisted(blocked):
+ raise Exception(f"blocked='{blocked}' is blacklisted but function invoked")
+
+ if reason != None:
+ # Maybe needs cleaning
+ reason = fba.tidyup_reason(reason)
+
+ print(f"INFO: New block: blocker='{blocker}',blocked='{blocked}', reason='{reason}', block_level='{block_level}'")
+ try:
+ fba.cursor.execute(
+ "INSERT INTO blocks (blocker, blocked, reason, block_level, first_seen, last_seen) VALUES(?, ?, ?, ?, ?, ?)",
+ (
+ blocker,
+ blocked,
+ reason,
+ block_level,
+ time.time(),
+ time.time()
+ ),
+ )
+ except BaseException as e:
+ print(f"ERROR: failed SQL query: blocker='{blocker}',blocked='{blocked}',reason='{reason}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
+ sys.exit(255)
+
+ # DEBUG: print("DEBUG: EXIT!")
import time
import validators
+from fba import blocks
from fba import boot
from fba import config
from fba import fba
# DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, blocker)
fba.add_instance(blocked, blocker, inspect.currentframe().f_code.co_name, nodeinfo_url)
- if not fba.is_instance_blocked(blocker, blocked, block_level):
- fba.block_instance(blocker, blocked, reason, block_level)
+ if not blocks.is_instance_blocked(blocker, blocked, block_level):
+ blocks.add_instance(blocker, blocked, reason, block_level)
if block_level == "reject":
blockdict.append({
else:
# DEBUG: print(f"DEBUG: Updating block last seen and reason for blocker='{blocker}',blocked='{blocked}' ...")
fba.update_last_seen(blocker, blocked, block_level)
- fba.update_block_reason(reason, blocker, blocked, block_level)
+ blocks.update_reason(reason, blocker, blocked, block_level)
# DEBUG: print("DEBUG: Committing changes ...")
fba.connection.commit()
# DEBUG: print(f"DEBUG: Domain blocked='{blocked}' wasn't found, adding ..., blocker='{blocker}',origin='{origin}',nodeinfo_url='{nodeinfo_url}'")
fba.add_instance(blocked, blocker, inspect.currentframe().f_code.co_name, nodeinfo_url)
- if not fba.is_instance_blocked(blocker, blocked, "reject"):
+ if not blocks.is_instance_blocked(blocker, blocked, "reject"):
# DEBUG: print(f"DEBUG: blocker='{blocker}' is blocking '{blocked}' for unknown reason at this point")
- fba.block_instance(blocker, blocked, "unknown", "reject")
+ blocks.add_instance(blocker, blocked, "unknown", "reject")
blockdict.append({
"blocked": blocked,
if "public_comment" in peer:
# DEBUG: print("DEBUG: Updating block reason:", blocker, blocked, peer["public_comment"])
- fba.update_block_reason(peer["public_comment"], blocker, blocked, "reject")
+ blocks.update_reason(peer["public_comment"], blocker, blocked, "reject")
for entry in blockdict:
if entry["blocked"] == blocked:
print(f"INFO: Fetching instances from domain='{row['domain']}' ...")
fba.fetch_instances(row["domain"], None, None, inspect.currentframe().f_code.co_name)
- if not fba.is_instance_blocked('chaos.social', row["domain"], block_level):
+ if not blocks.is_instance_blocked('chaos.social', row["domain"], block_level):
# DEBUG: print(f"DEBUG: domain='{row['domain']}',block_level='{block_level}' blocked by chaos.social, adding ...")
- fba.block_instance('chaos.social', row["domain"], row["reason"], block_level)
+ blocks.add_instance('chaos.social', row["domain"], row["reason"], block_level)
# DEBUG: print("DEBUG: Committing changes ...")
fba.connection.commit()
if type(key) != str:
raise ValueError(f"Parameter key[]='{type(key)}' is not 'str'")
elif key == "":
- raise ValueError("Parameter 'key' cannot be empty")
+ raise ValueError("Parameter 'key' is empty")
elif not key in _config:
raise KeyError(f"key='{key}' does not exist in _config array")
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
elif type(origin) != str and origin != None:
raise ValueError(f"Parameter origin[]={type(origin)} is not 'str'")
elif type(script) != str:
raise ValueError(f"Parameter script[]={type(script)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
if not is_instance_registered(domain):
# DEBUG: print("DEBUG: Adding new domain:", domain, origin)
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
blacklisted = False
for peer in blacklist:
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
try:
# Prevent updating any pending errors, nodeinfo was found
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
return hashlib.sha256(domain.encode("utf-8")).hexdigest()
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
# DEBUG: print("DEBUG: Updating last_blocked for domain", domain)
instances.set("last_blocked", domain, time.time())
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
try:
# DEBUG: print("DEBUG: BEFORE response[]:", type(response))
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
# DEBUG: print("DEBUG: BEFORE response[]:", type(response))
if isinstance(response, BaseException) or isinstance(response, json.decoder.JSONDecodeError):
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
# DEBUG: print("DEBUG: Updating last_instance_fetch for domain:", domain)
instances.set("last_instance_fetch", domain, time.time())
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
# DEBUG: print("DEBUG: Updating last_nodeinfo for domain:", domain)
instances.set("last_nodeinfo", domain, time.time())
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
elif type(software) != str and software != None:
raise ValueError(f"software[]={type(software)} is not 'str'")
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
elif type(path) != str:
raise ValueError(f"path[]={type(path)} is not 'str'")
elif path == "":
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
elif type(path) != str and path != None:
raise ValueError(f"Parameter path[]={type(path)} is not 'str'")
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
# DEBUG: print("DEBUG: Fetching .well-known info for domain:", domain)
data = {}
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
elif type(path) != str:
raise ValueError(f"path[]={type(path)} is not 'str'")
elif path == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
# DEBUG: print(f"DEBUG: domain='{domain}',path='{path}' - CALLED!")
software = None
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
elif type(path) != str and path != None:
raise ValueError(f"Parameter path[]={type(path)} is not 'str'")
# DEBUG: print("DEBUG: Returning domain,software:", domain, software)
return software
-def update_block_reason(reason: str, blocker: str, blocked: str, block_level: str):
- # DEBUG: print(f"DEBUG: reason='{reason}',blocker={blocker},blocked={blocked},block_level={block_level} - CALLED!")
- if type(reason) != str and reason != None:
- raise ValueError(f"Parameter reason[]='{type(reason)}' is not 'str'")
- elif type(blocker) != str:
- raise ValueError(f"Parameter blocker[]='{type(blocker)}' is not 'str'")
- elif type(blocked) != str:
- raise ValueError(f"Parameter blocked[]='{type(blocked)}' is not 'str'")
- elif type(block_level) != str:
- raise ValueError(f"Parameter block_level[]='{type(block_level)}' is not 'str'")
-
- # DEBUG: print("DEBUG: Updating block reason:", reason, blocker, blocked, block_level)
- try:
- cursor.execute(
- "UPDATE blocks SET reason = ?, last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? AND reason IN ('','unknown') LIMIT 1",
- (
- reason,
- time.time(),
- blocker,
- blocked,
- block_level
- ),
- )
-
- # DEBUG: print(f"DEBUG: cursor.rowcount={cursor.rowcount}")
- if cursor.rowcount == 0:
- # DEBUG: print(f"DEBUG: Did not update any rows: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',reason='{reason}' - EXIT!")
- return
-
- except BaseException as e:
- print(f"ERROR: failed SQL query: reason='{reason}',blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
- sys.exit(255)
-
- # DEBUG: print("DEBUG: EXIT!")
-
-def update_last_seen(blocker: str, blocked: str, block_level: str):
- # DEBUG: print("DEBUG: Updating last_seen for:", blocker, blocked, block_level)
- try:
- cursor.execute(
- "UPDATE blocks SET last_seen = ? WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
- (
- time.time(),
- blocker,
- blocked,
- block_level
- )
- )
-
- # DEBUG: print(f"DEBUG: cursor.rowcount={cursor.rowcount}")
- if cursor.rowcount == 0:
- # DEBUG: print(f"DEBUG: Did not update any rows: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}' - EXIT!")
- return
-
- except BaseException as e:
- print(f"ERROR: failed SQL query: blocker='{blocker}',blocked='{blocked}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
- sys.exit(255)
-
- # DEBUG: print("DEBUG: EXIT!")
-
-def is_instance_blocked(blocker: str, blocked: str, block_level: str) -> bool:
- # DEBUG: print(f"DEBUG: blocker={blocker},blocked={blocked},block_level={block_level} - CALLED!")
- if type(blocker) != str:
- raise ValueError(f"Parameter blocker[]={type(blocker)} is not of type 'str'")
- elif blocker == "":
- raise ValueError("Parameter 'blocker' cannot be empty")
- elif type(blocked) != str:
- raise ValueError(f"Parameter blocked[]={type(blocked)} is not of type 'str'")
- elif blocked == "":
- raise ValueError("Parameter 'blocked' cannot be empty")
- elif type(block_level) != str:
- raise ValueError(f"Parameter block_level[]={type(block_level)} is not of type 'str'")
- elif block_level == "":
- raise ValueError("Parameter 'block_level' cannot be empty")
-
- cursor.execute(
- "SELECT * FROM blocks WHERE blocker = ? AND blocked = ? AND block_level = ? LIMIT 1",
- (
- blocker,
- blocked,
- block_level
- ),
- )
-
- is_blocked = cursor.fetchone() != None
-
- # DEBUG: print(f"DEBUG: is_blocked='{is_blocked}' - EXIT!")
- return is_blocked
-
-def block_instance(blocker: str, blocked: str, reason: str, block_level: str):
- # DEBUG: print("DEBUG: blocker,blocked,reason,block_level:", blocker, blocked, reason, block_level)
- if type(blocker) != str:
- raise ValueError(f"Parameter blocker[]={type(blocker)} is not 'str'")
- elif blocker == "":
- raise ValueError(f"Parameter 'blocker' cannot be empty")
- elif not validators.domain(blocker.split("/")[0]):
- raise ValueError(f"Bad blocker='{blocker}'")
- elif type(blocked) != str:
- raise ValueError(f"Parameter blocked[]={type(blocked)} is not 'str'")
- elif blocked == "":
- raise ValueError(f"Parameter 'blocked' cannot be empty")
- elif not validators.domain(blocked.split("/")[0]):
- raise ValueError(f"Bad blocked='{blocked}'")
- elif is_blacklisted(blocker):
- raise Exception(f"blocker='{blocker}' is blacklisted but function invoked")
- elif is_blacklisted(blocked):
- raise Exception(f"blocked='{blocked}' is blacklisted but function invoked")
-
- if reason != None:
- # Maybe needs cleaning
- reason = tidyup_reason(reason)
-
- print(f"INFO: New block: blocker='{blocker}',blocked='{blocked}', reason='{reason}', block_level='{block_level}'")
- try:
- cursor.execute(
- "INSERT INTO blocks (blocker, blocked, reason, block_level, first_seen, last_seen) VALUES(?, ?, ?, ?, ?, ?)",
- (
- blocker,
- blocked,
- reason,
- block_level,
- time.time(),
- time.time()
- ),
- )
- except BaseException as e:
- print(f"ERROR: failed SQL query: blocker='{blocker}',blocked='{blocked}',reason='{reason}',block_level='{block_level}',exception[{type(e)}]:'{str(e)}'")
- sys.exit(255)
-
- # DEBUG: print("DEBUG: EXIT!")
-
def is_instance_registered(domain: str) -> bool:
# DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
# DEBUG: print(f"DEBUG: domain='{domain}' - CALLED!")
if not cache.key_exists("is_registered"):
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
elif type(origin) != str and origin != None:
raise ValueError(f"origin[]={type(origin)} is not 'str'")
elif type(originator) != str:
def send_bot_post(instance: str, blocks: dict):
# DEBUG: print(f"DEBUG: instance={instance},blocks()={len(blocks)} - CALLED!")
+ if type(domain) != str:
+ raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
+ elif domain == "":
+ raise ValueError("Parameter 'domain' is empty")
+ elif type(blocks) != dict:
+ raise ValueError(f"Parameter blocks[]='{type(blocks)}' is not 'dict'")
+
message = instance + " has blocked the following instances:\n\n"
truncated = False
truncated = True
blocks = blocks[0 : 19]
+ # DEBUG: print(f"DEBUG: blocks()={len(blocks)}")
for block in blocks:
+ # DEBUG: print(f"DEBUG: block['{type(block)}']={block}")
if block["reason"] == None or block["reason"] == '':
message = message + block["blocked"] + " with unspecified reason\n"
else:
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
# DEBUG: print("DEBUG: Fetching friendica blocks from domain:", domain)
blocks = []
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
# DEBUG: print("DEBUG: Fetching misskey blocks from domain:", domain)
blocks = {
if type(domain) != str:
raise ValueError(f"Parameter domain[]='{type(domain)}' is not 'str'")
elif domain == "":
- raise ValueError("Parameter 'domain' cannot be empty")
+ raise ValueError("Parameter 'domain' is empty")
elif type(path) != str:
raise ValueError(f"Parameter path[]='{type(path)}' is not 'str'")
elif path == "":
- raise ValueError("Parameter 'path' cannot be empty")
+ raise ValueError("Parameter 'path' is empty")
try:
# DEBUG: print(f"DEBUG: Sending request to '{domain}{path}' ...")
elif type(search) != str:
raise ValueError(f"Parameter search[]='{type(search)}' is not 'str'")
elif search == "":
- raise ValueError("Parameter 'search' cannot be empty")
+ raise ValueError("Parameter 'search' is empty")
has = False
# DEBUG: print(f"DEBUG: Checking keys()={len(keys)} ...")
if type(url) != str:
raise ValueError(f"Parameter url[]='{type(url)}' is not 'str'")
elif url == "":
- raise ValueError("Parameter 'url' cannot be empty")
+ raise ValueError("Parameter 'url' is empty")
# DEBUG: print(f"DEBUG: Parsing url='{url}'")
components = urlparse(url)
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
has_pending = False
for key in _pending:
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
elif not has_pending_instance_data(domain):
raise Exception(f"Domain '{domain}' has no pending instance data, but function invoked")
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
- elif type(software) != str and software != None:
- raise ValueError(f"software[]={type(software)} is not 'str'")
+ raise ValueError(f"Parameter 'domain' is empty")
# DEBUG: print(f"DEBUG: domain='{domain}' is Lemmy, fetching JSON ...")
peers = list()
try:
- response = fba.get_response(domain, "/api/v3/site", api_headers, (config.get("connection_timeout"), config.get("read_timeout")))
+ response = fba.get_response(domain, "/api/v3/site", fba.api_headers, (config.get("connection_timeout"), config.get("read_timeout")))
data = fba.json_from_response(response)
import bs4
import validators
+from fba import blocks
from fba import config
from fba import fba
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
# DEBUG: print("DEBUG: Fetching mastodon blocks from domain:", domain)
blocks = {
"followers_only": blocks["Limited servers"] + blocks["Silenced servers"],
}
-def fetch_blocks(domain: str, software: str, origin: str, nodeinfo_url: str):
- print(f"DEBUG: domain='{domain}',software='{software}',origin='{origin}',nodeinfo_url='{nodeinfo_url}' - CALLED!")
+def fetch_blocks(domain: str, origin: str, nodeinfo_url: str):
+ print(f"DEBUG: domain='{domain}',origin='{origin}',nodeinfo_url='{nodeinfo_url}' - CALLED!")
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
- elif type(software) != str:
- raise ValueError(f"Parameter software[]={type(software)} is not 'str'")
- elif software == "":
- raise ValueError(f"Parameter 'software' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
elif type(origin) != str and origin != None:
raise ValueError(f"Parameter origin[]={type(origin)} is not 'str'")
elif origin == "":
- raise ValueError(f"Parameter 'origin' cannot be empty")
+ raise ValueError(f"Parameter 'origin' is empty")
elif type(nodeinfo_url) != str:
raise ValueError(f"Parameter nodeinfo_url[]={type(nodeinfo_url)} is not 'str'")
elif nodeinfo_url == "":
- raise ValueError(f"Parameter 'nodeinfo_url' cannot be empty")
+ raise ValueError(f"Parameter 'nodeinfo_url' is empty")
try:
# json endpoint for newer mastodongs
# DEBUG: print("DEBUG: Querying API domain_blocks:", domain)
blocks = fba.get_response(domain, "/api/v1/instance/domain_blocks", reqheaders, (config.get("connection_timeout"), config.get("read_timeout"))).json()
- print(f"INFO: Checking {len(blocks)} entries from domain='{domain}',software='{software}' ...")
+ print(f"INFO: Checking {len(blocks)} entries from domain='{domain}',software='mastodon' ...")
for block in blocks:
entry = {
'domain': block['domain'],
# DEBUG: print(f"DEBUG: Failed, trying mastodon-specific fetches: domain='{domain}',exception[{type(e)}]={str(e)}")
json = fetch_blocks_from_about(domain)
- print(f"INFO: Checking {len(json.items())} entries from domain='{domain}',software='{software}' ...")
+ print(f"INFO: Checking {len(json.items())} entries from domain='{domain}',software='mastodon' ...")
for block_level, blocks in json.items():
# DEBUG: print("DEBUG: domain,block_level,blocks():", domain, block_level, len(blocks))
block_level = fba.tidyup_domain(block_level)
print("WARNING: block_level is empty, domain:", domain)
continue
- # DEBUG: print(f"DEBUG: Checking {len(blocks)} entries from domain='{domain}',software='{software}',block_level='{block_level}' ...")
+ # DEBUG: print(f"DEBUG: Checking {len(blocks)} entries from domain='{domain}',software='mastodon',block_level='{block_level}' ...")
for block in blocks:
blocked, blocked_hash, reason = block.values()
# DEBUG: print("DEBUG: blocked,hash,reason:", blocked, blocked_hash, reason)
# DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
if not validators.domain(blocked):
- print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domain name - skipped!")
+ print(f"WARNING: blocked='{blocked}',software='mastodon' is not a valid domain name - skipped!")
continue
elif not fba.is_instance_registered(blocked):
# DEBUG: print(f"DEBUG: Domain blocked='{blocked}' wasn't found, adding ..., domain='{domain}',origin='{origin}',nodeinfo_url='{nodeinfo_url}'")
fba.add_instance(blocked, domain, inspect.currentframe().f_code.co_name, nodeinfo_url)
elif not validators.domain(blocked):
- print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domain name - skipped!")
+ print(f"WARNING: blocked='{blocked}',software='mastodon' is not a valid domain name - skipped!")
continue
# DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
if not validators.domain(blocked):
- print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domain name - skipped!")
+ print(f"WARNING: blocked='{blocked}',software='mastodon' is not a valid domain name - skipped!")
continue
elif not fba.is_instance_registered(blocked):
# DEBUG: print("DEBUG: Hash wasn't found, adding:", blocked, domain)
blocking = blocked if blocked.count("*") <= 1 else blocked_hash
# DEBUG: print(f"DEBUG: blocking='{blocking}',blocked='{blocked}',blocked_hash='{blocked_hash}'")
- if not fba.is_instance_blocked(domain, blocked, block_level):
+ if not blocks.is_instance_blocked(domain, blocked, block_level):
# DEBUG: print("DEBUG: Blocking:", domain, blocked, block_level)
- fba.block_instance(domain, blocking, reason, block_level)
+ blocks.add_instance(domain, blocking, reason, block_level)
if block_level == "reject":
blockdict.append({
else:
# DEBUG: print(f"DEBUG: Updating block last seen and reason for domain='{domain}',blocking='{blocking}' ...")
fba.update_last_seen(domain, blocking, block_level)
- fba.update_block_reason(reason, domain, blocking, block_level)
+ blocks.update_reason(reason, domain, blocking, block_level)
# DEBUG: print("DEBUG: Committing changes ...")
fba.connection.commit()
except Exception as e:
- print(f"ERROR: domain='{domain}',software='{software}',exception[{type(e)}]:'{str(e)}'")
+ print(f"ERROR: domain='{domain}',software='mastodon',exception[{type(e)}]:'{str(e)}'")
# DEBUG: print("DEBUG: EXIT!")
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
# DEBUG: print(f"DEBUG: domain='{domain}' is misskey, sending API POST request ...")
peers = list()
elif type(row["host"]) != str:
print(f"WARNING: row[host][]={type(row['host'])} is not 'str'")
continue
- elif is_blacklisted(row["host"]):
+ elif fba.is_blacklisted(row["host"]):
# DEBUG: print(f"DEBUG: row[host]='{row['host']}' is blacklisted. domain='{domain}'")
continue
elif row["host"] in peers:
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
# DEBUG: print(f"DEBUG: domain='{domain}' is a PeerTube, fetching JSON ...")
peers = list()
import inspect
import validators
+from fba import blocks
from fba import fba
-def fetch_blocks(domain: str, software: str, origin: str, nodeinfo_url: str):
- print(f"DEBUG: domain='{domain}',software='{software}',origin='{origin}',nodeinfo_url='{nodeinfo_url}' - CALLED!")
+def fetch_blocks(domain: str, origin: str, nodeinfo_url: str):
+ print(f"DEBUG: domain='{domain}',origin='{origin}',nodeinfo_url='{nodeinfo_url}' - CALLED!")
if type(domain) != str:
raise ValueError(f"Parameter domain[]={type(domain)} is not 'str'")
elif domain == "":
- raise ValueError(f"Parameter 'domain' cannot be empty")
- elif type(software) != str:
- raise ValueError(f"Parameter software[]={type(software)} is not 'str'")
- elif software == "":
- raise ValueError(f"Parameter 'software' cannot be empty")
+ raise ValueError(f"Parameter 'domain' is empty")
elif type(origin) != str and origin != None:
raise ValueError(f"Parameter origin[]={type(origin)} is not 'str'")
elif origin == "":
- raise ValueError(f"Parameter 'origin' cannot be empty")
+ raise ValueError(f"Parameter 'origin' is empty")
elif type(nodeinfo_url) != str:
raise ValueError(f"Parameter nodeinfo_url[]={type(nodeinfo_url)} is not 'str'")
elif nodeinfo_url == "":
- raise ValueError(f"Parameter 'nodeinfo_url' cannot be empty")
+ raise ValueError(f"Parameter 'nodeinfo_url' is empty")
try:
# Blocks
print("WARNING: block_level is now empty!")
continue
- # DEBUG: print(f"DEBUG: Checking {len(blocks)} entries from domain='{domain}',software='{software}',block_level='{block_level}' ...")
+ # DEBUG: print(f"DEBUG: Checking {len(blocks)} entries from domain='{domain}',block_level='{block_level}' ...")
for blocked in blocks:
# DEBUG: print("DEBUG: BEFORE blocked:", blocked)
blocked = fba.tidyup_domain(blocked)
nodeinfo_url = searchres[1]
# DEBUG: print("DEBUG: Looked up domain:", blocked)
elif not validators.domain(blocked):
- print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domain name - skipped!")
+ print(f"WARNING: blocked='{blocked}',software='pleroma' is not a valid domain name - skipped!")
continue
# DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
if not validators.domain(blocked):
- print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domain name - skipped!")
+ print(f"WARNING: blocked='{blocked}',software='pleroma' is not a valid domain name - skipped!")
continue
elif not fba.is_instance_registered(blocked):
# DEBUG: print(f"DEBUG: Domain blocked='{blocked}' wasn't found, adding ..., domain='{domain}',origin='{origin}',nodeinfo_url='{nodeinfo_url}'")
fba.add_instance(blocked, domain, inspect.currentframe().f_code.co_name, nodeinfo_url)
- if not fba.is_instance_blocked(domain, blocked, block_level):
+ if not blocks.is_instance_blocked(domain, blocked, block_level):
# DEBUG: print("DEBUG: Blocking:", domain, blocked, block_level)
- fba.block_instance(domain, blocked, "unknown", block_level)
+ blocks.add_instance(domain, blocked, "unknown", block_level)
if block_level == "reject":
# DEBUG: print("DEBUG: Adding to blockdict:", blocked)
print("WARNING: block_level is now empty!")
continue
- # DEBUG: print(f"DEBUG: Checking {len(info.items())} entries from domain='{domain}',software='{software}',block_level='{block_level}' ...")
+ # DEBUG: print(f"DEBUG: Checking {len(info.items())} entries from domain='{domain}',software='pleroma',block_level='{block_level}' ...")
for blocked, reason in info.items():
# DEBUG: print("DEBUG: BEFORE blocked:", blocked)
blocked = fba.tidyup_domain(blocked)
origin = searchres[1]
nodeinfo_url = searchres[2]
elif not validators.domain(blocked):
- print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domain name - skipped!")
+ print(f"WARNING: blocked='{blocked}',software='pleroma' is not a valid domain name - skipped!")
continue
# DEBUG: print("DEBUG: Looking up instance by domain:", blocked)
if not validators.domain(blocked):
- print(f"WARNING: blocked='{blocked}',software='{software}' is not a valid domain name - skipped!")
+ print(f"WARNING: blocked='{blocked}',software='pleroma' is not a valid domain name - skipped!")
continue
elif not fba.is_instance_registered(blocked):
# DEBUG: print(f"DEBUG: Domain blocked='{blocked}' wasn't found, adding ..., domain='{domain}',origin='{origin}',nodeinfo_url='{nodeinfo_url}'")
fba.add_instance(blocked, domain, inspect.currentframe().f_code.co_name, nodeinfo_url)
# DEBUG: print("DEBUG: Updating block reason:", domain, blocked, reason["reason"])
- fba.update_block_reason(reason["reason"], domain, blocked, block_level)
+ blocks.update_reason(reason["reason"], domain, blocked, block_level)
# DEBUG: print(f"DEBUG: blockdict()={count(blockdict)")
for entry in blockdict:
fba.connection.commit()
except Exception as e:
- print(f"ERROR: domain='{domain}',software='{software}',exception[{type(e)}]:'{str(e)}'")
+ print(f"ERROR: domain='{domain}',software='pleroma',exception[{type(e)}]:'{str(e)}'")
# DEBUG: print("DEBUG: EXIT!")